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 |
| 9 | import subprocess |
| 10 | import sys |
apatrick@chromium.org | 38ff3d2 | 2011-12-13 09:34:15 +0900 | [diff] [blame^] | 11 | import tempfile |
apatrick@chromium.org | 1218a88 | 2010-07-29 08:02:22 +0900 | [diff] [blame] | 12 | |
apatrick@chromium.org | 38ff3d2 | 2011-12-13 09:34:15 +0900 | [diff] [blame^] | 13 | lock_file = os.path.join(tempfile.gettempdir(), 'expand.lock') |
| 14 | |
| 15 | def acquire_lock(): |
| 16 | while True: |
| 17 | try: |
| 18 | fd = os.open(lock_file, os.O_CREAT | os.O_EXCL | os.O_RDWR) |
| 19 | return fd |
| 20 | except OSError as e: |
| 21 | if e.errno != errno.EEXIST: |
| 22 | raise |
| 23 | print 'Cab extraction could not get exclusive lock. Retrying in 1 sec...' |
| 24 | time.sleep(1000) |
| 25 | |
| 26 | def release_lock(fd): |
| 27 | os.close(fd) |
| 28 | os.unlink(lock_file) |
apatrick@chromium.org | 1218a88 | 2010-07-29 08:02:22 +0900 | [diff] [blame] | 29 | |
maruel@chromium.org | 1f144a3 | 2011-11-24 04:13:44 +0900 | [diff] [blame] | 30 | def main(): |
| 31 | if len(sys.argv) != 4: |
| 32 | print 'Usage: extract_from_cab.py cab_path archived_file output_dir' |
| 33 | return 1 |
apatrick@chromium.org | 1218a88 | 2010-07-29 08:02:22 +0900 | [diff] [blame] | 34 | |
maruel@chromium.org | 1f144a3 | 2011-11-24 04:13:44 +0900 | [diff] [blame] | 35 | [cab_path, archived_file, output_dir] = sys.argv[1:] |
apatrick@chromium.org | 1218a88 | 2010-07-29 08:02:22 +0900 | [diff] [blame] | 36 | |
apatrick@chromium.org | 38ff3d2 | 2011-12-13 09:34:15 +0900 | [diff] [blame^] | 37 | lock_fd = acquire_lock() |
| 38 | try: |
| 39 | # Invoke the Windows expand utility to extract the file. |
maruel@chromium.org | c04ced4 | 2011-12-09 05:47:15 +0900 | [diff] [blame] | 40 | level = subprocess.call( |
| 41 | ['expand', cab_path, '-F:' + archived_file, output_dir]) |
| 42 | if level != 0: |
apatrick@chromium.org | 38ff3d2 | 2011-12-13 09:34:15 +0900 | [diff] [blame^] | 43 | print 'Cab extraction(%s, %s, %s) failed.' % ( |
| 44 | cab_path, archived_file, output_dir) |
| 45 | print 'Trying a second time.' |
| 46 | level = subprocess.call( |
| 47 | ['expand', cab_path, '-F:' + archived_file, output_dir]) |
| 48 | if level != 0: |
| 49 | return level |
| 50 | finally: |
| 51 | release_lock(lock_fd) |
maruel@chromium.org | 1f144a3 | 2011-11-24 04:13:44 +0900 | [diff] [blame] | 52 | |
| 53 | # The expand utility preserves the modification date and time of the archived |
| 54 | # file. Touch the extracted file. This helps build systems that compare the |
| 55 | # modification times of input and output files to determine whether to do an |
| 56 | # action. |
| 57 | os.utime(os.path.join(output_dir, archived_file), None) |
| 58 | return 0 |
| 59 | |
| 60 | |
| 61 | if __name__ == '__main__': |
| 62 | sys.exit(main()) |