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 |
| 11 | |
apatrick@chromium.org | 1218a88 | 2010-07-29 08:02:22 +0900 | [diff] [blame] | 12 | |
maruel@chromium.org | 1f144a3 | 2011-11-24 04:13:44 +0900 | [diff] [blame] | 13 | def main(): |
| 14 | if len(sys.argv) != 4: |
| 15 | print 'Usage: extract_from_cab.py cab_path archived_file output_dir' |
| 16 | return 1 |
apatrick@chromium.org | 1218a88 | 2010-07-29 08:02:22 +0900 | [diff] [blame] | 17 | |
maruel@chromium.org | 1f144a3 | 2011-11-24 04:13:44 +0900 | [diff] [blame] | 18 | [cab_path, archived_file, output_dir] = sys.argv[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 | # Invoke the Windows expand utility to extract the file. |
| 21 | level = subprocess.call( |
| 22 | ['expand', cab_path, '-F:' + archived_file, output_dir]) |
| 23 | if level != 0: |
maruel@chromium.org | c04ced4 | 2011-12-09 05:47:15 +0900 | [diff] [blame^] | 24 | print 'Cab extraction(%s, %s, %s) failed.' % ( |
| 25 | cab_path, archived_file, output_dir) |
| 26 | print 'Trying a second time.' |
| 27 | level = subprocess.call( |
| 28 | ['expand', cab_path, '-F:' + archived_file, output_dir]) |
| 29 | if level != 0: |
| 30 | return level |
maruel@chromium.org | 1f144a3 | 2011-11-24 04:13:44 +0900 | [diff] [blame] | 31 | |
| 32 | # The expand utility preserves the modification date and time of the archived |
| 33 | # file. Touch the extracted file. This helps build systems that compare the |
| 34 | # modification times of input and output files to determine whether to do an |
| 35 | # action. |
| 36 | os.utime(os.path.join(output_dir, archived_file), None) |
| 37 | return 0 |
| 38 | |
| 39 | |
| 40 | if __name__ == '__main__': |
| 41 | sys.exit(main()) |