[email protected] | dcfc4dd | 2010-07-28 23:02:22 | [diff] [blame] | 1 | #!/usr/bin/env python |
[email protected] | 3ea8fe2 | 2012-05-23 07:08:56 | [diff] [blame] | 2 | # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | dcfc4dd | 2010-07-28 23:02:22 | [diff] [blame] | 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
[email protected] | 3f09d18 | 2011-11-23 19:13:44 | [diff] [blame] | 6 | """Extracts a single file from a CAB archive.""" |
[email protected] | dcfc4dd | 2010-07-28 23:02:22 | [diff] [blame] | 7 | |
Raul Tambre | 9e24293b | 2019-05-12 06:11:07 | [diff] [blame] | 8 | from __future__ import print_function |
| 9 | |
[email protected] | dcfc4dd | 2010-07-28 23:02:22 | [diff] [blame] | 10 | import os |
[email protected] | c6f0ea4 | 2011-12-13 22:20:46 | [diff] [blame] | 11 | import shutil |
[email protected] | dcfc4dd | 2010-07-28 23:02:22 | [diff] [blame] | 12 | import subprocess |
| 13 | import sys |
[email protected] | 7741480 | 2011-12-13 00:34:15 | [diff] [blame] | 14 | import tempfile |
[email protected] | dcfc4dd | 2010-07-28 23:02:22 | [diff] [blame] | 15 | |
[email protected] | 3ea8fe2 | 2012-05-23 07:08:56 | [diff] [blame] | 16 | def run_quiet(*args): |
[email protected] | 7dcd93c | 2013-11-02 02:05:53 | [diff] [blame] | 17 | """Run 'expand' suppressing noisy output. Returns returncode from process.""" |
[email protected] | 3ea8fe2 | 2012-05-23 07:08:56 | [diff] [blame] | 18 | popen = subprocess.Popen(args, stdout=subprocess.PIPE) |
| 19 | out, _ = popen.communicate() |
| 20 | if popen.returncode: |
| 21 | # expand emits errors to stdout, so if we fail, then print that out. |
Raul Tambre | 9e24293b | 2019-05-12 06:11:07 | [diff] [blame] | 22 | print(out) |
[email protected] | 3ea8fe2 | 2012-05-23 07:08:56 | [diff] [blame] | 23 | return popen.returncode |
[email protected] | 9645286 | 2011-12-13 03:32:12 | [diff] [blame] | 24 | |
[email protected] | 3f09d18 | 2011-11-23 19:13:44 | [diff] [blame] | 25 | def main(): |
| 26 | if len(sys.argv) != 4: |
Raul Tambre | 9e24293b | 2019-05-12 06:11:07 | [diff] [blame] | 27 | print('Usage: extract_from_cab.py cab_path archived_file output_dir') |
[email protected] | 3f09d18 | 2011-11-23 19:13:44 | [diff] [blame] | 28 | return 1 |
[email protected] | dcfc4dd | 2010-07-28 23:02:22 | [diff] [blame] | 29 | |
[email protected] | 3f09d18 | 2011-11-23 19:13:44 | [diff] [blame] | 30 | [cab_path, archived_file, output_dir] = sys.argv[1:] |
[email protected] | dcfc4dd | 2010-07-28 23:02:22 | [diff] [blame] | 31 | |
[email protected] | c6f0ea4 | 2011-12-13 22:20:46 | [diff] [blame] | 32 | # Expand.exe does its work in a fixed-named temporary directory created within |
| 33 | # the given output directory. This is a problem for concurrent extractions, so |
| 34 | # create a unique temp dir within the desired output directory to work around |
| 35 | # this limitation. |
| 36 | temp_dir = tempfile.mkdtemp(dir=output_dir) |
| 37 | |
[email protected] | 7741480 | 2011-12-13 00:34:15 | [diff] [blame] | 38 | try: |
| 39 | # Invoke the Windows expand utility to extract the file. |
[email protected] | 3ea8fe2 | 2012-05-23 07:08:56 | [diff] [blame] | 40 | level = run_quiet('expand', cab_path, '-F:' + archived_file, temp_dir) |
[email protected] | c6f0ea4 | 2011-12-13 22:20:46 | [diff] [blame] | 41 | if level == 0: |
| 42 | # Move the output file into place, preserving expand.exe's behavior of |
| 43 | # paving over any preexisting file. |
| 44 | output_file = os.path.join(output_dir, archived_file) |
| 45 | try: |
| 46 | os.remove(output_file) |
| 47 | except OSError: |
| 48 | pass |
| 49 | os.rename(os.path.join(temp_dir, archived_file), output_file) |
[email protected] | 7741480 | 2011-12-13 00:34:15 | [diff] [blame] | 50 | finally: |
[email protected] | c6f0ea4 | 2011-12-13 22:20:46 | [diff] [blame] | 51 | shutil.rmtree(temp_dir, True) |
| 52 | |
| 53 | if level != 0: |
| 54 | return level |
[email protected] | 3f09d18 | 2011-11-23 19:13:44 | [diff] [blame] | 55 | |
| 56 | # The expand utility preserves the modification date and time of the archived |
| 57 | # file. Touch the extracted file. This helps build systems that compare the |
| 58 | # modification times of input and output files to determine whether to do an |
| 59 | # action. |
| 60 | os.utime(os.path.join(output_dir, archived_file), None) |
| 61 | return 0 |
| 62 | |
| 63 | |
| 64 | if __name__ == '__main__': |
| 65 | sys.exit(main()) |