-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackagers.py
230 lines (191 loc) · 7.01 KB
/
packagers.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
"""Wrap shell commands to backup/restore package lists."""
import json
import os
import subprocess
from utils import cmd
class AbstractPackager(object):
"""Base packager with most common access patterns."""
infocmd = None
backupcmd = None
restorecmd = None
verifycmd = None
filepath = None
def __init__(
self,
info, backup, restore, verify,
file_dir, file_name):
"""Set required data.
Args:
info (str): command to read existing package file
backup (str): command to create package file
restore (str): command to install based on package file
verify (str): command to verify tool is installed
file_dir (str): location of package file
file_name (str): name of package file
"""
self.infocmd = info
self.backupcmd = backup
self.restorecmd = restore
self.verifycmd = verify
self.filepath = os.path.join(file_dir, file_name)
def verify(self):
"""Verify tool is installed."""
cmd(self.verifycmd)
def info(self):
"""Run info command."""
cmd(self.infocmd.format(filepath=self.filepath))
def backup(self):
"""Run backup command."""
cmd(self.backupcmd.format(filepath=self.filepath))
self.info()
def restore(self):
"""Run restore command."""
cmd(self.restorecmd.format(filepath=self.filepath))
class Brew(AbstractPackager):
"""Manage mac packages via brew."""
def __init__(self, file_dir, file_name='Brewfile'):
"""Set brew specific options."""
super().__init__(
'cat {filepath}',
'brew bundle dump -f --describe --file {filepath}',
'brew bundle install -v --no-upgrade --file {filepath}',
'brew --version',
file_dir,
file_name
)
class Apt(AbstractPackager):
"""Manage debian packages via apt-clone."""
def __init__(self, file_dir, file_name='apt-clone.tar.gz'):
"""Set apt-clone specific options."""
super().__init__(
'apt-clone info {filepath}',
'apt-clone clone {filepath}',
'sudo apt-clone restore {filepath}',
'apt-clone --help',
file_dir,
file_name
)
class Pip(AbstractPackager):
"""Manage python packages via pip."""
def __init__(self, file_dir, file_name='pip-freeze.txt'):
"""Set pip specific options."""
super().__init__(
'cat {filepath}',
'pip freeze --no-python-version-warning',
'pip install -r {filepath}',
'pip --version',
file_dir,
file_name
)
def backup(self):
"""Override backup, write stdout to file."""
with open(self.filepath, 'w') as f:
cmd(self.backupcmd, stdout=f)
self.info()
class Npm(AbstractPackager):
"""Manage node packages via npm."""
def __init__(self, file_dir, file_name='package.json'):
"""Set npm specific options."""
super().__init__(
'cat {filepath}',
'npm list -g --json',
'npm install -g {package}',
'npm --version',
file_dir,
file_name
)
def backup(self):
"""Override backup, write stdout to file."""
with open(self.filepath, 'w') as f:
cmd(self.backupcmd, stdout=f)
self.info()
def restore(self):
"""Override restore to loop through each package install."""
with open(self.filepath, 'r') as f:
data = json.load(f)
for k, v in data['dependencies'].items():
package = '@'.join([k, v['version']])
cmd(self.restorecmd.format(package=package))
class Git(AbstractPackager):
"""Manage git repos."""
install_dir = None
default_remote = None
def __init__(self, file_dir, file_name='git_repos.json', install_dir=None, remote='origin'): # noqa:E501
"""Set git specific options."""
if not install_dir:
install_dir = os.path.join(
os.path.expanduser('~'),
'projects'
)
self.install_dir = install_dir
self.default_remote = remote
super().__init__(
'cat {filepath}',
None,
None,
'git --version',
file_dir,
file_name
)
def backup(self):
"""Override backup with custom git process."""
if not os.path.isdir(self.install_dir):
os.makedirs(self.install_dir)
# query for all repo names and remote urls
results = {}
for name in os.listdir(self.install_dir):
full_path = os.path.join(self.install_dir, name)
if os.path.isdir(full_path):
try:
remotes = cmd(f'git -C {full_path} remote').stdout.rstrip().split('\n') # noqa:E501
results[name] = {
'remotes': {
remote: cmd(f'git -C {full_path} remote get-url {remote}').stdout.rstrip() # noqa:E501
for remote in remotes
}
}
except subprocess.CalledProcessError:
pass
# save to backup file
json_results = json.dumps(results, indent=4, sort_keys=True)
with open(self.filepath, 'w') as f:
f.write(f'{json_results}\n')
self.info()
def restore(self):
"""Override restore with custom git process."""
if not os.path.isdir(self.install_dir):
os.makedirs(self.install_dir)
with open(self.filepath, 'r') as f:
data = json.load(f)
for name, conf in data.items():
try:
# clone into dir
remotes = conf['remotes']
full_path = os.path.join(self.install_dir, name)
default_remote = remotes[self.default_remote]
cmd(f'git clone {default_remote} {full_path}')
# setup remotes
for k, v in remotes.items():
if k != self.default_remote:
cmd(f'git -C {full_path} remote add {k} {v}')
else:
cmd(f'git -C {full_path} remote set-url {k} {v}') # noqa:E501
except subprocess.CalledProcessError:
pass
class Crontab(AbstractPackager):
"""Manage crontab install."""
def __init__(self, file_dir, file_name='crontab.txt'):
"""Crontab specific options."""
super().__init__(
'cat {filepath}',
'crontab -l',
'crontab {filepath}',
'which crontab',
file_dir,
file_name
)
def backup(self):
"""Override backup, write stdout to file."""
with open(self.filepath, 'w') as f:
cmd(self.backupcmd, stdout=f)
self.info()