r329 - branches/rewrite/src

Sergio Talens-Oliag partial-mirror-devel@lists.alioth.debian.org
Wed, 17 Nov 2004 05:06:52 -0700


Author: sto
Date: Wed Nov 17 05:06:51 2004
New Revision: 329

Modified:
   branches/rewrite/src/debpartial-mirror.in
Log:
Added simple command line parsing, just to be able to change the conffile


Modified: branches/rewrite/src/debpartial-mirror.in
==============================================================================
--- branches/rewrite/src/debpartial-mirror.in	(original)
+++ branches/rewrite/src/debpartial-mirror.in	Wed Nov 17 05:06:51 2004
@@ -18,42 +18,78 @@
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 # $Id$
 
+# DEFAULTS
+conffile = "../etc/debpartial-mirror.conf"
+
+# Imports
+import getopt
 from Config import *
 from Backend import *
 
 def version():
-    print "debpartial-mirror @VERSION@ - Partial mirroring tool for Debian - @DATE@"
-    print "This program is free software and was released under the terms of the GNU General Public License"
-    print
+  print """debpartial-mirror @VERSION@ - Partial mirroring tool for Debian - @DATE@
+This program is free software and was released under the terms of the GNU General Public License
+"""
 
 def usage():
-    print "Usage: debpartial-mirror [OPTION]"
-    print
-    print "-h --help\t\t\tDisplay this help end exit"
-    print "-c<file> --configfile=<file>\tSelect a config file"
-    print "-v --version\t\t\tShow the version"
-    print
-    exit(2)
-
-version()
-try:
-    cnf = Config("../etc/debpartial-mirror.conf")
-except InvalidOption, msg:
-    print("Wrong option [%s] found on [%s] section."
-          % (msg.option, msg.section))
+  global conffile
+  
+  print """Usage: debpartial-mirror [OPTIONS]
+
+Where OPTIONS is one of:
+  -h, --help                 Display this help end exit
+  -c, --configfile=FILE      Select a config file (currently '%s')
+  -v, --version              Show program version
+""" % (conffile)
+  
+  exit(2)
+
+def main():
+
+  global conffile
+
+  try:
+    opts, args = getopt.getopt(sys.argv[1:], 'hvc:', 
+                               ["help", "version", "configfile="])
+  except getopt.GetoptError:
+    print "ERROR reading program options\n"
+    usage()
+
+  # Parse options
+  for o, a in opts:
+    if o in ("-h", "--help"):
+      usage()
+    if o in ("-v", "--version"):
+      version()
+      exit(0)
+    if o in ("-c", "--configfile"):
+      if a == '':
+        usage()
+      conffile = a
+
+  # Main program
+  try:
+    cnf = Config(conffile)
+  except InvalidOption, msg:
+    print("Wrong option [%s] found on [%s] section of '%s'."
+          % (msg.option, msg.section, conffile))
     exit(1)
-except InvalidSection, msg:
-    print("Wrong section [%s] found." % msg.section)
+  except InvalidSection, msg:
+    print("Wrong section [%s] found on '%s'."
+          % (msg.section, conffile))
     exit(1)
-except RequiredOptionMissing, msg:
-    print("Required option [%s] was missing on [%s] section."
-          % (msg.option, msg.section))
+  except RequiredOptionMissing, msg:
+    print("Required option [%s] was missing on [%s] section of '%s'."
+          % (msg.option, msg.section, conffile))
     exit(1)
 
-backends = []
-for b in cnf.getBackends():
+  backends = []
+  for b in cnf.getBackends():
     backends.append(Backend(b.section, cnf))
-for b in backends:
+  for b in backends:
     #b.update()
     b.upgrade()
-    
+
+# Main Program
+if __name__ == '__main__':
+  main()