-
Notifications
You must be signed in to change notification settings - Fork 1
/
xbps.py
77 lines (59 loc) · 2.43 KB
/
xbps.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
import os, subprocess, dotbot
from enum import Enum
class PkgStatus(Enum):
UP_TO_DATE = 'Up to date'
INSTALLED = 'Installed'
NOT_FOUND = 'Not found'
ERROR = 'Errors occurred'
NOT_SURE = 'Could not determine'
class XBPS(dotbot.Plugin):
_directive = 'xbps'
def __init__(self, context):
super(XBPS, self).__init__(self)
self._context = context
self._strings = {}
self._strings[PkgStatus.UP_TO_DATE] = 'already installed'
self._strings[PkgStatus.INSTALLED] = 'installed'
self._strings[PkgStatus.NOT_FOUND] = 'Unable to locate'
def can_handle(self, directive):
return directive == self._directive
def handle(self, directive, data):
if not self.can_handle(directive):
raise ValueError('XBPS cannot handle directive %s' % directive)
return self._process(data)
def _process(self, packages):
results = {}
successful = [PkgStatus.UP_TO_DATE, PkgStatus.INSTALLED]
for pkg in packages:
process_result = self._install(pkg)
results[process_result] = results.get(process_result, 0) + 1
if process_result not in successful:
self._log.error('Could not install package {}'.format(pkg))
if all([result in successful for result in results.keys()]):
self._log.info('All packages installed successfully')
success = True
else:
success = False
for status, amount in results.items():
log = self._log.info if status in successful else self._log.error
log('{} {}'.format(amount, status.value))
return success
def _install(self, pkg):
cmd = 'xbps-install -Sy {}'.format(pkg)
with_sudo = ""
if os.getuid() != 0:
cmd = 'sudo ' + cmd
with_sudo = "with sudo"
self._log.info('Installing \'{}\' {}'.format(pkg, with_sudo))
process = subprocess.Popen(cmd,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
out = str(process.stdout.read())
process.stdout.close()
for item in self._strings.keys():
if out.find(self._strings[item]) >= 0:
return item
self._log.warn(
"Could not determine what happened wth package {}".format(pkg))
return PkgStatus.NOT_SURE