-
Notifications
You must be signed in to change notification settings - Fork 4
/
repo.py
78 lines (68 loc) · 1.92 KB
/
repo.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
import os
import stat
import subprocess
from xml.etree import ElementTree
try:
from . import api, msg
from . import shared as G
from .exc_fmt import str_e
assert api and G and msg and str_e
except ImportError:
import api
import msg
import shared as G
from exc_fmt import str_e
REPO_MAPPING = {
'git': {
'dir': '.git',
'cmd': ['git', 'config', '--get', 'remote.origin.url'],
},
'svn': {
'dir': '.svn',
'cmd': ['svn', 'info', '--xml'],
},
'hg': {
'dir': '.hg',
'cmd': ['hg', 'paths', 'default'],
},
}
def detect_type(d):
for repo_type, v in REPO_MAPPING.items():
repo_path = os.path.join(d, v['dir'])
try:
s = os.stat(repo_path)
except Exception:
continue
if stat.S_ISDIR(s.st_mode):
return repo_type
def parse_svn_xml(d):
root = ElementTree.XML(d)
repo_url = root.find('info/entry/url')
return repo_url and repo_url.text
def get_info(workspace_url, project_dir):
repo_type = detect_type(project_dir)
if not repo_type:
return
msg.debug('Detected ', repo_type, ' repo in ', project_dir)
data = {
'type': repo_type,
}
cmd = REPO_MAPPING[repo_type]['cmd']
try:
p = subprocess.Popen(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=project_dir)
result = p.communicate()
repo_url = result[0].decode('utf-8').strip()
if repo_type == 'svn':
repo_url = parse_svn_xml(repo_url)
msg.log(repo_type, ' url is ', repo_url)
if not repo_url:
msg.error('Error getting ', repo_type, ' url:', result[1])
return
except Exception as e:
msg.error('Error getting ', repo_type, ' url:', str_e(e))
return
data['url'] = repo_url
return data