/
repositories.py
469 lines (357 loc) · 13.6 KB
/
repositories.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
# -*- coding: utf8 -*-
from __future__ import absolute_import
import logging
import os
import scandir
import subprocess
from six import text_type
from django.conf import settings
log = logging.getLogger(__name__)
class PullFromRepositoryException(Exception):
pass
class PullFromRepository(object):
def __init__(self, source, target, branch):
self.source = source
self.target = target
self.branch = branch
def pull(self, source=None, target=None):
raise NotImplementedError
class PullFromGit(PullFromRepository):
def pull(self, source=None, target=None, branch=None):
log.debug("Git: Update repository.")
source = source or self.source
target = target or self.target
branch = branch or self.branch
command = ["git", "fetch", "--all"]
execute(command, target)
# Undo local changes
remote = "origin"
if branch:
remote += "/" + branch
command = ["git", "reset", "--hard", remote]
code, output, error = execute(command, target)
if code != 0:
log.info("Git: " + text_type(error))
log.debug("Git: Clone instead.")
command = ["git", "clone", source, target]
code, output, error = execute(command)
if code != 0:
raise PullFromRepositoryException(text_type(error))
log.debug("Git: Repository at " + source + " cloned.")
else:
log.debug("Git: Repository at " + source + " updated.")
if branch:
command = ["git", "checkout", branch]
code, output, error = execute(command, target)
if code != 0:
raise PullFromRepositoryException(text_type(error))
log.debug("Git: Branch " + branch + " checked out.")
class PullFromHg(PullFromRepository):
def pull(self, source=None, target=None):
log.debug("Mercurial: Update repository.")
source = source or self.source
target = target or self.target
# Undo local changes: Mercurial doesn't offer anything more elegant
command = ["rm", "-rf", target]
code, output, error = execute(command)
command = ["hg", "clone", source, target]
code, output, error = execute(command)
if code == 0:
log.debug("Mercurial: Repository at " + source + " cloned.")
else:
raise PullFromRepositoryException(text_type(error))
class PullFromSvn(PullFromRepository):
def pull(self, source=None, target=None):
log.debug("Subversion: Checkout or update repository.")
source = source or self.source
target = target or self.target
if os.path.exists(target):
status = "updated"
command = ["svn", "update", "--accept", "theirs-full", target]
else:
status = "checked out"
command = [
"svn",
"checkout",
"--trust-server-cert",
"--non-interactive",
source,
target,
]
code, output, error = execute(command, env=get_svn_env())
if code != 0:
raise PullFromRepositoryException(text_type(error))
log.debug("Subversion: Repository at " + source + " %s." % status)
class CommitToRepositoryException(Exception):
pass
class CommitToRepository(object):
def __init__(self, path, message, user, branch, url):
self.path = path
self.message = message
self.user = user
self.url = url
self.branch = branch
def commit(self, path=None, message=None, user=None):
raise NotImplementedError
def nothing_to_commit(self):
return log.warning("Nothing to commit")
class CommitToGit(CommitToRepository):
def commit(self, path=None, message=None, user=None, branch=None):
log.debug("Git: Commit to repository.")
path = path or self.path
message = message or self.message
user = user or self.user
branch = branch or self.branch
author = user.display_name_and_email
# Embed git identity info into commands
git_cmd = [
"git",
"-c",
"user.name=Mozilla Pontoon",
"-c",
"user.email=pontoon@mozilla.com",
]
# Add new and remove missing paths
execute(git_cmd + ["add", "-A", "--", path], path)
# Commit
commit = git_cmd + ["commit", "-m", message, "--author", author]
code, output, error = execute(commit, path)
if code != 0 and len(error):
raise CommitToRepositoryException(text_type(error))
# Push
push_target = "HEAD"
if branch:
push_target = branch
push = ["git", "push", self.url, push_target]
code, output, error = execute(push, path)
if code != 0:
raise CommitToRepositoryException(text_type(error))
if "Everything up-to-date" in error:
return self.nothing_to_commit()
log.info(message)
class CommitToHg(CommitToRepository):
def commit(self, path=None, message=None, user=None):
log.debug("Mercurial: Commit to repository.")
path = path or self.path
message = message or self.message
user = user or self.user
author = user.display_name_and_email
# Add new and remove missing paths
add = ["hg", "addremove"]
execute(add, path)
# Commit
commit = ["hg", "commit", "-m", message, "-u", author]
code, output, error = execute(commit, path)
if code != 0 and len(error):
raise CommitToRepositoryException(text_type(error))
# Push
push = ["hg", "push"]
code, output, error = execute(push, path)
if code == 1 and "no changes found" in output:
return self.nothing_to_commit()
if code != 0 and len(error):
raise CommitToRepositoryException(text_type(error))
log.info(message)
class CommitToSvn(CommitToRepository):
def commit(self, path=None, message=None, user=None):
log.debug("Subversion: Commit to repository.")
path = path or self.path
message = message or self.message
user = user or self.user
author = user.display_name_and_email
# Commit
command = [
"svn",
"commit",
"-m",
message,
"--with-revprop",
"author=%s" % author,
path,
]
code, output, error = execute(command, env=get_svn_env())
if code != 0:
raise CommitToRepositoryException(error)
if not output and not error:
return self.nothing_to_commit()
log.info(message)
def execute(command, cwd=None, env=None):
try:
st = subprocess.PIPE
proc = subprocess.Popen(
args=command, stdout=st, stderr=st, stdin=st, cwd=cwd, env=env
)
(output, error) = proc.communicate()
# Make sure that we manipulate strings instead of bytes, to avoid
# compatibility errors in Python 3.
if type(output) is bytes:
output = output.decode("utf-8")
if type(error) is bytes:
error = error.decode("utf-8")
code = proc.returncode
return code, output, error
except OSError as error:
if type(error) is bytes:
error = error.decode("utf-8")
return -1, "", error
def update_from_vcs(repo_type, url, path, branch):
obj = globals()["PullFrom%s" % repo_type.capitalize()](url, path, branch)
obj.pull()
def commit_to_vcs(repo_type, path, message, user, branch, url):
try:
obj = globals()["CommitTo%s" % repo_type.capitalize()](
path, message, user, branch, url
)
return obj.commit()
except CommitToRepositoryException as e:
log.debug("%s Commit Error for %s: %s" % (repo_type.upper(), path, e))
raise e
def get_svn_env():
"""Return an environment dict for running SVN in."""
if settings.SVN_LD_LIBRARY_PATH:
env = os.environ.copy()
env["LD_LIBRARY_PATH"] = (
settings.SVN_LD_LIBRARY_PATH + ":" + env["LD_LIBRARY_PATH"]
)
return env
else:
return None
class VCSRepository(object):
@classmethod
def for_type(cls, repo_type, path):
SubClass = cls.REPO_TYPES.get(repo_type)
if SubClass is None:
raise ValueError("No subclass found for repo type {0}.".format(repo_type))
return SubClass(path)
def __init__(self, path):
self.path = path
def execute(self, cmd, cwd=None, env=None, log_errors=True):
cwd = cwd or self.path
code, output, error = execute(cmd, cwd=cwd, env=env)
if log_errors and code != 0:
log.error(
"Error while executing command `{cmd}` in `{cwd}`: {stderr}".format(
cmd=text_type(cmd), cwd=cwd, stderr=error
)
)
return code, output, error
def get_changed_files(self, path, from_revision, statueses=None):
"""Get a list of changed files in the repository."""
raise NotImplementedError
def get_removed_files(self, from_revision):
"""Get a list of removed files in the repository."""
raise NotImplementedError
class SvnRepository(VCSRepository):
def execute(self, cmd, cwd=None, env=None, log_errors=False):
return execute(cmd, cwd=cwd, env=get_svn_env())
@property
def revision(self):
code, output, error = self.execute(["svnversion", self.path], log_errors=True)
return output.strip() if code == 0 else None
def get_changed_files(self, path, from_revision, statuses=None):
statuses = statuses or ("A", "M")
def normalize_revision(rev):
"""Remove all non digit characters from the revision number. """
return "".join(filter(lambda c: c.isdigit(), rev))
from_revision = normalize_revision(from_revision)
code, output, error = self.execute(
["svn", "diff", "-r", "{}:{}".format(from_revision, "HEAD"), "--summarize"],
cwd=path,
)
if code == 0:
# Mark added/modfied files as the changed ones
return [
line.split()[1]
for line in output.split("\n")
if line and line[0] in statuses
]
return []
def get_removed_files(self, path, from_revision):
return self.get_changed_files(path, from_revision, ("D",))
class GitRepository(VCSRepository):
@property
def revision(self):
code, output, error = self.execute(["git", "rev-parse", "HEAD"],)
return output.strip() if code == 0 else None
def get_changed_files(self, path, from_revision, statuses=None):
statuses = statuses or ("A", "M")
code, output, error = self.execute(
[
"git",
"diff",
"--name-status",
"{}..HEAD".format(from_revision),
"--",
path,
],
)
if code == 0:
return [
line.split()[1]
for line in output.split("\n")
if line and line[0] in statuses
]
return []
def get_removed_files(self, path, from_revision):
return self.get_changed_files(path, from_revision, ("D",))
class HgRepository(VCSRepository):
@property
def revision(self):
code, output, error = self.execute(
["hg", "identify", "--id", "--rev=default"], cwd=self.path, log_errors=True
)
return output.strip() if code == 0 else None
def _strip(self, rev):
"Ignore trailing + in revision number. It marks local changes."
return rev.rstrip("+")
def get_changed_files(self, path, from_revision, statuses=None):
statuses = statuses or ("A", "M")
code, output, error = self.execute(
[
"hg",
"status",
"-a",
"-m",
"-r",
"--rev={}".format(self._strip(from_revision)),
"--rev=default",
],
cwd=path,
)
if code == 0:
# Mark added / modified files as the changed ones
return [
line.split()[1]
for line in output.split("\n")
if line and line[0] in statuses
]
return []
def get_removed_files(self, path, from_revision):
return self.get_changed_files(path, self._strip(from_revision), ("R",))
# TODO: Tie these to the same constants that the Repository model uses.
VCSRepository.REPO_TYPES = {
"hg": HgRepository,
"svn": SvnRepository,
"git": GitRepository,
}
def get_revision(repo_type, path):
repo = VCSRepository.for_type(repo_type, path)
return repo.revision
def get_changed_files(repo_type, path, revision):
"""Return a list of changed files for the repository."""
repo = VCSRepository.for_type(repo_type, path)
log.info("Retrieving changed files for: {}:{}".format(path, revision))
# If there's no latest revision we should return all the files in the latest
# version of repository
if revision is None:
paths = []
for root, _, files in scandir.walk(path):
for f in files:
if root[0] == "." or "/." in root:
continue
paths.append(os.path.join(root, f).replace(path + "/", ""))
return paths, []
return (
repo.get_changed_files(path, revision),
repo.get_removed_files(path, revision),
)