Subversion Repositories programming

Compare Revisions

Ignore whitespace Rev 140 → Rev 141

/shell_programs/rarslave.py
35,6 → 35,16
# - 2005-10-20
# - Added a config option to extract with full path.
#
# - 2005-10-22
# - Re-wrote the config class so that there is a config file, which
# resides at ~/.config/rarslave/rarslave.conf by default.
# - Added the command-line option -c to write out an updated version
# of the config file (to fill in any missing options with the defaults)
# - Added the command-line option -f to write out a new default config file,
# which overwrites any user changes.
# - Made all regexes case insensitive.
# - Made all command-line options override the config file.
#
 
################################################################################
# REQUIREMENTS:
43,23 → 53,122
# to function properly. I will attempt to check that these are in your path.
################################################################################
 
import ConfigParser, os
 
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
EXTRACT_WITH_FULL_PATH = False
def __read_config(self, filename='~/.config/rarslave/rarslave.conf'):
"""Attempt to open and read the rarslave config file"""
 
# Make sure the filename is corrected
filename = os.path.abspath(os.path.expanduser(filename))
 
user_config = {}
 
# Write the default config if it doesn't exist
if not os.path.isfile(filename):
self.write_config(default=True)
 
config = ConfigParser.ConfigParser()
config.read(filename)
 
for section in config.sections():
for option in config.options(section):
user_config[(section, option)] = config.get(section, option)
 
return user_config
 
def write_config(self, filename='~/.config/rarslave/rarslave.conf', default=False):
"""Write out the current config to the config file. If you set default=True, then
the default config file will be written."""
 
config = ConfigParser.ConfigParser()
 
# Correct filename
filename = os.path.abspath(os.path.expanduser(filename))
 
# Reset all config to make sure we write the default one, if necessary
if default:
self.__user_config = {}
print 'Writing default config to %s' % (filename, )
 
# [directories] section
config.add_section('directories')
for (s, k) in self.__defaults.keys():
if s == 'directories':
config.set(s, k, self.get_value(s, k))
 
# [options] section
config.add_section('options')
for (s, k) in self.__defaults.keys():
if s == 'options':
config.set(s, k, self.get_value(s, k))
 
# [regular_expressions] section
config.add_section('regular expressions')
for (s, k) in self.__defaults.keys():
if s == 'regular expressions':
config.set(s, k, self.get_value(s, k))
 
# Try to make the ~/.config/rarslave/ directory
if not os.path.isdir(os.path.split(filename)[0]):
try:
os.makedirs(os.path.split(filename)[0])
except:
print 'Could not make directory: %s' % (os.path.split(filename)[0], )
sys.exit()
 
# Try to write the config file to disk
try:
fsock = open(filename, 'w')
try:
config.write(fsock)
finally:
fsock.close()
except:
print 'Could not open: %s for writing' % (filename, )
sys.exit()
 
def __get_default_val(self, section, key):
return self.__defaults[(section, key)]
 
def get_value(self, section, key):
"""Get a config value. Attempts to get the value from the user's
config first, and then uses the default."""
 
try:
value = self.__user_config[(section, key)]
except:
# This should work, unless you write something stupid
# into the code, so DON'T DO IT
value = self.__get_default_val(section, key)
 
# Convert config options to booleans for easier use
if value == 'True':
value = True
 
if value == 'False':
value = False
 
return value
 
def __init__(self):
pass
self.__defaults = {
('directories', 'working_directory') : '~/downloads/usenet',
('options', 'recursive') : True,
('options', 'check_required_programs') : False,
('options', 'extract_with_full_path') : False,
('regular expressions', 'par2_regex') : '.*\.par2$',
('regular expressions', 'video_file_regex') : '.*\.(avi|ogm|mkv|mp4)$',
('regular expressions', 'temp_repair_regex') : '.*\.1$',
('regular expressions', 'remove_regex') : '^.*\.(rar|r\d\d)$' }
 
self.__user_config = self.__read_config()
 
# This is the global config variable.
config = rarslave_config()
 
################################################################################
# The PAR2 Parser
#
243,8 → 352,10
return self.verified
 
def __has_video_file(self):
regex = re.compile(config.VIDEO_FILE_REGEX)
regex = re.compile(
config.get_value('regular expressions', 'video_file_regex'),
re.IGNORECASE)
 
for f in self.files:
if regex.match(f):
return True
268,7 → 379,9
 
# remove any rars that are associated (leave EVERYTHING else)
# This regex matches both old and new style rar(s) by default.
regex = re.compile(config.DELETEABLE_TYPES_REGEX)
regex = re.compile(
config.get_value('regular expressions', 'remove_regex'),
re.IGNORECASE)
 
for i in self.files:
if regex.match(i):
281,7 → 394,9
os.remove(i)
 
# remove any temp repair files
regex = re.compile(config.TEMP_REPAIR_REGEX)
regex = re.compile(
config.get_value('regular expressions', 'temp_repair_regex'),
re.IGNORECASE)
[os.remove(f) for f in os.listdir(os.getcwd()) if regex.match(f)]
 
def __get_extract_file(self):
302,7 → 417,7
extract_file = self.__get_extract_file()
 
if extract_file != None:
if config.EXTRACT_WITH_FULL_PATH:
if config.get_value('options', 'extract_with_full_path'):
retval = os.system('rar x -o+ "%s"' % (extract_file, ))
else:
retval = os.system('rar e -o+ "%s"' % (extract_file, ))
354,7 → 469,9
"""Get a representation of each parset in the current directory, and
return them as a list of parset instances"""
 
regex = re.compile(config.PAR2_REGEX)
regex = re.compile(
config.get_value('regular expressions', 'par2_regex'),
re.IGNORECASE)
par2files = [f for f in os.listdir(os.getcwd()) if regex.match(f)]
 
parsets = []
407,16 → 524,27
parser = OptionParser()
parser.add_option('-n', '--not-recursive',
action='store_false', dest='recursive',
default=config.RECURSIVE, help="Don't run recursively")
default=config.get_value('options', 'recursive'),
help="Don't run recursively")
 
parser.add_option('-d', '--work-dir',
dest='work_dir', default=config.WORK_DIR,
dest='work_dir',
default=config.get_value('directories', 'working_directory'),
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")
default=config.get_value('options', 'check_required_programs'),
help="Check for required programs")
 
parser.add_option('-f', '--write-default-config',
action='store_true', dest='write_def_config',
default=False, help="Write out a new default config")
 
parser.add_option('-c', '--write-new-config',
action='store_true', dest='write_config',
default=False, help="Write out the current config")
 
# Parse the given options
(options, args) = parser.parse_args()
 
427,6 → 555,14
if options.check_progs:
check_required_progs()
 
# Write out a new default config, if we need it
if options.write_def_config:
config.write_config(default=True)
 
# Write out the current config (adds new options to an existing config)
if options.write_config:
config.write_config()
 
# Run rarslave!
if options.recursive:
for root, dirs, files in os.walk(options.work_dir):