2010-07-29 03:02:22 +04:00
|
|
|
#!/usr/bin/env python
|
2012-05-23 11:08:56 +04:00
|
|
|
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
2010-07-29 03:02:22 +04:00
|
|
|
# Use of this source code is governed by a BSD-style license that can be
|
|
|
|
# found in the LICENSE file.
|
|
|
|
|
2011-11-23 23:13:44 +04:00
|
|
|
"""Extracts a single file from a CAB archive."""
|
2010-07-29 03:02:22 +04:00
|
|
|
|
|
|
|
import os
|
2011-12-14 02:20:46 +04:00
|
|
|
import shutil
|
2010-07-29 03:02:22 +04:00
|
|
|
import subprocess
|
|
|
|
import sys
|
2011-12-13 04:34:15 +04:00
|
|
|
import tempfile
|
2010-07-29 03:02:22 +04:00
|
|
|
|
2012-05-23 11:08:56 +04:00
|
|
|
def run_quiet(*args):
|
|
|
|
"""Run 'expand' supressing noisy output. Returns returncode from process."""
|
|
|
|
popen = subprocess.Popen(args, stdout=subprocess.PIPE)
|
|
|
|
out, _ = popen.communicate()
|
|
|
|
if popen.returncode:
|
|
|
|
# expand emits errors to stdout, so if we fail, then print that out.
|
|
|
|
print out
|
|
|
|
return popen.returncode
|
2011-12-13 07:32:12 +04:00
|
|
|
|
2011-11-23 23:13:44 +04:00
|
|
|
def main():
|
|
|
|
if len(sys.argv) != 4:
|
|
|
|
print 'Usage: extract_from_cab.py cab_path archived_file output_dir'
|
|
|
|
return 1
|
2010-07-29 03:02:22 +04:00
|
|
|
|
2011-11-23 23:13:44 +04:00
|
|
|
[cab_path, archived_file, output_dir] = sys.argv[1:]
|
2010-07-29 03:02:22 +04:00
|
|
|
|
2011-12-14 02:20:46 +04:00
|
|
|
# Expand.exe does its work in a fixed-named temporary directory created within
|
|
|
|
# the given output directory. This is a problem for concurrent extractions, so
|
|
|
|
# create a unique temp dir within the desired output directory to work around
|
|
|
|
# this limitation.
|
|
|
|
temp_dir = tempfile.mkdtemp(dir=output_dir)
|
|
|
|
|
2011-12-13 04:34:15 +04:00
|
|
|
try:
|
|
|
|
# Invoke the Windows expand utility to extract the file.
|
2012-05-23 11:08:56 +04:00
|
|
|
level = run_quiet('expand', cab_path, '-F:' + archived_file, temp_dir)
|
2011-12-14 02:20:46 +04:00
|
|
|
if level == 0:
|
|
|
|
# Move the output file into place, preserving expand.exe's behavior of
|
|
|
|
# paving over any preexisting file.
|
|
|
|
output_file = os.path.join(output_dir, archived_file)
|
|
|
|
try:
|
|
|
|
os.remove(output_file)
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
os.rename(os.path.join(temp_dir, archived_file), output_file)
|
2011-12-13 04:34:15 +04:00
|
|
|
finally:
|
2011-12-14 02:20:46 +04:00
|
|
|
shutil.rmtree(temp_dir, True)
|
|
|
|
|
|
|
|
if level != 0:
|
|
|
|
return level
|
2011-11-23 23:13:44 +04:00
|
|
|
|
|
|
|
# The expand utility preserves the modification date and time of the archived
|
|
|
|
# file. Touch the extracted file. This helps build systems that compare the
|
|
|
|
# modification times of input and output files to determine whether to do an
|
|
|
|
# action.
|
|
|
|
os.utime(os.path.join(output_dir, archived_file), None)
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
sys.exit(main())
|