~ubuntu-archive/ubuntu-archive-tools/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#!/usr/bin/python3

# Copyright (C) 2011, 2012  Canonical Ltd.
# Author: Martin Pitt <martin.pitt@ubuntu.com>
# Author: Brian Murray <brian@ubuntu.com>

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Generate a HTML report of current NBS binary packages from a checkrdepends
# output directory

from collections import defaultdict
import csv
import functools
from optparse import OptionParser
import os
import sys
import time

from charts import make_chart, make_chart_header
from utils import read_tag_file

default_base = '/home/ubuntu-archive/mirror/ubuntu'
rdeps_with_alternates = []


def parse_checkrdepends_file(path, pkgmap):
    '''Parse one checkrdepends file into the NBS map'''

    cur_component = None
    cur_arch = None

    with open(path) as f:
        for line in f:
            if line.startswith('-- '):
                (cur_component, cur_arch) = line.split('/', 1)[1].split()[:2]
                continue
            assert cur_component
            assert cur_arch

            rdep = line.strip().split()[0]
            pkgmap.setdefault(rdep, (cur_component, []))[1].append(cur_arch)


def _pkg_removable(options, pkg, nbs, checked_v):
    '''Recursively check if package is removable.

    checked_v is the working set of already checked vertices, to avoid infinite
    loops.
    '''
    checked_v.add(pkg)
    packages = {}
    for rdep in nbs.get(pkg, []):
        if rdep in checked_v:
            continue
        global rdeps_with_alternates
        # utilize a copy of the arches as nbs will be modified
        arches = list(nbs[pkg][rdep][1])
        for arch in arches:
            alternate_available = False
            if arch == 'build':
                ptype = 'source'
                file = 'Sources'
            else:
                ptype = 'binary-%s' % arch
                file = 'Packages'
            key = '%s/dists/%s/%s/%s/%s.gz' % \
                  (options.archive_base, options.suite, nbs[pkg][rdep][0],
                   ptype, file)
            if key not in packages:
                packages[key] = read_tag_file(key, rdep)
            stanzas = packages[key]
            for stanza in stanzas:
                if 'binary' in ptype:
                    fields = ('Pre-Depends', 'Depends', 'Recommends')
                else:
                    fields = ('Build-Depends', 'Build-Depends-Indep')
                for field in fields:
                    if field not in stanza:
                        continue
                    if '|' not in stanza[field]:
                        continue
                    for or_dep in stanza[field].split(','):
                        if '|' not in or_dep:
                            continue
                        alternatives = [dep.strip()
                                        for dep in or_dep.split('|')]
                        if pkg not in alternatives:
                            continue
                        for dep in alternatives:
                            if dep == pkg:
                                continue
                            if dep not in nbs:
                                alternate_available = True
                                break
            if alternate_available:
                nbs[pkg][rdep][1].remove(arch)

        if len(nbs[pkg][rdep][1]) == 0:
            rdeps_with_alternates.append(rdep)

        if rdep not in nbs and rdep not in rdeps_with_alternates:
            try:
                checked_v.remove(rdep)
            except KeyError:
                pass
            return False
        if not _pkg_removable(options, rdep, nbs, checked_v):
            try:
                checked_v.remove(rdep)
            except KeyError:
                pass
            return False
    return True


def get_removables(options, nbs):
    '''Get set of removable packages.

    This includes packages with no rdepends and disconnected subgraphs, i. e.
    clusters of NBS packages which only depend on each other.
    '''
    removable = set()

    for p in nbs:
        if p in removable:
            continue
        checked_v = set()
        if _pkg_removable(options, p, nbs, checked_v):
            # we only add packages which are nbs to removable
            removable.update([p for p in checked_v if p in nbs])

    return removable


def html_report(options, nbs, removables):
    '''Generate HTML report from NBS map.'''

    global rdeps_with_alternates
    print('''\
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>NBS packages</title>
  <style type="text/css">
    body { background: #CCCCB0; color: black; }
    a { text-decoration: none; }
    table { border-collapse: collapse; border-style: none none;
            margin-bottom: 3ex; empty-cells: show; }
    table th { text-align: left; border-style: solid none none none;
               border-width: 3px; padding-right: 10px; }
    table td { vertical-align:top; text-align: left; border-style: dotted none;
               border-width: 1px; padding-right: 10px; }
    .normal { }
    .removable { color: green; font-weight: bold; }
    .nbs { color: blue; }
    .componentsup { font-size: 70%%; color: red; font-weight: bold; }
    .componentunsup { font-size: 70%%; color: darkred; }
  </style>
  %s
</head>
<body>
<h1>NBS: Binary packages not built from any source</h1>

<h2>Archive Administrator commands</h2>
<p>Run this command to remove NBS packages which are not required any more:</p>
''' % make_chart_header())

    print('<p style="font-family: monospace">remove-package -m NBS '
          '-d %s -s %s -b -y %s</p>' %
          (options.distribution, options.suite, ' '.join(sorted(removables))))

    print('''
<h2>Reverse dependencies</h2>

<p><span class="nbs">Reverse dependencies which are NBS themselves</span><br/>
<span class="removable">NBS package which can be removed safely</span></p>
<table>
''')
    reverse_nbs = defaultdict(list)  # non_nbs_pkg -> [nbspkg1, ...]
    pkg_component = {}  # non_nbs_pkg -> (component, component_class)

    for pkg in sorted(nbs):
        nbsmap = nbs[pkg]
        if pkg in removables:
            cls = 'removable'
        else:
            cls = 'normal'
        print('<tr><th colspan="4"><span class="%s">%s</span></th></tr>\n' %
              (cls, pkg), end="")
        for rdep in sorted(nbsmap):
            if rdep in rdeps_with_alternates:
                continue
            (component, arches) = nbsmap[rdep]

            if component in ('main', 'restricted'):
                component_cls = 'sup'
            else:
                component_cls = 'unsup'

            if rdep in nbs:
                if rdep in removables:
                    cls = 'removable'
                else:
                    cls = 'nbs'
            else:
                cls = 'normal'
                reverse_nbs[rdep].append(pkg)
                pkg_component[rdep] = (component, component_cls)

            print('<tr><td>&nbsp; &nbsp; </td>', end='')
            print('<td><span class="%s">%s</span></td> ' % (cls, rdep), end='')
            print('<td><span class="component%s">%s</span></td>' %
                  (component_cls, component), end='')
            print('<td>%s</td></tr>' % ' '.join(arches))

    print('''</table>
<h2>Packages which depend on NBS packages</h2>
<table>''')

    def sort_rev_nbs(k1, k2):
        def cmp(a, b):
            return (a > b) - (a < b)
        len_cmp = cmp(len(reverse_nbs[k1]), len(reverse_nbs[k2]))
        if len_cmp == 0:
            return cmp(k1, k2)
        else:
            return -len_cmp

    for pkg in sorted(reverse_nbs, key=functools.cmp_to_key(sort_rev_nbs)):
        print('<tr><td>%s</td> '
              '<td><span class="component%s">%s</span></td><td>' % (
                  pkg, pkg_component[pkg][1], pkg_component[pkg][0]), end="")
        print(" ".join(sorted(reverse_nbs[pkg])), end="")
        print('</td></tr>')

    print('</table>')

    if options.csv_file is not None:
        print("<h2>Over time</h2>")
        print(make_chart(
            os.path.basename(options.csv_file), ["removable", "total"]))

    print('<p><small>Generated at %s.</small></p>' %
          time.strftime('%Y-%m-%d %H:%M:%S %Z', time.gmtime(options.time)))
    print('</body></html>')


def main():
    parser = OptionParser(
        usage="%prog <checkrdepends output directory>",
        description="Generate an HTML report of current NBS binary packages.")
    parser.add_option('-B', '--archive-base', dest='archive_base',
                      help=('archive base directory (default: %s)' %
                            default_base),
                      default=default_base)
    parser.add_option('-d', '--distribution', default='ubuntu')
    parser.add_option('-s', '--suite', default='hirsute')
    parser.add_option(
        '--csv-file', help='record CSV time series data in this file')
    options, args = parser.parse_args()
    if len(args) != 1:
        parser.error("need a checkrdepends output directory")

    options.time = time.time()

    # pkg -> rdep_pkg -> (component, [arch1, arch2, ...])
    nbs = defaultdict(dict)

    for f in os.listdir(args[0]):
        if f.startswith('.') or f.endswith('.html'):
            continue
        parse_checkrdepends_file(os.path.join(args[0], f), nbs[f])

    #with open('/tmp/dot', 'w') as dot:
    #    print('digraph {', file=dot)
    #    print('   ratio 0.1', file=dot)
    #    pkgnames = set(nbs)
    #    for m in nbs.itervalues():
    #        pkgnames.update(m)
    #    for n in pkgnames:
    #        print('  %s [label="%s"' % (n.replace('-', '').replace('.', ''), n),
    #              end="", file=dot)
    #        if n in nbs:
    #            print(', style="filled", fillcolor="lightblue"', end="", file=dot)
    #        print(']', file=dot)
    #    print(file=dot)
    #    for pkg, map in nbs.iteritems():
    #        for rd in map:
    #            print('  %s -> %s' % (
    #                    pkg.replace('-', '').replace('.', ''),
    #                    rd.replace('-', '').replace('.', '')), file=dot)
    #    print('}', file=dot)

    removables = get_removables(options, nbs)

    html_report(options, nbs, removables)

    if options.csv_file is not None:
        if sys.version < "3":
            open_mode = "ab"
            open_kwargs = {}
        else:
            open_mode = "a"
            open_kwargs = {"newline": ""}
        csv_is_new = not os.path.exists(options.csv_file)
        with open(options.csv_file, open_mode, **open_kwargs) as csv_file:
            # Field names deliberately hardcoded; any changes require
            # manually rewriting the output file.
            fieldnames = [
                "time",
                "removable",
                "total",
                ]
            csv_writer = csv.DictWriter(csv_file, fieldnames)
            if csv_is_new:
                csv_writer.writeheader()
            csv_writer.writerow({
                "time": int(options.time * 1000),
                "removable": len(removables),
                "total": len(nbs),
                })


if __name__ == '__main__':
    main()