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