-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheopkg.py
83 lines (67 loc) · 2.82 KB
/
eopkg.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
#!/usr/bin/env python3
import os, subprocess, dotbot, time
from enum import Enum
class PkgStatus(Enum):
# These names will be displayed
UP_TO_DATE = 'Up to date'
INSTALLED = 'Installed'
NOT_FOUND = 'Not Found'
ERROR = "Build Error"
NOT_SURE = 'Could not determine'
class Eopkg(dotbot.Plugin):
_directive = 'eopkg'
def __init__(self, context):
super(Eopkg, self).__init__(self)
self._context = context
self._strings = {}
# Names to search the query string for
self._strings[PkgStatus.UP_TO_DATE] = "The following package(s)"
self._strings[PkgStatus.INSTALLED] = "Installed"
self._strings[PkgStatus.NOT_FOUND] = "System error. Program terminated."
self._strings[PkgStatus.ERROR] = "==> ERROR:"
def can_handle(self, directive):
return directive == self._directive
def handle(self, directive, data):
if directive != self._directive:
raise ValueError('Eopkg cannot handle directive %s' %
directive)
return self._process(data)
def _process(self, packages):
defaults = self._context.defaults().get('eopkg', {})
results = {}
successful = [PkgStatus.UP_TO_DATE, PkgStatus.INSTALLED]
for pkg in packages:
if isinstance(pkg, dict):
self._log.error('Incorrect format')
elif isinstance(pkg, list):
# self._log.error('Incorrect format')
pass
else:
pass
result = self._install(pkg)
results[result] = results.get(result, 0) + 1
if 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('\nAll 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):
# to have a unified string which we can query
# we need to execute the command with LANG=en_US.UTF-8
cmd = 'LANG=en_US.UTF-8 sudo eopkg -y it {}'.format(pkg)
self._log.info("Installing \"{}\". Please wait...".format(pkg))
proc = subprocess.Popen(cmd, shell=True,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
out = proc.stdout.read()
proc.stdout.close()
for item in self._strings.keys():
if out.decode("utf-8").find(self._strings[item]) >= 0:
return item
self._log.warn("Could not determine what happened with package {}".format(pkg))
return PkgStatus.NOT_SURE