Extract zip file in tmp directory

As-is: extract zip file in-place
To-be: extract zip file in tmp dir

Test: run the tool with -u option, and check if there is no file in a
directory where zip file is located

Change-Id: I2654bfa257714cb5b266abf4feb31acaabdbb7cb
This commit is contained in:
Jeongik Cha
2019-12-11 21:55:50 +09:00
parent 42f7dbcbd1
commit af646f7981

View File

@@ -22,6 +22,7 @@ import hashlib
import argparse import argparse
import zipfile import zipfile
import fnmatch import fnmatch
import tempfile
def silent_call(cmd): def silent_call(cmd):
return subprocess.call(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) == 0 return subprocess.call(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) == 0
@@ -41,6 +42,7 @@ def sha1sum_without_signing_key(filepath):
return hashlib.sha1(",".join(l).encode()).hexdigest() return hashlib.sha1(",".join(l).encode()).hexdigest()
def strip_and_sha1sum(filepath): def strip_and_sha1sum(filepath):
# TODO: save striped file in tmp directory to support readonly directory.
tmp_filepath = filepath + '.tmp.no-build-id' tmp_filepath = filepath + '.tmp.no-build-id'
strip_all_and_remove_build_id = lambda: silent_call( strip_all_and_remove_build_id = lambda: silent_call(
["llvm-strip", "--strip-all", "--keep-section=.ARM.attributes", ["llvm-strip", "--strip-all", "--keep-section=.ARM.attributes",
@@ -217,8 +219,14 @@ if __name__ == "__main__":
if len(args.target) < 2: if len(args.target) < 2:
parser.error("The number of targets has to be at least two.") parser.error("The number of targets has to be at least two.")
if args.unzip: if args.unzip:
for t in args.target: with tempfile.TemporaryDirectory() as tmpdir:
unzip_cmd = ["unzip", "-qd", t, os.path.join(t, "*.zip")] target_in_tmp = [os.path.join(tmpdir, t) for t in args.target]
for p in target_in_tmp:
os.makedirs(p)
for origin_path, tmp_path in zip(args.target, target_in_tmp):
unzip_cmd = ["unzip", "-qd", tmp_path, os.path.join(origin_path, "*.zip")]
unzip_cmd.extend([os.path.join(s, "*") for s in args.search_path]) unzip_cmd.extend([os.path.join(s, "*") for s in args.search_path])
subprocess.call(unzip_cmd) subprocess.call(unzip_cmd)
main(target_in_tmp, args.search_path, args.whitelist, args.ignore_signing_key)
else:
main(args.target, args.search_path, args.whitelist, args.ignore_signing_key) main(args.target, args.search_path, args.whitelist, args.ignore_signing_key)