|
1 | 1 | import datetime |
2 | 2 | import logging |
3 | 3 | import os |
4 | | -from string import strip |
5 | | -from subprocess import Popen, PIPE |
6 | 4 |
|
| 5 | +from subprocess import Popen, PIPE |
7 | 6 | from django.conf import settings |
8 | | - |
9 | 7 | from .exceptions import CommitLogError |
10 | 8 |
|
11 | 9 | logger = logging.getLogger(__name__) |
12 | 10 |
|
13 | 11 |
|
| 12 | +def execute_command(cmd, cwd): |
| 13 | + p = Popen(cmd, stdout=PIPE, stderr=PIPE, cwd=cwd) |
| 14 | + stdout, stderr = p.communicate() |
| 15 | + stdout = stdout.decode('utf8') if stdout is not None else stdout |
| 16 | + stderr = stderr.decode('utf8') if stderr is not None else stderr |
| 17 | + return (p, stdout, stderr) |
| 18 | + |
| 19 | + |
14 | 20 | def updaterepo(project, update=True): |
15 | 21 | if os.path.exists(project.working_copy): |
16 | 22 | if not update: |
17 | 23 | return |
18 | 24 |
|
19 | | - p = Popen(['git', 'pull'], stdout=PIPE, stderr=PIPE, |
20 | | - cwd=project.working_copy) |
| 25 | + p, _, stderr = execute_command(['git', 'pull'], cwd=project.working_copy) |
21 | 26 |
|
22 | | - stdout, stderr = p.communicate() |
23 | 27 | if p.returncode != 0: |
24 | 28 | raise CommitLogError("git pull returned %s: %s" % (p.returncode, |
25 | 29 | stderr)) |
26 | 30 | else: |
27 | 31 | return [{'error': False}] |
28 | 32 | else: |
29 | 33 | cmd = ['git', 'clone', project.repo_path, project.repo_name] |
30 | | - p = Popen(cmd, stdout=PIPE, stderr=PIPE, |
31 | | - cwd=settings.REPOSITORY_BASE_PATH) |
| 34 | + p, stdout, stderr = execute_command(cmd, settings.REPOSITORY_BASE_PATH) |
32 | 35 | logger.debug('Cloning Git repo {0} for project {1}'.format( |
33 | 36 | project.repo_path, project)) |
34 | | - stdout, stderr = p.communicate() |
35 | 37 |
|
36 | 38 | if p.returncode != 0: |
37 | 39 | raise CommitLogError("%s returned %s: %s" % ( |
@@ -59,32 +61,24 @@ def getlogs(endrev, startrev): |
59 | 61 | cmd.append(endrev.commitid) |
60 | 62 |
|
61 | 63 | working_copy = endrev.branch.project.working_copy |
62 | | - p = Popen(cmd, stdout=PIPE, stderr=PIPE, cwd=working_copy) |
63 | | - |
64 | | - stdout, stderr = p.communicate() |
| 64 | + p, stdout, stderr = execute_command(cmd, working_copy) |
65 | 65 |
|
66 | 66 | if p.returncode != 0: |
67 | 67 | raise CommitLogError("%s returned %s: %s" % ( |
68 | 68 | " ".join(cmd), p.returncode, stderr)) |
69 | 69 | logs = [] |
70 | | - for log in filter(None, stdout.split(b'\x1e')): |
| 70 | + for log in filter(None, stdout.split('\x1e')): |
71 | 71 | (short_commit_id, commit_id, date_t, author_name, author_email, |
72 | | - subject, body) = map(strip, log.split(b'\x00', 7)) |
73 | | - |
74 | | - tag = "" |
| 72 | + subject, body) = map(lambda s: s.strip(), log.split('\x00', 7)) |
75 | 73 |
|
76 | 74 | cmd = ["git", "tag", "--points-at", commit_id] |
77 | | - proc = Popen(cmd, stdout=PIPE, stderr=PIPE, cwd=working_copy) |
78 | 75 |
|
79 | 76 | try: |
80 | | - stdout, stderr = proc.communicate() |
81 | | - except ValueError: |
82 | | - stdout = b'' |
83 | | - stderr = b'' |
84 | | - |
85 | | - if proc.returncode == 0: |
86 | | - tag = stdout.strip() |
| 77 | + p, stdout, stderr = execute_command(cmd, working_copy) |
| 78 | + except Exception: |
| 79 | + logger.debug('Failed to get tag', exc_info=True) |
87 | 80 |
|
| 81 | + tag = stdout.strip() if p.returncode == 0 else "" |
88 | 82 | date = datetime.datetime.fromtimestamp( |
89 | 83 | int(date_t)).strftime("%Y-%m-%d %H:%M:%S") |
90 | 84 |
|
|
0 commit comments