Index: trunk/tools/editor_trends/utils/compression.py |
— | — | @@ -13,7 +13,7 @@ |
14 | 14 | ''' |
15 | 15 | |
16 | 16 | __author__ = '''\n'''.join(['Diederik van Liere (dvanliere@gmail.com)', ]) |
17 | | -__author__email = 'dvanliere at gmail dot com' |
| 17 | +__email__ = 'dvanliere at gmail dot com' |
18 | 18 | __date__ = '2010-11-27' |
19 | 19 | __version__ = '0.1' |
20 | 20 | |
— | — | @@ -24,13 +24,15 @@ |
25 | 25 | |
26 | 26 | import configuration |
27 | 27 | settings = configuration.Settings() |
28 | | -import utils |
| 28 | +import file_utils |
29 | 29 | import exceptions |
| 30 | +import timer |
| 31 | +import log |
30 | 32 | |
31 | 33 | class Compressor(object): |
32 | 34 | |
33 | 35 | def __init__(self, location, file, output=None): |
34 | | - self.extension = utils.determine_file_extension(file) |
| 36 | + self.extension = file_utils.determine_file_extension(file) |
35 | 37 | self.file = file |
36 | 38 | self.location = location |
37 | 39 | self.path = os.path.join(self.location, self.file) |
— | — | @@ -60,7 +62,10 @@ |
61 | 63 | if self.program_installed == None: |
62 | 64 | raise exceptions.CompressionNotSupportedError |
63 | 65 | |
64 | | - args = {'7z': ['%s' % self.program_installed, 'a', '-scsUTF-8', '-t%s' % self.compression, '%s' % self.output, '%s' % self.input], |
| 66 | + args = {'7z': ['%s' % self.program_installed, 'a', '-scsUTF-8', |
| 67 | + '-t%s' % self.compression, |
| 68 | + '%s' % self.output, |
| 69 | + '%s' % self.input], |
65 | 70 | } |
66 | 71 | |
67 | 72 | commands = args.get(self.name, None) |
— | — | @@ -84,11 +89,9 @@ |
85 | 90 | |
86 | 91 | print self.location |
87 | 92 | print self.file |
88 | | - if not utils.check_file_exists(self.location, self.file): |
| 93 | + if not file_utils.check_file_exists(self.location, self.file): |
89 | 94 | raise exceptions.FileNotFoundException(self.location, self.file) |
90 | 95 | |
91 | | - |
92 | | - |
93 | 96 | args = {'7z': ['%s' % self.program_installed, 'e', '-y', '-o%s' % self.location, '%s' % self.path], |
94 | 97 | 'bunzip2': ['%s' % self.program_installed, '-k', '%s' % self.path], |
95 | 98 | 'zip': ['%s' % self.program_installed, '-o', '%s' % self.path], |
— | — | @@ -96,7 +99,7 @@ |
97 | 100 | 'tar': ['%s' % self.program_installed, '-xvf', '%s' % self.path] |
98 | 101 | } |
99 | 102 | commands = args.get(self.name, None) |
100 | | - print commands |
| 103 | + #print commands |
101 | 104 | if commands != None: |
102 | 105 | p = subprocess.call(commands) |
103 | 106 | #p = subprocess.Popen(commands, shell=True).wait() |
— | — | @@ -104,19 +107,6 @@ |
105 | 108 | raise exceptions.CompressionNotSupportedError |
106 | 109 | return p |
107 | 110 | |
108 | | -# if self.name == '7z': |
109 | | -# p = subprocess.Popen(['%s' % tool.extract_installed, 'e', '-o%s' % location, '%s' % input], shell=True).wait() |
110 | | -# elif tool_extract_installed.endswith('bunzip2'): |
111 | | -# p = subprocess.Popen(['%s' % tool.extract_installed, '-k', '%s' % input], shell=True).wait() |
112 | | -# elif tool.extract_installed.endswith('zip'): |
113 | | -# p = subprocess.Popen(['%s' % tool.extract_installed, '-o', '%s' % input], shell=True).wait() |
114 | | -# elif tool.extract_installed.endswith('gz'): |
115 | | -# p = subprocess.Popen(['%s' % tool.extract_installed, '-xzvf', '%s' % input], shell=True).wait() |
116 | | -# elif tool.extract_installed.endswith('tar'): |
117 | | -# p = subprocess.Popen([]) |
118 | | -# else: |
119 | | -# raise exceptions.CompressionNotSupportedError |
120 | | - |
121 | 111 | def init_compression_tool(self, extension, action): |
122 | 112 | compression = {'gz': [['tar', 'tar'], ['7z', '7z']], |
123 | 113 | 'bz2': [['bzip2', 'bunzip2'], ['7z', '7z']], |
— | — | @@ -138,6 +128,18 @@ |
139 | 129 | self.name = p |
140 | 130 | self.program_installed = path |
141 | 131 | |
| 132 | +def launch_zip_extractor(location, filename, properties): |
| 133 | + ''' |
| 134 | + |
| 135 | + ''' |
| 136 | + print 'Unzipping zip file' |
| 137 | + stopwatch = timer.Timer() |
| 138 | + log.log_to_mongo(properties, 'dataset', 'unpack', stopwatch, event='start') |
| 139 | + compressor = Compressor(location, filename) |
| 140 | + retcode = compressor.extract() |
| 141 | + stopwatch.elapsed() |
| 142 | + log.log_to_mongo(properties, 'dataset', 'unpack', stopwatch, event='finish') |
| 143 | + return retcode |
142 | 144 | |
143 | 145 | if __name__ == '__main__': |
144 | 146 | c = Compressor('C:\Users\diederik.vanliere\Documents', 'django.zip') |