blob: 2321d2f7dd6ef5a730957bad5630db702ddd0c61 [file] [log] [blame]
apatrick@chromium.org1218a882010-07-29 08:02:22 +09001#!/usr/bin/env python
maruel@chromium.org1f144a32011-11-24 04:13:44 +09002# Copyright (c) 2011 The Chromium Authors. All rights reserved.
apatrick@chromium.org1218a882010-07-29 08:02:22 +09003# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
maruel@chromium.org1f144a32011-11-24 04:13:44 +09006"""Extracts a single file from a CAB archive."""
apatrick@chromium.org1218a882010-07-29 08:02:22 +09007
8import os
9import subprocess
10import sys
11
apatrick@chromium.org1218a882010-07-29 08:02:22 +090012
maruel@chromium.org1f144a32011-11-24 04:13:44 +090013def main():
14 if len(sys.argv) != 4:
15 print 'Usage: extract_from_cab.py cab_path archived_file output_dir'
16 return 1
apatrick@chromium.org1218a882010-07-29 08:02:22 +090017
maruel@chromium.org1f144a32011-11-24 04:13:44 +090018 [cab_path, archived_file, output_dir] = sys.argv[1:]
apatrick@chromium.org1218a882010-07-29 08:02:22 +090019
maruel@chromium.org1f144a32011-11-24 04:13:44 +090020 # Invoke the Windows expand utility to extract the file.
21 level = subprocess.call(
22 ['expand', cab_path, '-F:' + archived_file, output_dir])
23 if level != 0:
maruel@chromium.orgc04ced42011-12-09 05:47:15 +090024 print 'Cab extraction(%s, %s, %s) failed.' % (
25 cab_path, archived_file, output_dir)
26 print 'Trying a second time.'
27 level = subprocess.call(
28 ['expand', cab_path, '-F:' + archived_file, output_dir])
29 if level != 0:
30 return level
maruel@chromium.org1f144a32011-11-24 04:13:44 +090031
32 # The expand utility preserves the modification date and time of the archived
33 # file. Touch the extracted file. This helps build systems that compare the
34 # modification times of input and output files to determine whether to do an
35 # action.
36 os.utime(os.path.join(output_dir, archived_file), None)
37 return 0
38
39
40if __name__ == '__main__':
41 sys.exit(main())