blob: 9695b96789b76f44cc8db526157c7613bfbd72f2 [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
Raul Tambre9e24293b2019-05-12 06:11:078from __future__ import print_function
9
[email protected]dcfc4dd2010-07-28 23:02:2210import os
[email protected]c6f0ea42011-12-13 22:20:4611import shutil
[email protected]dcfc4dd2010-07-28 23:02:2212import subprocess
13import sys
[email protected]77414802011-12-13 00:34:1514import tempfile
[email protected]dcfc4dd2010-07-28 23:02:2215
[email protected]3ea8fe22012-05-23 07:08:5616def run_quiet(*args):
[email protected]7dcd93c2013-11-02 02:05:5317 """Run 'expand' suppressing noisy output. Returns returncode from process."""
[email protected]3ea8fe22012-05-23 07:08:5618 popen = subprocess.Popen(args, stdout=subprocess.PIPE)
19 out, _ = popen.communicate()
20 if popen.returncode:
21 # expand emits errors to stdout, so if we fail, then print that out.
Raul Tambre9e24293b2019-05-12 06:11:0722 print(out)
[email protected]3ea8fe22012-05-23 07:08:5623 return popen.returncode
[email protected]96452862011-12-13 03:32:1224
[email protected]3f09d182011-11-23 19:13:4425def main():
26 if len(sys.argv) != 4:
Raul Tambre9e24293b2019-05-12 06:11:0727 print('Usage: extract_from_cab.py cab_path archived_file output_dir')
[email protected]3f09d182011-11-23 19:13:4428 return 1
[email protected]dcfc4dd2010-07-28 23:02:2229
[email protected]3f09d182011-11-23 19:13:4430 [cab_path, archived_file, output_dir] = sys.argv[1:]
[email protected]dcfc4dd2010-07-28 23:02:2231
[email protected]c6f0ea42011-12-13 22:20:4632 # Expand.exe does its work in a fixed-named temporary directory created within
33 # the given output directory. This is a problem for concurrent extractions, so
34 # create a unique temp dir within the desired output directory to work around
35 # this limitation.
36 temp_dir = tempfile.mkdtemp(dir=output_dir)
37
[email protected]77414802011-12-13 00:34:1538 try:
39 # Invoke the Windows expand utility to extract the file.
[email protected]3ea8fe22012-05-23 07:08:5640 level = run_quiet('expand', cab_path, '-F:' + archived_file, temp_dir)
[email protected]c6f0ea42011-12-13 22:20:4641 if level == 0:
42 # Move the output file into place, preserving expand.exe's behavior of
43 # paving over any preexisting file.
44 output_file = os.path.join(output_dir, archived_file)
45 try:
46 os.remove(output_file)
47 except OSError:
48 pass
49 os.rename(os.path.join(temp_dir, archived_file), output_file)
[email protected]77414802011-12-13 00:34:1550 finally:
[email protected]c6f0ea42011-12-13 22:20:4651 shutil.rmtree(temp_dir, True)
52
53 if level != 0:
54 return level
[email protected]3f09d182011-11-23 19:13:4455
56 # The expand utility preserves the modification date and time of the archived
57 # file. Touch the extracted file. This helps build systems that compare the
58 # modification times of input and output files to determine whether to do an
59 # action.
60 os.utime(os.path.join(output_dir, archived_file), None)
61 return 0
62
63
64if __name__ == '__main__':
65 sys.exit(main())