]> scripts.mit.edu Git - wizard.git/blob - wizard/command/summary/__init__.py
Refactor to get rid of _package.py using __import__ magic.
[wizard.git] / wizard / command / summary / __init__.py
1 import optparse
2 import os
3 import logging
4
5 import wizard
6 from wizard import command, deploy
7
8 def main(argv, baton):
9     usage = """usage: %prog summary [ARGS] APPS
10
11 Scans all of the collected data from parallel-find.pl, and
12 calculates interesting information about them.
13
14 Its subcommands are:
15     count-exists    Counts how many autoinstalls contain a file
16     list            Prints the locations of all autoinstalls
17     list-errors     Prints all errors that occurred during parsing
18     version         Breakdown of autoinstalls by version (default)
19
20 Use %prog summary SUBCOMMAND --help for more information."""
21     parser = command.WizardOptionParser(usage)
22     parser.disable_interspersed_args()
23     baton.push(parser, "versions_path")
24     _, args = parser.parse_all(argv)
25     rest_argv = args[1:]
26     try:
27         command_name = args[0]
28     except IndexError:
29         command_name = "version"
30     def get_command(name):
31         member = name.replace("-", "_")
32         module = "wizard.command.summary." + member
33         __import__(module)
34         return getattr(wizard.command.summary, member)
35     if command == "help":
36         try:
37             get_command(rest_argv[0]).main(['--help'], baton)
38         except ImportError:
39             parser.error("invalid action")
40         except IndexError:
41             parser.print_help()
42             raise SystemExit(1)
43     try:
44         command_module = get_command(command_name)
45     except ImportError:
46         parser.error("invalid action")
47     command_module.main(rest_argv, baton)
48
49 ## -- some generic helper stuff --
50
51 def parse_install_lines(show, options, yield_errors = False):
52     if not show: show = deploy.applications
53     show = frozenset(show)
54     for line in deploy.getInstallLines(options.versions_path):
55         # construction
56         try:
57             d = deploy.Deployment.parse(line)
58             name = d.application.name
59         except deploy.NoSuchApplication as e:
60             if yield_errors:
61                 yield e
62             continue
63         except deploy.Error:
64             # we consider this a worse error
65             logging.warning("Error with '%s'" % line.rstrip())
66             continue
67         # filter
68         if name + "-" + str(d.version) in show or name in show:
69             pass
70         else:
71             continue
72         # yield
73         yield d
74
75 class Counter(object):
76     def __init__(self):
77         self.dict = {}
78     def count(self, value):
79         self.dict.setdefault(value, 0)
80         self.dict[value] += 1
81     def __getitem__(self, key):
82         return self.dict[key]
83     def __iter__(self):
84         return self.dict.__iter__()
85