From a19b4fb59bafd03043b0cee1014dd454ac342e60 Mon Sep 17 00:00:00 2001 From: "Edward Z. Yang" Date: Thu, 30 Jul 2009 23:49:27 -0400 Subject: [PATCH 1/1] Move a bunch of summary items to full class commands. Signed-off-by: Edward Z. Yang --- bin/wizard | 2 + .../{summary/list_errors.py => errors.py} | 5 +-- wizard/command/list.py | 42 +++++++++++++++++++ wizard/command/summary/__init__.py | 40 ------------------ wizard/command/summary/count_exists.py | 31 -------------- wizard/command/summary/list.py | 36 ---------------- wizard/command/summary/version.py | 9 ++-- wizard/deploy.py | 24 +++++++++++ wizard/util.py | 11 +++++ 9 files changed, 85 insertions(+), 115 deletions(-) rename wizard/command/{summary/list_errors.py => errors.py} (81%) create mode 100644 wizard/command/list.py delete mode 100644 wizard/command/summary/count_exists.py delete mode 100644 wizard/command/summary/list.py diff --git a/bin/wizard b/bin/wizard index 795adad..95ad172 100755 --- a/bin/wizard +++ b/bin/wizard @@ -15,7 +15,9 @@ def main(): Wizard is a Git-based autoinstall management system for scripts. Its commands are: + errors Lists all broken autoinstall metadata info Reports information about an autoinstall + list Lists autoinstalls, with optional filtering massmigrate Performs mass migration of autoinstalls of an application migrate Migrate autoinstalls from old format to Git-based format summary Generate statistics (see help for subcommands) diff --git a/wizard/command/summary/list_errors.py b/wizard/command/errors.py similarity index 81% rename from wizard/command/summary/list_errors.py rename to wizard/command/errors.py index 7a6702f..5250254 100644 --- a/wizard/command/summary/list_errors.py +++ b/wizard/command/errors.py @@ -1,11 +1,10 @@ import logging from wizard import deploy, command -from wizard.command import summary def main(argv, baton): options, show = parse_args(argv, baton) - for e in summary.parse_install_lines(show, options, True): + for e in deploy.parse_install_lines(show, options, True): if not isinstance(e, deploy.Error): if isinstance(e, Exception): raise e @@ -16,7 +15,7 @@ def main(argv, baton): print e.location def parse_args(argv, baton): - usage = """usage: %prog summary list-errors [ARGS] + usage = """usage: %prog errors [ARGS] Lists all errors that occurred while parsing the versions directory.""" diff --git a/wizard/command/list.py b/wizard/command/list.py new file mode 100644 index 0000000..d96f12e --- /dev/null +++ b/wizard/command/list.py @@ -0,0 +1,42 @@ +import logging +import traceback +import os.path + +from wizard import command, deploy + +def main(argv, baton): + options, show = parse_args(argv, baton) + errors = 0 + for d in deploy.parse_install_lines(show, options, True): + if isinstance(d, Exception): + errors += 1 + if options.exists and not os.path.exists(os.path.join(d.location, options.exists)): + continue + print d.location + if errors: + logging.warning("%d errors, see wizard errors for details" % errors) + +def parse_args(argv, baton): + usage = """usage: %prog list [ARGS] [APP[-VERSION]] + +Lists the locations of all autoinstalls, optionally +filtered on parameters such as application name and version. + +Examples: + %prog list + List all autoinstalls + %prog list --exists php.ini + List all autoinstalls with php.ini + %prog list mediawiki + List only MediaWiki autoinstalls + %prog list mediawiki-1.11.0 + List only Mediawiki 1.11.0 autoinstalls""" + parser = command.WizardOptionParser(usage) + parser.add_option("-e", "--exists", dest="exists", + help="only print deployment if FILE exists", metavar="FILE") + baton.push(parser, "versions_path") + options, args = parser.parse_all(argv) + if len(args) > 1: + parser.error("too many arguments") + return options, args + diff --git a/wizard/command/summary/__init__.py b/wizard/command/summary/__init__.py index 7284f8c..f47c61a 100644 --- a/wizard/command/summary/__init__.py +++ b/wizard/command/summary/__init__.py @@ -12,9 +12,6 @@ Scans all of the collected data from parallel-find.pl, and calculates interesting information about them. Its subcommands are: - count-exists Counts how many autoinstalls contain a file - list Prints the locations of all autoinstalls - list-errors Prints all errors that occurred during parsing version Breakdown of autoinstalls by version (default) Use %prog summary SUBCOMMAND --help for more information.""" @@ -46,40 +43,3 @@ Use %prog summary SUBCOMMAND --help for more information.""" parser.error("invalid action") command_module.main(rest_argv, baton) -## -- some generic helper stuff -- - -def parse_install_lines(show, options, yield_errors = False): - if not show: show = deploy.applications() - show = frozenset(show) - for line in deploy.getInstallLines(options.versions_path): - # construction - try: - d = deploy.Deployment.parse(line) - name = d.application.name - except deploy.NoSuchApplication as e: - if yield_errors: - yield e - continue - except deploy.Error: - # we consider this a worse error - logging.warning("Error with '%s'" % line.rstrip()) - continue - # filter - if name + "-" + str(d.version) in show or name in show: - pass - else: - continue - # yield - yield d - -class Counter(object): - def __init__(self): - self.dict = {} - def count(self, value): - self.dict.setdefault(value, 0) - self.dict[value] += 1 - def __getitem__(self, key): - return self.dict[key] - def __iter__(self): - return self.dict.__iter__() - diff --git a/wizard/command/summary/count_exists.py b/wizard/command/summary/count_exists.py deleted file mode 100644 index e9f6eea..0000000 --- a/wizard/command/summary/count_exists.py +++ /dev/null @@ -1,31 +0,0 @@ -import os - -from wizard import command -from wizard.command import summary - -def main(argv, baton): - options, args = parse_args(argv, baton) - value = args[0] - show = args[1:] - for d in summary.parse_install_lines(show, options): - if os.path.exists(d.location + "/" + value): - print d.location - -def parse_args(argv, baton): - usage = """usage: %prog summary count-exists [ARGS] FILE [APP[-VERSION]] - -Lists all autoinstalls that contain FILE in their -working copy. - -Examples: - %prog summary count-exists php.ini - Finds all autoinstalls that contain php.ini files""" - parser = command.WizardOptionParser(usage) - baton.push(parser, "versions_path") - options, args = parser.parse_all(argv) - if len(args) > 1: - parser.error("too many arguments") - if not args: - parser.error("need to specify FILE") - return options, args - diff --git a/wizard/command/summary/list.py b/wizard/command/summary/list.py deleted file mode 100644 index 7d3feb1..0000000 --- a/wizard/command/summary/list.py +++ /dev/null @@ -1,36 +0,0 @@ -import logging -import traceback - -from wizard import command -from wizard.command import summary - -def main(argv, baton): - options, show = parse_args(argv, baton) - errors = 0 - for d in summary.parse_install_lines(show, options, True): - if isinstance(d, Exception): - errors += 1 - print d.location - if errors: - logging.warning("%d errors, see wizard summary list-errors for details" % errors) - -def parse_args(argv, baton): - usage = """usage: %prog summary list [ARGS] [APP[-VERSION]] - -Lists the locations of all autoinstalls, optionally -filtered on application name and version. - -Examples: - %prog summary list - List all autoinstalls - %prog summary list mediawiki - List only MediaWiki autoinstalls - %prog summary list mediawiki-1.11.0 - List only Mediawiki 1.11.0 autoinstalls""" - parser = command.WizardOptionParser(usage) - baton.push(parser, "versions_path") - options, args = parser.parse_all(argv) - if len(args) > 1: - parser.error("too many arguments") - return options, args - diff --git a/wizard/command/summary/version.py b/wizard/command/summary/version.py index 09d17ee..2dc29c8 100644 --- a/wizard/command/summary/version.py +++ b/wizard/command/summary/version.py @@ -1,15 +1,14 @@ import math -from wizard import command -from wizard.command import summary +from wizard import command, deploy, util def main(argv, baton): options, show = parse_args(argv, baton) HISTOGRAM_WIDTH = 30 show = set() - c_version = summary.Counter() - c_application = summary.Counter() - for d in summary.parse_install_lines(show, options): + c_version = util.Counter() + c_application = util.Counter() + for d in deploy.parse_install_lines(show, options): version = d.app_version c_version.count(version) c_application.count(version.application) diff --git a/wizard/deploy.py b/wizard/deploy.py index 5b4ae3d..e6f5918 100644 --- a/wizard/deploy.py +++ b/wizard/deploy.py @@ -15,6 +15,30 @@ def getInstallLines(vs): return fileinput.input([vs]) return fileinput.input([vs + "/" + f for f in os.listdir(vs)]) +def parse_install_lines(show, options, yield_errors = False): + if not show: show = applications() + show = frozenset(show) + for line in getInstallLines(options.versions_path): + # construction + try: + d = Deployment.parse(line) + name = d.application.name + except deploy.NoSuchApplication as e: + if yield_errors: + yield e + continue + except deploy.Error: + # we consider this a worse error + logging.warning("Error with '%s'" % line.rstrip()) + continue + # filter + if name + "-" + str(d.version) in show or name in show: + pass + else: + continue + # yield + yield d + ## -- Model Objects -- class Deployment(object): diff --git a/wizard/util.py b/wizard/util.py index 08a6a66..2588d17 100644 --- a/wizard/util.py +++ b/wizard/util.py @@ -17,6 +17,17 @@ class ChangeDirectory(object): def __exit__(self, *args): os.chdir(self.olddir) +class Counter(object): + def __init__(self): + self.dict = {} + def count(self, value): + self.dict.setdefault(value, 0) + self.dict[value] += 1 + def __getitem__(self, key): + return self.dict[key] + def __iter__(self): + return self.dict.__iter__() + def dictmap(f, d): """A map function for dictionaries. Does not allow changing keys, only values""" -- 2.45.1