|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
|
|
3
|
|
|
from __future__ import unicode_literals |
|
4
|
|
|
import os |
|
5
|
|
|
import re |
|
6
|
|
|
|
|
7
|
|
|
from .exceptions import RepositoryNotFound |
|
8
|
|
|
from .vcs import clone |
|
9
|
|
|
|
|
10
|
|
|
BUILTIN_ABBREVIATIONS = { |
|
11
|
|
|
'gh': 'https://github.com/{0}.git', |
|
12
|
|
|
'bb': 'https://bitbucket.org/{0}', |
|
13
|
|
|
} |
|
14
|
|
|
|
|
15
|
|
|
REPO_REGEX = re.compile(r""" |
|
16
|
|
|
(?x) |
|
17
|
|
|
((((git|hg)\+)?(git|ssh|https?):(//)?) # something like git:// ssh:// etc. |
|
18
|
|
|
| # or |
|
19
|
|
|
(\w+@[\w\.]+) # something like user@... |
|
20
|
|
|
) |
|
21
|
|
|
""") |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
def is_repo_url(value): |
|
25
|
|
|
"""Return True if value is a repository URL.""" |
|
26
|
|
|
return bool(REPO_REGEX.match(value)) |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
def expand_abbreviations(template, user_abbreviations): |
|
30
|
|
|
""" |
|
31
|
|
|
Expand abbreviations in a template name. |
|
32
|
|
|
|
|
33
|
|
|
:param template: The project template name. |
|
34
|
|
|
:param config_dict: The user config, which will contain abbreviation |
|
35
|
|
|
definitions. |
|
36
|
|
|
""" |
|
37
|
|
|
|
|
38
|
|
|
abbreviations = BUILTIN_ABBREVIATIONS.copy() |
|
39
|
|
|
abbreviations.update(user_abbreviations) |
|
40
|
|
|
|
|
41
|
|
|
if template in abbreviations: |
|
42
|
|
|
return abbreviations[template] |
|
43
|
|
|
|
|
44
|
|
|
# Split on colon. If there is no colon, rest will be empty |
|
45
|
|
|
# and prefix will be the whole template |
|
46
|
|
|
prefix, sep, rest = template.partition(':') |
|
47
|
|
|
if prefix in abbreviations: |
|
48
|
|
|
return abbreviations[prefix].format(rest) |
|
49
|
|
|
|
|
50
|
|
|
return template |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
def determine_repo_dir(template, abbreviations, cookiecutters_dir, checkout, |
|
54
|
|
|
no_input): |
|
55
|
|
|
template = expand_abbreviations(template, abbreviations) |
|
56
|
|
|
|
|
57
|
|
|
if is_repo_url(template): |
|
58
|
|
|
repo_dir = clone( |
|
59
|
|
|
repo_url=template, |
|
60
|
|
|
checkout=checkout, |
|
61
|
|
|
clone_to_dir=cookiecutters_dir, |
|
62
|
|
|
no_input=no_input, |
|
63
|
|
|
) |
|
64
|
|
|
else: |
|
65
|
|
|
# If it's a local repo, no need to clone or copy to your |
|
66
|
|
|
# cookiecutters_dir |
|
67
|
|
|
repo_dir = template |
|
68
|
|
|
|
|
69
|
|
|
if not os.path.isdir(repo_dir): |
|
70
|
|
|
raise RepositoryNotFound( |
|
71
|
|
|
'The repository {0} could not be located.'.format(template) |
|
72
|
|
|
) |
|
73
|
|
|
return repo_dir |
|
74
|
|
|
|