blob: 278c657e04e4917cd43ebc972a3ffbd881727f74 [file] [log] [blame]
[email protected]dcfc4dd2010-07-28 23:02:221#!/usr/bin/env python
[email protected]3f09d182011-11-23 19:13:442# Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]dcfc4dd2010-07-28 23:02:223# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
[email protected]3f09d182011-11-23 19:13:446"""Extracts a single file from a CAB archive."""
[email protected]dcfc4dd2010-07-28 23:02:227
8import os
9import subprocess
10import sys
[email protected]77414802011-12-13 00:34:1511import tempfile
[email protected]dcfc4dd2010-07-28 23:02:2212
[email protected]77414802011-12-13 00:34:1513lock_file = os.path.join(tempfile.gettempdir(), 'expand.lock')
14
15def 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 print 'Cab extraction could not get exclusive lock. Retrying in 1 sec...'
24 time.sleep(1000)
25
26def release_lock(fd):
27 os.close(fd)
28 os.unlink(lock_file)
[email protected]dcfc4dd2010-07-28 23:02:2229
[email protected]3f09d182011-11-23 19:13:4430def main():
31 if len(sys.argv) != 4:
32 print 'Usage: extract_from_cab.py cab_path archived_file output_dir'
33 return 1
[email protected]dcfc4dd2010-07-28 23:02:2234
[email protected]3f09d182011-11-23 19:13:4435 [cab_path, archived_file, output_dir] = sys.argv[1:]
[email protected]dcfc4dd2010-07-28 23:02:2236
[email protected]77414802011-12-13 00:34:1537 lock_fd = acquire_lock()
38 try:
39 # Invoke the Windows expand utility to extract the file.
[email protected]72ec3082011-12-08 20:47:1540 level = subprocess.call(
41 ['expand', cab_path, '-F:' + archived_file, output_dir])
42 if level != 0:
[email protected]77414802011-12-13 00:34:1543 print 'Cab extraction(%s, %s, %s) failed.' % (
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:
51 release_lock(lock_fd)
[email protected]3f09d182011-11-23 19:13:4452
53 # 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
55 # modification times of input and output files to determine whether to do an
56 # action.
57 os.utime(os.path.join(output_dir, archived_file), None)
58 return 0
59
60
61if __name__ == '__main__':
62 sys.exit(main())