| 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') | 13 lock_file = os.path.join(tempfile.gettempdir(), 'expand.lock') |
| 14 | 14 |
| 15 |
| 15 def acquire_lock(): | 16 def acquire_lock(): |
| 16 while True: | 17 while True: |
| 17 try: | 18 try: |
| 18 fd = os.open(lock_file, os.O_CREAT | os.O_EXCL | os.O_RDWR) | 19 fd = os.open(lock_file, os.O_CREAT | os.O_EXCL | os.O_RDWR) |
| 19 return fd | 20 return fd |
| 20 except OSError as e: | 21 except OSError as e: |
| 21 if e.errno != errno.EEXIST: | 22 if e.errno != errno.EEXIST: |
| 22 raise | 23 raise |
| 23 print 'Cab extraction could not get exclusive lock. Retrying in 1 sec...' | 24 print 'Cab extraction could not get exclusive lock. Retrying in 100ms...' |
| 24 time.sleep(1000) | 25 time.sleep(0.1) |
| 26 |
| 25 | 27 |
| 26 def release_lock(fd): | 28 def release_lock(fd): |
| 27 os.close(fd) | 29 os.close(fd) |
| 28 os.unlink(lock_file) | 30 os.unlink(lock_file) |
| 29 | 31 |
| 32 |
| 30 def main(): | 33 def main(): |
| 31 if len(sys.argv) != 4: | 34 if len(sys.argv) != 4: |
| 32 print 'Usage: extract_from_cab.py cab_path archived_file output_dir' | 35 print 'Usage: extract_from_cab.py cab_path archived_file output_dir' |
| 33 return 1 | 36 return 1 |
| 34 | 37 |
| 35 [cab_path, archived_file, output_dir] = sys.argv[1:] | 38 [cab_path, archived_file, output_dir] = sys.argv[1:] |
| 36 | 39 |
| 37 lock_fd = acquire_lock() | 40 lock_fd = acquire_lock() |
| 38 try: | 41 try: |
| 39 # Invoke the Windows expand utility to extract the file. | 42 # Invoke the Windows expand utility to extract the file. |
| 40 level = subprocess.call( | 43 level = subprocess.call( |
| 41 ['expand', cab_path, '-F:' + archived_file, output_dir]) | 44 ['expand', cab_path, '-F:' + archived_file, output_dir]) |
| 42 if level != 0: | 45 if level != 0: |
| 43 print 'Cab extraction(%s, %s, %s) failed.' % ( | 46 return level |
| 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: | 47 finally: |
| 51 release_lock(lock_fd) | 48 release_lock(lock_fd) |
| 52 | 49 |
| 53 # The expand utility preserves the modification date and time of the archived | 50 # 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 | 51 # 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 | 52 # modification times of input and output files to determine whether to do an |
| 56 # action. | 53 # action. |
| 57 os.utime(os.path.join(output_dir, archived_file), None) | 54 os.utime(os.path.join(output_dir, archived_file), None) |
| 58 return 0 | 55 return 0 |
| 59 | 56 |
| 60 | 57 |
| 61 if __name__ == '__main__': | 58 if __name__ == '__main__': |
| 62 sys.exit(main()) | 59 sys.exit(main()) |
| OLD | NEW |