| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # | 2 # |
| 3 # Copyright 2014 The Chromium Authors. All rights reserved. | 3 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
| 5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
| 6 | 6 |
| 7 """Archives a set of files. | 7 """Archives a set of files. |
| 8 """ | 8 """ |
| 9 | 9 |
| 10 import ast | 10 import ast |
| 11 import optparse | 11 import optparse |
| 12 import os | 12 import os |
| 13 import sys | 13 import sys |
| 14 import zipfile | 14 import zipfile |
| 15 | 15 |
| 16 def DoZip(inputs, zip_inputs, output, base_dir): | 16 def DoZip(inputs, link_inputs, zip_inputs, output, base_dir): |
| 17 files = [] | 17 files = [] |
| 18 with zipfile.ZipFile(output, 'w', zipfile.ZIP_DEFLATED) as outfile: | 18 with zipfile.ZipFile(output, 'w', zipfile.ZIP_DEFLATED) as outfile: |
| 19 for f in inputs: | 19 for f in inputs: |
| 20 file_name = os.path.relpath(f, base_dir) | 20 file_name = os.path.relpath(f, base_dir) |
| 21 files.append(file_name) | 21 files.append(file_name) |
| 22 outfile.write(f, file_name) | 22 outfile.write(f, file_name) |
| 23 for f in link_inputs: |
| 24 realf = os.path.realpath(f) # Resolve symlinks. |
| 25 file_name = os.path.relpath(realf, base_dir) |
| 26 files.append(file_name) |
| 27 outfile.write(realf, file_name) |
| 23 for zf_name in zip_inputs: | 28 for zf_name in zip_inputs: |
| 24 with zipfile.ZipFile(zf_name, 'r') as zf: | 29 with zipfile.ZipFile(zf_name, 'r') as zf: |
| 25 for f in zf.namelist(): | 30 for f in zf.namelist(): |
| 26 if f not in files: | 31 if f not in files: |
| 27 files.append(f) | 32 files.append(f) |
| 28 with zf.open(f) as zff: | 33 with zf.open(f) as zff: |
| 29 outfile.writestr(f, zff.read()) | 34 outfile.writestr(f, zff.read()) |
| 30 | 35 |
| 31 | 36 |
| 32 def main(): | 37 def main(): |
| 33 parser = optparse.OptionParser() | 38 parser = optparse.OptionParser() |
| 34 | 39 |
| 35 parser.add_option('--inputs', help='List of files to archive.') | 40 parser.add_option('--inputs', help='List of files to archive.') |
| 41 parser.add_option('--link-inputs', |
| 42 help='List of files to archive. Symbolic links are resolved.') |
| 36 parser.add_option('--zip-inputs', help='List of zip files to re-archive.') | 43 parser.add_option('--zip-inputs', help='List of zip files to re-archive.') |
| 37 parser.add_option('--output', help='Path to output archive.') | 44 parser.add_option('--output', help='Path to output archive.') |
| 38 parser.add_option('--base-dir', | 45 parser.add_option('--base-dir', |
| 39 help='If provided, the paths in the archive will be ' | 46 help='If provided, the paths in the archive will be ' |
| 40 'relative to this directory', default='.') | 47 'relative to this directory', default='.') |
| 41 | 48 |
| 42 options, _ = parser.parse_args() | 49 options, _ = parser.parse_args() |
| 43 | 50 |
| 44 inputs = [] | 51 inputs = [] |
| 45 if (options.inputs): | 52 if (options.inputs): |
| 46 inputs = ast.literal_eval(options.inputs) | 53 inputs = ast.literal_eval(options.inputs) |
| 54 link_inputs = [] |
| 55 if options.link_inputs: |
| 56 link_inputs = ast.literal_eval(options.link_inputs) |
| 47 zip_inputs = [] | 57 zip_inputs = [] |
| 48 if options.zip_inputs: | 58 if options.zip_inputs: |
| 49 zip_inputs = ast.literal_eval(options.zip_inputs) | 59 zip_inputs = ast.literal_eval(options.zip_inputs) |
| 50 output = options.output | 60 output = options.output |
| 51 base_dir = options.base_dir | 61 base_dir = options.base_dir |
| 52 | 62 |
| 53 DoZip(inputs, zip_inputs, output, base_dir) | 63 DoZip(inputs, link_inputs, zip_inputs, output, base_dir) |
| 54 | 64 |
| 55 if __name__ == '__main__': | 65 if __name__ == '__main__': |
| 56 sys.exit(main()) | 66 sys.exit(main()) |
| OLD | NEW |