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 shutil | |
| 9 import subprocess | 10 import subprocess |
| 10 import sys | 11 import sys |
| 11 import tempfile | 12 import tempfile |
| 12 | 13 |
| 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 | 14 |
| 33 def main(): | 15 def main(): |
| 34 if len(sys.argv) != 4: | 16 if len(sys.argv) != 4: |
| 35 print 'Usage: extract_from_cab.py cab_path archived_file output_dir' | 17 print 'Usage: extract_from_cab.py cab_path archived_file output_dir' |
| 36 return 1 | 18 return 1 |
| 37 | 19 |
| 38 [cab_path, archived_file, output_dir] = sys.argv[1:] | 20 [cab_path, archived_file, output_dir] = sys.argv[1:] |
| 39 | 21 |
| 40 lock_fd = acquire_lock() | 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 | |
| 41 try: | 28 try: |
| 42 # Invoke the Windows expand utility to extract the file. | 29 # Invoke the Windows expand utility to extract the file. |
| 43 level = subprocess.call( | 30 level = subprocess.call( |
| 44 ['expand', cab_path, '-F:' + archived_file, output_dir]) | 31 ['expand', cab_path, '-F:' + archived_file, temp_dir]) |
| 45 if level != 0: | 32 if level == 0: |
| 46 return level | 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 | |
|
M-A Ruel
2011/12/13 20:17:10
Can you add a warning print out here? Do you know
grt (UTC plus 2)
2011/12/13 20:25:48
it fails if the file doesn't exist, which is actua
M-A Ruel
2011/12/13 20:26:46
That's fine, I don't really mind.
| |
| 40 os.rename(os.path.join(temp_dir, archived_file), output_file) | |
| 47 finally: | 41 finally: |
| 48 release_lock(lock_fd) | 42 shutil.rmtree(temp_dir, True) |
| 43 | |
| 44 if level != 0: | |
| 45 return level | |
| 49 | 46 |
| 50 # The expand utility preserves the modification date and time of the archived | 47 # 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 | 48 # 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 | 49 # modification times of input and output files to determine whether to do an |
| 53 # action. | 50 # action. |
| 54 os.utime(os.path.join(output_dir, archived_file), None) | 51 os.utime(os.path.join(output_dir, archived_file), None) |
| 55 return 0 | 52 return 0 |
| 56 | 53 |
| 57 | 54 |
| 58 if __name__ == '__main__': | 55 if __name__ == '__main__': |
| 59 sys.exit(main()) | 56 sys.exit(main()) |
| OLD | NEW |