-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcheck_pkg.py
More file actions
80 lines (64 loc) · 1.89 KB
/
check_pkg.py
File metadata and controls
80 lines (64 loc) · 1.89 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/python
import os
from subprocess import Popen, PIPE
import logging
import os.path
import json
logging.basicConfig(filename='/var/log/server_pkg.log', level=logging.INFO)
def unique(lst):
s = {}
[s.__setitem__(repr(p), p) for p in lst]
return s.values()
class Repository():
"""
Check new update package.
"""
def __init__(self):
self.path = "/var/www/repos/apt/debian"
def _reprepro(self, args):
os.chdir(self.path)
p = Popen(['/usr/bin/reprepro', '-Vb.'] + args.split(' '), stdout=PIPE, stderr=PIPE)
return (p.communicate(), p.returncode)
def get_packages(self, dist):
try:
results = {}
distdir = os.path.join(self.path, 'dists/%s' % dist)
for dirpath, dirnames, filenames in os.walk(distdir):
for name in filenames:
if name != 'Packages': continue
path = os.path.join(dirpath, name)
packages = file(path, 'r').read()
packages = packages.split('\n\n')
for pkg in packages:
fields = []
for field in pkg.split('\n'):
if not field: continue
if field[0].isalpha():
fields.append(field.split(': ', 1))
else:
fields[-1][1] += field
if not fields:
continue
pkg = dict(fields)
pkgname = pkg['Package']
if not pkgname in results:
results[pkgname] = []
results[pkgname].append(pkg)
# print 'results: {}'.format(results)
pkg_lists = results.keys()
pkgs = {'Jessie': []}
for item in pkg_lists:
pkgs['Jessie'].append({'pkg': item, 'version': results[item][0]["Version"]})
logging.info('pkgs: {}'.format(pkgs))
print('pkgs: {}'.format(pkgs))
json_data = json.dumps(pkgs)
return json_data
except Exception as e:
print 'Error: {}'.format(e)
return []
def get_package(self, dist, package):
p = self.get_packages(dist)
return unique(p.get(package, []))
if __name__ == '__main__':
repos = Repository()
repos.get_packages('Jessie')