-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapt.py
383 lines (324 loc) · 9.9 KB
/
apt.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
#!/usr/bin/python
from id import get_binary_id, get_package_id
from task import tasks, Task
import package
from utils import get_config, null_dev, root_dir, get_temp_dir
import os
import sys
import re
import logging
import subprocess
import shutil
import stat
import tempfile
import struct
import string
package_exclude_rules = [
r'^linux-image',
r'^linux-headers',
r'.+-dev$',
r'.+-dbg$',
r'.+-debug$',
r'.+-doc(s)?(-.+)?$'
]
def get_packages():
package_source = get_config('package_source')
package_arch = get_config('package_arch')
package_options = get_config('package_options')
cmd = ["apt-cache"]
if package_source:
cmd += apt_options_for_source(package_source)
if package_arch:
cmd += ["-o", "APT::Architectures=" + package_arch]
if package_options:
for (opt, val) in package_options.items():
cmd += ["-o", opt + "=" + val]
process = subprocess.Popen(cmd + ["pkgnames"], stdout=subprocess.PIPE, stderr=null_dev)
(stdout, stderr) = process.communicate()
packages = []
for name in stdout.split():
excluded = False
for rule in package_exclude_rules:
if re.match(rule, name):
excluded = True
break
if not excluded:
packages.append(name)
process.wait()
return packages
def get_package_info(pkgname):
package_source = get_config('package_source')
package_arch = get_config('package_arch')
package_options = get_config('package_options')
cmd = []
if package_source:
cmd += apt_options_for_source(package_source)
if package_arch:
cmd += ["-o", "APT::Architectures=" + package_arch]
if package_options:
for (opt, val) in package_options.items():
cmd += ["-o", opt + "=" + val]
process = subprocess.Popen(["apt-get"] + cmd + ["download", "--print-uris", pkgname], stdout=subprocess.PIPE, stderr=null_dev)
(stdout, _) = process.communicate()
for line in stdout.split('\n'):
m = re.match('\'([^\']+)\'\s+[^\s_]+_([^\s_]+)_([^\s_]+).deb\s+\d+\s+(\S+)', line)
if m:
uri = m.group(1)
version = m.group(2)
arch = m.group(3)
hash = m.group(4)
has_source = False
extensions = [".c", ".cpp", ".c++", ".cxx", ".cc", ".cp"]
dir = None
process = None
try:
dir = download_package_source(pkgname)
if (dir):
for (root, subdirs, files) in os.walk(dir):
for f in files:
args = None
if f.endswith(".tar"):
args = "-tf"
elif f.endswith(".tar.gz") or f.endswith(".tgz"):
args = "-tzf"
elif f.endswith(".tar.bz2"):
args = "-tjf"
elif f.endswith(".tar.xz"):
args = "-tJf"
if args is None:
continue
process = subprocess.Popen(["tar", args, root + "/" + f], stdout=subprocess.PIPE, stderr=null_dev)
for line in process.stdout:
line = line.strip()
for ext in extensions:
if line.endswith(ext):
has_source = True
except Exception as e:
logging.info(str(e))
pass
if process:
process.kill()
if dir:
package.remove_dir(dir)
return {'arch': arch, 'version': version, 'uri': uri, 'opensource': has_source, 'hash': hash}
def get_package_dependency(pkgname):
package_source = get_config('package_source')
package_arch = get_config('package_arch')
package_options = get_config('package_options')
cmd = ["apt-cache"]
if package_source:
cmd += apt_options_for_source(package_source)
if package_arch:
cmd += ["-o", "APT::Architectures=" + package_arch]
if package_options:
for (opt, val) in package_options.items():
cmd += ["-o", opt + "=" + val]
process = subprocess.Popen(cmd + ["depends", pkgname], stdout=subprocess.PIPE, stderr=null_dev)
stdout = process.communicate()[0]
deps = []
for line in stdout.split('\n'):
m = re.match('\s*\|?Depends:\s+(\S+)', line)
if m:
if m.group(1) not in deps:
deps.append(m.group(1))
return deps
def apt_options_for_source(source):
return [
"-o", "Dir::Etc::SourceList=" + os.path.join(root_dir, source),
"-o", "Dir::Etc::SourceParts=-",
"-o", "Dir::Cache=" + os.path.join(root_dir, 'apt/cache'),
"-o", "Dir::State::Lists=" + os.path.join(root_dir, 'apt/lists'),
"-o", "Dir::State::Status=" + os.path.join(root_dir, 'apt/status'),
]
def update_apt(source=None, force=False):
cmd = ["apt-get", "update"]
if source:
for dir in ['apt', 'apt/lists', 'apt/cache']:
if not os.path.exists(dir):
os.mkdir(dir)
force = True
if not os.path.exists('apt/status'):
open('apt/status', 'w').close()
cmd += apt_options_for_source(source)
if not force:
return
logging.info("updating APT...")
process = subprocess.Popen(cmd, stdout = subprocess.PIPE,
stderr = subprocess.PIPE)
(stdout, stderr) = process.communicate()
if process.returncode != 0:
logging.info("Cannot update package")
def UpdateApt(jmgr, os_target, sql, args):
update_apt(get_config('package_source'), True)
tasks['UpdateApt'] = Task(
name = "Update APT repository",
func = UpdateApt,
order = 99)
def download_from_apt(name, source=None, arch=None, options=None):
cmd = ["apt-get", "download"]
if source:
cmd += apt_options_for_source(source)
if arch:
cmd += ["-o", "APT::Architectures=" + arch]
if options:
for (opt, val) in options.items():
cmd += ["-o", opt + "=" + val]
process = subprocess.Popen(cmd + [name], stdout=subprocess.PIPE, stderr=null_dev)
stdout = process.communicate()[0]
if process.returncode != 0:
raise Exception("Cannot download \'" + name + "\'")
for filename in os.listdir('.'):
if filename.endswith('.deb'):
return filename
raise Exception("\'" + name + "\' is not properly downloaded")
def download_source_from_apt(name, source=None, arch=None, options=None, unpack=False):
cmd = ["apt-get", "source"]
if not unpack:
cmd += ["--download-only"]
if source:
cmd += apt_options_for_source(source)
if arch:
cmd += ["-o", "APT::Architectures=" + arch]
if options:
for (opt, val) in options.items():
cmd += ["-o", opt + "=" + val]
process = subprocess.Popen(cmd + [name], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(stdout, stderr) = process.communicate()
if process.returncode != 0:
logging.error(stderr)
raise Exception("Cannot download \'" + name + "\'")
def unpack_package(name):
package_source = get_config('package_source')
package_arch = get_config('package_arch')
package_options = get_config('package_options')
dir = tempfile.mkdtemp('', '', get_temp_dir())
os.chdir(dir)
try:
filename = download_from_apt(name, package_source,
package_arch, package_options)
result = re.match('([^_]+)_([^_]+)_([^.]+).deb', filename)
if not result:
raise Exception("\'" + name + "\' is not properly downloaded")
name = result.group(1)
version = result.group(2)
arch = result.group(3)
result = subprocess.call(["dpkg", "-x", filename, "."], stdout=null_dev, stderr=null_dev)
if result != 0:
raise Exception("Cannot unpack \'" + name + "\'")
except:
os.chdir(root_dir)
package.remove_dir(dir)
raise
os.chdir(root_dir)
return (dir, name, version)
def download_package_source(name, unpack=False):
package_source = get_config('package_source')
package_arch = get_config('package_arch')
package_options = get_config('package_options')
dir = tempfile.mkdtemp('', '', get_temp_dir())
os.chdir(dir)
try:
download_source_from_apt(name, package_source,
package_arch, package_options, unpack)
except:
os.chdir(root_dir)
package.remove_dir(dir)
return None
#raise
os.mkdir(dir + '/refs')
os.chdir(root_dir)
return dir
def check_elf(path):
process = subprocess.Popen(["readelf", "--file-header", "-W", path], stdout=subprocess.PIPE, stderr=null_dev)
for line in process.stdout:
results = re.match(r"([^\:]+)\: +(.+)", line.strip())
if results:
key = results.group(1)
val = results.group(2)
if key == 'Class':
if val != 'ELF64':
return False
else:
break
if process.wait() != 0:
return False
process = subprocess.Popen(["readelf", "--section-headers", "-W", path], stdout=subprocess.PIPE, stderr=null_dev)
has_text = False
for line in process.stdout:
parts = line[6:].strip().split()
# a valid elf needs to have a .text section (or it could be debug object)
if parts and parts[0] == '.text':
has_text = True
if process.wait() != 0:
return False
return has_text
def which(file):
for prefix in os.environ["PATH"].split(":"):
path = os.path.join(prefix, file)
if os.path.exists(path):
return path
return None
def check_script(path):
binary = open(path, 'rb')
interpreter = ''
try:
ch1 = struct.unpack('s', binary.read(1))[0]
ch2 = struct.unpack('s', binary.read(1))[0]
if ch1 != '#' or ch2 != '!':
binary.close()
return None
while True:
ch = struct.unpack('s', binary.read(1))[0]
if ch == '\r' or ch == '\n':
break
if ch not in string.printable:
binary.close()
return None
interpreter += ch
parts = interpreter.strip().split()
if parts[0] == '/usr/bin/env':
interpreter = which(parts[1])
else:
interpreter = parts[0]
except:
interpreter = None
binary.close()
return interpreter
def get_binaries(dir, find_script=False):
binaries = []
for (root, subdirs, files) in os.walk(dir):
if not os.access(root, os.X_OK):
try:
os.chmod(root, 0755)
except:
continue
rel_root = root[len(dir) + 1:]
for f in files:
path = root + '/' + f
rel_path = '/' + rel_root + '/' + f
s = os.lstat(path)
if re.match('[0-9A-Za-z\_\-\+\.]+\.so[0-9\.]*', f):
if stat.S_ISLNK(s.st_mode):
binaries.append((rel_path, 'lnk', None))
continue
if check_elf(path):
binaries.append((rel_path, 'lib', None))
continue
if stat.S_ISLNK(s.st_mode):
continue
if s.st_mode & (stat.S_IXUSR|stat.S_IXGRP|stat.S_IXOTH):
if check_elf(path):
binaries.append((rel_path, 'exe', None))
continue
if find_script:
interpreter = check_script(path)
if interpreter:
binaries.append((rel_path, 'scr', interpreter))
continue
if find_script:
ext = os.path.splitext(rel_path)
if ext in ['.py', '.sh', 'pl', '.PL']:
interpreter = check_script(path)
if interpreter:
binaries.append((rel_path, 'scr', interpreter))
return binaries