-
Notifications
You must be signed in to change notification settings - Fork 0
/
smartbackend.py
153 lines (113 loc) · 4.62 KB
/
smartbackend.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
from smart import init, initDistro, initPlugins, initPsyco
from smart.commands import *
from smart.transaction import *
from smart import *
class Backend:
def __init__(self):
self.ctrl = init()
initDistro(self.ctrl)
initPlugins()
initPsyco()
self.ctrl.reloadChannels()
self.cache = self.ctrl.getCache()
self.trans = Transaction(self.cache, PolicyInstall)
def package_lists(self):
"""Get a list of all available packages."""
packages_to_add = []
for pkg in self.cache.getPackages():
packages_to_add.append(self.format_package(pkg))
return packages_to_add
def get_change_log(self, name, arch, ver, rel):
self.search(name=name, arch=arch,
ver=ver, rel=rel)
return []
def get_updates_list(self):
"""Get a list of all packages with a pending update."""
packages_to_update = []
metainfo_to_update = []
metabug_to_update = []
trans = Transaction(self.cache, PolicyUpgrade)
for pkg in self.cache.getPackages():
if pkg.installed:
trans.enqueue(pkg, UPGRADE)
for pkg in trans._queue:
packages_to_update.append(self.format_package(pkg))
return packages_to_update, metainfo_to_update, metabug_to_update
def get_installed_list(self):
"""Get a list of all installed packages."""
packages_installed = []
for pkg in self.cache.getPackages():
if pkg.installed:
packages_installed.append(self.format_package(pkg))
return packages_installed
def install(self, pkg):
"""Install a package."""
self.trans.enqueue(pkg, INSTALL)
def update(self, pkg):
"""Update a package."""
self.trans.enqueue(pkg, INSTALL)
def build_transaction(self):
"""Build transaction"""
self.trans.run()
def process_transaction(self):
"""Run transaction"""
self.ctrl.commitTransaction(self.trans, confirm=False)
def search(self, name=None, epoch=None, ver=None, rel=None, arch=None):
"""Search for a package with the provided keys."""
packages = []
pkg = name + '-' + ver + '-' + rel + '@' + arch
ratio, results, suggestions = self.ctrl.search(pkg, addprovides=False)
if not results:
return False
else:
for obj in results:
if isinstance(obj, Package):
packages.append(obj)
return packages
def get_keys(self):
"""List updated/installed packages to send back to the server."""
if (self.trans.numTaskCompleted == 0):
return None
else:
packages_updated = []
for pkg in self.trans.getChangeSet().getPersistentState():
ratio, results, suggestions = self.ctrl.search( \
pkg[1] + "-" + pkg[2],
addprovides=False)
if not results:
return False
else:
for obj in results:
if isinstance(obj, Package):
pkginfo = self.format_package(obj).split(',')
packages_updated.append("%s,%s,%s,%s" % (pkginfo[0],
pkginfo[1],
pkginfo[2],
pkginfo[3]))
return packages_updated
def format_package(self, pkg):
"""Helper function to correctly format package info"""
seen = {}
info = pkg.version
arch = info[info.find('@') + 1:]
version = info[:info.find('-')]
release = info[info.find('-') + 1:info.find('@')]
channels = []
for loader in pkg.loaders:
channels.append(loader.getChannel().getAlias())
if loader not in seen:
seen[loader] = True
errata = loader.getErrata(pkg)
if errata:
print " ", _("Type:"), errata.getType()
channels.sort()
return "%s,%s,%s,%s,%s,%s,%s" % (pkg.name,
arch,
version,
release,
channels[0],
'',
'')
def set_proxy(self, proxy):
"""Set proxy"""
sysconf.set("http-proxy", proxy)