-
Notifications
You must be signed in to change notification settings - Fork 0
/
namcap.py
executable file
·197 lines (170 loc) · 5.65 KB
/
namcap.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
#! /usr/bin/env python
#
# namcap - A Pacman package analyzer
# Copyright (C) 2003-2007 Jason Chu <[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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#
# System wide global stuff
import sys, os, os.path, imp, getopt, types, tarfile, re, string, Namcap, pacman
import shutil
sandbox_directory = '/tmp/namcap.' + str(os.getpid())
# Functions
# Return all possible modules (rules)
def get_modules():
return Namcap.__all__
# Display usage information
def usage():
print "usage: " + sys.argv[0] + " [-r rulelist | --rules=rulelist] [-i | --info] package .."
print " -r list : returns list of available rules"
print " -i : prints information responses from rules"
sys.exit(2)
# Is the package a valid package file?
def verify_package(filename):
if not os.path.isfile(filename):
return 0
if not tarfile.is_tarfile(filename):
return 0
try:
tar = tarfile.open(package, "r")
if not tar:
return 0
if not '.PKGINFO' in tar.getnames():
tar.close()
return 0
except IOError:
if tar:
tar.close()
return 0
return tar
def process_tags(filename="/usr/share/namcap/tags"):
tags = {}
f = open(filename)
for i in f.readlines():
if i[0] == "#" or i.strip() == "": continue
tagdata = i[:-1].split("::")
tags[tagdata[0].strip()] = tagdata[1].strip()
return tags
# Main
modules = get_modules()
tags = process_tags()
info_reporting = 0
# get our options and process them
try:
optlist, args = getopt.getopt(sys.argv[1:], "ihmr:", ["rules=","info","help","machine-readable"])
except getopt.GetoptError:
usage()
active_modules = []
m = lambda s: tags[s]
for i, k in optlist:
if i in ('-r', '--rules') and active_modules == []:
if k == 'list':
print "-"*20 + " Namcap rule list " + "-"*20
for j in modules:
print string.ljust(j, 20) + ": " + __import__('Namcap.' + j, globals(), locals(), [Namcap]).package().long_name()
sys.exit(2)
module_list = k.split(',')
for j in module_list:
if j in modules:
active_modules.append(j)
else:
print "Error: Rule '" + j + "' does not exist"
usage()
if i in ('-i', '--info'):
info_reporting = 1
if i in ('-m', '--machine-readable'):
machine_readable = 1
m = lambda s: s
if i in ('-h', '--help'):
usage()
# If there are no args, print usage
if (args == []):
usage()
packages = args
# Go through each package, get the info, and apply the rules
for package in packages:
extracted = 0
if not os.access(package, os.R_OK):
print "Error: Problem reading " + package
usage()
if package[-7:] == '.tar.gz':
pkgtar = verify_package(package)
if not pkgtar:
print "Error: " + package + " is not a package"
if len(packages) > 1:
continue
pkginfo = pacman.load(package)
# No rules selected? Then select them all!
if active_modules == []:
active_modules = modules
# Loop through each one, load them apply if possible
for i in active_modules:
cur_class = __import__('Namcap.' + i, globals(), locals(), [Namcap])
pkg = cur_class.package()
ret = [[],[],[]]
if pkg.type() == "tarball":
if pkg.prereq() == "extract":
# If it's not extracted, then extract it and then analyze the package
if not extracted:
os.mkdir(sandbox_directory)
for j in pkgtar.getmembers():
pkgtar.extract(j, sandbox_directory)
extracted = 1
ret = pkg.analyze(pkginfo, sandbox_directory)
elif pkg.prereq() == "pkg":
ret = pkg.analyze(pkginfo, None)
elif pkg.prereq() == "tar":
ret = pkg.analyze(pkginfo, pkgtar)
else:
ret = [['Error running rule (' + i + ')'],[],[]]
# Output the three types of messages
if ret[0] != []:
for j in ret[0]:
print string.ljust(pkginfo.name, 10) + " E: " + m(j[0]) % j[1]
if ret[1] != []:
for j in ret[1]:
print string.ljust(pkginfo.name, 10) + " W: " + m(j[0]) % j[1]
if ret[2] != [] and info_reporting:
for j in ret[2]:
print string.ljust(pkginfo.name, 10) + " I: " + m(j[0]) % j[1]
# Clean up if we extracted anything
if extracted:
shutil.rmtree(sandbox_directory)
elif package[-8:] == 'PKGBUILD':
# We might want to do some verifying in here... but really... isn't that what pacman.load is for?
pkginfo = pacman.load(package)
if pkginfo == None:
print "Error: " + package + " is not a valid PKGBUILD"
continue
if active_modules == []:
active_modules = modules
for i in active_modules:
cur_class = __import__('Namcap.' + i, globals(), locals(), [Namcap])
pkg = cur_class.package()
ret = [[],[],[]]
if pkg.type() == "pkgbuild":
ret = pkg.analyze(pkginfo, package)
# Output the PKGBUILD messages
if ret[0] != []:
for j in ret[0]:
print string.ljust("PKGBUILD (" + pkginfo.name + ")", 20) + " E: " + m(j[0]) % j[1]
if ret[1] != []:
for j in ret[1]:
print string.ljust("PKGBUILD (" + pkginfo.name + ")", 20) + " W: " + m(j[0]) % j[1]
if ret[2] != [] and info_reporting:
for j in ret[2]:
print string.ljust("PKGBUILD (" + pkginfo.name + ")", 20) + " I: " + m(j[0]) % j[1]
# vim: set ts=4 sw=4 noet: