don't inline code, do __name__=="__main__" so the module can be used instead of stric...
authorBrian Harring <ferringb@gentoo.org>
Thu, 12 Jan 2006 11:16:49 +0000 (11:16 -0000)
committerBrian Harring <ferringb@gentoo.org>
Thu, 12 Jan 2006 11:16:49 +0000 (11:16 -0000)
svn path=/main/trunk/; revision=2560

bin/emaint

index 34956926fdc9ea95af15fbb728d5f441fbf6113b..a3868772fcd97fca3f11a46f3a7a4e7b903c0a6b 100755 (executable)
@@ -45,70 +45,71 @@ class WorldHandler(object):
                return errors
 
 
+# this sucks, should track this in a different manner.
 modules = {"world" : WorldHandler}
 
-
 module_names = modules.keys()
 module_names.sort()
 module_names.insert(0, "all")
 
-
-def exclusive(option, unused1, unused2, unused3, var=None):
-       if not var:
-               raise ValueError("var not specified to exclusive()")
-       if getattr(parser, var, ""):
-               raise OptionValueError("%s and %s are exclusive options" % (getattr(parser, var), option))
-       setattr(parser, var, str(option))
+if __name__ == "__main__":
+       def exclusive(option, *args, **kw):
+               var = kw.get("var", None)
+               if var is None:
+                       raise ValueError("var not specified to exclusive()")
+               if getattr(parser, var, ""):
+                       raise OptionValueError("%s and %s are exclusive options" % (getattr(parser, var), option))
+               setattr(parser, var, str(option))
 
 
-usage = "usage: emaint [options] " + " | ".join(module_names)
+       usage = "usage: emaint [options] " + " | ".join(module_names)
 
-usage+= "\n\nCurrently emaint can only check and fix problems with one's world\n"
-usage+= "file.  Future versions will integrate other portage check-and-fix\n"
-usage+= "tools and provide a single interface to system health checks."
+       usage+= "\n\nCurrently emaint can only check and fix problems with one's world\n"
+       usage+= "file.  Future versions will integrate other portage check-and-fix\n"
+       usage+= "tools and provide a single interface to system health checks."
 
 
-parser = OptionParser(usage=usage)
-parser.add_option("-c", "--check", help="check for problems",
-       action="callback", callback=exclusive, callback_kwargs={"var":"action"})
-parser.add_option("-f", "--fix", help="attempt to fix problems",
-       action="callback", callback=exclusive, callback_kwargs={"var":"action"})
-parser.action = None
+       parser = OptionParser(usage=usage)
+       parser.add_option("-c", "--check", help="check for problems",
+               action="callback", callback=exclusive, callback_kwargs={"var":"action"})
+       parser.add_option("-f", "--fix", help="attempt to fix problems",
+               action="callback", callback=exclusive, callback_kwargs={"var":"action"})
+       parser.action = None
 
 
-(options, args) = parser.parse_args()
-if len(args) != 1:
-       parser.error("Incorrect number of arguments")
-if args[0] not in module_names:
-       parser.error("%s target is not a known target" % args[0])
+       (options, args) = parser.parse_args()
+       if len(args) != 1:
+               parser.error("Incorrect number of arguments")
+       if args[0] not in module_names:
+               parser.error("%s target is not a known target" % args[0])
 
-if parser.action:
-       action = parser.action
-else:
-       print "Defaulting to --check"
-       action = "-c/--check"
+       if parser.action:
+               action = parser.action
+       else:
+               print "Defaulting to --check"
+               action = "-c/--check"
 
-if args[0] == "all":
-       tasks = modules.values()
-else:
-       tasks = [modules[args[0]]]
+       if args[0] == "all":
+               tasks = modules.values()
+       else:
+               tasks = [modules[args[0]]]
 
 
-if action == "-c/--check":
-       status = "Checking %s for problems"
-       func = "check"
-else:
-       status = "Attempting to fix %s"
-       func = "fix"
+       if action == "-c/--check":
+               status = "Checking %s for problems"
+               func = "check"
+       else:
+               status = "Attempting to fix %s"
+               func = "fix"
 
 
-for task in tasks:
-       print status % task.name()
-       inst = task()
-       result = getattr(inst, func)()
-       if result:
-               print
-               print "\n".join(result)
-               print "\n"
+       for task in tasks:
+               print status % task.name()
+               inst = task()
+               result = getattr(inst, func)()
+               if result:
+                       print
+                       print "\n".join(result)
+                       print "\n"
 
-print "Finished"
+       print "Finished"