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 | 12 |
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 time.sleep(1000) | |
M-A Ruel
2011/12/13 01:34:23
time.sleep(10)
| |
24 | |
25 def release_lock(fd): | |
26 os.close(fd) | |
27 os.unlink(lock_file) | |
12 | 28 |
M-A Ruel
2011/12/13 01:34:23
please keep 2 vertical lines between file level sy
| |
13 def main(): | 29 def main(): |
14 if len(sys.argv) != 4: | 30 if len(sys.argv) != 4: |
15 print 'Usage: extract_from_cab.py cab_path archived_file output_dir' | 31 print 'Usage: extract_from_cab.py cab_path archived_file output_dir' |
16 return 1 | 32 return 1 |
17 | 33 |
18 [cab_path, archived_file, output_dir] = sys.argv[1:] | 34 [cab_path, archived_file, output_dir] = sys.argv[1:] |
19 | 35 |
20 # Invoke the Windows expand utility to extract the file. | 36 lock_fd = acquire_lock() |
21 level = subprocess.call( | 37 try: |
22 ['expand', cab_path, '-F:' + archived_file, output_dir]) | 38 # Invoke the Windows expand utility to extract the file. |
23 if level != 0: | |
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( | 39 level = subprocess.call( |
28 ['expand', cab_path, '-F:' + archived_file, output_dir]) | 40 ['expand', cab_path, '-F:' + archived_file, output_dir]) |
29 if level != 0: | 41 if level != 0: |
30 return level | 42 print 'Cab extraction(%s, %s, %s) failed.' % ( |
43 cab_path, archived_file, output_dir) | |
44 print 'Trying a second time.' | |
M-A Ruel
2011/12/13 01:34:23
Remove the retry with the locking you add?
| |
45 level = subprocess.call( | |
46 ['expand', cab_path, '-F:' + archived_file, output_dir]) | |
47 if level != 0: | |
48 return level | |
49 finally: | |
50 release_lock(lock_fd) | |
31 | 51 |
32 # The expand utility preserves the modification date and time of the archived | 52 # 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 | 53 # 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 | 54 # modification times of input and output files to determine whether to do an |
35 # action. | 55 # action. |
36 os.utime(os.path.join(output_dir, archived_file), None) | 56 os.utime(os.path.join(output_dir, archived_file), None) |
37 return 0 | 57 return 0 |
38 | 58 |
39 | 59 |
40 if __name__ == '__main__': | 60 if __name__ == '__main__': |
41 sys.exit(main()) | 61 sys.exit(main()) |
OLD | NEW |