|
1 #!/usr/bin/env python |
|
2 # -*- coding: utf-8 -*- |
|
3 # |
|
4 # ============================================================================ |
|
5 # Name : extract.py |
|
6 # Part of : Hb |
|
7 # Description : Hb themes script for extracting theme archives |
|
8 # Version : %version: % |
|
9 # |
|
10 # Copyright (c) 2008-2010 Nokia. All rights reserved. |
|
11 # This material, including documentation and any related computer |
|
12 # programs, is protected by copyright controlled by Nokia. All |
|
13 # rights are reserved. Copying, including reproducing, storing, |
|
14 # adapting or translating, any or all of this material requires the |
|
15 # prior written consent of Nokia. This material also contains |
|
16 # confidential information which may not be disclosed to others |
|
17 # without the prior written consent of Nokia. |
|
18 # ============================================================================ |
|
19 |
|
20 import os |
|
21 import re |
|
22 import sys |
|
23 import shutil |
|
24 import zipfile |
|
25 import fnmatch |
|
26 import optparse |
|
27 |
|
28 # ============================================================================ |
|
29 # Globals |
|
30 # ============================================================================ |
|
31 VERBOSE = False |
|
32 INPUT_DIR = os.getcwd() |
|
33 OUTPUT_DIR = os.getcwd() |
|
34 INCLUDE = None |
|
35 EXCLUDE = None |
|
36 |
|
37 # ============================================================================ |
|
38 # OptionParser |
|
39 # ============================================================================ |
|
40 class OptionParser(optparse.OptionParser): |
|
41 def __init__(self): |
|
42 optparse.OptionParser.__init__(self) |
|
43 self.add_option("-v", "--verbose", action="store_true", dest="verbose", |
|
44 help="print verbose information about each step of the sync process") |
|
45 self.add_option("-q", "--quiet", action="store_false", dest="verbose", |
|
46 help="do not print information about each step of the sync process") |
|
47 self.add_option("-i", "--input", dest="input", metavar="dir", |
|
48 help="specify the input <dir> (default %s)" % INPUT_DIR) |
|
49 self.add_option("-o", "--output", dest="output", metavar="dir", |
|
50 help="specify the output <dir> (default %s)" % OUTPUT_DIR) |
|
51 self.add_option("--include", dest="include", action="append", metavar="pattern", |
|
52 help="specify the include <pattern> (default %s)" % INCLUDE) |
|
53 self.add_option("--exclude", dest="exclude", action="append", metavar="pattern", |
|
54 help="specify the exclude <pattern> (default %s)" % EXCLUDE) |
|
55 |
|
56 # ============================================================================ |
|
57 # Utils |
|
58 # ============================================================================ |
|
59 if not hasattr(os.path, "relpath"): |
|
60 def relpath(path, start=os.curdir): |
|
61 abspath = os.path.abspath(path) |
|
62 absstart = os.path.abspath(start) |
|
63 if abspath == absstart: |
|
64 return "." |
|
65 i = len(absstart) |
|
66 if not absstart.endswith(os.path.sep): |
|
67 i += len(os.path.sep) |
|
68 if not abspath.startswith(absstart): |
|
69 i = 0 |
|
70 return abspath[i:] |
|
71 os.path.relpath = relpath |
|
72 |
|
73 def extract(archive): |
|
74 global INPUT_DIR, OUTPUT_DIR |
|
75 |
|
76 path, filename = os.path.split(archive) |
|
77 relpath = os.path.relpath(path, INPUT_DIR) |
|
78 outpath = os.path.join(OUTPUT_DIR, relpath) |
|
79 |
|
80 # extract |
|
81 zip = zipfile.ZipFile(archive) |
|
82 for name in zip.namelist(): |
|
83 data = zip.read(name) |
|
84 outfile = os.path.join(outpath, name) |
|
85 # overwrite only if different size |
|
86 if not os.path.exists(outfile) or os.path.getsize(outfile) != len(data): |
|
87 file = open(outfile, "w") |
|
88 file.write(data) |
|
89 file.close() |
|
90 |
|
91 def include_exclude(filepath): |
|
92 global INCLUDE, EXCLUDE |
|
93 result = True |
|
94 if INCLUDE != None: |
|
95 for pattern in INCLUDE: |
|
96 if not fnmatch.fnmatch(filepath, pattern): |
|
97 result = False |
|
98 if EXCLUDE != None: |
|
99 for pattern in EXCLUDE: |
|
100 if fnmatch.fnmatch(filepath, pattern): |
|
101 result = False |
|
102 return result |
|
103 |
|
104 # ============================================================================ |
|
105 # main() |
|
106 # ============================================================================ |
|
107 def main(): |
|
108 global VERBOSE, INPUT_DIR, OUTPUT_DIR, INCLUDE, EXCLUDE |
|
109 |
|
110 parser = OptionParser() |
|
111 (options, args) = parser.parse_args() |
|
112 |
|
113 if options.verbose != None: |
|
114 VERBOSE = options.verbose |
|
115 if options.input != None: |
|
116 INPUT_DIR = options.input |
|
117 if options.output != None: |
|
118 OUTPUT_DIR = options.output |
|
119 if options.include != None: |
|
120 INCLUDE = options.include |
|
121 if options.exclude != None: |
|
122 EXCLUDE = options.exclude |
|
123 |
|
124 extracted = 0 |
|
125 copied = 0 |
|
126 omitted = 0 |
|
127 workpath = None |
|
128 newline = False |
|
129 sys.stdout.write("Processing: ") |
|
130 for root, dirs, files in os.walk(INPUT_DIR): |
|
131 for file in files: |
|
132 filepath = os.path.join(root, file) |
|
133 extension = os.path.splitext(file)[1] |
|
134 if include_exclude(filepath): |
|
135 # ensure that output dir exists |
|
136 relfilepath = os.path.relpath(filepath, INPUT_DIR) |
|
137 relpath = os.path.split(relfilepath)[0] |
|
138 outpath = os.path.join(OUTPUT_DIR, relpath) |
|
139 if not os.path.exists(outpath): |
|
140 os.makedirs(outpath) |
|
141 |
|
142 # output processing dir info |
|
143 tmppath = os.path.split(filepath)[0] |
|
144 if tmppath != workpath: |
|
145 if workpath != None: |
|
146 newline = True |
|
147 sys.stdout.write("\n ") |
|
148 workpath = tmppath |
|
149 sys.stdout.write(os.path.relpath(workpath, INPUT_DIR).replace("\\", "/")) |
|
150 sys.stdout.write(".") |
|
151 else: |
|
152 sys.stdout.write(".") |
|
153 |
|
154 # extract zips |
|
155 if extension == ".zip": |
|
156 extracted += 1 |
|
157 extract(filepath) |
|
158 # copy others |
|
159 else: |
|
160 copied += 1 |
|
161 shutil.copy(filepath, outpath) |
|
162 else: |
|
163 omitted += 1 |
|
164 if newline: |
|
165 sys.stdout.write("\n") |
|
166 print " ==> %s archives(s) extracted" % extracted |
|
167 print " ==> %s file(s) copied" % copied |
|
168 print " ==> %s file(s) omitted" % omitted |
|
169 |
|
170 return 0 |
|
171 |
|
172 if __name__ == "__main__": |
|
173 sys.exit(main()) |