media/webrtc/trunk/build/extract_from_cab.py

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/webrtc/trunk/build/extract_from_cab.py	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,63 @@
     1.4 +#!/usr/bin/env python
     1.5 +# Copyright (c) 2012 The Chromium Authors. All rights reserved.
     1.6 +# Use of this source code is governed by a BSD-style license that can be
     1.7 +# found in the LICENSE file.
     1.8 +
     1.9 +"""Extracts a single file from a CAB archive."""
    1.10 +
    1.11 +import os
    1.12 +import shutil
    1.13 +import subprocess
    1.14 +import sys
    1.15 +import tempfile
    1.16 +
    1.17 +def run_quiet(*args):
    1.18 +  """Run 'expand' supressing noisy output. Returns returncode from process."""
    1.19 +  popen = subprocess.Popen(args, stdout=subprocess.PIPE)
    1.20 +  out, _ = popen.communicate()
    1.21 +  if popen.returncode:
    1.22 +    # expand emits errors to stdout, so if we fail, then print that out.
    1.23 +    print out
    1.24 +  return popen.returncode
    1.25 +
    1.26 +def main():
    1.27 +  if len(sys.argv) != 4:
    1.28 +    print 'Usage: extract_from_cab.py cab_path archived_file output_dir'
    1.29 +    return 1
    1.30 +
    1.31 +  [cab_path, archived_file, output_dir] = sys.argv[1:]
    1.32 +
    1.33 +  # Expand.exe does its work in a fixed-named temporary directory created within
    1.34 +  # the given output directory. This is a problem for concurrent extractions, so
    1.35 +  # create a unique temp dir within the desired output directory to work around
    1.36 +  # this limitation.
    1.37 +  temp_dir = tempfile.mkdtemp(dir=output_dir)
    1.38 +
    1.39 +  try:
    1.40 +    # Invoke the Windows expand utility to extract the file.
    1.41 +    level = run_quiet('expand', cab_path, '-F:' + archived_file, temp_dir)
    1.42 +    if level == 0:
    1.43 +      # Move the output file into place, preserving expand.exe's behavior of
    1.44 +      # paving over any preexisting file.
    1.45 +      output_file = os.path.join(output_dir, archived_file)
    1.46 +      try:
    1.47 +        os.remove(output_file)
    1.48 +      except OSError:
    1.49 +        pass
    1.50 +      os.rename(os.path.join(temp_dir, archived_file), output_file)
    1.51 +  finally:
    1.52 +    shutil.rmtree(temp_dir, True)
    1.53 +
    1.54 +  if level != 0:
    1.55 +    return level
    1.56 +
    1.57 +  # The expand utility preserves the modification date and time of the archived
    1.58 +  # file. Touch the extracted file. This helps build systems that compare the
    1.59 +  # modification times of input and output files to determine whether to do an
    1.60 +  # action.
    1.61 +  os.utime(os.path.join(output_dir, archived_file), None)
    1.62 +  return 0
    1.63 +
    1.64 +
    1.65 +if __name__ == '__main__':
    1.66 +  sys.exit(main())

mercurial