1
|
|
|
"""Wrappers for the dependency configuration files.""" |
2
|
|
|
|
3
|
1 |
|
import os |
4
|
1 |
|
import logging |
5
|
1 |
|
import warnings |
6
|
|
|
|
7
|
1 |
|
import yorm |
|
|
|
|
8
|
1 |
|
from yorm.types import String, NullableString, List, AttributeDictionary |
|
|
|
|
9
|
|
|
|
10
|
1 |
|
from .. import common, exceptions, shell, git |
11
|
|
|
|
12
|
|
|
|
13
|
1 |
|
log = logging.getLogger(__name__) |
|
|
|
|
14
|
|
|
|
15
|
|
|
|
16
|
1 |
|
@yorm.attr(name=String) |
17
|
1 |
|
@yorm.attr(repo=String) |
18
|
1 |
|
@yorm.attr(rev=String) |
19
|
1 |
|
@yorm.attr(link=NullableString) |
20
|
1 |
|
@yorm.attr(scripts=List.of_type(String)) |
21
|
1 |
|
class Source(AttributeDictionary): |
22
|
|
|
"""A dictionary of `git` and `ln` arguments.""" |
23
|
|
|
|
24
|
1 |
|
DIRTY = '<dirty>' |
25
|
1 |
|
UNKNOWN = '<unknown>' |
26
|
|
|
|
27
|
1 |
|
def __init__(self, repo, name=None, rev='master', link=None, scripts=None): |
|
|
|
|
28
|
1 |
|
super().__init__() |
29
|
1 |
|
self.repo = repo |
30
|
1 |
|
self.name = self._infer_name(repo) if name is None else name |
31
|
1 |
|
self.rev = rev |
32
|
1 |
|
self.link = link |
33
|
1 |
|
self.scripts = scripts or [] |
34
|
|
|
|
35
|
1 |
|
for key in ['name', 'repo', 'rev']: |
36
|
1 |
|
if not self[key]: |
37
|
1 |
|
msg = "'{}' required for {}".format(key, repr(self)) |
38
|
1 |
|
raise exceptions.InvalidConfig(msg) |
39
|
|
|
|
40
|
1 |
|
def __repr__(self): |
41
|
1 |
|
return "<source {}>".format(self) |
42
|
|
|
|
43
|
1 |
|
def __str__(self): |
44
|
1 |
|
pattern = "'{r}' @ '{v}' in '{d}'" |
45
|
1 |
|
if self.link: |
46
|
1 |
|
pattern += " <- '{s}'" |
47
|
1 |
|
return pattern.format(r=self.repo, v=self.rev, d=self.name, s=self.link) |
48
|
|
|
|
49
|
1 |
|
def __eq__(self, other): |
50
|
1 |
|
return self.name == other.name |
51
|
|
|
|
52
|
1 |
|
def __ne__(self, other): |
53
|
1 |
|
return self.name != other.name |
54
|
|
|
|
55
|
1 |
|
def __lt__(self, other): |
56
|
1 |
|
return self.name < other.name |
57
|
|
|
|
58
|
1 |
|
def update_files(self, force=False, fetch=False, clean=True): |
59
|
|
|
"""Ensure the source matches the specified revision.""" |
60
|
1 |
|
log.info("Updating source files...") |
61
|
|
|
|
62
|
|
|
# Clone the repository if needed |
63
|
1 |
|
if not os.path.exists(self.name): |
64
|
1 |
|
git.clone(self.repo, self.name) |
65
|
|
|
|
66
|
|
|
# Enter the working tree |
67
|
1 |
|
shell.cd(self.name) |
68
|
1 |
|
if not git.valid(): |
69
|
1 |
|
raise self._invalid_repository |
70
|
|
|
|
71
|
|
|
# Check for uncommitted changes |
72
|
1 |
|
if not force: |
73
|
1 |
|
log.debug("Confirming there are no uncommitted changes...") |
74
|
1 |
|
if git.changes(include_untracked=clean): |
75
|
|
|
msg = "Uncommitted changes in {}".format(os.getcwd()) |
76
|
|
|
raise exceptions.UncommittedChanges(msg) |
77
|
|
|
|
78
|
|
|
# Fetch the desired revision |
79
|
1 |
|
if fetch or self.rev not in (git.get_branch(), |
80
|
|
|
git.get_hash(), |
81
|
|
|
git.get_tag()): |
82
|
1 |
|
git.fetch(self.repo, self.rev) |
83
|
|
|
|
84
|
|
|
# Update the working tree to the desired revision |
85
|
1 |
|
git.update(self.rev, fetch=fetch, clean=clean) |
86
|
|
|
|
87
|
1 |
|
def create_link(self, root, force=False): |
88
|
|
|
"""Create a link from the target name to the current directory.""" |
89
|
1 |
|
if not self.link: |
90
|
1 |
|
return |
91
|
|
|
|
92
|
1 |
|
log.info("Creating a symbolic link...") |
93
|
|
|
|
94
|
1 |
|
if os.name == 'nt': |
95
|
|
|
warnings.warn("Symbolic links are not supported on Windows") |
96
|
|
|
return |
97
|
|
|
|
98
|
1 |
|
target = os.path.join(root, self.link) |
99
|
1 |
|
source = os.path.relpath(os.getcwd(), os.path.dirname(target)) |
100
|
|
|
|
101
|
1 |
|
if os.path.islink(target): |
102
|
1 |
|
os.remove(target) |
103
|
1 |
|
elif os.path.exists(target): |
104
|
1 |
|
if force: |
105
|
1 |
|
shell.rm(target) |
106
|
|
|
else: |
107
|
1 |
|
msg = "Preexisting link location at {}".format(target) |
108
|
1 |
|
raise exceptions.UncommittedChanges(msg) |
109
|
|
|
|
110
|
1 |
|
shell.ln(source, target) |
111
|
|
|
|
112
|
1 |
|
def run_scripts(self, force=False): |
|
|
|
|
113
|
1 |
|
log.info("Running install scripts...") |
114
|
|
|
|
115
|
|
|
# Enter the working tree |
116
|
1 |
|
shell.cd(self.name) |
117
|
1 |
|
if not git.valid(): |
118
|
|
|
raise self._invalid_repository |
119
|
|
|
|
120
|
|
|
# Check for scripts |
121
|
1 |
|
if not self.scripts: |
122
|
1 |
|
common.show("(no scripts to run)", color='shell_info') |
123
|
1 |
|
common.newline() |
124
|
1 |
|
return |
125
|
|
|
|
126
|
|
|
# Run all scripts |
127
|
1 |
|
for script in self.scripts: |
128
|
1 |
|
try: |
129
|
1 |
|
lines = shell.call(script, _shell=True) |
130
|
1 |
|
except exceptions.ShellError as exc: |
131
|
1 |
|
common.show(*exc.output, color='shell_error') |
132
|
1 |
|
cmd = exc.program |
133
|
1 |
|
if force: |
134
|
1 |
|
log.debug("Ignored error from call to '%s'", cmd) |
135
|
|
|
else: |
136
|
1 |
|
msg = "Command '{}' failed in {}".format(cmd, os.getcwd()) |
137
|
1 |
|
raise exceptions.ScriptFailure(msg) |
138
|
|
|
else: |
139
|
|
|
common.show(*lines, color='shell_output') |
|
|
|
|
140
|
1 |
|
common.newline() |
141
|
|
|
|
142
|
1 |
|
def identify(self, allow_dirty=True, allow_missing=True): |
143
|
|
|
"""Get the path and current repository URL and hash.""" |
144
|
1 |
|
if os.path.isdir(self.name): |
145
|
|
|
|
146
|
1 |
|
shell.cd(self.name) |
147
|
1 |
|
if not git.valid(): |
148
|
1 |
|
raise self._invalid_repository |
149
|
|
|
|
150
|
1 |
|
path = os.getcwd() |
151
|
1 |
|
url = git.get_url() |
152
|
1 |
|
if git.changes(display_status=not allow_dirty, _show=True): |
153
|
1 |
|
if not allow_dirty: |
154
|
1 |
|
msg = "Uncommitted changes in {}".format(os.getcwd()) |
155
|
1 |
|
raise exceptions.UncommittedChanges(msg) |
156
|
|
|
|
157
|
|
|
common.show(self.DIRTY, color='git_dirty', log=False) |
158
|
|
|
common.newline() |
159
|
|
|
return path, url, self.DIRTY |
160
|
|
|
else: |
161
|
1 |
|
rev = git.get_hash(_show=True) |
162
|
1 |
|
common.show(rev, color='git_rev', log=False) |
163
|
1 |
|
common.newline() |
164
|
1 |
|
return path, url, rev |
165
|
|
|
|
166
|
1 |
|
elif allow_missing: |
167
|
|
|
|
168
|
1 |
|
return os.getcwd(), '<missing>', self.UNKNOWN |
169
|
|
|
|
170
|
|
|
else: |
171
|
|
|
|
172
|
1 |
|
raise self._invalid_repository |
173
|
|
|
|
174
|
1 |
|
def lock(self, rev=None): |
175
|
|
|
"""Return a locked version of the current source.""" |
176
|
1 |
|
if rev is None: |
177
|
1 |
|
_, _, rev = self.identify(allow_dirty=False, allow_missing=False) |
178
|
1 |
|
source = self.__class__(self.repo, self.name, rev, |
179
|
|
|
self.link, self.scripts) |
180
|
1 |
|
return source |
181
|
|
|
|
182
|
1 |
|
@property |
183
|
|
|
def _invalid_repository(self): |
|
|
|
|
184
|
1 |
|
path = os.path.join(os.getcwd(), self.name) |
185
|
1 |
|
msg = "Not a valid repository: {}".format(path) |
186
|
1 |
|
return exceptions.InvalidRepository(msg) |
187
|
|
|
|
188
|
1 |
|
@staticmethod |
189
|
|
|
def _infer_name(repo): |
|
|
|
|
190
|
1 |
|
filename = repo.split('/')[-1] |
191
|
1 |
|
name = filename.split('.')[0] |
192
|
|
|
return name |
193
|
|
|
|
This can be caused by one of the following:
1. Missing Dependencies
This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.
2. Missing __init__.py files
This error could also result from missing
__init__.py
files in your module folders. Make sure that you place one file in each sub-folder.