Debian Codegraph @

popbugs

raw
1 #!/usr/bin/python3
2
3 # popbugs - Find RC bugs in packages you commonly use
4 # Copyright (C) 2001-2004 Matt Zimmerman <[email protected]>
5
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2, or (at your option)
9 # any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 # 02111-1307, USA.
20 #
21
22
23 import sys
24 import re
25 import os
26 import urllib.request, urllib.error, urllib.parse
27 import tempfile
28 import getopt
29
30 usage = '''Usage: popbugs [-d|--debug] [{-o|--output=} {outputfile|-}] [popularity-contest log]\n'''
31
32 popconfile = '/var/log/popularity-contest'
33 bugurl = 'https://bugs.debian.org/release-critical/other/all.html'
34 # For testing purposes:
35 #bugurl = 'file:///tmp/all.html'
36 outputfile = None
37
38 debug = False
39 try:
40 opts, args = getopt.getopt(sys.argv[1:], "dho:",
41 ["debug", "help","output="])
42 except getopt.GetoptError:
43 sys.stderr.write(usage)
44 sys.exit(2)
45 for o, a in opts:
46 if o in ("-h", "--help"):
47 sys.stdout.write(usage)
48 sys.exit()
49 elif o in ("-o", "--output"):
50 outputfile = a
51 elif o in ("-d", "--debug"):
52 debug = True
53
54 if len(args) >= 1:
55 popconfile = args[0]
56 if not os.path.exists(popconfile):
57 sys.stderr.write('''
58 I cannot find the popularity-contest data you pointed me to.
59 This program requires the data collected from popularity-contest
60 in order to work.
61
62 ''')
63 sys.exit(1)
64 else:
65 if not os.path.exists(popconfile):
66 if not os.path.exists('/usr/sbin/popularity-contest'):
67 sys.stderr.write('''
68 The popularity-contest package does not appear to be installed.
69 This program requires the data collected from popularity-contest
70 in order to work.
71
72 ''')
73 sys.exit(1)
74
75 sys.stderr.write('''
76 There is no popularity-contest data present on your system. This
77 probably means that popularity-contest has not yet run since it
78 was installed. Try waiting for /etc/cron.daily/popularity-contest to
79 to collect some data or manually run (as root user):
80
81 /usr/sbin/popularity-contest >/var/log/popularity-contest
82
83 ''')
84 sys.exit(1)
85
86 if outputfile == None:
87 fd, outputfile = tempfile.mkstemp(prefix='popbugs-', suffix='.html')
88 output = os.fdopen(fd, 'w')
89 view = True
90 elif outputfile == '-':
91 output = sys.stdout
92 view = False
93 else:
94 output = open(outputfile, 'w')
95 view = False
96
97 class Package:
98 def __init__(self, name, atime):
99 self.name = name
100 self.atime = int(atime)
101
102 packages = {}
103 pkglist = []
104 popcon = open(popconfile,'r')
105 for line in popcon.readlines():
106 if len(line) == 0 or line.find(':') != -1:
107 continue
108
109 fields = line.split()
110 if len(fields) != 4:
111 continue
112 if (fields[0] == 'POPULARITY-CONTEST-0' or
113 fields[0] == 'END-POPULARITY-CONTEST-0'):
114 continue
115
116 (atime, ctime, package, pathname) = fields
117 # if pathname == '<NOFILES>' or pathname == '<RECENT-CTIME>':
118 # continue
119
120 if debug:
121 print("POPCON: Adding package " + package)
122 packages[package] = Package(package,atime)
123 pkglist.append(packages[package])
124
125 popcon.close()
126
127 page = urllib.request.urlopen(bugurl).readlines()
128
129 while page:
130 line = page.pop(0).decode('utf-8')
131 if line.startswith('<div'):
132 break
133 output.write(line)
134
135 packagere = re.compile('.*<a name="([^"]+)"><strong>Package:.*')
136 while page:
137 m = packagere.match(page[0].decode('utf-8'))
138 if m:
139 html = ''
140 while page:
141 line = page.pop(0).decode('utf-8')
142 html += line
143
144 if line == '\n' or (page and packagere.match(page[0])):
145 # another paragraph started with no newline
146 break
147
148 pkgname = m.group(1)
149 if debug:
150 print("REGEX: Parsing package " + pkgname)
151
152 if pkgname in packages:
153 if debug:
154 print("REGEX: Package " + pkgname + "is in packages")
155 packages[pkgname].html = html
156 else:
157 if page[0].startswith(bytes('<hr>', 'ascii')):
158 break
159 page.pop(0)
160
161 pkglist.sort(key=lambda k: -k.atime)
162 for package in pkglist:
163 if hasattr(package,'html'):
164 output.write(package.html)
165 output.write('\n')
166
167 while page:
168 line = page.pop(0).decode('utf-8')
169 output.write(line)
170
171 output.flush()
172
173 if view:
174 os.system('sensible-browser file://' + outputfile)