r336 - branches/rewrite/src

Sergio Talens-Oliag partial-mirror-devel@lists.alioth.debian.org
Wed, 17 Nov 2004 07:21:30 -0700


Author: sto
Date: Wed Nov 17 07:21:29 2004
New Revision: 336

Modified:
   branches/rewrite/src/debpartial-mirror.in
Log:
First version of main program with commands, note that now it shows the usage
message if you don't pass a command; feel free to change it, but I believe it
is a better aproach for newbies


Modified: branches/rewrite/src/debpartial-mirror.in
==============================================================================
--- branches/rewrite/src/debpartial-mirror.in	(original)
+++ branches/rewrite/src/debpartial-mirror.in	Wed Nov 17 07:21:29 2004
@@ -18,36 +18,70 @@
 # 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 *
 
+# ---------
+# Variables
+# ---------
+
+# Defaults
+conffile  = "../etc/debpartial-mirror.conf"
+
+# User command descriptions
+cmnds_desc = { 
+  'all':     'Updates and upgrades the selected mirror(s)',
+  'update':  'Update selected mirror(s) (download Package and Source lists)',
+  'upgrade': 'Upgrade selected mirror(s) (download binary and source packages)',
+}
+
+# Sorted list of user commands
+cmnds_list = cmnds_desc.keys()
+cmnds_list.sort()
+
+# ---------
+# Functions
+# ---------
 def version():
   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():
+def usage(ret=2):
   global conffile
+  global cmnds_desc
+  global cmnds_list
   
-  print """Usage: debpartial-mirror [OPTIONS]
+  cmnds_string = ""
+  for c in cmnds_list:
+    cmnds_string += "  %-27s %s\n" % (c, cmnds_desc[c])
+
+  print """Usage: debpartial-mirror [OPTIONS] COMMAND [MIRROR]
 
 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():
+COMMAND is one of:
+%s
+And MIRROR selects which mirror we should work with (all by default).
+""" % (conffile, cmnds_string)
 
+  exit(ret)
+
+def main():
   global conffile
+  global cmnds_list
+
+  cmnd = None
+  sect = None
 
+  # Parse options
   try:
     opts, args = getopt.getopt(sys.argv[1:], 'hvc:', 
                                ["help", "version", "configfile="])
@@ -55,19 +89,26 @@
     print "ERROR reading program options\n"
     usage()
 
-  # Parse options
-  for o, a in opts:
+  for o, v in opts:
     if o in ("-h", "--help"):
       usage()
     if o in ("-v", "--version"):
       version()
       exit(0)
     if o in ("-c", "--configfile"):
-      if a == '':
+      if v == '':
         usage()
-      conffile = a
+      conffile = v
 
-  # Main program
+  if len(args) > 0:
+    cmnd = args[0]
+    if cmnd not in cmnds_list:
+      print "ERROR: Unknown command '%s'" % cmnd
+      usage()
+    if len(args) > 1:
+      sect = args[1]
+      
+  # Load configuration file
   try:
     cnf = Config(conffile)
   except InvalidOption, msg:
@@ -83,13 +124,33 @@
           % (msg.option, msg.section, conffile))
     exit(1)
 
+  # Verify if the section is valid
+  if sect != None and not cnf.has_section(sect):
+    print("Unknown MIRROR [%s] on '%s'."
+          % (sect, conffile))
+    exit(1)
+
+  # Get available backends
   backends = []
   for b in cnf.getBackends():
-    backends.append(Backend(b.section, cnf))
+    if sect != "":
+      backends.append(Backend(b.section, cnf))
+    elif b.section == sect:
+      backends.append(Backend(b.section, cnf))
+
   for b in backends:
-    #b.update()
-    b.upgrade()
+    if cmnd == 'all':
+      b.update()
+      b.upgrade()
+    elif cmnd == 'update':
+      b.update()
+    elif cmnd == 'upgrade':
+      b.upgrade()
+    else:
+      usage()
 
+# ------------
 # Main Program
+# ------------
 if __name__ == '__main__':
   main()