apatrick@chromium.org | 1218a88 | 2010-07-29 08:02:22 +0900 | [diff] [blame] | 1 | #!/usr/bin/env python |
maruel@chromium.org | 1f144a3 | 2011-11-24 04:13:44 +0900 | [diff] [blame] | 2 | # Copyright (c) 2011 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 | |
apatrick@chromium.org | 3faf3c8 | 2011-12-13 12:32:12 +0900 | [diff] [blame] | 14 | |
maruel@chromium.org | 1f144a3 | 2011-11-24 04:13:44 +0900 | [diff] [blame] | 15 | def main(): |
| 16 | if len(sys.argv) != 4: |
| 17 | print 'Usage: extract_from_cab.py cab_path archived_file output_dir' |
| 18 | return 1 |
apatrick@chromium.org | 1218a88 | 2010-07-29 08:02:22 +0900 | [diff] [blame] | 19 | |
maruel@chromium.org | 1f144a3 | 2011-11-24 04:13:44 +0900 | [diff] [blame] | 20 | [cab_path, archived_file, output_dir] = sys.argv[1:] |
apatrick@chromium.org | 1218a88 | 2010-07-29 08:02:22 +0900 | [diff] [blame] | 21 | |
grt@chromium.org | 509c754 | 2011-12-14 07:20:46 +0900 | [diff] [blame^] | 22 | # Expand.exe does its work in a fixed-named temporary directory created within |
| 23 | # the given output directory. This is a problem for concurrent extractions, so |
| 24 | # create a unique temp dir within the desired output directory to work around |
| 25 | # this limitation. |
| 26 | temp_dir = tempfile.mkdtemp(dir=output_dir) |
| 27 | |
apatrick@chromium.org | 38ff3d2 | 2011-12-13 09:34:15 +0900 | [diff] [blame] | 28 | try: |
| 29 | # Invoke the Windows expand utility to extract the file. |
maruel@chromium.org | c04ced4 | 2011-12-09 05:47:15 +0900 | [diff] [blame] | 30 | level = subprocess.call( |
grt@chromium.org | 509c754 | 2011-12-14 07:20:46 +0900 | [diff] [blame^] | 31 | ['expand', cab_path, '-F:' + archived_file, temp_dir]) |
| 32 | if level == 0: |
| 33 | # Move the output file into place, preserving expand.exe's behavior of |
| 34 | # paving over any preexisting file. |
| 35 | output_file = os.path.join(output_dir, archived_file) |
| 36 | try: |
| 37 | os.remove(output_file) |
| 38 | except OSError: |
| 39 | pass |
| 40 | os.rename(os.path.join(temp_dir, archived_file), output_file) |
apatrick@chromium.org | 38ff3d2 | 2011-12-13 09:34:15 +0900 | [diff] [blame] | 41 | finally: |
grt@chromium.org | 509c754 | 2011-12-14 07:20:46 +0900 | [diff] [blame^] | 42 | shutil.rmtree(temp_dir, True) |
| 43 | |
| 44 | if level != 0: |
| 45 | return level |
maruel@chromium.org | 1f144a3 | 2011-11-24 04:13:44 +0900 | [diff] [blame] | 46 | |
| 47 | # The expand utility preserves the modification date and time of the archived |
| 48 | # file. Touch the extracted file. This helps build systems that compare the |
| 49 | # modification times of input and output files to determine whether to do an |
| 50 | # action. |
| 51 | os.utime(os.path.join(output_dir, archived_file), None) |
| 52 | return 0 |
| 53 | |
| 54 | |
| 55 | if __name__ == '__main__': |
| 56 | sys.exit(main()) |