forked from gofed/gofed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inspectpackage.py
406 lines (301 loc) · 10.4 KB
/
inspectpackage.py
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# ####################################################################
# gofed - set of tools to automize packaging of golang devel codes
# Copyright (C) 2014 Jan Chaloupka, [email protected]
#
# 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; either version 2
# of the License, or (at your option) any later version.
#
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
# ####################################################################
from subprocess import PIPE
from subprocess import Popen
import os
import optparse
import sys
RED = '\033[91m'
GREEN = '\033[92m'
BLUE = '\033[94m'
CYAN = '\033[96m'
WHITE = '\033[97m'
YELLOW = '\033[93m'
MAGENTA = '\033[95m'
GREY = '\033[90m'
BLACK = '\033[90m'
DEFAULT = '\033[99m'
ENDC = '\033[0m'
script_dir = os.path.dirname(os.path.realpath(__file__))
# golang(github.com/vishvananda/netlink)
# golang(%{import_path})
def getImportPath(raw_p, macro_p):
if raw_p[:7] != "golang(":
return ""
if macro_p[:7] != "golang(":
return ""
raw_p = raw_p[7:]
macro_p = macro_p[7:]
r_l = len(raw_p) - 1
m_l = len(macro_p) - 1
while r_l > 0 and m_l > 0:
if raw_p[r_l] == macro_p[m_l]:
r_l = r_l - 1
m_l = m_l - 1
else:
break
return raw_p[:r_l+1]
def err(msg):
print RED + msg + ENDC
def warning(msg):
print BLUE + msg + ENDC
def debug(msg):
print WHITE + msg + ENDC
def info(msg):
print GREEN + msg + ENDC
def step(msg):
print "%s%s %s%s" % (YELLOW, step.s_counter, msg, ENDC)
step.s_counter = step.s_counter + 1
step.s_counter = 1
a = "golang(github.com/vishvananda/netlink)"
b = "golang(%{import_path})"
#print getImportPath(a, b)
# devel build: missing/superfluous provides, list of executables
# other buils: list of executables
#
#
#
class Build:
name = ""
missing_provides = []
super_provides = []
executables = []
def __init__(self, name):
self.name = name
def addMissingProvides(self, provides):
self.missing_provides = provides
def addSuperProvides(self, provides):
self.super_provides = provides
def addExecutables(self, execs):
self.executables = execs
def __str__(self):
repre = "Name: %s" % self.name
repre = repre + "\nMissing provides: %s" % ",".join(self.missing_provides)
repre = repre + "\nSuperfluous provides: %s" % ",".join(self.super_provides)
repre = repre + "\nExecutables: %s" % ",".join(self.executables)
return repre
def toXml(self, tab = 1):
xml = tab*'\t' + "<build>\n"
xml += (tab+1)*'\t' + "<name>%s</name>\n" % self.name
xml += (tab+1)*'\t' + "<missing_provides>%s</missing_provides>\n" % ",".join(self.missing_provides)
xml += (tab+1)*'\t' + "<superfluous_provides>%s</superfluous_provides>\n" % ",".join(self.super_provides)
xml += (tab+1)*'\t' + "<executables>%s</executables>\n" % ",".join(self.executables)
xml += tab*'\t' + "</build>\n"
return xml
class Branch:
branch = ""
devel = None
others = []
def __init__(self, branch):
self.branch = branch
self.others = []
def addDevel(self, devel):
self.devel = devel
def addBuild(self, build):
self.others.append(build)
def __str__(self):
repre = "Branch: %s" % self.branch
if self.devel:
repre = repre + "\n====devel====\n"
repre = repre + str(self.devel)
for build in self.others:
repre = repre + "\n=============\n"
repre = repre + str(build)
return repre
def toXml(self, tab = 1):
xml = tab*'\t' + "<branch>\n"
xml += (tab+1)*'\t' + "<name>%s</name>\n" % self.branch
xml += (tab+1)*'\t' + "<builds>\n"
if self.devel:
xml += self.devel.toXml(tab+2)
for build in self.others:
xml += build.toXml(tab+2)
xml += (tab+1)*'\t' + "</builds>\n"
xml += tab*'\t' + "</branch>\n"
return xml
class Package:
name = ""
branches = []
def __init__(self, name):
self.name = name
def addBranch(self, branch):
self.branches.append(branch)
def __str__(self):
repre = "Package: %s" % self.name
for branch in self.branches:
repre = repre + "\n\n"
repre = repre + str(branch)
return repre
def toXml(self, tab = 1):
xml = tab*'\t' + "<package>\n"
xml += (tab+1)*'\t' + "<name>%s</name>\n" % self.name
xml += (tab+1)*'\t' + "<branches>\n"
for branch in self.branches:
xml += branch.toXml(tab+2)
xml += (tab+1)*'\t' + "</branches>\n"
xml += tab*'\t' + "</package>\n"
return xml
def runCommand(cmd):
#cmd = cmd.split(' ')
process = Popen(cmd, stderr=PIPE, stdout=PIPE, shell=True)
rt = process.returncode
stdout, stderr = process.communicate()
return stdout, stderr, rt
def getBuildProvides(build):
stdout, stderr, _ = runCommand("rpm -qp --provides %s | grep '^golang(' | cut -d' ' -f1 | sed 's/golang(//' | sed 's/)//' | sort -u" % build)
return sorted(stdout.split('\n')[:-1])
def getTarballProvides(import_path, directory):
stdout, stderr, _ = runCommand("%s/inspecttarball.py -p %s" % (script_dir, directory))
return sorted(map(lambda p: import_path if p == '.' else import_path + "/" + p, stdout.split('\n')[:-1]))
# from http://stackoverflow.com/questions/377017/test-if-executable-exists-in-python
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
def getBuildExecutable(build):
executables = []
stdout, stderr, _ = runCommand("mkdir -p unpackaged")
if stderr != "":
err("Unable to create unpackaged folder: %s" % stderr)
return 1
stdout, stderr, _ = runCommand("cp %s unpackaged/." % build)
os.chdir("unpackaged")
stdout, stderr, _ = runCommand("rpm2cpio %s | cpio -idmv" % build)
for dirName, subdirList, fileList in os.walk('.'):
for file in fileList:
fullPath = dirName + '/' + file
if is_exe(fullPath):
executables.append(fullPath)
os.chdir("..")
stdout, stderr, _ = runCommand("rm -rf unpackaged")
return executables
def checkProvides(pkg_name, devel):
info("\tChecking build Provides:")
bp = getBuildProvides(devel)
stdout, stderr, _ = runCommand("rpmspec -P *.spec | grep Provides | grep golang | grep ')' | grep '(' | sed 's/[ \t][ \t]*/ /g' | cut -d' ' -f2 | head -1")
subs_ip = stdout.split('\n')[0]
stdout, stderr, _ = runCommand("grep '%{import_path}' *.spec | grep Provides | sed 's/[ \t][ \t]*/ /g' | cut -d' ' -f2 | head -1")
raw_ip = stdout.split('\n')[0]
ip = getImportPath(subs_ip, raw_ip)
stdout, stderr, _ = runCommand("tar -tf $(cat sources | sed 's/[ \t][ \t]*/ /g' | cut -d' ' -f2) | cut -d '/' -f1 | head -1")
directory = stdout.split('\n')[0]
info("\tChecking tarball Provides:")
tp = getTarballProvides(ip, directory)
bp_missing = list(set(tp) - set(bp))
bp_superf = list(set(bp) - set(tp))
return bp_missing, bp_superf
def inspectBranch(pkg_name, branch, distro):
branch_obj = Branch(branch)
step("Branch: %s" % branch)
stdout, stderr, _ = runCommand("fedpkg switch-branch %s" % branch)
if stderr != "":
err("Unable to switch to %s: %s" % (branch, stderr))
return None, 1
stdout, stderr, _ = runCommand("fedpkg prep")
if stderr != "":
err("Unable to prep: %s" % stderr)
return None, 1
info("\tGetting builds from koji")
stdout, stderr, _ = runCommand("koji latest-build %s %s --quiet" % (distro, pkg_name))
if stderr != "":
err("Unable to get latest-build: %s" % stderr)
return None, 1
build = stdout.split(' ')[0]
if build == "GenericError:":
err("Unable to get latest-build: %s" % stdout)
return None, 1
stdout, stderr, _ = runCommand("koji download-build %s" % build)
# no test for stderr as the command writes to stderr
devel = ""
stdout, stderr, _ = runCommand("ls %s-devel-*.rpm" % pkg_name)
if stderr != "":
warning("No %s-devel package" % pkg_name)
else:
devel = stdout.split('\n')[0]
if devel != "":
build_obj = Build(devel)
info("\tInspecting %s" % devel)
mp, sp = checkProvides(pkg_name, devel)
build_obj.addMissingProvides(mp)
build_obj.addSuperProvides(sp)
info("\tChecking %s for executable" % devel)
executables = getBuildExecutable(devel)
build_obj.addExecutables(executables)
branch_obj.addDevel(build_obj)
# print build_obj.toXml()
cmd = "ls *.rpm | grep -v '.src.rpm$'"
if devel != "":
cmd += " | grep -v %s" % devel
stdout, stderr, _ = runCommand(cmd)
others = stdout.split('\n')[:-1]
if others:
info("\tChecking other builds")
for build in others:
build_obj = Build(build)
info("\tChecking %s for executable" % build)
executables = getBuildExecutable(build)
build_obj.addExecutables(executables)
branch_obj.addBuild(build_obj)
stdout, stderr, _ = runCommand("git clean -fd")
return branch_obj, 0
def inspectPackage(pkg_name):
cwd = os.getcwd()
pkg_obj = Package(pkg_name)
tmp_dir, stderr, _ = runCommand("mktemp -d")
if stderr != "":
err("Unable to create temp directory: %s" % stderr)
return None
tmp_dir = tmp_dir.split('\n')[0]
os.chdir("%s" % tmp_dir)
#err("Changing directory to %s" % tmp_dir)
step("Cloning %s" % pkg_name)
stdout, stderr, _ = runCommand("fedpkg clone %s" % pkg_name)
if stderr != "":
err("Unable to clone %s's repo: %s" % (pkg_name, stderr))
return None
os.chdir("%s" % pkg_name)
for (branch, distro) in [('master', 'rawhide'), ('f21', 'f21'), ('f20', 'f20'), ('el6', 'dist-6E-epel-build')]:
obj, er = inspectBranch(pkg_name, branch, distro)
if er == 0 and obj:
pkg_obj.addBranch(obj)
runCommand("rm -rf %s" % tmp_dir)
os.chdir(cwd)
return pkg_obj
if __name__ == "__main__":
parser = optparse.OptionParser("%prog [-o|--output FILE] pkg_name")
parser.add_option(
"", "-o", "--output", dest="output",
help = "Output to xml file"
)
options, args = parser.parse_args()
if len(args) != 1:
print "Synopsis: %prog [-o|--output FILE] pkg_name"
exit(1)
pkg_obj = inspectPackage(args[0])
if pkg_obj == None:
exit(1)
if options.output:
try:
with open(options.output, 'w') as file:
file.write('<?xml version="1.0" encoding="utf-8" ?>\n')
file.write(pkg_obj.toXml(0))
except IOError, e:
sys.stderr.write("%s\n", e)
exit(0)
else:
print pkg_obj
exit(0)