jolt 0.9.354__py3-none-any.whl → 0.9.370__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
jolt/plugins/repo.py DELETED
@@ -1,253 +0,0 @@
1
- from copy import copy
2
- from threading import RLock
3
- from xml.dom import minidom
4
-
5
- from jolt.influence import HashInfluenceProvider, HashInfluenceRegistry
6
- from jolt.tools import Tools
7
- from jolt import filesystem as fs
8
- from jolt import log
9
- from jolt.plugins import git
10
- from jolt.scheduler import NetworkExecutorExtension, NetworkExecutorExtensionFactory
11
- from jolt.xmldom import Attribute, Composition, SubElement, Element, ElementTree, ET
12
-
13
-
14
- @Attribute('name')
15
- @Attribute('fetch')
16
- class RepoRemote(SubElement):
17
- def __init__(self, elem=None):
18
- super(RepoRemote, self).__init__('remote', elem=elem)
19
-
20
-
21
- @Attribute('revision')
22
- @Attribute('remote')
23
- @Attribute('sync-j')
24
- class RepoDefault(SubElement):
25
- def __init__(self, elem=None):
26
- super(RepoDefault, self).__init__('default', elem=elem)
27
-
28
-
29
- @Attribute('path')
30
- @Attribute('name')
31
- @Attribute('revision')
32
- @Attribute('upstream')
33
- @Attribute('remote')
34
- class RepoProject(SubElement):
35
- def __init__(self, elem=None):
36
- super(RepoProject, self).__init__('project', elem=elem)
37
-
38
- def get_diff(self):
39
- with self.tools.cwd(self.path_or_name):
40
- return self.tools.run("git diff --no-ext-diff HEAD", output_on_error=True)
41
- assert False, "git command failed"
42
-
43
- def get_head(self):
44
- with self.tools.cwd(self.path_or_name):
45
- return self.tools.run("git rev-parse HEAD", output_on_error=True).strip()
46
- assert False, "git command failed"
47
-
48
- def get_remote_branches(self, commit):
49
- with self.tools.cwd(self.path_or_name):
50
- result = self.tools.run("git branch -r --contains {0}", commit, output_on_error=True)
51
- if not result:
52
- return []
53
- result = result.strip().splitlines()
54
- result = [line.strip() for line in result]
55
- return result
56
- assert False, "git command failed"
57
-
58
- def get_local_commits(self):
59
- with self.tools.cwd(self.path_or_name):
60
- result = self.tools.run("git rev-list HEAD ^@{{upstream}}", output_on_error=True)
61
- if not result:
62
- return []
63
- result = result.strip("\r\n ")
64
- return result.splitlines()
65
- assert False, "git command failed"
66
-
67
- def has_local_ref(self, ref):
68
- with self.tools.cwd(self.path_or_name):
69
- try:
70
- return True if self.tools.run("git show-ref {0}", ref, output=False) else False
71
- except Exception:
72
- return False
73
-
74
- def get_remote_ref(self, commit, remote, pattern=None):
75
- with self.tools.cwd(self.path_or_name):
76
- result = self.tools.run(
77
- "git ls-remote {0}{1}",
78
- remote,
79
- " {0}".format(pattern) if pattern else "",
80
- output_on_error=True)
81
- if not result:
82
- return None
83
- result = result.strip().splitlines()
84
- result = [line.split("\t") for line in result]
85
- result = {line[0]: line[1] for line in result}
86
- return result.get(commit)
87
- assert False, "git command failed"
88
-
89
- @property
90
- def path_or_name(self):
91
- return self.path or self.name
92
-
93
-
94
- @Composition(RepoRemote, "remote")
95
- @Composition(RepoDefault, "default")
96
- @Composition(RepoProject, "project")
97
- class RepoManifest(ElementTree):
98
- def __init__(self, task, path):
99
- super(RepoManifest, self).__init__(element=Element('manifest'))
100
- self.path = path
101
- self.tools = task.tools
102
-
103
- def append(self, element):
104
- self.getroot().append(element)
105
-
106
- def get(self, key):
107
- self.getroot().get(key)
108
-
109
- def set(self, key, value):
110
- self.getroot().set(key, value)
111
-
112
- def parse(self, filename=".repo/manifest.xml"):
113
- with open(fs.path.join(self.tools.getcwd(), filename)) as f:
114
- root = ET.fromstring(f.read())
115
- self._setroot(root)
116
- for project in self.projects:
117
- project.tools = self.tools
118
- return self
119
- raise Exception("failed to parse xml file")
120
-
121
- def format(self):
122
- return minidom.parseString(ET.tostring(self.getroot())).toprettyxml(indent=" ")
123
-
124
- def write(self, filename):
125
- with open(filename, 'w') as f:
126
- f.write(self.format())
127
-
128
- def get_remote(self, project):
129
- if len(self.defaults) > 0:
130
- return project.remote or self.defaults[0].remote
131
- return project.remote
132
-
133
- def get_upstream(self, project):
134
- if len(self.defaults) > 0:
135
- return project.upstream or self.defaults[0].revision or "master"
136
- return project.upstream or "master"
137
-
138
- def assert_clean(self):
139
- for project in self.projects:
140
- assert not project.get_diff(), \
141
- "repo project '{0}' has local changes"\
142
- .format(project.path_or_name)
143
-
144
- def lock_revisions(self):
145
- for project in self.projects:
146
- head = project.get_head()
147
- if not project.get_remote_branches(head):
148
- remote_ref = project.get_remote_ref(
149
- head, self.get_remote(project))
150
- assert remote_ref, \
151
- "repo project '{0}' has unpublished commits"\
152
- .format(project.path_or_name)
153
- head = remote_ref
154
-
155
- if not project.upstream:
156
- if project.revision and project.has_local_ref(project.revision):
157
- project.upstream = project.revision
158
- else:
159
- project.upstream = self.get_upstream(project)
160
-
161
- if not project.revision.startswith("refs/changes"):
162
- project.revision = head
163
-
164
-
165
- _git_repos = {}
166
- _git_repo_lock = RLock()
167
-
168
- _repo_manifests = {}
169
- _repo_manifest_lock = RLock()
170
-
171
-
172
- class RepoInfluenceProvider(HashInfluenceProvider):
173
- name = "Repo"
174
- path = "."
175
-
176
- def __init__(self, path=None, include=None, exclude=None, network=True):
177
- self.path = path or self.__class__.path
178
- self.include = include
179
- self.exclude = exclude
180
- self.network = network
181
-
182
- def get_influence(self, task):
183
- self.tools = Tools(task, task.joltdir)
184
- try:
185
- manifest_path = fs.path.join(task.joltdir, task.expand(self.path))
186
- manifest = RepoManifest(task, manifest_path)
187
- manifest.parse(fs.path.join(manifest_path, ".repo", "manifest.xml"))
188
-
189
- result = []
190
- for project in sorted(manifest.projects, key=lambda p: p.name):
191
- if self.include is not None and project.path_or_name not in self.include:
192
- continue
193
- if self.exclude is not None and project.path_or_name in self.exclude:
194
- continue
195
-
196
- with _git_repo_lock:
197
- gip = _git_repos.get(project.path_or_name)
198
- if gip is None:
199
- gip = git.GitInfluenceProvider(project.path_or_name)
200
- _git_repos[project.path_or_name] = gip
201
- result.append(gip.get_influence(task))
202
-
203
- return "\n".join(result)
204
-
205
- except KeyError:
206
- log.exception()
207
- assert False, "failed to calculate hash influence for repo manifest at {0}".format(self.path)
208
-
209
- def get_manifest(self, task):
210
- manifest_path = fs.path.join(task.joltdir, task.expand(self.path))
211
- with _repo_manifest_lock:
212
- manifest = _repo_manifests.get(manifest_path)
213
- if manifest is None:
214
- manifest = RepoManifest(task, manifest_path)
215
- manifest.parse()
216
- manifest.assert_clean()
217
- manifest.lock_revisions()
218
- _repo_manifests[manifest_path] = manifest
219
- return manifest
220
-
221
-
222
- class RepoNetworkExecutorExtension(NetworkExecutorExtension):
223
- def get_parameters(self, task):
224
- rip = list(filter(
225
- lambda n: isinstance(n, RepoInfluenceProvider),
226
- task.task.influence))
227
- if rip:
228
- assert len(rip) == 1, "task influenced by multiple repo manifests"
229
- rip = rip[0]
230
- if rip.network:
231
- manifest = rip.get_manifest(task.task)
232
- return {"repo_manifest": manifest.format()}
233
- return {}
234
-
235
-
236
- @NetworkExecutorExtensionFactory.Register
237
- class RepoNetworkExecutorExtensionFactory(NetworkExecutorExtensionFactory):
238
- def create(self):
239
- return RepoNetworkExecutorExtension()
240
-
241
-
242
- def global_influence(path, include=None, exclude=None, network=True, cls=RepoInfluenceProvider):
243
- HashInfluenceRegistry.get().register(cls(path, include, exclude, network))
244
-
245
-
246
- def influence(path='.', include=None, exclude=None, network=True, cls=RepoInfluenceProvider):
247
- def _decorate(taskcls):
248
- if "influence" not in taskcls.__dict__:
249
- taskcls.influence = copy(taskcls.influence)
250
- provider = cls(path, include, exclude, network)
251
- taskcls.influence.append(provider)
252
- return taskcls
253
- return _decorate
File without changes