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