|
1
|
|
|
"""Functions to manage the installation of dependencies.""" |
|
2
|
|
|
|
|
3
|
1 |
|
import os |
|
4
|
1 |
|
import functools |
|
5
|
1 |
|
import datetime |
|
6
|
1 |
|
import logging |
|
7
|
|
|
|
|
8
|
1 |
|
from . import common, system |
|
9
|
1 |
|
from .models import load_config, Config, Source |
|
10
|
|
|
|
|
11
|
1 |
|
log = logging.getLogger(__name__) |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
1 |
|
def restore_cwd(func): |
|
15
|
1 |
|
@functools.wraps(func) |
|
16
|
|
|
def wrapped(*args, **kwargs): |
|
17
|
1 |
|
cwd = os.getcwd() |
|
18
|
1 |
|
result = func(*args, **kwargs) |
|
19
|
1 |
|
os.chdir(cwd) |
|
20
|
1 |
|
return result |
|
21
|
1 |
|
return wrapped |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
1 |
|
def init(): |
|
25
|
|
|
"""Create a new config file for the project.""" |
|
26
|
1 |
|
success = False |
|
27
|
|
|
|
|
28
|
1 |
|
config = load_config() |
|
29
|
|
|
|
|
30
|
1 |
|
if config: |
|
31
|
1 |
|
msg = "Configuration file already exists: {}".format(config.path) |
|
32
|
1 |
|
common.show(msg, color='error') |
|
33
|
|
|
|
|
34
|
|
|
else: |
|
35
|
1 |
|
config = Config() |
|
36
|
1 |
|
source = Source(name="sample_dependency", |
|
37
|
|
|
repo="https://github.com/githubtraining/hellogitworld") |
|
38
|
1 |
|
config.sources.append(source) |
|
39
|
1 |
|
source = source.lock(rev="ebbbf773431ba07510251bb03f9525c7bab2b13a") |
|
40
|
1 |
|
config.sources_locked.append(source) |
|
41
|
1 |
|
config.save() |
|
42
|
|
|
|
|
43
|
1 |
|
msg = "Created sample config file: {}".format(config.path) |
|
44
|
1 |
|
common.show(msg, color='success') |
|
45
|
1 |
|
success = True |
|
46
|
|
|
|
|
47
|
1 |
|
msg = "To edit this config file, run: gitman edit" |
|
48
|
1 |
|
common.show(msg, color='message') |
|
49
|
|
|
|
|
50
|
1 |
|
return success |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
1 |
|
@restore_cwd |
|
54
|
1 |
|
def install(*names, root=None, depth=None, |
|
55
|
|
|
force=False, fetch=False, clean=True): |
|
56
|
|
|
"""Install dependencies for a project. |
|
57
|
|
|
|
|
58
|
|
|
Optional arguments: |
|
59
|
|
|
|
|
60
|
|
|
- `*names`: optional list of dependency directory names to filter on |
|
61
|
|
|
- `root`: specifies the path to the root working tree |
|
62
|
|
|
- `depth`: number of levels of dependencies to traverse |
|
63
|
|
|
- `force`: indicates uncommitted changes can be overwritten and |
|
64
|
|
|
script errors can be ignored |
|
65
|
|
|
- `fetch`: indicates the latest branches should always be fetched |
|
66
|
|
|
- `clean`: indicates untracked files should be deleted from dependencies |
|
67
|
|
|
|
|
68
|
|
|
""" |
|
69
|
1 |
|
log.info("%sInstalling dependencies: %s", |
|
70
|
|
|
'force-' if force else '', |
|
71
|
|
|
', '.join(names) if names else '<all>') |
|
72
|
1 |
|
count = None |
|
73
|
|
|
|
|
74
|
1 |
|
config = load_config(root) |
|
75
|
|
|
|
|
76
|
1 |
|
if config: |
|
77
|
1 |
|
common.newline() |
|
78
|
1 |
|
common.show("Installing dependencies...", color='message', log=False) |
|
79
|
1 |
|
common.newline() |
|
80
|
1 |
|
count = config.install_dependencies( |
|
81
|
|
|
*names, update=False, depth=depth, |
|
82
|
|
|
force=force, fetch=fetch, clean=clean, |
|
83
|
|
|
) |
|
|
|
|
|
|
84
|
|
|
|
|
85
|
1 |
|
if count: |
|
86
|
1 |
|
_run_scripts(*names, depth=depth, force=force, _config=config) |
|
87
|
|
|
|
|
88
|
1 |
|
return _display_result("install", "Installed", count) |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
1 |
|
@restore_cwd |
|
92
|
1 |
|
def update(*names, root=None, depth=None, |
|
93
|
|
|
recurse=False, force=False, clean=True, lock=None): # pylint: disable=redefined-outer-name |
|
94
|
|
|
"""Update dependencies for a project. |
|
95
|
|
|
|
|
96
|
|
|
Optional arguments: |
|
97
|
|
|
|
|
98
|
|
|
- `*names`: optional list of dependency directory names to filter on |
|
99
|
|
|
- `root`: specifies the path to the root working tree |
|
100
|
|
|
- `depth`: number of levels of dependencies to traverse |
|
101
|
|
|
- `recurse`: indicates nested dependencies should also be updated |
|
102
|
|
|
- `force`: indicates uncommitted changes can be overwritten and |
|
103
|
|
|
script errors can be ignored |
|
104
|
|
|
- `clean`: indicates untracked files should be deleted from dependencies |
|
105
|
|
|
- `lock`: indicates actual dependency versions should be recorded |
|
106
|
|
|
|
|
107
|
|
|
""" |
|
108
|
1 |
|
log.info("%s dependencies%s: %s", |
|
109
|
|
|
'Force updating' if force else 'Updating', |
|
110
|
|
|
', recursively' if recurse else '', |
|
111
|
|
|
', '.join(names) if names else '<all>') |
|
112
|
1 |
|
count = None |
|
113
|
|
|
|
|
114
|
1 |
|
config = load_config(root) |
|
115
|
|
|
|
|
116
|
1 |
|
if config: |
|
117
|
1 |
|
common.newline() |
|
118
|
1 |
|
common.show("Updating dependencies...", color='message', log=False) |
|
119
|
1 |
|
common.newline() |
|
120
|
1 |
|
count = config.install_dependencies( |
|
121
|
|
|
*names, update=True, depth=depth, |
|
122
|
|
|
recurse=recurse, force=force, fetch=True, clean=clean, |
|
123
|
|
|
) |
|
124
|
|
|
|
|
125
|
1 |
|
if count and lock is not False: |
|
126
|
1 |
|
common.show("Recording installed versions...", |
|
127
|
|
|
color='message', log=False) |
|
128
|
1 |
|
common.newline() |
|
129
|
1 |
|
config.lock_dependencies(*names, obey_existing=lock is None) |
|
130
|
|
|
|
|
131
|
1 |
|
if count: |
|
132
|
1 |
|
_run_scripts(*names, depth=depth, force=force, _config=config) |
|
133
|
|
|
|
|
134
|
1 |
|
return _display_result("update", "Updated", count) |
|
135
|
|
|
|
|
136
|
|
|
|
|
137
|
1 |
|
def _run_scripts(*names, depth=None, force=False, _config=None): |
|
138
|
|
|
"""Run post-install scripts. |
|
139
|
|
|
|
|
140
|
|
|
Optional arguments: |
|
141
|
|
|
|
|
142
|
|
|
- `*names`: optional list of dependency directory names filter on |
|
143
|
|
|
- `depth`: number of levels of dependencies to traverse |
|
144
|
|
|
- `force`: indicates script errors can be ignored |
|
145
|
|
|
|
|
146
|
|
|
""" |
|
147
|
1 |
|
assert _config, "'_config' is required" |
|
148
|
|
|
|
|
149
|
1 |
|
common.show("Running scripts...", color='message', log=False) |
|
150
|
1 |
|
common.newline() |
|
151
|
1 |
|
_config.run_scripts(*names, depth=depth, force=force) |
|
152
|
|
|
|
|
153
|
|
|
|
|
154
|
1 |
|
@restore_cwd |
|
155
|
1 |
|
def display(*, root=None, depth=None, allow_dirty=True): |
|
156
|
|
|
"""Display installed dependencies for a project. |
|
157
|
|
|
|
|
158
|
|
|
Optional arguments: |
|
159
|
|
|
|
|
160
|
|
|
- `root`: specifies the path to the root working tree |
|
161
|
|
|
- `depth`: number of levels of dependencies to traverse |
|
162
|
|
|
- `allow_dirty`: causes uncommitted changes to be ignored |
|
163
|
|
|
|
|
164
|
|
|
""" |
|
165
|
1 |
|
log.info("Displaying dependencies...") |
|
166
|
1 |
|
count = None |
|
167
|
|
|
|
|
168
|
1 |
|
config = load_config(root) |
|
169
|
|
|
|
|
170
|
1 |
|
if config: |
|
171
|
1 |
|
common.newline() |
|
172
|
1 |
|
common.show("Displaying current dependency versions...", |
|
173
|
|
|
color='message', log=False) |
|
174
|
1 |
|
common.newline() |
|
175
|
1 |
|
config.log(datetime.datetime.now().strftime("%F %T")) |
|
176
|
1 |
|
count = 0 |
|
177
|
1 |
|
for identity in config.get_dependencies(depth=depth, |
|
178
|
|
|
allow_dirty=allow_dirty): |
|
179
|
1 |
|
count += 1 |
|
180
|
1 |
|
config.log("{}: {} @ {}", *identity) |
|
181
|
1 |
|
config.log() |
|
182
|
|
|
|
|
183
|
1 |
|
return _display_result("display", "Displayed", count) |
|
184
|
|
|
|
|
185
|
|
|
|
|
186
|
1 |
|
@restore_cwd |
|
187
|
1 |
|
def lock(*names, root=None): |
|
188
|
|
|
"""Lock current dependency versions for a project. |
|
189
|
|
|
|
|
190
|
|
|
Optional arguments: |
|
191
|
|
|
|
|
192
|
|
|
- `*names`: optional list of dependency directory names to filter on |
|
193
|
|
|
- `root`: specifies the path to the root working tree |
|
194
|
|
|
|
|
195
|
|
|
""" |
|
196
|
1 |
|
log.info("Locking dependencies...") |
|
197
|
1 |
|
count = None |
|
198
|
|
|
|
|
199
|
1 |
|
config = load_config(root) |
|
200
|
|
|
|
|
201
|
1 |
|
if config: |
|
202
|
1 |
|
common.newline() |
|
203
|
1 |
|
common.show("Locking dependencies...", color='message', log=False) |
|
204
|
1 |
|
common.newline() |
|
205
|
1 |
|
count = config.lock_dependencies(*names, obey_existing=False) |
|
206
|
1 |
|
common.dedent(level=0) |
|
207
|
|
|
|
|
208
|
1 |
|
return _display_result("lock", "Locked", count) |
|
209
|
|
|
|
|
210
|
|
|
|
|
211
|
1 |
|
@restore_cwd |
|
212
|
1 |
|
def delete(*, root=None, force=False): |
|
213
|
|
|
"""Delete dependencies for a project. |
|
214
|
|
|
|
|
215
|
|
|
Optional arguments: |
|
216
|
|
|
|
|
217
|
|
|
- `root`: specifies the path to the root working tree |
|
218
|
|
|
- `force`: indicates uncommitted changes can be overwritten |
|
219
|
|
|
|
|
220
|
|
|
""" |
|
221
|
1 |
|
log.info("Deleting dependencies...") |
|
222
|
1 |
|
count = None |
|
223
|
|
|
|
|
224
|
1 |
|
config = load_config(root) |
|
225
|
|
|
|
|
226
|
1 |
|
if config: |
|
227
|
1 |
|
common.newline() |
|
228
|
1 |
|
common.show("Checking for uncommitted changes...", |
|
229
|
|
|
color='message', log=False) |
|
230
|
1 |
|
common.newline() |
|
231
|
1 |
|
count = len(list(config.get_dependencies(allow_dirty=force))) |
|
232
|
1 |
|
common.dedent(level=0) |
|
233
|
1 |
|
common.show("Deleting all dependencies...", color='message', log=False) |
|
234
|
1 |
|
common.newline() |
|
235
|
1 |
|
config.uninstall_dependencies() |
|
236
|
|
|
|
|
237
|
1 |
|
return _display_result("delete", "Deleted", count, allow_zero=True) |
|
238
|
|
|
|
|
239
|
|
|
|
|
240
|
1 |
|
def show(*names, root=None): |
|
241
|
|
|
"""Display the path of an installed dependency or internal file. |
|
242
|
|
|
|
|
243
|
|
|
- `name`: dependency name or internal file keyword |
|
244
|
|
|
- `root`: specifies the path to the root working tree |
|
245
|
|
|
|
|
246
|
|
|
""" |
|
247
|
1 |
|
log.info("Finding paths...") |
|
248
|
|
|
|
|
249
|
1 |
|
config = load_config(root) |
|
250
|
|
|
|
|
251
|
1 |
|
if not config: |
|
252
|
1 |
|
log.error("No config found") |
|
253
|
1 |
|
return False |
|
254
|
|
|
|
|
255
|
1 |
|
for name in names or [None]: |
|
256
|
1 |
|
common.show(config.get_path(name), color='path') |
|
257
|
|
|
|
|
258
|
1 |
|
return True |
|
259
|
|
|
|
|
260
|
|
|
|
|
261
|
1 |
|
def edit(*, root=None): |
|
262
|
|
|
"""Open the confuration file for a project. |
|
263
|
|
|
|
|
264
|
|
|
Optional arguments: |
|
265
|
|
|
|
|
266
|
|
|
- `root`: specifies the path to the root working tree |
|
267
|
|
|
|
|
268
|
|
|
""" |
|
269
|
1 |
|
log.info("Launching config...") |
|
270
|
|
|
|
|
271
|
1 |
|
config = load_config(root) |
|
272
|
|
|
|
|
273
|
1 |
|
if not config: |
|
274
|
1 |
|
log.error("No config found") |
|
275
|
1 |
|
return False |
|
276
|
|
|
|
|
277
|
1 |
|
return system.launch(config.path) |
|
278
|
|
|
|
|
279
|
|
|
|
|
280
|
1 |
|
def _display_result(present, past, count, allow_zero=False): |
|
281
|
|
|
"""Convert a command's dependency count to a return status. |
|
282
|
|
|
|
|
283
|
|
|
>>> _display_result("sample", "Sampled", 1) |
|
284
|
|
|
True |
|
285
|
|
|
|
|
286
|
|
|
>>> _display_result("sample", "Sampled", None) |
|
287
|
|
|
False |
|
288
|
|
|
|
|
289
|
|
|
>>> _display_result("sample", "Sampled", 0) |
|
290
|
|
|
False |
|
291
|
|
|
|
|
292
|
|
|
>>> _display_result("sample", "Sampled", 0, allow_zero=True) |
|
293
|
|
|
True |
|
294
|
|
|
|
|
295
|
|
|
""" |
|
296
|
1 |
|
if count is None: |
|
297
|
1 |
|
log.warning("No dependencies to %s", present) |
|
298
|
1 |
|
elif count == 1: |
|
299
|
1 |
|
log.info("%s 1 dependency", past) |
|
300
|
|
|
else: |
|
301
|
1 |
|
log.info("%s %s dependencies", past, count) |
|
302
|
|
|
|
|
303
|
1 |
|
if count: |
|
304
|
1 |
|
return True |
|
305
|
1 |
|
elif count is None: |
|
306
|
1 |
|
return False |
|
307
|
|
|
else: |
|
308
|
1 |
|
assert count == 0 |
|
309
|
|
|
return allow_zero |
|
310
|
|
|
|