1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package libcore; 18 19 import java.io.File; 20 import java.io.IOException; 21 import java.nio.file.Files; 22 import java.nio.file.Path; 23 import java.util.Arrays; 24 import java.util.List; 25 import java.util.Objects; 26 27 public class CopyUpstreamFiles { 28 29 private final StandardRepositories standardRepositories; 30 private final Path outputDir; 31 CopyUpstreamFiles(StandardRepositories standardRepositories, Path outputDir)32 private CopyUpstreamFiles(StandardRepositories standardRepositories, Path outputDir) { 33 this.standardRepositories = Objects.requireNonNull(standardRepositories); 34 this.outputDir = Objects.requireNonNull(outputDir); 35 } 36 run()37 public void run() throws IOException { 38 List<Path> relPaths = standardRepositories.ojluni().loadRelPathsFromBlueprint(); 39 if (outputDir.toFile().exists()) { 40 throw new IOException(outputDir + " already exists"); 41 } else { 42 boolean success = outputDir.toFile().mkdir(); 43 if (!success) { 44 throw new IOException("Failed to create directory " + outputDir); 45 } 46 } 47 for (Path relPath : relPaths) { 48 Repository expectedUpstream = standardRepositories.referenceUpstream(relPath); 49 for (Repository upstream : standardRepositories.upstreams()) { 50 Path upstreamFile = upstream.absolutePath(relPath); 51 if (upstreamFile != null) { 52 Path outputFile = outputDir 53 .resolve(upstream.name()) 54 .resolve(relPath); 55 copyFile(upstreamFile, outputFile); 56 if (upstream.equals(expectedUpstream)) { 57 copyFile(upstreamFile, outputDir.resolve("expected").resolve(relPath)); 58 } 59 } 60 } 61 } 62 } 63 copyFile(Path from, Path to)64 private void copyFile(Path from, Path to) throws IOException { 65 if (!from.toFile().canRead()) { 66 throw new IOException("Error reading " + from); 67 } 68 Path toDir = to.getParent(); 69 if (!toDir.toFile().exists()) { 70 boolean success = toDir.toFile().mkdirs(); 71 if (!success) { 72 throw new IOException("Failed to create directory " + toDir); 73 } 74 } 75 Files.copy(from, to); 76 } 77 main(String[] args)78 public static void main(String[] args) throws Exception { 79 if (args.length != 1) { 80 throw new IllegalArgumentException(Arrays.asList(args).toString()); 81 } 82 Path outputDir = new File(args[0]).toPath(); 83 StandardRepositories standardRepositories = StandardRepositories.fromEnv(); 84 new CopyUpstreamFiles(standardRepositories, outputDir).run(); 85 } 86 } 87