2 # Copyright (c) 2011 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 """Extracts a single file from a CAB archive."""
16 if len(sys
.argv
) != 4:
17 print 'Usage: extract_from_cab.py cab_path archived_file output_dir'
20 [cab_path
, archived_file
, output_dir
] = sys
.argv
[1:]
22 # Expand.exe does its work in a fixed-named temporary directory created within
23 # the given output directory. This is a problem for concurrent extractions, so
24 # create a unique temp dir within the desired output directory to work around
26 temp_dir
= tempfile
.mkdtemp(dir=output_dir
)
29 # Invoke the Windows expand utility to extract the file.
30 level
= subprocess
.call(
31 ['expand', cab_path
, '-F:' + archived_file
, temp_dir
])
33 # Move the output file into place, preserving expand.exe's behavior of
34 # paving over any preexisting file.
35 output_file
= os
.path
.join(output_dir
, archived_file
)
37 os
.remove(output_file
)
40 os
.rename(os
.path
.join(temp_dir
, archived_file
), output_file
)
42 shutil
.rmtree(temp_dir
, True)
47 # The expand utility preserves the modification date and time of the archived
48 # file. Touch the extracted file. This helps build systems that compare the
49 # modification times of input and output files to determine whether to do an
51 os
.utime(os
.path
.join(output_dir
, archived_file
), None)
55 if __name__
== '__main__':