Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """Extracts a single file from a CAB archive.""" | 6 """Extracts a single file from a CAB archive.""" |
| 7 | 7 |
| 8 import os | 8 import os |
| 9 import subprocess | 9 import subprocess |
| 10 import sys | 10 import sys |
| 11 import tempfile | 11 import tempfile |
| 12 | 12 |
| 13 lock_file = os.path.join(tempfile.gettempdir(), 'expand.lock') | |
| 14 | |
| 15 | |
| 16 def acquire_lock(): | |
| 17 while True: | |
| 18 try: | |
| 19 fd = os.open(lock_file, os.O_CREAT | os.O_EXCL | os.O_RDWR) | |
| 20 return fd | |
| 21 except OSError as e: | |
| 22 if e.errno != errno.EEXIST: | |
| 23 raise | |
| 24 print 'Cab extraction could not get exclusive lock. Retrying in 100ms...' | |
| 25 time.sleep(0.1) | |
| 26 | |
| 27 | |
| 28 def release_lock(fd): | |
| 29 os.close(fd) | |
| 30 os.unlink(lock_file) | |
| 31 | |
| 32 | 13 |
| 33 def main(): | 14 def main(): |
| 34 if len(sys.argv) != 4: | 15 if len(sys.argv) != 4: |
| 35 print 'Usage: extract_from_cab.py cab_path archived_file output_dir' | 16 print 'Usage: extract_from_cab.py cab_path archived_file output_dir' |
| 36 return 1 | 17 return 1 |
| 37 | 18 |
| 38 [cab_path, archived_file, output_dir] = sys.argv[1:] | 19 [cab_path, archived_file, output_dir] = sys.argv[1:] |
| 39 | 20 |
| 40 lock_fd = acquire_lock() | 21 # Expand.exe does its work in a fixed-named temporary directory created within |
| 22 # the given output directory. This is a problem for concurrent extractions, so | |
| 23 # create a unique temp dir within the desired output directory to work around | |
| 24 # this limitation. | |
| 25 temp_dir = tempfile.mkdtemp(dir=output_dir) | |
| 26 | |
| 41 try: | 27 try: |
| 28 # Setup the environment in case other versions of expand.exe do their work | |
| 29 # in TEMP/TMP instead of or in addition to the output directory. | |
| 30 subprocess_env = os.environ.copy() | |
| 31 subprocess_env['TEMP'] = temp_dir | |
| 32 subprocess_env['TMP'] = temp_dir | |
| 42 # Invoke the Windows expand utility to extract the file. | 33 # Invoke the Windows expand utility to extract the file. |
| 43 level = subprocess.call( | 34 level = subprocess.call( |
| 44 ['expand', cab_path, '-F:' + archived_file, output_dir]) | 35 ['expand', cab_path, '-F:' + archived_file, temp_dir], |
| 45 if level != 0: | 36 env=subprocess_env) |
| 46 return level | 37 if level == 0: |
| 38 # Move the output file into place, preserving expand.exe's behavior of | |
| 39 # paving over any preexsing file. | |
|
apatrick_chromium
2011/12/13 19:17:09
typo: preexisting
grt (UTC plus 2)
2011/12/13 19:25:13
Done.
| |
| 40 output_file = os.path.join(output_dir, archived_file) | |
| 41 try: | |
| 42 os.remove(output_file) | |
| 43 except OSError: | |
| 44 pass | |
| 45 os.rename(os.path.join(temp_dir, archived_file), output_file) | |
| 47 finally: | 46 finally: |
| 48 release_lock(lock_fd) | 47 try: |
| 48 os.rmdir(temp_dir) | |
|
grt (UTC plus 2)
2011/12/13 05:29:55
question for maruel: is shutil.rmtree(temp_dir, Tr
| |
| 49 except OSError: | |
| 50 print 'Warning: unable to delete temporary directory ' + temp_dir | |
| 51 | |
| 52 if level != 0: | |
| 53 return level | |
| 49 | 54 |
| 50 # The expand utility preserves the modification date and time of the archived | 55 # The expand utility preserves the modification date and time of the archived |
| 51 # file. Touch the extracted file. This helps build systems that compare the | 56 # file. Touch the extracted file. This helps build systems that compare the |
| 52 # modification times of input and output files to determine whether to do an | 57 # modification times of input and output files to determine whether to do an |
| 53 # action. | 58 # action. |
| 54 os.utime(os.path.join(output_dir, archived_file), None) | 59 os.utime(os.path.join(output_dir, archived_file), None) |
| 55 return 0 | 60 return 0 |
| 56 | 61 |
| 57 | 62 |
| 58 if __name__ == '__main__': | 63 if __name__ == '__main__': |
| 59 sys.exit(main()) | 64 sys.exit(main()) |
| OLD | NEW |