From a41f0878f0745f3026f4f610eeb52cba42f03da1 Mon Sep 17 00:00:00 2001 From: "Ira W. Snyder" Date: Mon, 1 Jan 2007 16:40:43 -0800 Subject: [PATCH] Move common functionality into rsutil package This patch moves all of the common functionality from the RarslaveCommon, RarslaveConfig, and RarslaveGlobals classes into a new package, called rsutil. It then converts everything over to the new package. Signed-off-by: Ira W. Snyder --- Makefile | 2 +- PAR2Set/Base.py | 31 ++++++++-------- PAR2Set/ExtractFirstBase.py | 2 +- PAR2Set/ExtractFirstNewRAR.py | 6 ++-- PAR2Set/ExtractFirstOldRAR.py | 6 ++-- PAR2Set/Join.py | 16 ++++----- PAR2Set/NewRAR.py | 10 +++--- PAR2Set/OldRAR.py | 10 +++--- PAR2Set/ZIP.py | 12 +++---- RarslaveDetector.py | 10 +++--- rarslave.py | 48 ++++++++++++------------- rsutil/__init__.py | 7 ++++ RarslaveCommon.py => rsutil/common.py | 10 +++--- RarslaveConfig.py => rsutil/config.py | 2 +- RarslaveGlobals.py => rsutil/globals.py | 4 +-- Par2Parser.py => rsutil/par2parser.py | 0 16 files changed, 89 insertions(+), 87 deletions(-) create mode 100644 rsutil/__init__.py rename RarslaveCommon.py => rsutil/common.py (93%) rename RarslaveConfig.py => rsutil/config.py (99%) rename RarslaveGlobals.py => rsutil/globals.py (89%) rename Par2Parser.py => rsutil/par2parser.py (100%) diff --git a/Makefile b/Makefile index 7fd2ad2..74487a2 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ all: @echo "Use another target, this has no meaning" clean: - rm -f *.pyc + find -name '*.pyc' -exec rm -f '{}' \; rm -rf extract_dir test: diff --git a/PAR2Set/Base.py b/PAR2Set/Base.py index 012c01a..46a69de 100644 --- a/PAR2Set/Base.py +++ b/PAR2Set/Base.py @@ -1,8 +1,7 @@ #!/usr/bin/env python # vim: set ts=4 sts=4 sw=4 textwidth=92: -from RarslaveCommon import * -import Par2Parser +import rsutil.common import re import os @@ -45,34 +44,34 @@ class Base (object): # The real "meat" of the class self.dir = dir self.p2file = p2file - self.basename = get_basename (p2file) + self.basename = rsutil.common.get_basename (p2file) # Find files that match by name only - self.name_matched_files = find_name_matches (self.dir, self.basename) + self.name_matched_files = rsutil.common.find_name_matches (self.dir, self.basename) # Find all par2 files for this set using name matches - self.all_p2files = find_par2_files (self.name_matched_files) + self.all_p2files = rsutil.common.find_par2_files (self.name_matched_files) # Try to get the protected files for this set - self.prot_matched_files = parse_all_par2 (self.dir, self.p2file, self.all_p2files) + self.prot_matched_files = rsutil.common.parse_all_par2 (self.dir, self.p2file, self.all_p2files) # Setup the all_files combined set (for convenience only) - self.all_files = no_duplicates (self.name_matched_files + self.prot_matched_files) + self.all_files = rsutil.common.no_duplicates (self.name_matched_files + self.prot_matched_files) def __eq__ (self, rhs): return (self.dir == rhs.dir) and (self.basename == rhs.basename) and \ - list_eq (self.name_matched_files, rhs.name_matched_files) and \ - list_eq (self.prot_matched_files, rhs.prot_matched_files) + rsutil.common.list_eq (self.name_matched_files, rhs.name_matched_files) and \ + rsutil.common.list_eq (self.prot_matched_files, rhs.prot_matched_files) def update_matches (self): """Updates the contents of instance variables which are likely to change after running an operation, usually one which will create new files.""" - self.name_matched_files = find_name_matches (self.dir, self.basename) - self.all_files = no_duplicates (self.name_matched_files + self.prot_matched_files) + self.name_matched_files = rsutil.common.find_name_matches (self.dir, self.basename) + self.all_files = rsutil.common.no_duplicates (self.name_matched_files + self.prot_matched_files) def runVerifyAndRepair (self): - PAR2_CMD = config_get_value ('commands', 'par2repair') + PAR2_CMD = rsutil.common.config_get_value ('commands', 'par2repair') # assemble the command # par2repair -- PAR2 PAR2_EXTRA [JOIN_FILES] @@ -83,7 +82,7 @@ class Base (object): command += "\"%s\" " % os.path.split (f)[1] # run the command - ret = run_command (command, self.dir) + ret = rsutil.common.run_command (command, self.dir) # check the result if ret != 0: @@ -93,7 +92,7 @@ class Base (object): return SUCCESS def find_deleteable_files (self): - DELETE_REGEX = config_get_value ('regular expressions', 'delete_regex') + DELETE_REGEX = rsutil.common.config_get_value ('regular expressions', 'delete_regex') dregex = re.compile (DELETE_REGEX, re.IGNORECASE) return [f for f in self.all_files if dregex.match (f)] @@ -133,7 +132,7 @@ class Base (object): def runDelete (self): deleteable_files = self.find_deleteable_files () ret = self.delete_list_of_files (self.dir, deleteable_files, \ - options_get_value ('interactive')) + rsutil.common.options_get_value ('interactive')) return ret @@ -198,7 +197,7 @@ class Base (object): # Call the extraction function on each head for h in self.find_extraction_heads (): - full_head = full_abspath (os.path.join (self.dir, h)) + full_head = rsutil.common.full_abspath (os.path.join (self.dir, h)) ret = self.extraction_function (full_head, todir) logging.debug ('Extraction Function returned: %d' % ret) diff --git a/PAR2Set/ExtractFirstBase.py b/PAR2Set/ExtractFirstBase.py index 7385c6f..4ebbfc8 100644 --- a/PAR2Set/ExtractFirstBase.py +++ b/PAR2Set/ExtractFirstBase.py @@ -3,7 +3,7 @@ import logging import PAR2Set.Base -from RarslaveCommon import * +import rsutil.common # # This is another base class for types that must diff --git a/PAR2Set/ExtractFirstNewRAR.py b/PAR2Set/ExtractFirstNewRAR.py index f646c89..caed58f 100644 --- a/PAR2Set/ExtractFirstNewRAR.py +++ b/PAR2Set/ExtractFirstNewRAR.py @@ -3,7 +3,7 @@ import PAR2Set.ExtractFirstBase import PAR2Set.NewRAR -from RarslaveCommon import * +import rsutil.common # # This is an old-style rar type @@ -27,8 +27,8 @@ from RarslaveCommon import * # def detector (name_files, prot_files): - return has_a_match ('^.*\.part0*1\.rar$', name_files) \ - and not has_a_match ('^.*\.part0*1\.rar$', prot_files) + return rsutil.common.has_a_match ('^.*\.part0*1\.rar$', name_files) \ + and not rsutil.common.has_a_match ('^.*\.part0*1\.rar$', prot_files) class ExtractFirstNewRAR (PAR2Set.ExtractFirstBase.ExtractFirstBase, \ diff --git a/PAR2Set/ExtractFirstOldRAR.py b/PAR2Set/ExtractFirstOldRAR.py index ae2b1f2..1a45c9e 100644 --- a/PAR2Set/ExtractFirstOldRAR.py +++ b/PAR2Set/ExtractFirstOldRAR.py @@ -3,7 +3,7 @@ import PAR2Set.ExtractFirstBase import PAR2Set.OldRAR -from RarslaveCommon import * +import rsutil.common # # This is an old-style rar type @@ -27,8 +27,8 @@ from RarslaveCommon import * # def detector (name_files, prot_files): - return has_a_match ('^.*\.r00$', name_files) \ - and not has_a_match ('^.*\.r00$', prot_files) + return rsutil.common.has_a_match ('^.*\.r00$', name_files) \ + and not rsutil.common.has_a_match ('^.*\.r00$', prot_files) class ExtractFirstOldRAR (PAR2Set.ExtractFirstBase.ExtractFirstBase, \ diff --git a/PAR2Set/Join.py b/PAR2Set/Join.py index c2b055f..58648fa 100644 --- a/PAR2Set/Join.py +++ b/PAR2Set/Join.py @@ -3,7 +3,7 @@ import logging import PAR2Set.Base -from RarslaveCommon import * +import rsutil.common # # This is a regular joined file type @@ -21,8 +21,8 @@ from RarslaveCommon import * # def detector (name_files, prot_files): - return has_a_match ('^.*\.\d\d\d$', name_files) \ - and not has_a_match ('^.*\.\d\d\d$', prot_files) + return rsutil.common.has_a_match ('^.*\.\d\d\d$', name_files) \ + and not rsutil.common.has_a_match ('^.*\.\d\d\d$', prot_files) class Join (PAR2Set.Base.Base): @@ -31,10 +31,10 @@ class Join (PAR2Set.Base.Base): return 'JOIN' def find_joinfiles (self): - return find_matches ('^.*\.\d\d\d$', self.name_matched_files) + return rsutil.common.find_matches ('^.*\.\d\d\d$', self.name_matched_files) def runVerifyAndRepair (self): - PAR2_CMD = config_get_value ('commands', 'par2repair') + PAR2_CMD = rsutil.common.config_get_value ('commands', 'par2repair') # assemble the command # par2repair -- PAR2 PAR2_EXTRA [JOIN_FILES] @@ -48,7 +48,7 @@ class Join (PAR2Set.Base.Base): command += "\"%s\" " % os.path.split (f)[1] # run the command - ret = run_command (command, self.dir) + ret = rsutil.common.run_command (command, self.dir) # check the result if ret != 0: @@ -63,14 +63,14 @@ class Join (PAR2Set.Base.Base): def extraction_function (self, file, todir): """Extract a single file of this type to the given directory""" - NOEXTRACT_CMD = config_get_value ('commands', 'noextract') + NOEXTRACT_CMD = rsutil.common.config_get_value ('commands', 'noextract') # Make sure that both files are not the same file. If they are, don't run at all. if os.path.samefile (file, os.path.join (todir, file)): return SUCCESS cmd = NOEXTRACT_CMD % (file, todir) - ret = run_command (cmd) + ret = rsutil.common.run_command (cmd) # Check error code if ret != 0: diff --git a/PAR2Set/NewRAR.py b/PAR2Set/NewRAR.py index f6702c9..e780ec8 100644 --- a/PAR2Set/NewRAR.py +++ b/PAR2Set/NewRAR.py @@ -2,7 +2,7 @@ # vim: set ts=4 sts=4 sw=4 textwidth=92: import PAR2Set.Base -from RarslaveCommon import * +import rsutil.common # # This is an new-style rar type @@ -26,7 +26,7 @@ from RarslaveCommon import * # def detector (name_files, prot_files): - return has_a_match ('^.*\.part0*1\.rar$', prot_files) + return rsutil.common.has_a_match ('^.*\.part0*1\.rar$', prot_files) class NewRAR (PAR2Set.Base.Base): @@ -35,16 +35,16 @@ class NewRAR (PAR2Set.Base.Base): return 'NEWRAR' def find_extraction_heads (self): - return find_matches ('^.*\.part0*1\.rar$', self.all_files) + return rsutil.common.find_matches ('^.*\.part0*1\.rar$', self.all_files) def extraction_function (self, file, todir): assert os.path.isfile (file) assert os.path.isdir (todir) - RAR_CMD = config_get_value ('commands', 'unrar') + RAR_CMD = rsutil.common.config_get_value ('commands', 'unrar') cmd = '%s \"%s\"' % (RAR_CMD, file) - ret = run_command (cmd, todir) + ret = rsutil.common.run_command (cmd, todir) # Check error code if ret != 0: diff --git a/PAR2Set/OldRAR.py b/PAR2Set/OldRAR.py index 9c89069..a2639a3 100644 --- a/PAR2Set/OldRAR.py +++ b/PAR2Set/OldRAR.py @@ -2,7 +2,7 @@ # vim: set ts=4 sts=4 sw=4 textwidth=92: import PAR2Set.Base -from RarslaveCommon import * +import rsutil.common # # This is an old-style rar type @@ -26,7 +26,7 @@ from RarslaveCommon import * # def detector (name_files, prot_files): - return has_a_match ('^.*\.r00$', prot_files) + return rsutil.common.has_a_match ('^.*\.r00$', prot_files) class OldRAR (PAR2Set.Base.Base): @@ -35,16 +35,16 @@ class OldRAR (PAR2Set.Base.Base): return 'OLDRAR' def find_extraction_heads (self): - return find_matches ('^.*\.rar', self.all_files) + return rsutil.common.find_matches ('^.*\.rar', self.all_files) def extraction_function (self, file, todir): assert os.path.isfile (file) assert os.path.isdir (todir) - RAR_CMD = config_get_value ('commands', 'unrar') + RAR_CMD = rsutil.common.config_get_value ('commands', 'unrar') cmd = '%s \"%s\"' % (RAR_CMD, file) - ret = run_command (cmd, todir) + ret = rsutil.common.run_command (cmd, todir) # Check error code if ret != 0: diff --git a/PAR2Set/ZIP.py b/PAR2Set/ZIP.py index f7ddc54..35bc73f 100644 --- a/PAR2Set/ZIP.py +++ b/PAR2Set/ZIP.py @@ -2,7 +2,7 @@ # vim: set ts=4 sts=4 sw=4 textwidth=92: import PAR2Set.Base -from RarslaveCommon import * +import rsutil.common # # This is a regular zip file type @@ -18,8 +18,8 @@ from RarslaveCommon import * # def detector (name_files, prot_files): - all_files = no_duplicates (name_files + prot_files) - return has_a_match ('^.*\.zip$', all_files) + all_files = rsutil.common.no_duplicates (name_files + prot_files) + return rsutil.common.has_a_match ('^.*\.zip$', all_files) class ZIP (PAR2Set.Base.Base): @@ -28,13 +28,13 @@ class ZIP (PAR2Set.Base.Base): return 'ZIP' def find_extraction_heads (self): - return find_matches ('^.*\.zip', self.all_files) + return rsutil.common.find_matches ('^.*\.zip', self.all_files) def extraction_function (self, file, todir): - ZIP_CMD = self.config_get_value ('commands', 'unzip') + ZIP_CMD = rsutil.common.config_get_value ('commands', 'unzip') cmd = ZIP_CMD % (file, todir) - ret = run_command (cmd) + ret = rsutil.common.run_command (cmd) # Check error code if ret != 0: diff --git a/RarslaveDetector.py b/RarslaveDetector.py index 0339336..e9988f9 100644 --- a/RarslaveDetector.py +++ b/RarslaveDetector.py @@ -1,7 +1,7 @@ #!/usr/bin/env python # vim: set ts=4 sts=4 sw=4 textwidth=92: -from RarslaveCommon import * +import rsutil.common # PAR2Set-derived types import PAR2Set.Join @@ -31,16 +31,16 @@ class RarslaveDetector (object): # The real "meat" of the class self.dir = dir self.p2file = p2file - self.basename = get_basename (p2file) + self.basename = rsutil.common.get_basename (p2file) # Find files that match by name only - self.name_matched_files = find_name_matches (self.dir, self.basename) + self.name_matched_files = rsutil.common.find_name_matches (self.dir, self.basename) # Find all par2 files for this set using name matches - self.all_p2files = find_par2_files (self.name_matched_files) + self.all_p2files = rsutil.common.find_par2_files (self.name_matched_files) # Try to get the protected files for this set - self.prot_matched_files = parse_all_par2 (self.dir, self.p2file, self.all_p2files) + self.prot_matched_files = rsutil.common.parse_all_par2 (self.dir, self.p2file, self.all_p2files) def runMatchingTypes (self): # Now tries to run every type of PAR2Set-derived class for which the detector diff --git a/rarslave.py b/rarslave.py index 16b8c78..a27c334 100644 --- a/rarslave.py +++ b/rarslave.py @@ -5,13 +5,12 @@ VERSION="2.0.0" PROGRAM="rarslave2" import os, sys, optparse, logging +import rsutil import RarslaveDetector -import RarslaveGlobals -from RarslaveCommon import * -# Global options from the RarslaveGlobals class -options = RarslaveGlobals.options -config = RarslaveGlobals.config +# Global options from the rsutil.globals class +options = rsutil.globals.options +config = rsutil.globals.config # A tiny class to hold logging output until we're finished class DelayedLogger (object): @@ -43,13 +42,13 @@ class CompareSet (object): self.dir = dir self.p2file = p2file - self.basename = get_basename (self.p2file) - self.name_matches = find_name_matches (self.dir, self.basename) + self.basename = rsutil.common.get_basename (self.p2file) + self.name_matches = rsutil.common.find_name_matches (self.dir, self.basename) def __eq__ (self, rhs): return (self.dir == rhs.dir) \ and (self.basename == rhs.basename) \ - and list_eq (self.name_matches, rhs.name_matches) + and rsutil.common.list_eq (self.name_matches, rhs.name_matches) def find_all_par2_files (dir): @@ -62,7 +61,7 @@ def find_all_par2_files (dir): dir = os.path.abspath (dir) files = os.listdir (dir) - return find_par2_files (files) + return rsutil.common.find_par2_files (files) def generate_all_parsets (dir): # Generate all parsets in the given directory. @@ -85,13 +84,13 @@ def check_required_progs(): shell_not_found = 32512 needed = [] - if run_command ('par2repair --help > /dev/null 2>&1') == shell_not_found: + if rsutil.common.run_command ('par2repair --help > /dev/null 2>&1') == shell_not_found: needed.append ('par2repair') - if run_command ('unrar --help > /dev/null 2>&1') == shell_not_found: + if rsutil.common.run_command ('unrar --help > /dev/null 2>&1') == shell_not_found: needed.append ('unrar') - if run_command ('unzip --help > /dev/null 2>&1') == shell_not_found: + if rsutil.common.run_command ('unzip --help > /dev/null 2>&1') == shell_not_found: needed.append ('unzip') if needed: @@ -103,7 +102,7 @@ def check_required_progs(): def run_options (options): # Fix directories - options.work_dir = full_abspath (options.work_dir) + options.work_dir = rsutil.common.full_abspath (options.work_dir) # Make sure that the directory is valid if not os.path.isdir (options.work_dir): @@ -113,7 +112,7 @@ def run_options (options): sys.exit (1) if options.extract_dir != None: - options.extract_dir = full_abspath (options.extract_dir) + options.extract_dir = rsutil.common.full_abspath (options.extract_dir) if options.version: print PROGRAM + ' - ' + VERSION @@ -162,19 +161,16 @@ def main (): # Build the OptionParser parser = optparse.OptionParser() - parser.add_option('-n', '--not-recursive', - action='store_false', dest='recursive', - default=config_get_value('options', 'recursive'), + parser.add_option('-n', '--not-recursive', action='store_false', dest='recursive', + default=rsutil.common.config_get_value('options', 'recursive'), help="Don't run recursively") - parser.add_option('-d', '--work-dir', - dest='work_dir', type='string', - default=config_get_value('directories', 'working_directory'), + parser.add_option('-d', '--work-dir', dest='work_dir', type='string', + default=rsutil.common.config_get_value('directories', 'working_directory'), help="Start running at DIR", metavar='DIR') - parser.add_option('-e', '--extract-dir', - dest='extract_dir', type='string', - default=config_get_value('directories', 'extract_directory'), + parser.add_option('-e', '--extract-dir', dest='extract_dir', type='string', + default=rsutil.common.config_get_value('directories', 'extract_directory'), help="Extract to DIR", metavar='DIR') parser.add_option('-p', '--check-required-programs', @@ -191,7 +187,7 @@ def main (): default=False, help="Write out the current config") parser.add_option('-i', '--interactive', dest='interactive', action='store_true', - default=config_get_value('options', 'interactive'), + default=rsutil.common.config_get_value('options', 'interactive'), help="Confirm before removing files") parser.add_option('-q', '--quiet', dest='quiet', action='count', @@ -207,8 +203,8 @@ def main (): # Parse the given options global options - (RarslaveGlobals.options, args) = parser.parse_args() - options = RarslaveGlobals.options + (rsutil.globals.options, args) = parser.parse_args() + options = rsutil.globals.options # Run any special actions that are needed on these options run_options (options) diff --git a/rsutil/__init__.py b/rsutil/__init__.py new file mode 100644 index 0000000..73c4551 --- /dev/null +++ b/rsutil/__init__.py @@ -0,0 +1,7 @@ +import globals +import common +import config +import par2parser + +__all__ = ['common', 'globals', 'config', 'par2parser'] + diff --git a/RarslaveCommon.py b/rsutil/common.py similarity index 93% rename from RarslaveCommon.py rename to rsutil/common.py index 8ffac91..b633171 100644 --- a/RarslaveCommon.py +++ b/rsutil/common.py @@ -11,8 +11,8 @@ import os import re import logging -import RarslaveGlobals -import Par2Parser +import rsutil.globals +import rsutil.par2parser # Global constants (SUCCESS, ECHECK, EEXTRACT, EDELETE, ECREATE, EDETECT, EPARSE) = range(7) @@ -90,7 +90,7 @@ def parse_all_par2 (dir, p2head, p2files): break try: - files = Par2Parser.get_protected_files (dir, f) + files = rsutil.par2parser.get_protected_files (dir, f) done = True except (EnvironmentError, OSError, OverflowError): logging.warning ('Corrupt PAR2 file: %s' % f) @@ -133,12 +133,12 @@ def list_eq (l1, l2): # Convience functions for the config def config_get_value (section, name): - return RarslaveGlobals.config.get_value (section, name) + return rsutil.globals.config.get_value (section, name) # Convience functions for the options def options_get_value (name): - return getattr (RarslaveGlobals.options, name) + return getattr (rsutil.globals.options, name) def main (): pass diff --git a/RarslaveConfig.py b/rsutil/config.py similarity index 99% rename from RarslaveConfig.py rename to rsutil/config.py index 4ba7adf..17e87d3 100644 --- a/RarslaveConfig.py +++ b/rsutil/config.py @@ -3,7 +3,7 @@ import os, ConfigParser -class RarslaveConfig (object): +class config (object): """A simple class to hold the default configs for the whole program""" DEFAULT_CONFIG='~/.config/rarslave2/rarslave2.conf' diff --git a/RarslaveGlobals.py b/rsutil/globals.py similarity index 89% rename from RarslaveGlobals.py rename to rsutil/globals.py index 0ecc0f0..a57fcd0 100644 --- a/RarslaveGlobals.py +++ b/rsutil/globals.py @@ -1,7 +1,7 @@ #!/usr/bin/env python # vim: set ts=4 sts=4 sw=4 textwidth=92: -import RarslaveConfig +import rsutil.config # I know that this sucks majorly, but I kinda need it to keep things # sane in the code. I don't /want/ to have to keep passing the config @@ -9,7 +9,7 @@ import RarslaveConfig # This will hold the configuration from the configuration file. This should # only be used to hold statically, non-runtime alterable content. -config = RarslaveConfig.RarslaveConfig () +config = rsutil.config.config () # This will hold the configuration from the command-line options. You should # probably be using this to get values in most cases. It takes its defaults diff --git a/Par2Parser.py b/rsutil/par2parser.py similarity index 100% rename from Par2Parser.py rename to rsutil/par2parser.py -- 2.25.1