blob: d5410d64d8e6a0af24508da30345222c07e82377 [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
[email protected]c6f0ea42011-12-13 22:20:469import shutil
[email protected]dcfc4dd2010-07-28 23:02:2210import subprocess
11import sys
[email protected]77414802011-12-13 00:34:1512import tempfile
[email protected]dcfc4dd2010-07-28 23:02:2213
[email protected]96452862011-12-13 03:32:1214
[email protected]3f09d182011-11-23 19:13:4415def main():
16 if len(sys.argv) != 4:
17 print 'Usage: extract_from_cab.py cab_path archived_file output_dir'
18 return 1
[email protected]dcfc4dd2010-07-28 23:02:2219
[email protected]3f09d182011-11-23 19:13:4420 [cab_path, archived_file, output_dir] = sys.argv[1:]
[email protected]dcfc4dd2010-07-28 23:02:2221
[email protected]c6f0ea42011-12-13 22:20:4622 # 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
[email protected]77414802011-12-13 00:34:1528 try:
29 # Invoke the Windows expand utility to extract the file.
[email protected]72ec3082011-12-08 20:47:1530 level = subprocess.call(
[email protected]c6f0ea42011-12-13 22:20:4631 ['expand', cab_path, '-F:' + archived_file, temp_dir])
32 if level == 0:
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
40 os.rename(os.path.join(temp_dir, archived_file), output_file)
[email protected]77414802011-12-13 00:34:1541 finally:
[email protected]c6f0ea42011-12-13 22:20:4642 shutil.rmtree(temp_dir, True)
43
44 if level != 0:
45 return level
[email protected]3f09d182011-11-23 19:13:4446
47 # The expand utility preserves the modification date and time of the archived
48 # file. Touch the extracted file. This helps build systems that compare the
49 # modification times of input and output files to determine whether to do an
50 # action.
51 os.utime(os.path.join(output_dir, archived_file), None)
52 return 0
53
54
55if __name__ == '__main__':
56 sys.exit(main())