Merge "Add -stripDir and -zipToNotStrip option in merge_zips."

This commit is contained in:
Nan Zhang
2017-09-20 19:48:27 +00:00
committed by Gerrit Code Review

View File

@@ -19,6 +19,7 @@ import (
"fmt" "fmt"
"log" "log"
"os" "os"
"path/filepath"
"sort" "sort"
"strings" "strings"
@@ -26,14 +27,26 @@ import (
"android/soong/third_party/zip" "android/soong/third_party/zip"
) )
type strip struct{} type stripDir struct{}
func (s *strip) String() string { func (s *stripDir) String() string {
return `""` return `""`
} }
func (s *strip) Set(path_prefix string) error { func (s *stripDir) Set(dir string) error {
strippings = append(strippings, path_prefix) stripDirs = append(stripDirs, filepath.Clean(dir))
return nil
}
type zipToNotStrip struct{}
func (s *zipToNotStrip) String() string {
return `""`
}
func (s *zipToNotStrip) Set(zip_path string) error {
zipsToNotStrip[zip_path] = true
return nil return nil
} }
@@ -41,11 +54,13 @@ func (s *strip) Set(path_prefix string) error {
var ( var (
sortEntries = flag.Bool("s", false, "sort entries (defaults to the order from the input zip files)") sortEntries = flag.Bool("s", false, "sort entries (defaults to the order from the input zip files)")
emulateJar = flag.Bool("j", false, "sort zip entries using jar ordering (META-INF first)") emulateJar = flag.Bool("j", false, "sort zip entries using jar ordering (META-INF first)")
strippings []string stripDirs []string
zipsToNotStrip = make(map[string]bool)
) )
func init() { func init() {
flag.Var(&strip{}, "strip", "the prefix of file path to be excluded from the output zip") flag.Var(&stripDir{}, "stripDir", "the prefix of file path to be excluded from the output zip")
flag.Var(&zipToNotStrip{}, "zipToNotStrip", "the input zip file which is not applicable for stripping")
} }
func main() { func main() {
@@ -132,12 +147,21 @@ func mergeZips(readers []namedZipReader, writer *zip.Writer, sortEntries bool, e
orderedMappings := []fileMapping{} orderedMappings := []fileMapping{}
for _, namedReader := range readers { for _, namedReader := range readers {
_, skipStripThisZip := zipsToNotStrip[namedReader.path]
FileLoop: FileLoop:
for _, file := range namedReader.reader.File { for _, file := range namedReader.reader.File {
for _, path_prefix := range strippings { if !skipStripThisZip {
if strings.HasPrefix(file.Name, path_prefix) { for _, dir := range stripDirs {
if strings.HasPrefix(file.Name, dir+"/") {
if emulateJar {
if file.Name != jar.MetaDir && file.Name != jar.ManifestFile {
continue FileLoop continue FileLoop
} }
} else {
continue FileLoop
}
}
}
} }
// check for other files or directories destined for the same path // check for other files or directories destined for the same path
dest := file.Name dest := file.Name