-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsb_autogen_slack-required.2.7
executable file
·209 lines (169 loc) · 5.71 KB
/
sb_autogen_slack-required.2.7
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
#!/usr/bin/env python
# Copyright (c) 2009, Christoph Willing [email protected]
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
import sys
import os
from os.path import islink, realpath
import subprocess
pkgdir = '/var/log/packages'
def find_package(target=None):
'''
Name: find_package
Paramaters:
target=None
Given the name of a file (any file, but typically a library)
search for its existence in the record of all packages
installed on the system. Ideally it would only be found
once but, in any case, we stop searching after finding the
first occurence.
Returns either:
package name in which the target was found
or:
None
'''
#print target
packageFound = False
packageFoundName = ""
for pkg in os.listdir(pkgdir):
if not packageFound:
for line in open(os.path.join(pkgdir, pkg)):
if target in line:
packageFound = True
packageFoundName = pkg
break
else:
break
if packageFound:
return packageFoundName
else:
#print "Couldn't find anything for", target
return None
def shortPackageName(fullName=None):
'''
Name: shortPackageName
Parameters:
fullName=None
Given a full package name, return just the name and
version components i.e. no architecture or build
Returns:
Tuple of package name and version
'''
if fullName is None:
return None
(nameverarch,sep,build) = fullName.rpartition('-')
(namever,sep,architecture) = nameverarch.rpartition('-')
(name,sep,version) = namever.rpartition('-')
return (name, version)
def unique(t=[]):
'''
Name: unique
Parameters:
t=[]
Remove duplicate members
Returns:
a list without any duplicate members
From: http://code.activestate.com/recipes/52560/
'''
n = len(t)
if n == 0:
return []
t.sort()
assert n > 0
last = t[0]
lasti = i = 1
while i < n:
if t[i] != last:
t[lasti] = last = t[i]
lasti += 1
i += 1
#print "Prereturn list:", t
return t[:lasti]
def verify_path(path=None):
if path is None:
return False
return os.path.exists(path)
def write_required_file(destdir, uniquePkgsFound):
'''
Name: write_required_file
Parameters:
destdir - the directory in which to write
uniquePkgsFound - a list of tuples containing
the names and versions of packages to be
written
Write a slack-required file into a directory (destdir)
Returns:
Nothing
'''
for fullname in uniquePkgsFound:
(shortname,version) = shortPackageName(fullname)
#print "%s %s" % (shortname,version)
with open(os.path.join(destdir, 'slack-required'), 'w') as f:
for fullname in uniquePkgsFound:
(shortname,version) = shortPackageName(fullname)
#f.write('{0}\t>= {1}\n'.format(shortname, version))
f.write('{0}\n'.format(shortname))
f.close()
def main(verified_paths=None, installdir=None):
if installdir is None:
return
pkgsFound = []
noPkgFound = []
#print "Verified Paths:", verified_paths
for path in verified_paths:
(libs, err) = subprocess.Popen(["ldd", path], stdout=subprocess.PIPE).communicate()
liblines = str(libs).split("\n")
if str(liblines[0]).endswith("not a dynamic executable"):
#print "%s is no good; ldd reports: %s" % (path, liblines)
continue
for line in liblines:
parts = line.split()
# is it a symbolic link
if len(parts) > 2:
pkgsearch = find_package(os.path.basename(os.path.realpath(parts[2])))
if pkgsearch is None:
if parts[0].startswith('linux-vdso.so'):
continue
else:
noPkgFound.append(parts)
else:
pkgsFound.append(pkgsearch)
#print "Libs without packages:"
#print noPkgFound
#print "Packages found:"
#print pkgsFound
uniquePkgsFound = unique(pkgsFound)
#print "duplicates removed:\n", print uniquePkgsFound
write_required_file(installdir, uniquePkgsFound)
if __name__ == "__main__":
verified_paths = []
test_paths = []
#test_paths = ['/usr/bin/vic', '/usr/bin/VenueClient3.py', '/usr/bin/xsane']
for path in test_paths:
if verify_path(path):
verified_paths.append(path)
for path in sys.argv[1:]:
if verify_path(path):
verified_paths.append(path)
'''
The directory to write to should already exist.
'''
try:
pkginstdir = os.environ['PKGINSTDIR']
if not os.path.isdir(pkginstdir):
print "Non existent write directory. Exiting now ..."
sys.exit(1)
except KeyError:
pkginstdir = os.getcwd()
#print "Write to %s directory" % pkginstdir
main(verified_paths, pkginstdir)