// Copyright 2017 The Bazel Authors. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package com.google.devtools.build.android; import com.beust.jcommander.IStringConverter; import com.beust.jcommander.IValueValidator; import com.beust.jcommander.JCommander; import com.beust.jcommander.Parameter; import com.beust.jcommander.ParameterException; import com.beust.jcommander.Parameters; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Joiner; import com.google.common.base.Stopwatch; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSetMultimap; import com.google.common.collect.Multimap; import com.google.devtools.build.singlejar.ZipCombiner; import com.google.devtools.build.singlejar.ZipCombiner.OutputMode; import com.google.devtools.build.zip.ZipFileEntry; import com.google.devtools.build.zip.ZipReader; import java.io.IOException; import java.io.OutputStream; import java.nio.file.FileSystems; import java.nio.file.Files; import java.nio.file.Path; import java.util.Collection; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.concurrent.TimeUnit; import java.util.logging.Logger; import java.util.regex.Pattern; /** * Action to filter entries out of a Zip file. * *
The entries to remove are determined from the filterZips and filterTypes. All entries from the * filter Zip files that have an extension listed in filterTypes will be removed. If no filterZips * are specified, no entries will be removed. Specifying no filterTypes is treated as if an * extension of '.*' was specified. * *
Assuming each Zip as a set of entries, the result is: * *
outputZip = inputZip - union[x intersect filterTypes for x in filterZips]* *
* *
* Example Usage: * java/com/google/build/android/ZipFilterAction\ * --inputZip path/to/inputZip * --outputZip path/to/outputZip * --filterZips [path/to/filterZip[,path/to/filterZip]...] * --filterTypes [fileExtension[,fileExtension]...] * --explicitFilters [fileRegex[,fileRegex]...] * --outputMode [DONT_CARE|FORCE_DEFLATE|FORCE_STORED] * --checkHashMismatch [IGNORE|WARN|ERROR] **/ public class ZipFilterAction { private static final Logger logger = Logger.getLogger(ZipFilterAction.class.getName()); /** Modes of performing content hash checking during zip filtering. */ public enum HashMismatchCheckMode { /** Filter file from input zip iff a file is found with the same filename in filter zips. */ IGNORE, /** * Filter file from input zip iff a file is found with the same filename and content hash in * filter zips. Print warning if the filename is identical but content hash is not. */ WARN, /** * Same behavior as WARN, but throw an error if a file is found with the same filename with * different content hash. */ ERROR } @Parameters(optionPrefixes = "--") static class Options { @Parameter( names = "--inputZip", description = "Path of input zip.", converter = PathFlagConverter.class, validateValueWith = PathExistsValidator.class ) Path inputZip; @Parameter( names = "--outputZip", description = "Path to write output zip.", converter = PathFlagConverter.class ) Path outputZip; @Parameter( names = "--filterZips", description = "Filter zips.", converter = PathFlagConverter.class, validateValueWith = AllPathsExistValidator.class ) List
This is a hack to support existing users of --noerrorOnHashMismatch. JCommander does
* not support setting boolean flags with "--no", so instead we set the default to false and
* just ignore anyone who passes --noerrorOnHashMismatch.
*/
@Deprecated
@Parameter(names = "--noerrorOnHashMismatch")
boolean ignored = false;
}
/** Converts string flags to paths. Public because JCommander invokes this by reflection. */
public static class PathFlagConverter implements IStringConverter> {
@Override
public void validate(String s, List