~ubuntu-core-dev/merge-o-matic/trunk

76 by Scott James Remnant
add a stats collector
1
#!/usr/bin/env python
97 by Scott James Remnant
make emacs happy about coding
2
# -*- coding: utf-8 -*-
114 by Scott James Remnant
add the GPL 3 to everything
3
# stats.py - collect difference stats
4
#
5
# Copyright © 2008 Canonical Ltd.
6
# Author: Scott James Remnant <scott@ubuntu.com>.
7
#
8
# This program is free software: you can redistribute it and/or modify
9
# it under the terms of version 3 of the GNU General Public License as
10
# published by the Free Software Foundation.
11
#
12
# This program is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
76 by Scott James Remnant
add a stats collector
19
20
import os
21
import time
22
import logging
23
24
from momlib import *
25
from deb.version import Version
26
27
28
def options(parser):
29
    parser.add_option("-D", "--source-distro", type="string", metavar="DISTRO",
30
                      default=SRC_DISTRO,
31
                      help="Source distribution")
32
    parser.add_option("-S", "--source-suite", type="string", metavar="SUITE",
33
                      default=SRC_DIST,
34
                      help="Source suite (aka distrorelease)")
35
36
    parser.add_option("-d", "--dest-distro", type="string", metavar="DISTRO",
37
                      default=OUR_DISTRO,
38
                      help="Destination distribution")
39
    parser.add_option("-s", "--dest-suite", type="string", metavar="SUITE",
40
                      default=OUR_DIST,
41
                      help="Destination suite (aka distrorelease)")
42
43
    parser.add_option("-p", "--package", type="string", metavar="PACKAGE",
44
                      action="append",
45
                      help="Process only theses packages")
46
    parser.add_option("-c", "--component", type="string", metavar="COMPONENT",
47
                      action="append",
48
                      help="Process only these destination components")
49
50
def main(options, args):
51
    src_distro = options.source_distro
52
    src_dist = options.source_suite
53
54
    our_distro = options.dest_distro
55
    our_dist = options.dest_suite
56
84 by Scott James Remnant
treat blacklisted packages as local
57
    blacklist = read_blacklist()
58
76 by Scott James Remnant
add a stats collector
59
    # For each package in the destination distribution, locate the latest in
60
    # the source distribution; calculate the base from the destination
61
    for our_component in DISTROS[our_distro]["components"]:
62
        stats = {}
63
        stats["total"] = 0
64
        stats["local"] = 0
65
        stats["unmodified"] = 0
66
        stats["needs-sync"] = 0
67
        stats["needs-merge"] = 0
68
        stats["repackaged"] = 0
69
        stats["modified"] = 0
70
71
        if options.component is not None \
72
               and our_component not in options.component:
73
            continue
74
75
        for our_source in get_sources(our_distro, our_dist, our_component):
76
            if options.package is not None \
77
                   and our_source["Package"] not in options.package:
78
                continue
79
80
            package = our_source["Package"]
81
            our_version = Version(our_source["Version"])
82
            logging.debug("%s: %s is %s", package, our_distro, our_version)
83
84
            stats["total"] += 1
85
84 by Scott James Remnant
treat blacklisted packages as local
86
            if package in blacklist:
87
                logging.debug("%s: blacklisted (locally packaged)", package)
88
                stats["local"] += 1
89
                continue
90
76 by Scott James Remnant
add a stats collector
91
            try:
92
                (src_source, src_version, src_pool_source) \
93
                             = get_same_source(src_distro, src_dist, package)
94
                logging.debug("%s: %s is %s", package, src_distro, src_version)
95
            except IndexError:
96
                logging.debug("%s: locally packaged", package)
97
                stats["local"] += 1
98
                continue
99
100
            base = get_base(our_source)
101
102
            if our_version == src_version:
103
                logging.debug("%s: unmodified", package)
104
                stats["unmodified"] += 1
86 by Scott James Remnant
if base is newer than src, local repackage without 0ubuntu - tsk
105
            elif base > src_version:
106
                logging.debug("%s: locally repackaged", package)
107
                stats["repackaged"] += 1
76 by Scott James Remnant
add a stats collector
108
            elif our_version == base:
109
                logging.debug("%s: needs sync", package)
110
                stats["needs-sync"] += 1
111
            elif our_version < src_version:
112
                logging.debug("%s: needs merge", package)
113
                stats["needs-merge"] += 1
114
            elif "-0ubuntu" in str(our_version):
115
                logging.debug("%s: locally repackaged", package)
116
                stats["repackaged"] += 1
117
            else:
118
                logging.debug("%s: modified", package)
119
                stats["modified"] += 1
120
121
        write_stats(our_component, stats)
122
123
def write_stats(component, stats):
124
    """Write out the collected stats."""
125
    stats_file = "%s/stats.txt" % ROOT
126
    stf = open(stats_file, "a");
127
    try:
128
        stamp = time.strftime("%Y-%m-%d %H:%M", time.gmtime())
129
        text = " ".join("%s=%d" % (k, v) for k,v in stats.items())
130
        print >>stf, "%s %s %s" % (stamp, component, text)
131
    finally:
132
        stf.close()
133
134
if __name__ == "__main__":
135
    run(main, options, usage="%prog",
77 by Scott James Remnant
correct stats.py description
136
        description="collect difference stats")