OLD | NEW |
---|---|
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Standalone python script to unzip an archive. Intended to be used by 'zip' | 5 """Standalone python script to unzip an archive. Intended to be used by 'zip' |
6 recipe module internally. Should not be used elsewhere. | 6 recipe module internally. Should not be used elsewhere. |
7 """ | 7 """ |
8 | 8 |
9 import json | 9 import json |
10 import os | 10 import os |
11 import shutil | 11 import shutil |
12 import subprocess | 12 import subprocess |
13 import sys | 13 import sys |
14 import zipfile | 14 import zipfile |
15 | 15 |
16 | 16 |
17 def unzip_with_subprocess(zip_file, output): | 17 def unzip_with_subprocess(zip_file, output, quiet): |
18 """Unzips an archive using 'zip' utility. | 18 """Unzips an archive using 'zip' utility. |
19 | 19 |
20 Works only on Linux and Mac, uses system 'zip' program. | 20 Works only on Linux and Mac, uses system 'zip' program. |
21 | 21 |
22 Args: | 22 Args: |
23 zip_file: absolute path to an archive to unzip. | 23 zip_file: absolute path to an archive to unzip. |
24 output: existing directory to unzip to. | 24 output: existing directory to unzip to. |
25 quiet (bool): If True, instruct the subprocess to unzip with | |
26 minimal output. | |
25 | 27 |
26 Returns: | 28 Returns: |
27 Exit code (0 on success). | 29 Exit code (0 on success). |
28 """ | 30 """ |
31 args = ['unzip'] | |
32 if quiet: | |
33 args += ['-q'] | |
34 args += [zip_file] | |
35 | |
29 return subprocess.call( | 36 return subprocess.call( |
30 args=['unzip', zip_file], | 37 args=args, |
31 cwd=output) | 38 cwd=output) |
32 | 39 |
33 | 40 |
34 def unzip_with_python(zip_file, output): | 41 def unzip_with_python(zip_file, output): |
35 """Unzips an archive using 'zipfile' python module. | 42 """Unzips an archive using 'zipfile' python module. |
36 | 43 |
37 Works everywhere where python works (Windows and Posix). | 44 Works everywhere where python works (Windows and Posix). |
38 | 45 |
39 Args: | 46 Args: |
40 zip_file: absolute path to an archive to unzip. | 47 zip_file: absolute path to an archive to unzip. |
41 output: existing directory to unzip to. | 48 output: existing directory to unzip to. |
42 | 49 |
43 Returns: | 50 Returns: |
44 Exit code (0 on success). | 51 Exit code (0 on success). |
45 """ | 52 """ |
46 with zipfile.ZipFile(zip_file) as zip_file_obj: | 53 with zipfile.ZipFile(zip_file) as zip_file_obj: |
47 for name in zip_file_obj.namelist(): | 54 for name in zip_file_obj.namelist(): |
48 print 'Extracting %s' % name | 55 print 'Extracting %s' % name |
49 zip_file_obj.extract(name, output) | 56 zip_file_obj.extract(name, output) |
50 return 0 | 57 return 0 |
51 | 58 |
52 | 59 |
53 def main(): | 60 def main(): |
54 # See zip/api.py, def unzip(...) for format of |data|. | 61 # See zip/api.py, def unzip(...) for format of |data|. |
55 data = json.load(sys.stdin) | 62 data = json.load(sys.stdin) |
56 output = data['output'] | 63 output = data['output'] |
57 zip_file = data['zip_file'] | 64 zip_file = data['zip_file'] |
65 quiet = data.get('quiet', False) | |
iannucci
2016/09/30 21:05:21
this script will only ever be invoked by recipes.
dnj
2016/09/30 22:54:54
Done.
| |
58 | 66 |
59 # Archive path should exist and be an absolute path to a file. | 67 # Archive path should exist and be an absolute path to a file. |
60 assert os.path.exists(zip_file), zip_file | 68 assert os.path.exists(zip_file), zip_file |
61 assert os.path.isfile(zip_file), zip_file | 69 assert os.path.isfile(zip_file), zip_file |
62 | 70 |
63 # Output path should be an absolute path, and should NOT exist. | 71 # Output path should be an absolute path, and should NOT exist. |
64 assert os.path.isabs(output), output | 72 assert os.path.isabs(output), output |
65 assert not os.path.exists(output), output | 73 assert not os.path.exists(output), output |
66 | 74 |
67 print 'Unzipping %s...' % zip_file | 75 print 'Unzipping %s...' % zip_file |
68 exit_code = -1 | 76 exit_code = -1 |
69 try: | 77 try: |
70 os.makedirs(output) | 78 os.makedirs(output) |
71 if sys.platform == 'win32': | 79 if sys.platform == 'win32': |
72 # Used on Windows, since there's no builtin 'unzip' utility there. | 80 # Used on Windows, since there's no builtin 'unzip' utility there. |
73 exit_code = unzip_with_python(zip_file, output) | 81 exit_code = unzip_with_python(zip_file, output) |
74 else: | 82 else: |
75 # On mac and linux 'unzip' utility handles symlink and file modes. | 83 # On mac and linux 'unzip' utility handles symlink and file modes. |
76 exit_code = unzip_with_subprocess(zip_file, output) | 84 exit_code = unzip_with_subprocess(zip_file, output, quiet) |
77 finally: | 85 finally: |
78 # On non-zero exit code or on unexpected exception, clean up. | 86 # On non-zero exit code or on unexpected exception, clean up. |
79 if exit_code: | 87 if exit_code: |
80 shutil.rmtree(output, ignore_errors=True) | 88 shutil.rmtree(output, ignore_errors=True) |
81 return exit_code | 89 return exit_code |
82 | 90 |
83 | 91 |
84 if __name__ == '__main__': | 92 if __name__ == '__main__': |
85 sys.exit(main()) | 93 sys.exit(main()) |
OLD | NEW |