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.nio.file.Paths; 24 import java.util.Arrays; 25 import java.util.List; 26 import java.util.Objects; 27 28 public class CopyUpstreamFiles { 29 30 private final StandardRepositories standardRepositories; 31 private final Path outputDir; 32 33 private CopyUpstreamFiles(StandardRepositories standardRepositories, Path outputDir) { 34 this.standardRepositories = Objects.requireNonNull(standardRepositories); 35 this.outputDir = Objects.requireNonNull(outputDir); 36 } 37 38 public void run() throws IOException { 39 List<Path> relPaths = standardRepositories.ojluni().loadRelPathsFromMakefile(); 40 if (outputDir.toFile().exists()) { 41 throw new IOException(outputDir + " already exists"); 42 } else { 43 boolean success = outputDir.toFile().mkdir(); 44 if (!success) { 45 throw new IOException("Failed to create directory " + outputDir); 46 } 47 } 48 for (Path relPath : relPaths) { 49 Repository expectedUpstream = standardRepositories.currentUpstream(relPath); 50 for (Repository upstream : standardRepositories.upstreams()) { 51 Path upstreamFile = upstream.absolutePath(relPath); 52 if (upstreamFile != null) { 53 Path outputFile = outputDir 54 .resolve(upstream.name()) 55 .resolve(relPath); 56 copyFile(upstreamFile, outputFile); 57 if (upstream.equals(expectedUpstream)) { 58 copyFile(upstreamFile, outputDir.resolve("expected").resolve(relPath)); 59 } 60 } 61 } 62 } 63 } 64 65 private void copyFile(Path from, Path to) throws IOException { 66 if (!from.toFile().canRead()) { 67 throw new IOException("Error reading " + from); 68 } 69 Path toDir = to.getParent(); 70 if (!toDir.toFile().exists()) { 71 boolean success = toDir.toFile().mkdirs(); 72 if (!success) { 73 throw new IOException("Failed to create directory " + toDir); 74 } 75 } 76 Files.copy(from, to); 77 } 78 79 public static void main(String[] args) throws Exception { 80 if (args.length != 1) { 81 throw new IllegalArgumentException(Arrays.asList(args).toString()); 82 } 83 Path outputDir = new File(args[0]).toPath(); 84 StandardRepositories standardRepositories = StandardRepositories.fromEnv(); 85 new CopyUpstreamFiles(standardRepositories, outputDir).run(); 86 } 87 } 88