Use exceptions for error handling
[rarslave2.git] / rsutil / common.py
index b633171..370d7d6 100644 (file)
@@ -1,22 +1,40 @@
 #!/usr/bin/env python
 # vim: set ts=4 sts=4 sw=4 textwidth=92:
 
+"""
+Module holding all of the common functions used throughout the rarslave project
+"""
+
 __author__    = "Ira W. Snyder (devel@irasnyder.com)"
 __copyright__ = "Copyright (c) 2006, Ira W. Snyder (devel@irasnyder.com)"
 __license__   = "GNU GPL v2 (or, at your option, any later version)"
 
-# This module holds all of the common functions used throughout the rarslave project.
+#    common.py -- holds all of the common functions used throughout the rarslave project
+#
+#    Copyright (C) 2006,2007  Ira W. Snyder (devel@irasnyder.com)
+#
+#    This program is free software; you can redistribute it and/or modify
+#    it under the terms of the GNU General Public License as published by
+#    the Free Software Foundation; either version 2 of the License, or
+#    (at your option) any later version.
+#
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU General Public License for more details.
+#
+#    You should have received a copy of the GNU General Public License
+#    along with this program; if not, write to the Free Software
 
 import os
 import re
 import logging
+import subprocess
+import exceptions
 
 import rsutil.globals
 import rsutil.par2parser
 
-# Global constants
-(SUCCESS, ECHECK, EEXTRACT, EDELETE, ECREATE, EDETECT, EPARSE) = range(7)
-
 def find_matches (regex, li, ignorecase=True):
 
        if ignorecase:
@@ -47,14 +65,22 @@ def run_command (cmd, indir=None):
        # Runs the specified command-line in the directory given (or, in the current directory
        # if none is given). It returns the status code given by the application.
 
-       pwd = os.getcwd ()
-
-       if indir != None:
+       if indir == None:
+               indir = os.getcwd()
+       else:
                assert os.path.isdir (indir) # MUST be a directory!
-               os.chdir (indir)
 
-       ret = os.system (cmd)
-       os.chdir (pwd)
+       print 'RUNNING COMMAND'
+       print 'Directory: %s' % indir
+       print 'Command: %s' % cmd[0]
+       for f in cmd[1:]:
+               print '-> %s' % f
+
+       ret = subprocess.Popen(cmd, cwd=indir).wait()
+
+       if ret != 0:
+               raise RuntimeError
+
        return ret
 
 def full_abspath (p):
@@ -124,11 +150,7 @@ def list_eq (l1, l2):
        if len(l1) != len(l2):
                return False
 
-       for e in l1:
-               if e not in l2:
-                       return False
-
-       return True
+       return set(l1) == set(l2)
 
 # Convience functions for the config
 
@@ -140,9 +162,3 @@ def config_get_value (section, name):
 def options_get_value (name):
        return getattr (rsutil.globals.options, name)
 
-def main ():
-       pass
-
-if __name__ == '__main__':
-       main ()
-