Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | #!/usr/bin/env python |
michael@0 | 2 | # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
michael@0 | 3 | # Use of this source code is governed by a BSD-style license that can be |
michael@0 | 4 | # found in the LICENSE file. |
michael@0 | 5 | |
michael@0 | 6 | """Extracts a single file from a CAB archive.""" |
michael@0 | 7 | |
michael@0 | 8 | import os |
michael@0 | 9 | import shutil |
michael@0 | 10 | import subprocess |
michael@0 | 11 | import sys |
michael@0 | 12 | import tempfile |
michael@0 | 13 | |
michael@0 | 14 | def run_quiet(*args): |
michael@0 | 15 | """Run 'expand' supressing noisy output. Returns returncode from process.""" |
michael@0 | 16 | popen = subprocess.Popen(args, stdout=subprocess.PIPE) |
michael@0 | 17 | out, _ = popen.communicate() |
michael@0 | 18 | if popen.returncode: |
michael@0 | 19 | # expand emits errors to stdout, so if we fail, then print that out. |
michael@0 | 20 | print out |
michael@0 | 21 | return popen.returncode |
michael@0 | 22 | |
michael@0 | 23 | def main(): |
michael@0 | 24 | if len(sys.argv) != 4: |
michael@0 | 25 | print 'Usage: extract_from_cab.py cab_path archived_file output_dir' |
michael@0 | 26 | return 1 |
michael@0 | 27 | |
michael@0 | 28 | [cab_path, archived_file, output_dir] = sys.argv[1:] |
michael@0 | 29 | |
michael@0 | 30 | # Expand.exe does its work in a fixed-named temporary directory created within |
michael@0 | 31 | # the given output directory. This is a problem for concurrent extractions, so |
michael@0 | 32 | # create a unique temp dir within the desired output directory to work around |
michael@0 | 33 | # this limitation. |
michael@0 | 34 | temp_dir = tempfile.mkdtemp(dir=output_dir) |
michael@0 | 35 | |
michael@0 | 36 | try: |
michael@0 | 37 | # Invoke the Windows expand utility to extract the file. |
michael@0 | 38 | level = run_quiet('expand', cab_path, '-F:' + archived_file, temp_dir) |
michael@0 | 39 | if level == 0: |
michael@0 | 40 | # Move the output file into place, preserving expand.exe's behavior of |
michael@0 | 41 | # paving over any preexisting file. |
michael@0 | 42 | output_file = os.path.join(output_dir, archived_file) |
michael@0 | 43 | try: |
michael@0 | 44 | os.remove(output_file) |
michael@0 | 45 | except OSError: |
michael@0 | 46 | pass |
michael@0 | 47 | os.rename(os.path.join(temp_dir, archived_file), output_file) |
michael@0 | 48 | finally: |
michael@0 | 49 | shutil.rmtree(temp_dir, True) |
michael@0 | 50 | |
michael@0 | 51 | if level != 0: |
michael@0 | 52 | return level |
michael@0 | 53 | |
michael@0 | 54 | # The expand utility preserves the modification date and time of the archived |
michael@0 | 55 | # file. Touch the extracted file. This helps build systems that compare the |
michael@0 | 56 | # modification times of input and output files to determine whether to do an |
michael@0 | 57 | # action. |
michael@0 | 58 | os.utime(os.path.join(output_dir, archived_file), None) |
michael@0 | 59 | return 0 |
michael@0 | 60 | |
michael@0 | 61 | |
michael@0 | 62 | if __name__ == '__main__': |
michael@0 | 63 | sys.exit(main()) |