Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(269)

Side by Side Diff: scripts/slave/recipe_modules/isolate/resources/remove_build_metadata.py

Issue 708803003: Remove the timestamps from the zip archives in remove_build_metadata.py (Closed) Base URL: https://chromium.googlesource.com/chromium/tools/build.git@master
Patch Set: Address maruel's comments. Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # Copyright 2014 The Chromium Authors. All rights reserved. 2 # Copyright 2014 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be 3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file. 4 # found in the LICENSE file.
5 """Remove the build metadata embedded in the artifacts of a build.""" 5 """Remove the build metadata embedded in the artifacts of a build."""
6 6
7 import json 7 import json
8 import optparse 8 import optparse
9 import os 9 import os
10 import shutil
10 import subprocess 11 import subprocess
11 import sys 12 import sys
13 import tempfile
14 import zipfile
12 15
13 16
14 BASE_DIR = os.path.dirname(os.path.abspath(__file__)) 17 BASE_DIR = os.path.dirname(os.path.abspath(__file__))
15 18
16 19
17 def RunZapTimestamp(src_dir, filepath): 20 def get_files_to_clean(build_dir, recursive=False):
21 """Get the list of files to clean."""
22 allowed = frozenset(
23 ('', '.apk', '.app', '.dll', '.dylib', '.exe', '.nexe', '.so'))
24 non_x_ok_exts = frozenset(('.apk', '.isolated'))
25 def check(f):
26 if not os.path.isfile(f) or os.path.basename(f).startswith('.'):
27 return False
28 ext = os.path.splitext(f)[1]
29 return (ext in non_x_ok_exts) or (ext in allowed and os.access(f, os.X_OK))
30
31 ret_files = set()
32 for root, dirs, files in os.walk(build_dir):
33 if not recursive:
34 dirs[:] = [d for d in dirs if d.endswith('_apk')]
35 for f in (f for f in files if check(os.path.join(root, f))):
36 ret_files.add(os.path.relpath(os.path.join(root, f), build_dir))
37 return ret_files
38
39
40 def run_zap_timestamp(src_dir, filepath):
41 """Run zap_timestamp.exe on a PE binary."""
42 assert sys.platform == 'win32'
18 syzygy_dir = os.path.join( 43 syzygy_dir = os.path.join(
19 src_dir, 'third_party', 'syzygy', 'binaries', 'exe') 44 src_dir, 'third_party', 'syzygy', 'binaries', 'exe')
20 zap_timestamp_exe = os.path.join(syzygy_dir, 'zap_timestamp.exe') 45 zap_timestamp_exe = os.path.join(syzygy_dir, 'zap_timestamp.exe')
21 print('Processing: %s' % os.path.basename(filepath)) 46 print('Processing: %s' % os.path.basename(filepath))
22 proc = subprocess.Popen( 47 proc = subprocess.Popen(
23 [zap_timestamp_exe, '--input-image=%s' % filepath, '--overwrite'], 48 [zap_timestamp_exe, '--input-image=%s' % filepath, '--overwrite'],
24 stdout=subprocess.PIPE, stderr=subprocess.STDOUT) 49 stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
25 log, _ = proc.communicate() 50 log, _ = proc.communicate()
26 if proc.returncode != 0: 51 if proc.returncode != 0:
27 print >> sys.stderr, log 52 print >> sys.stderr, log
28 return proc.returncode 53 return proc.returncode
29 54
30 55
31 def RemovePEMetadata(build_dir, src_dir): 56 def remove_pe_metadata(filename, src_dir):
32 """Remove the build metadata from a PE file.""" 57 """Remove the build metadata from a PE file."""
33 files = (i for i in os.listdir(build_dir) if i.endswith(('.dll', '.exe'))) 58 # Only run zap_timestamp on the PE files for which we have a PDB.
59 ret = 0
60 if os.path.exists(filename + '.pdb'):
61 ret = run_zap_timestamp(src_dir, filename)
62 return ret
34 63
64
65 def remove_apk_timestamps(filename):
66 """Remove the timestamps embedded in an apk archive."""
67 print('Processing: %s' % os.path.basename(filename))
68 with zipfile.ZipFile(filename, 'r') as zf:
69 # Creates a temporary file.
70 try:
71 out_file, out_filename = tempfile.mkstemp(prefix='remote_apk_timestamp')
72 finally:
M-A Ruel 2014/11/06 19:44:09 No, I meant in finally at end of function: if os.
Sébastien Marchand 2014/11/06 20:40:11 Of course ! Sorry :)
73 os.close(out_file)
74 with zipfile.ZipFile(out_filename, 'w') as zf_o:
75 # Copy the data from the original file to the new one.
76 for info in zf.infolist():
77 # Overwrite the timestamp with a constant value.
78 info.date_time = (1980, 1, 1, 0, 0, 0)
79 zf_o.writestr(info, zf.read(info.filename))
80 # Remove the original file and replace it by the modified one.
81 os.remove(filename)
82 shutil.move(out_filename, filename)
83
84
85 def remove_metadata(build_dir, src_dir, recursive):
86 """Remove the build metadata from the artifacts of a build."""
35 with open(os.path.join(BASE_DIR, 'deterministic_build_blacklist.json')) as f: 87 with open(os.path.join(BASE_DIR, 'deterministic_build_blacklist.json')) as f:
36 blacklist = frozenset(json.load(f)) 88 blacklist = frozenset(json.load(f))
89 files = get_files_to_clean(build_dir, recursive) - blacklist
90 failed_files = []
91 ret = 0
92 for f in files:
93 if f.endswith(('.dll', '.exe')):
94 if remove_pe_metadata(os.path.join(build_dir, f), src_dir):
95 ret = 1
96 failed_files.append(f)
97 elif f.endswith('.apk'):
98 remove_apk_timestamps(os.path.join(build_dir, f))
37 99
38 failed = [] 100 if failed_files:
39 for filename in files: 101 print >> sys.stderr, 'Failed for the following files:'
40 # Ignore the blacklisted files. 102 print >> sys.stderr, '\n'.join(' ' + i for i in sorted(failed_files))
41 if filename in blacklist:
42 print('Ignored: %s' % filename)
43 continue
44 # Only run zap_timestamp on the PE files for which we have a PDB.
45 if os.path.exists(os.path.join(build_dir, filename + '.pdb')):
46 ret = RunZapTimestamp(src_dir, os.path.join(build_dir, filename))
47 if ret != 0:
48 failed.append(filename)
49
50 if failed:
51 print >> sys.stderr, 'zap_timestamp.exe failed for the following files:'
52 print >> sys.stderr, '\n'.join(' ' + i for i in sorted(failed))
53 return 1 103 return 1
54 104
55 return 0 105 return ret
56 106
57 107
58 def main(): 108 def main():
59 parser = optparse.OptionParser(usage='%prog [options]') 109 parser = optparse.OptionParser(usage='%prog [options]')
60 # TODO(sebmarchand): Add support for reading the list of artifact from a 110 # TODO(sebmarchand): Add support for reading the list of artifact from a
61 # .isolated file. 111 # .isolated file.
62 parser.add_option('--build-dir', help='The build directory.') 112 parser.add_option('--build-dir', help='The build directory.')
63 parser.add_option('--src-dir', help='The source directory.') 113 parser.add_option('--src-dir', help='The source directory.')
114 parser.add_option('-r', '--recursive', action='store_true', default=False,
115 help='Indicates if the script should be recursive.')
64 options, _ = parser.parse_args() 116 options, _ = parser.parse_args()
65 117
66 if not options.build_dir: 118 if not options.build_dir:
67 parser.error('--build-dir is required') 119 parser.error('--build-dir is required')
68 if not options.src_dir: 120
121 if sys.platform == 'win32' and not options.src_dir:
69 parser.error('--src-dir is required') 122 parser.error('--src-dir is required')
70 123
71 # There's nothing to do for the non-Windows platform yet. 124 return remove_metadata(options.build_dir, options.src_dir, options.recursive)
72 if sys.platform == 'win32':
73 return RemovePEMetadata(options.build_dir, options.src_dir)
74 125
75 126
76 if __name__ == '__main__': 127 if __name__ == '__main__':
77 sys.exit(main()) 128 sys.exit(main())
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698