scripts/x

branch
trunk
changeset 15
28dbd52a8bb8
parent 14
6f9e1bb59719
child 16
29794d4d41aa
equal deleted inserted replaced
14:6f9e1bb59719 15:28dbd52a8bb8
20 import errno 20 import errno
21 import logging 21 import logging
22 import mimetypes 22 import mimetypes
23 import optparse 23 import optparse
24 import os 24 import os
25 import stat
25 import subprocess 26 import subprocess
26 import sys 27 import sys
27 import tempfile 28 import tempfile
28 29
29 from cStringIO import StringIO 30 from cStringIO import StringIO
169 170
170 def prepare(self): 171 def prepare(self):
171 pass 172 pass
172 173
173 def check_contents(self): 174 def check_contents(self):
174 self.archive.seek(0, 0)
175 archive_type = None 175 archive_type = None
176 filenames = self.get_filenames() 176 filenames = self.get_filenames()
177 try: 177 try:
178 filename = filenames.next() 178 filename = filenames.next()
179 if extractor_map.has_key(mimetypes.guess_type(filename)[0]): 179 if extractor_map.has_key(mimetypes.guess_type(filename)[0]):
214 os.chdir(old_path) 214 os.chdir(old_path)
215 215
216 216
217 class TarExtractor(BaseExtractor): 217 class TarExtractor(BaseExtractor):
218 def get_filenames(self): 218 def get_filenames(self):
219 self.archive.seek(0, 0)
219 return ProcessStreamer(['tar', '-t'], self.archive) 220 return ProcessStreamer(['tar', '-t'], self.archive)
220 221
221 def extract_archive(self): 222 def extract_archive(self):
222 self.run(['tar', '-x'], stdin=self.archive) 223 self.run(['tar', '-x'], stdin=self.archive)
223 224
229 self.encoding = encoding 230 self.encoding = encoding
230 self.included_archives = [] 231 self.included_archives = []
231 self.archive = StringIO() 232 self.archive = StringIO()
232 233
233 def get_filenames(self): 234 def get_filenames(self):
235 self.archive.seek(0, 0)
234 return ProcessStreamer(['zipinfo', '-1', self.filename], None) 236 return ProcessStreamer(['zipinfo', '-1', self.filename], None)
235 237
236 def extract_archive(self): 238 def extract_archive(self):
237 self.run(['unzip', '-q', self.filename]) 239 self.run(['unzip', '-q', self.filename])
238 240
239 241
240 class CpioExtractor(BaseExtractor): 242 class CpioExtractor(BaseExtractor):
241 def get_filenames(self): 243 def get_filenames(self):
244 self.archive.seek(0, 0)
242 return ProcessStreamer(['cpio', '-t'], self.archive, 245 return ProcessStreamer(['cpio', '-t'], self.archive,
243 stderr=subprocess.PIPE) 246 stderr=subprocess.PIPE)
244 247
245 def extract_archive(self): 248 def extract_archive(self):
246 self.run(['cpio', '-i', '--make-directories', 249 self.run(['cpio', '-i', '--make-directories',
299 extension = '.' + pieces[-1] 302 extension = '.' + pieces[-1]
300 if mimetypes.encodings_map.has_key(extension): 303 if mimetypes.encodings_map.has_key(extension):
301 pieces.pop() 304 pieces.pop()
302 return '.'.join(pieces) 305 return '.'.join(pieces)
303 306
304 def suggest_target(self): 307 def get_filenames(self):
305 return FilenameChecker().check(self.basename()) 308 yield self.basename()
306 309
307 def check_contents(self): 310 def check_contents(self):
308 return self.basename() 311 return DECOMPRESSED
309 312
310 def extract(self, path): 313 def extract(self, path):
311 output = open(path, 'w') 314 output = open(path, 'w')
312 self.run(['cat'], "output write", stdin=self.archive, stdout=output) 315 self.run(['cat'], "output write", stdin=self.archive, stdout=output)
313 output.close() 316 output.close()
320 self.contents = contents 323 self.contents = contents
321 self.options = options 324 self.options = options
322 325
323 def extract(self): 326 def extract(self):
324 checker = self.extractor.name_checker(self.extractor.basename()) 327 checker = self.extractor.name_checker(self.extractor.basename())
325 if self.options.overwrite or checker.is_free(): 328 if self.options.flat:
329 self.target = '.'
330 return self.do_extract('.')
331 elif self.options.overwrite or checker.is_free():
326 self.target = self.extractor.basename() 332 self.target = self.extractor.basename()
327 self.overwrite() 333 return self.overwrite()
328 else: 334 else:
329 self.target = checker.check() 335 self.target = checker.check()
330 self.safe_extract() 336 return self.safe_extract()
331 337
332 def do_extract(self, directory): 338 def do_extract(self, directory):
333 try: 339 try:
334 self.extractor.extract(directory) 340 self.extractor.extract(directory)
335 except ExtractorError, error: 341 except ExtractorError, error:
336 return error.strerror 342 return str(error)
337 343
338 def cleanup(self): 344 def cleanup(self):
345 if self.options.flat:
346 self.cleanup_files()
347 else:
348 self.cleanup_directory()
349
350 def cleanup_files(self):
351 for filename in self.extractor.get_filenames():
352 stat_info = os.stat(filename)
353 perms = stat.S_IRUSR | stat.S_IWUSR
354 if stat.S_ISDIR(stat_info.st_mode):
355 perms |= stat.S_IXUSR
356 os.chmod(filename, stat_info.st_mode | perms)
357
358 def cleanup_directory(self):
339 command = 'find' 359 command = 'find'
340 status = subprocess.call(['find', self.target, '-type', 'd', 360 status = subprocess.call(['find', self.target, '-type', 'd',
341 '-exec', 'chmod', 'u+rwx', '{}', ';']) 361 '-exec', 'chmod', 'u+rwx', '{}', ';'])
342 if status == 0: 362 if status == 0:
343 command = 'chmod' 363 command = 'chmod'
410 action='count', default=0, 430 action='count', default=0,
411 help='be verbose/print debugging information') 431 help='be verbose/print debugging information')
412 parser.add_option('-o', '--overwrite', dest='overwrite', 432 parser.add_option('-o', '--overwrite', dest='overwrite',
413 action='store_true', default=False, 433 action='store_true', default=False,
414 help='overwrite any existing target directory') 434 help='overwrite any existing target directory')
415 ## parser.add_option('-f', '--flat', '--no-directory', dest='flat', 435 parser.add_option('-f', '--flat', '--no-directory', dest='flat',
416 ## action='store_true', default=False, 436 action='store_true', default=False,
417 ## help="don't put contents in their own directory") 437 help="don't put contents in their own directory")
418 ## parser.add_option('-n', '--noninteractive', dest='batch', 438 ## parser.add_option('-n', '--noninteractive', dest='batch',
419 ## action='store_true', default=False, 439 ## action='store_true', default=False,
420 ## help="don't ask how to handle special cases") 440 ## help="don't ask how to handle special cases")
421 self.options, filenames = parser.parse_args(arguments) 441 self.options, filenames = parser.parse_args(arguments)
422 if not filenames: 442 if not filenames:

mercurial