2 # vim: set ts=4 sts=4 sw=4 textwidth=112 :
7 import re, os, sys, optparse
13 (TYPE_OLDRAR, TYPE_NEWRAR, TYPE_ZIP, TYPE_NOEXTRACT) = range (4)
14 (SUCCESS, ECHECK, EEXTRACT, EDELETE) = range(4)
15 config = RarslaveConfig.RarslaveConfig()
16 logger = RarslaveLogger.RarslaveLogger ()
18 # Global options to be set / used later.
21 class RarslaveExtractor (object):
23 def __init__ (self, type):
27 def addHead (self, dir, head):
28 assert os.path.isdir (dir)
29 assert os.path.isfile (os.path.join (dir, head))
31 full_head = os.path.join (dir, head)
32 logger.addMessage ('Adding extraction head: %s' % full_head, RarslaveLogger.MessageType.Debug)
33 self.heads.append (full_head)
35 def extract (self, todir=None):
36 # Extract all heads of this set
38 # Create the directory $todir if it doesn't exist
39 if todir != None and not os.path.isdir (todir):
40 logger.addMessage ('Creating directory: %s' % todir, RarslaveLogger.MessageType.Verbose)
44 logger.addMessage ('FAILED to create directory: %s' % todir, RarslaveLogger.MessageType.Fatal)
49 { TYPE_OLDRAR : self.__extract_rar,
50 TYPE_NEWRAR : self.__extract_rar,
51 TYPE_ZIP : self.__extract_zip,
52 TYPE_NOEXTRACT : self.__extract_noextract }[self.type]
54 # Call the extraction function on each head
57 # Run in the head's directory
58 ret = extraction_func (h, os.path.dirname (h))
60 ret = extraction_func (h, todir)
62 logger.addMessage ('Extraction Function returned: %d' % ret, RarslaveLogger.MessageType.Debug)
66 logger.addMessage ('Failed extracting: %s' % h, RarslaveLogger.MessageType.Fatal)
71 def __extract_rar (self, file, todir):
72 assert os.path.isfile (file)
73 assert os.path.isdir (todir)
75 RAR_CMD = config.get_value ('commands', 'unrar')
77 cmd = '%s \"%s\"' % (RAR_CMD, file)
78 ret = run_command (cmd, todir)
86 def __extract_zip (self, file, todir):
87 ZIP_CMD = config.get_value ('commands', 'unzip')
89 cmd = ZIP_CMD % (file, todir)
90 ret = run_command (cmd)
98 def __extract_noextract (self, file, todir):
99 # Just move this file to the $todir, since no extraction is needed
100 # FIXME: NOTE: mv will fail by itself if you're moving to the same dir!
101 NOEXTRACT_CMD = config.get_value ('commands', 'noextract')
103 # Make sure that both files are not the same file. If they are, don't run at all.
104 if os.path.samefile (file, os.path.join (todir, file)):
107 cmd = NOEXTRACT_CMD % (file, todir)
108 ret = run_command (cmd)
118 class RarslaveRepairer (object):
119 # Verify (and repair) the set
120 # Make sure it worked, otherwise clean up and return failure
122 def __init__ (self, dir, file, join=False):
123 self.dir = dir # the directory containing the par2 file
124 self.file = file # the par2 file
125 self.join = join # True if the par2 set is 001 002 ...
127 assert os.path.isdir (dir)
128 assert os.path.isfile (os.path.join (dir, file))
130 def checkAndRepair (self):
132 # par2repair -- PAR2 PAR2_EXTRA [JOIN_FILES]
133 PAR2_CMD = config.get_value ('commands', 'par2repair')
136 basename = get_basename (self.file)
137 all_files = find_likely_files (self.dir, self.file)
139 par2_files = find_par2_files (all_files)
141 # assemble the command
142 command = "%s \"%s\" " % (PAR2_CMD, self.file)
146 command += "\"%s\" " % os.path.split (f)[1]
150 if f not in par2_files:
151 command += "\"%s\" " % os.path.split (f)[1]
154 ret = run_command (command, self.dir)
158 logger.addMessage ('PAR2 Check / Repair failed: %s' % self.file, RarslaveLogger.MessageType.Fatal)
163 def run_command (cmd, indir=None):
164 # Runs the specified command-line in the directory given (or, in the current directory
165 # if none is given). It returns the status code given by the application.
170 assert os.path.isdir (indir) # MUST be a directory!
173 ret = os.system (cmd)
177 def full_abspath (p):
178 return os.path.abspath (os.path.expanduser (p))
180 def get_basename (name):
181 """Strips most kinds of endings from a filename"""
183 regex = config.get_value ('regular expressions', 'basename_regex')
184 r = re.compile (regex, re.IGNORECASE)
191 g = r.match (name).groups()
197 def find_likely_files (dir, p2file):
198 """Finds files which are likely to be part of the set corresponding
199 to $name in the directory $dir"""
201 assert os.path.isdir (dir)
202 assert os.path.isfile (os.path.join (dir, p2file))
204 basename = get_basename (p2file)
206 dir = os.path.abspath (dir)
207 ename = re.escape (basename)
208 regex = re.compile ('^%s.*$' % (ename, ))
210 name_matches = [f for f in os.listdir (dir) if regex.match (f)]
212 parsed_matches = Par2Parser.get_protected_files (dir, p2file)
213 except (EnvironmentError, OSError, OverflowError):
215 logger.addMessage ('Bad par2 file: %s' % p2file, RarslaveLogger.MessageType.Fatal)
217 return name_matches + parsed_matches
219 def find_par2_files (files):
220 """Find all par2 files in the list $files"""
222 PAR2_REGEX = config.get_value ('regular expressions', 'par2_regex')
223 regex = re.compile (PAR2_REGEX, re.IGNORECASE)
224 return [f for f in files if regex.match (f)]
226 def find_all_par2_files (dir):
227 """Finds all par2 files in a directory"""
228 # NOTE: does NOT return absolute paths
230 if not os.path.isdir (os.path.abspath (dir)):
231 raise ValueError # bad directory given
233 dir = os.path.abspath (dir)
234 files = os.listdir (dir)
236 return find_par2_files (files)
238 def find_extraction_heads (dir, files):
239 """Takes a list of possible files and finds likely heads of
242 # NOTE: perhaps this should happen AFTER repair is
243 # NOTE: successful. That way all files would already exist
245 # According to various sources online:
246 # 1) pre rar-3.0: .rar .r00 .r01 ...
247 # 2) post rar-3.0: .part01.rar .part02.rar
248 # 3) zip all ver: .zip
251 p2files = find_par2_files (files)
253 # Old RAR type, find all files ending in .rar
254 if is_oldrar (files):
255 extractor = RarslaveExtractor (TYPE_OLDRAR)
256 regex = re.compile ('^.*\.rar$', re.IGNORECASE)
259 extractor.addHead (dir, f)
261 if is_newrar (files):
262 extractor = RarslaveExtractor (TYPE_NEWRAR)
263 regex = re.compile ('^.*\.part0*1.rar$', re.IGNORECASE)
266 extractor.addHead (dir, f)
269 extractor = RarslaveExtractor (TYPE_ZIP)
270 regex = re.compile ('^.*\.zip$', re.IGNORECASE)
273 extractor.addHead (dir, f)
275 if is_noextract (files):
276 # Use the Par2 Parser (from cfv) here to find out what files are protected.
277 # Since these are not being extracted, they will be mv'd to another directory
279 extractor = RarslaveExtractor (TYPE_NOEXTRACT)
284 prot_files = Par2Parser.get_protected_files (dir, f)
286 except (EnvironmentError, OverflowError, OSError):
287 logger.addMessage ('Error parsing PAR2 file: %s', f)
295 extractor.addHead (dir, f)
297 logger.addMessage ('Error parsing all PAR2 files in this set ...')
299 # Make sure we found the type
300 if extractor == None:
301 logger.addMessage ('Not able to find an extractor for this type of set: %s' % p2files[0],
302 RarslaveLogger.MessageType.Verbose)
304 # No-heads here, but it's better than failing completely
305 extractor = RarslaveExtractor (TYPE_NOEXTRACT)
309 def generic_matcher (files, regex, nocase=False):
310 """Run the regex over the files, and see if one matches or not.
311 NOTE: this does not return the matches, just if a match occurred."""
314 cregex = re.compile (regex, re.IGNORECASE)
316 cregex = re.compile (regex)
324 def is_oldrar (files):
325 return generic_matcher (files, '^.*\.r00$')
327 def is_newrar (files):
328 return generic_matcher (files, '^.*\.part0*1\.rar$')
331 return generic_matcher (files, '^.*\.zip$')
333 def is_noextract (files):
334 # Type that needs no extraction.
335 # TODO: Add others ???
336 return generic_matcher (files, '^.*\.001$')
338 def find_deleteable_files (dir, p2file):
339 likely = find_likely_files (dir, p2file)
340 DELETE_REGEX = config.get_value ('regular expressions', 'delete_regex')
341 dregex = re.compile (DELETE_REGEX, re.IGNORECASE)
343 return [f for f in likely if dregex.match (f)]
349 class PAR2Set (object):
355 def __init__ (self, dir, file):
356 assert os.path.isdir (dir)
357 assert os.path.isfile (os.path.join (dir, file))
362 basename = get_basename (file)
363 self.likely_files = find_likely_files (dir, file)
365 def __list_eq (self, l1, l2):
367 if len(l1) != len(l2):
376 def __eq__ (self, rhs):
377 return self.__list_eq (self.likely_files, rhs.likely_files)
380 par2files = find_par2_files (self.likely_files)
381 par2head = par2files[0]
383 join = is_noextract (self.likely_files)
386 repairer = RarslaveRepairer (self.dir, par2head, join)
387 ret = repairer.checkAndRepair ()
390 logger.addMessage ('Repair stage failed for: %s' % par2head, RarslaveLogger.MessageType.Fatal)
394 EXTRACT_DIR = options.extract_dir
395 extractor = find_extraction_heads (self.dir, self.likely_files)
396 ret = extractor.extract (EXTRACT_DIR)
399 logger.addMessage ('Extraction stage failed for: %s' % par2head, RarslaveLogger.MessageType.Fatal)
403 DELETE_INTERACTIVE = options.interactive
404 deleteable_files = find_deleteable_files (self.dir, par2head)
405 ret = delete_list (self.dir, deleteable_files, DELETE_INTERACTIVE)
408 logger.addMessage ('Deletion stage failed for: %s' % par2head, RarslaveLogger.MessageType.Fatal)
411 logger.addMessage ('Successfully completed: %s' % par2head)
414 def delete_list (dir, files, interactive=False):
415 # Delete a list of files
417 assert os.path.isdir (dir)
420 valid_y = ['Y', 'YES']
421 valid_n = ['N', 'NO', '']
425 print 'Do you want to delete the following?:'
427 s = raw_input ('Delete [y/N]: ').upper()
429 if s in valid_y + valid_n:
436 os.remove (os.path.join (dir, f))
441 def generate_all_parsets (dir):
442 # Generate all parsets in the given directory.
444 assert os.path.isdir (dir) # Directory MUST be valid
447 p2files = find_all_par2_files (dir)
456 def check_required_progs():
457 """Check if the required programs are installed"""
459 shell_not_found = 32512
462 if run_command ('par2repair --help > /dev/null 2>&1') == shell_not_found:
463 needed.append ('par2repair')
465 if run_command ('unrar --help > /dev/null 2>&1') == shell_not_found:
466 needed.append ('unrar')
468 if run_command ('unzip --help > /dev/null 2>&1') == shell_not_found:
469 needed.append ('unzip')
473 print 'Needed program "%s" not found in $PATH' % (n, )
477 def run_options (options):
480 options.work_dir = full_abspath (options.work_dir)
482 # Make sure that the directory is valid
483 if not os.path.isdir (options.work_dir):
484 sys.stderr.write ('\"%s\" is not a valid directory. Use the \"-d\"\n' % options.work_dir)
485 sys.stderr.write ('option to override the working directory temporarily, or edit the\n')
486 sys.stderr.write ('configuration file to override the working directory permanently.\n')
489 if options.extract_dir != None:
490 options.extract_dir = full_abspath (options.extract_dir)
493 print PROGRAM + ' - ' + VERSION
495 print 'Copyright (c) 2005,2006 Ira W. Snyder (devel@irasnyder.com)'
497 print 'This program comes with ABSOLUTELY NO WARRANTY.'
498 print 'This is free software, and you are welcome to redistribute it'
499 print 'under certain conditions. See the file COPYING for details.'
502 if options.check_progs:
503 check_required_progs ()
505 if options.write_def_config:
506 config.write_config (default=True)
508 if options.write_config:
509 config.write_config ()
511 def find_loglevel (options):
513 loglevel = options.verbose - options.quiet
515 if loglevel < RarslaveLogger.MessageType.Fatal:
516 loglevel = RarslaveLogger.MessageType.Fatal
518 if loglevel > RarslaveLogger.MessageType.Debug:
519 loglevel = RarslaveLogger.MessageType.Debug
523 def printMessageTable (loglevel):
525 if logger.hasFatalMessages ():
526 print '\nFatal Messages\n' + '=' * 80
527 logger.printLoglevel (RarslaveLogger.MessageType.Fatal)
529 if loglevel == RarslaveLogger.MessageType.Fatal:
532 if logger.hasNormalMessages ():
533 print '\nNormal Messages\n' + '=' * 80
534 logger.printLoglevel (RarslaveLogger.MessageType.Normal)
536 if loglevel == RarslaveLogger.MessageType.Normal:
539 if logger.hasVerboseMessages ():
540 print '\nVerbose Messages\n' + '=' * 80
541 logger.printLoglevel (RarslaveLogger.MessageType.Verbose)
543 if loglevel == RarslaveLogger.MessageType.Verbose:
546 if logger.hasDebugMessages ():
547 print '\nDebug Messages\n' + '=' * 80
548 logger.printLoglevel (RarslaveLogger.MessageType.Debug)
554 # Build the OptionParser
555 parser = optparse.OptionParser()
556 parser.add_option('-n', '--not-recursive',
557 action='store_false', dest='recursive',
558 default=config.get_value('options', 'recursive'),
559 help="Don't run recursively")
561 parser.add_option('-d', '--work-dir',
562 dest='work_dir', type='string',
563 default=config.get_value('directories', 'working_directory'),
564 help="Start running at DIR", metavar='DIR')
566 parser.add_option('-e', '--extract-dir',
567 dest='extract_dir', type='string',
568 default=config.get_value('directories', 'extract_directory'),
569 help="Extract to DIR", metavar='DIR')
571 parser.add_option('-p', '--check-required-programs',
572 action='store_true', dest='check_progs',
574 help="Check for required programs")
576 parser.add_option('-f', '--write-default-config',
577 action='store_true', dest='write_def_config',
578 default=False, help="Write out a new default config")
580 parser.add_option('-c', '--write-new-config',
581 action='store_true', dest='write_config',
582 default=False, help="Write out the current config")
584 parser.add_option('-i', '--interactive', dest='interactive', action='store_true',
585 default=config.get_value('options', 'interactive'),
586 help="Confirm before removing files")
588 parser.add_option('-q', '--quiet', dest='quiet', action='count',
589 default=0, help="Output fatal messages only")
591 parser.add_option('-v', '--verbose', dest='verbose', action='count',
592 default=0, help="Output extra information")
594 parser.add_option('-V', '--version', dest='version', action='store_true',
595 default=False, help="Output version information")
597 parser.version = VERSION
599 # Parse the given options
601 (options, args) = parser.parse_args()
603 # Run any special actions that are needed on these options
604 run_options (options)
606 # Find the loglevel using the options given
607 loglevel = find_loglevel (options)
610 if options.recursive:
611 for (dir, subdirs, files) in os.walk (options.work_dir):
612 parsets = generate_all_parsets (dir)
618 parsets = generate_all_parsets (options.work_dir)
623 printMessageTable (loglevel)
628 if __name__ == '__main__':