Subversion Repositories programming

Compare Revisions

Ignore whitespace Rev 133 → Rev 134

/shell_programs/rarslave.py
19,6 → 19,19
# - Made start directory an option.
# - Check for appropriate programs before starting.
#
# - 2005-10-17
# - Use a regular expression to handle the deletable types.
#
# - 2005-10-18
# - Use regular expressions to handle all finding of files, instead of
# using the glob module.
# - Add a config class to handle all the default config stuff sanely.
# This makes it easier to change some of the main parts of the program to
# your specific configuration.
# - Move the docrcchecks variable inside the get_par2_filenames() function,
# which is where it belongs anyway.
# - Added command-line option to check for required programs at start.
#
 
################################################################################
# REQUIREMENTS:
27,12 → 40,22
# to function properly. I will attempt to check that these are in your path.
################################################################################
 
################################################################################
# Global Variables
################################################################################
WORK_DIR = '~/downloads/usenet'
################################################################################
class rarslave_config:
"""A simple class to hold the default configs for the whole program"""
 
WORK_DIR = '~/downloads/usenet'
DELETEABLE_TYPES_REGEX = '^.*\.(rar|r\d\d)$'
TEMP_REPAIR_REGEX = '.*\.1$'
PAR2_REGEX = '.*\.(PAR|par)2$'
VIDEO_FILE_REGEX = '.*\.(AVI|avi|OGM|ogm|MKV|mkv)$'
RECURSIVE = True
CHECK_REQ_PROGS = False
 
def __init__(self):
pass
 
config = rarslave_config()
################################################################################
# The PAR2 Parser
#
41,9 → 64,6
 
import struct, errno
 
# We always want to do crc checks
docrcchecks = True
 
def chompnulls(line):
p = line.find('\0')
if p < 0: return line
59,6 → 79,9
print 'Could not open %s' % (filename, )
return []
 
# We always want to do crc checks
docrcchecks = True
 
pkt_header_fmt = '< 8s Q 16s 16s 16s'
pkt_header_size = struct.calcsize(pkt_header_fmt)
file_pkt_fmt = '< 16s 16s 16s Q'
216,8 → 239,10
return self.verified
 
def __has_video_file(self):
regex = re.compile(config.VIDEO_FILE_REGEX)
for f in self.files:
if os.path.splitext(f)[1] in ('.avi', '.ogm', '.mkv'):
if regex.match(f):
return True
 
return False
238,13 → 263,14
os.remove(i)
 
# remove any rars that are associated (leave EVERYTHING else)
# This regex matches both old and new style rar(s).
regex = re.compile('^.*\.(rar|r\d\d)$')
# This regex matches both old and new style rar(s) by default.
regex = re.compile(config.DELETEABLE_TYPES_REGEX)
 
for i in self.files:
if regex.match(i):
os.remove(i)
 
# remove any .0?? files (from parjoin)
# remove any .{001,002,...} files (from parjoin)
if self.used_parjoin:
for i in os.listdir(os.getcwd()):
if i != self.files[0] and self.files[0] in i:
251,8 → 277,8
os.remove(i)
 
# remove any temp repair files
for i in glob.glob('*.1'):
os.remove(i)
regex = re.compile(config.TEMP_REPAIR_REGEX)
[os.remove(f) for f in os.listdir(os.getcwd()) if regex.match(f)]
 
def __get_extract_file(self):
"""Find the first extractable file"""
290,7 → 316,7
# The rarslave program itself
################################################################################
 
import os, sys, glob
import os, sys
from optparse import OptionParser
 
def check_required_progs():
321,8 → 347,8
"""Get a representation of each parset in the current directory, and
return them as a list of parset instances"""
 
par2files = glob.glob('*.par2')
par2files += glob.glob('*.PAR2')
regex = re.compile(config.PAR2_REGEX)
par2files = [f for f in os.listdir(os.getcwd()) if regex.match(f)]
 
parsets = []
 
372,10 → 398,17
 
# Build the OptionParser
parser = OptionParser()
parser.add_option('-n', '--not-recursive', action='store_false', dest='recursive',
default=True, help="don't run recursively")
parser.add_option('-d', '--start-dir', dest='work_dir', default=WORK_DIR,
help='start running at DIR', metavar='DIR')
parser.add_option('-n', '--not-recursive',
action='store_false', dest='recursive',
default=config.RECURSIVE, help="Don't run recursively")
parser.add_option('-d', '--work-dir',
dest='work_dir', default=config.WORK_DIR,
help="Start running at DIR", metavar='DIR')
parser.add_option('-p', '--check-required-programs',
action='store_true', dest='check_progs',
default=config.CHECK_REQ_PROGS, help="Don't check for required programs")
 
# Parse the given options
(options, args) = parser.parse_args()
384,7 → 417,8
options.work_dir = os.path.abspath(os.path.expanduser(options.work_dir))
 
# Check that we have the required programs installed
check_required_progs()
if options.check_progs:
check_required_progs()
 
# Run rarslave!
if options.recursive: