-
Notifications
You must be signed in to change notification settings - Fork 3
/
aptget.py
86 lines (68 loc) · 2.79 KB
/
aptget.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
import os, subprocess, dotbot
from enum import Enum
class PkgStatus(Enum):
UP_TO_DATE = 'Already up to date'
INSTALLED = 'Newly installed'
NOT_FOUND = 'Not found'
NOT_SURE = 'Could not determine'
class AptGet(dotbot.Plugin):
_directive = 'apt-get'
def __init__(self, context):
super(AptGet, self).__init__(self)
self._context = context
self._strings = {}
self._strings[PkgStatus.UP_TO_DATE] = "is already the newest"
self._strings[PkgStatus.INSTALLED] = ""
self._strings[PkgStatus.NOT_FOUND] = "Unable to locate package"
def can_handle(self, directive):
return directive == self._directive
def handle(self, directive, data):
if directive != self._directive:
raise ValueError('Apt-get cannot handle directive %s' %
directive)
return self._process(data)
def _process(self, packages):
defaults = self._context.defaults().get('apt-get', {})
results = {}
successful = [PkgStatus.UP_TO_DATE, PkgStatus.INSTALLED]
# apt-get update
self._update_index()
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('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 _update_index(self):
cmd = 'apt-get update'
process = subprocess.Popen(cmd, shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
out = process.stdout.read()
process.stdout.close()
def _install(self, pkg):
cmd = 'apt-get install {} -y'.format(pkg)
process = subprocess.Popen(cmd, shell=True,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
out = 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 with package {}".format(pkg))
return PkgStatus.NOT_SURE