-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlistPkgs.py
More file actions
executable file
·62 lines (51 loc) · 1.52 KB
/
listPkgs.py
File metadata and controls
executable file
·62 lines (51 loc) · 1.52 KB
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
#!/usr/bin/env python
# listPkgs.py
import json
import sys
import subprocess
import re
def safe_str(obj):
try:
return str(obj)
except UnicodeEncodeError:
return obj.encode('ascii', 'ignore').decode('ascii')
return ''
def listPkgs():
req = subprocess.Popen(
('dpkg', '-l'),
stdout=subprocess.PIPE)
res = subprocess.check_output(
('grep', '^ii'),
stdin=req.stdout)
req.wait()
lines = safe_str(res).split('\\n')
i = 0
while len([l for l in re.split(" {2,}", lines [i]) if l]) != 5:
i += 1
pkgs = []
for line in lines:
parsed_line = re.split(r" {2,}", line)
if (len(parsed_line) == 5) and (len (parsed_line [1]) > 0):
pkgs.append({
'State': parsed_line[0],
'Package': parsed_line [1],
'Version':parsed_line [2],
'Architecture': parsed_line [3],
'Description': parsed_line [4],
})
return pkgs
def listPkgsFilter(filt=[]):
pkgs = listPkgs()
res = []
for p in pkgs:
for f in filt:
if 'Package' in p and f in p['package']:
res.append(p)
return res
if __name__ == "__main__":
print('listPkgs() ...............................')
res = listPkgs()
print(json.dumps (res, indent=2, sort_keys=True))
print('listPkgsFilter() .........................')
res = listPkgsFilter(filt=['uplynk'])
print(json.dumps (res, indent=2, sort_keys=True))