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