blob: 080370ca9adee2dd953456d45d29e1fa7398e12e [file] [log] [blame]
[email protected]dcfc4dd2010-07-28 23:02:221#!/usr/bin/env python
[email protected]3ea8fe22012-05-23 07:08:562# Copyright (c) 2012 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]3ea8fe22012-05-23 07:08:5614def run_quiet(*args):
[email protected]7dcd93c2013-11-02 02:05:5315 """Run 'expand' suppressing noisy output. Returns returncode from process."""
[email protected]3ea8fe22012-05-23 07:08:5616 popen = subprocess.Popen(args, stdout=subprocess.PIPE)
17 out, _ = popen.communicate()
18 if popen.returncode:
19 # expand emits errors to stdout, so if we fail, then print that out.
20 print out
21 return popen.returncode
[email protected]96452862011-12-13 03:32:1222
[email protected]3f09d182011-11-23 19:13:4423def main():
24 if len(sys.argv) != 4:
25 print 'Usage: extract_from_cab.py cab_path archived_file output_dir'
26 return 1
[email protected]dcfc4dd2010-07-28 23:02:2227
[email protected]3f09d182011-11-23 19:13:4428 [cab_path, archived_file, output_dir] = sys.argv[1:]
[email protected]dcfc4dd2010-07-28 23:02:2229
[email protected]c6f0ea42011-12-13 22:20:4630 # Expand.exe does its work in a fixed-named temporary directory created within
31 # the given output directory. This is a problem for concurrent extractions, so
32 # create a unique temp dir within the desired output directory to work around
33 # this limitation.
34 temp_dir = tempfile.mkdtemp(dir=output_dir)
35
[email protected]77414802011-12-13 00:34:1536 try:
37 # Invoke the Windows expand utility to extract the file.
[email protected]3ea8fe22012-05-23 07:08:5638 level = run_quiet('expand', cab_path, '-F:' + archived_file, temp_dir)
[email protected]c6f0ea42011-12-13 22:20:4639 if level == 0:
40 # Move the output file into place, preserving expand.exe's behavior of
41 # paving over any preexisting file.
42 output_file = os.path.join(output_dir, archived_file)
43 try:
44 os.remove(output_file)
45 except OSError:
46 pass
47 os.rename(os.path.join(temp_dir, archived_file), output_file)
[email protected]77414802011-12-13 00:34:1548 finally:
[email protected]c6f0ea42011-12-13 22:20:4649 shutil.rmtree(temp_dir, True)
50
51 if level != 0:
52 return level
[email protected]3f09d182011-11-23 19:13:4453
54 # The expand utility preserves the modification date and time of the archived
55 # file. Touch the extracted file. This helps build systems that compare the
56 # modification times of input and output files to determine whether to do an
57 # action.
58 os.utime(os.path.join(output_dir, archived_file), None)
59 return 0
60
61
62if __name__ == '__main__':
63 sys.exit(main())