]> scripts.mit.edu Git - wizard.git/blob - wizard/command/research.py
6ddd07cbfca1865bc4260bf82d6420baaae6b41a
[wizard.git] / wizard / command / research.py
1 import logging
2 import traceback
3 import os.path
4 import itertools
5 import random
6
7 from wizard import command, deploy, shell, util
8
9 def main(argv, baton):
10     options, show = parse_args(argv, baton)
11     sh = shell.Shell()
12     deploys = deploy.parse_install_lines(show, options.versions_path)
13     stats = {}
14     iffy = 0
15     clean = 0
16     total = 0
17     deploys = itertools.islice(deploys, options.limit)
18     if options.sample:
19         deploys = random.sample(list(deploys), options.sample)
20     try:
21         for d in deploys:
22             logging.info("Processing " + d.location)
23             try:
24                 d.verify()
25                 d.verifyTag(options.srv_path)
26                 d.verifyGit(options.srv_path)
27                 d.verifyConfigured()
28                 with util.ChangeDirectory(d.location):
29                     results = []
30                     out = sh.safeCall('git', 'diff', '--numstat', d.app_version.scripts_tag, strip=True)
31                     total += 1
32                     for line in out.split("\n"):
33                         added, deleted, filename = line.split(None, 3)
34                         if filename.endswith("php.ini"): continue
35                         if added == '-': continue
36                         if deleted == '-': continue
37                         added = int(added)
38                         deleted = int(deleted)
39                         # hook
40                         if filename == "LocalSettings.php":
41                             if added == deleted == 10:
42                                 continue
43                             elif added == deleted == 9:
44                                 continue
45                         elif filename == "AdminSettings.php":
46                             if added == 0 and deleted == 20:
47                                 continue
48                         elif filename == "config/index.php" or filename == "config/index.php5":
49                             if added == 0:
50                                 continue
51                         if not added and not deleted:
52                             continue
53                         results.append((added,deleted,filename))
54                     if len(results) > options.filter:
55                         print "-       -       " +  d.location
56                         iffy += 1
57                         continue
58                     if not results:
59                         clean += 1
60                     for added,deleted,filename in results:
61                         stats.setdefault(filename, 0)
62                         stats[filename] += 1
63                         # hook
64                         if filename == "LocalSettings.php" and not options.verbose:
65                             continue
66                         print "%-7d %-7d %s/%s" % (added,deleted,d.location,filename)
67             except (deploy.NotConfiguredError, deploy.NotMigratedError):
68                 # XXX: These should error, but for now don't
69                 pass
70             except (deploy.Error, shell.CallError):
71                 # XXX: Maybe should also do app.Error
72                 logging.error("%s in %s" % (traceback.format_exc(), d.location))
73             except KeyboardInterrupt:
74                 raise
75             except:
76                 logging.critical("%s in %s" % (traceback.format_exc(), d.location))
77     except KeyboardInterrupt:
78         print
79         print "Caught signal..."
80         pass
81     print '-' * 50
82     for filename in sorted(stats.keys()):
83         count = stats[filename]
84         if not count: continue
85         print "%-7d %s" % (count, filename)
86     print '-' * 50
87     print "%d out of %d (%.1f%%) had large diffstats" % (iffy, total, float(iffy)/total*100)
88     print "%d out of %d (%.1f%%) had clean diffstats" % (clean, total, float(clean)/total*100)
89
90 def parse_args(argv, baton):
91     usage = """usage: %prog research APP
92
93 Tells you how spectacularly an upgrade here will explode."""
94     parser = command.WizardOptionParser(usage)
95     parser.add_option("--limit", dest="limit", type="int",
96             default=None, help="Limit the number of autoinstalls to look at.")
97     parser.add_option("--sample", dest="sample", type="int", metavar="N",
98             default=None, help="Instead of researching all installs, research a random sample of N size.")
99     parser.add_option("--filter", dest="filter", type="int", metavar="N",
100             default=4, help="How many files are permitted in a diffstat before treating the install as having a 'large diffstat'")
101     baton.push(parser, "srv_path")
102     baton.push(parser, "versions_path")
103     options, args = parser.parse_all(argv)
104     if len(args) > 1:
105         parser.error("too many arguments")
106     if not args:
107         parser.error("must specify application to research")
108     return options, args
109