-
Notifications
You must be signed in to change notification settings - Fork 11
/
user-installed.py
executable file
·403 lines (352 loc) · 12.3 KB
/
user-installed.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
#!/usr/bin/env python3
# List all manually installed packages.
#
# Supported distributions:
#
# - Fedora
# - CentOS/RHEL
# - Termux
# - Debian
# - Ubuntu
#
# 2017, Georg Sauthoff <mail@gms.tf>, GPLv3+
import gzip
import io
import itertools
import operator
import os
import distro
import re
import subprocess
import sys
import unittest.mock as mock
split_version_re = re.compile('[.+-]')
class LooseVersion:
def __init__(self, s):
self.vs = split_version_re.split(s)
self.n = max(len(x) for x in self.vs)
def cmp(self, other, op):
n = max(self.n, other.n)
return op(tuple(x.zfill(n) for x in self.vs), tuple(x.zfill(n) for x in other.vs))
def __lt__(self, other):
return self.cmp(other, operator.lt)
def __le__(self, other):
return self.cmp(other, operator.le)
def __gt__(self, other):
return self.cmp(other, operator.gt)
def __ge__(self, other):
return self.cmp(other, operator.ge)
def __eq__(self, other):
return self.cmp(other, operator.eq)
def __ne__(self, other):
return self.cmp(other, operator.ne)
def cmp_version(a, b, cmp):
xs = split_version_re.split(a)
ys = split_version_re.split(b)
n = max(len(x) for x in xs + ys)
return cmp(tuple(x.zfill(n) for x in xs), tuple(x.zfill(n) for x in ys))
def test_cmp_version():
assert LooseVersion('10.3.5') > LooseVersion('9.3.5')
assert LooseVersion('1.2.4-rc3') < LooseVersion('1.2.4-rc4')
# to try something different: pytest instead of unittest and
# in-module tests instead of separate test file
# unittests: pytest style test_ functions
# call e.g.: py.test-3 user-installed.py
# For Fedora >= 26 this functionality is also available via the dnf command:
# dnf repoquery --qf '%{name}' --userinstalled \
# | grep -v -- '-debuginfo$' \
# | grep -v '^\(kernel-modules\|kernel\|kernel-core\|kernel-devel\)$'
# Fedora >= 23
# cf. http://unix.stackexchange.com/questions/82880/how-to-replicate-installed-package-selection-from-one-fedora-instance-to-another/82882#82882
def list_fedora():
import dnf
b = dnf.Base()
b.fill_sack();
l = sorted(set(x.name for x in b.iter_userinstalled()
if not x.name.endswith("-debuginfo") \
and x.name not in \
["kernel-modules", "kernel", "kernel-core", "kernel-devel"] ))
print("\n".join(l))
def test_list_fedora():
f_distro_id = lambda : 'fedora'
f_distro_version = lambda : '23'
import collections
Rec = collections.namedtuple('Rec', ['name'])
f_dnf = mock.Mock()
f_Base = mock.Mock()
f_Base.fill_sack.return_value = None
f_Base.iter_userinstalled.return_value = iter([Rec('kernel-modules'), Rec('foo-debuginfo'), Rec('zsh'), Rec('vim')])
f_dnf.Base.return_value = f_Base
# yes, we are mocking the local import of dnf
with mock.patch('distro.id', f_distro_id), \
mock.patch('distro.version', f_distro_version), \
mock.patch.dict('sys.modules', {'dnf': f_dnf}), \
mock.patch('sys.stdout', new=io.StringIO()) as fake_out:
main() # calls list_fedora()
assert fake_out.getvalue() == 'vim\nzsh\n'
# cf. http://unix.stackexchange.com/questions/82880/how-to-replicate-installed-package-selection-from-one-fedora-instance-to-another/82882#82882
def list_centos():
rq = subprocess.check_output(['repoquery', '--installed',
'--qf', '%{n},%{yumdb_info.reason},%{yumdb_info.installed_by}', '--all']
).decode()
ps = [ row[0] for row in (line.split(',') for line in rq.splitlines())
if row[1] == 'user' and row[2] != '4294967295' ]
for p in map(operator.itemgetter(0), itertools.groupby(sorted(ps))):
print(p)
def test_list_centos(name='centos'):
f_distro_id = lambda : name
f_distro_version = lambda : '7.3.1611'
f_check_output = mock.Mock(return_value=b'''time,user,0
xfsprogs,user,4294967295
xz,dep,4294967295
yum,user,4294967295
yum-cron,user,0
yum-cron,user,0
yum-utils,dep,0
zsh,user,1000''')
with mock.patch('distro.id', f_distro_id), \
mock.patch('distro.version', f_distro_version), \
mock.patch('subprocess.check_output', f_check_output), \
mock.patch('sys.stdout', new=io.StringIO()) as fake_out:
main() # calls list_centos
assert fake_out.getvalue() == 'time\nyum-cron\nzsh\n'
def test_list_rhel(name='rhel'):
test_list_centos(name)
# i.e. lines from files/usr/var/lib/apt/extended_states
# thus should be similar to what e.g. apt-mark showmanual does
# (apt-mark is not available on termux)
# cf. http://askubuntu.com/questions/2389/generating-list-of-manually-installed-packages-and-querying-individual-packages
def parse_auto_installed(lines):
ps = set()
package = None
for line in lines:
if line.startswith('Package: '):
package = line[9:]
elif line.startswith('Auto-Installed: 1'):
ps.add(package)
return ps
def test_parse_auto_installed():
s = parse_auto_installed('''Package: libgcc
Architecture: aarch64
Auto-Installed: 1
Package: binutils
Architecture: aarch64
Auto-Installed: 1
Package: ndk-sysroot
Architecture: aarch64
Auto-Installed: 1
'''.splitlines())
l = sorted(s)
assert l == ['binutils', 'libgcc', 'ndk-sysroot']
# i.e. lines from dpkg --get-selections
def get_selections(lines):
ps = [ line.split('\t', maxsplit=1)[0] for line in lines ]
return ps
def test_get_selections():
lines = '''apt install
bash install
bc install
binutils install'''.splitlines()
assert get_selections(lines) == [ 'apt', 'bash', 'bc', 'binutils' ]
# Termux uses apt and dpkg - but doesn't have something like
# var/log/installer/initial-status.gz
# thus, some base packages are not marked as auto-installed
# in the extended_states file
#
# this is an approximation of what is per-installed on Termux (as of 2016-12)
default_termux_pkgs = set([
'apt',
'bash',
'busybox',
'command-not-found',
'dash',
'dpkg',
'gpgv',
'libandroid-support',
'libgnustl',
'liblzma',
'ncurses',
'readline',
'termux-tools'
])
# Test on the command line:
# paste - - - - < /data/data/com.termux/files/usr/var/lib/apt/extended_states | grep 'Auto-Installed: 1' | awk '{print $2}' > ~/a
# dpkg --get-selections | cut -f1 > ~/i
# grep install /data/data/com.termux/files/usr/var/log/apt/history.log | sed 's/^.*install //' | tr ' ' '\n' > ~/h
# sed -e 's/^/^/' -e 's/$/$/' -i a h
# grep -v -f ~/a ~/i | grep -v -f ~/h | sed "s/\(.*\)/'\1',/"
def list_termux():
extended_states_filename = \
'/data/data/com.termux/files/usr/var/lib/apt/extended_states'
with open(extended_states_filename, 'r') as es:
ss = subprocess.check_output(
['dpkg', '--get-selections']).decode().splitlines()
# when line comes from open() it has a trailing newline that we strip
auto_installed = parse_auto_installed(map(lambda line : line[:-1], es))
selections = get_selections(ss)
for s in selections:
if s not in auto_installed and s not in default_termux_pkgs:
print(s)
def takefind(f, g):
for i in g:
yield i
if f(i):
break
# this basically approximate a package list similar to what would be
# in /var/log/installer/initial-status.gz via looking at
# the mtime of /var/lib/dpkg/info/*.list files - until a well-known
# system package
# limitation: upgraded packages get new mtime/ctimes such that
# the grub-common marker may miss some or catch too much (if grub-common
# is upgraded)
def get_initial_lst(selection, last_pkg='grub-common'):
l = takefind(lambda fn: fn == last_pkg, selection)
return l
def get_all_lst(dirname='/var/lib/dpkg/info'):
l = map(lambda fn: fn.split(':')[0],
map(lambda fn: fn[:-5],
map(operator.itemgetter(0),
sorted(
map(lambda fn: (fn, os.path.getmtime(dirname+'/'+fn)),
filter(lambda fn: fn.endswith('.list'),
os.listdir(dirname))),
key=operator.itemgetter(1)))))
return l
def dpkg_log_names(dirname='/var/log'):
expr = re.compile(r'dpkg\.log(\.[0-9]+)?(\.gz)?')
l = sorted(filter(expr.match, os.listdir(dirname)),
key=LooseVersion, reverse=True)
return l
def dpkg_log_lines(dirname='/var/log'):
fns = dpkg_log_names(dirname)
for fn in fns:
ofn = gzip.open if fn.endswith('.gz') else open;
with ofn(dirname+'/'+fn, 'rt') as f:
for line in f:
yield line
# kind of complement to /var/log/installer/initial-status.gz
# note that dpkg.log* is rotated, by default
# (montly, 12 times), thus this is not sufficient after 1 year
#
# i.e. /var/log/dpkg.log* - grepping for 'install|remove' there
# yields all packages that are installed after sys-creation (i.e.
# inner join with apt-mark showmanual)
def get_dpkg_installed(dirname='/var/log'):
ps = set()
expr = re.compile('^[^ ]+ [^ ]+ (install|remove) ')
for line in filter(expr.match, dpkg_log_lines(dirname)):
(action, p) = line.split(' ')[2:4]
p = p.split(':')[0]
if action == 'install':
ps.add(p)
else:
ps.discard(p)
return ps
# - in contrast to termux: debian has `apt-mark showmanual`, i.e.
# we don't need to parse extended_states ourselves
# - similar to termux: there are installations without
# /var/log/installer/initial-status.gz (debian/ubuntu)
# - /var/log/apt/history.log gets also rotated away and takes a
# bit more effort to parse
def list_debian():
ms = subprocess.check_output(['apt-mark', 'showmanual']).decode() \
.splitlines()
ds = get_dpkg_installed()
for p in ms:
if p in ds:
print(p)
def test_list_debian(dname='debian'):
f_distro_id = lambda : dname
f_distro_version = lambda : ''
def f_check_output(*args, **kargs):
return b'''acl
apt
grub-common
curl
vim
'''
#f_listdir = lambda x : [ 'acl:amd64.list', 'apt.list', 'e',
# 'grub-common.list', 'curl.list', 'd' ]
f_listdir = lambda x : [ 'dpkg.log' ]
# mock.patch('os.path.getmtime', F_Getmtime()),
class F_Getmtime:
def __init__(self):
self.l = range(10).__iter__()
def __call__(self, fn):
return next(self.l)
f_open = mock_open(read_data='''2017-01-22 17:44:14 install make:amd64 <none> 3.81-8.2ubuntu3
2017-01-22 17:44:31 remove make:amd64 3.81-8.2ubuntu3 <none>
2017-01-22 17:48:06 install curl foo bar
2017-01-22 17:48:06 install vim foo bar
''')
with mock.patch('distro.id', f_distro_id), \
mock.patch('distro.version', f_distro_version), \
mock.patch('subprocess.check_output', f_check_output), \
mock.patch('os.listdir', f_listdir), \
mock.patch('builtins.open', f_open), \
mock.patch('sys.stdout', new=io.StringIO()) as fake_out:
main() # calls list_debian()
assert fake_out.getvalue() == 'curl\nvim\n'
def test_list_ubuntu():
test_list_debian('ubuntu')
# work-around bug:
# http://bugs.python.org/issue21258
# cf. http://stackoverflow.com/questions/24779893/customizing-unittest-mock-mock-open-for-iteration
def mock_open(*args, **kargs):
f_open = mock.mock_open(*args, **kargs)
f_open.return_value.__iter__ = lambda self : iter(self.readline, '')
return f_open
def test_list_termux():
f_distro_id = lambda : ''
f_distro_version = lambda : ''
def f_check_output(*args, **kargs):
return b'''apt install
bash install
bc install
binutils install
zsh install'''
def f_exists(a):
return a == '/data/data/com.termux'
f_open = mock_open(read_data='''Package: libgcc
Architecture: aarch64
Auto-Installed: 1
Package: binutils
Architecture: aarch64
Auto-Installed: 1
Package: ndk-sysroot
Architecture: aarch64
Auto-Installed: 1
''')
with mock.patch('distro.id', f_distro_id), \
mock.patch('distro.version', f_distro_version), \
mock.patch('subprocess.check_output', f_check_output), \
mock.patch('os.path.exists', f_exists), \
mock.patch('builtins.open', f_open), \
mock.patch('sys.stdout', new=io.StringIO()) as fake_out:
main() # calls list_termux()
assert fake_out.getvalue() == 'bc\nzsh\n'
list_fn = {
'fedora': list_fedora
,'centos': list_centos
,'rhel': list_centos
,'debian': list_debian
,'ubuntu': list_debian
}
def main():
dname, version = distro.id(), distro.version()
try:
fn = list_fn[dname]
if fn == list_centos and LooseVersion(version) >= LooseVersion('8.0'):
list_fedora()
elif fn == list_fedora and LooseVersion(version) < LooseVersion('23'):
list_centos()
else:
fn()
except KeyError:
if os.path.exists('/data/data/com.termux'):
list_termux()
else:
raise RuntimeError(f'Unknown system (distribution: {dname})')
return 0
if __name__ == '__main__':
sys.exit(main())