|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
|
|
3
|
|
|
"""Cookiecutter repository functions.""" |
|
4
|
|
|
from __future__ import unicode_literals |
|
5
|
|
|
import os |
|
6
|
|
|
import re |
|
7
|
|
|
|
|
8
|
|
|
from .exceptions import RepositoryNotFound |
|
9
|
|
|
from .vcs import clone |
|
10
|
|
|
|
|
11
|
|
|
REPO_REGEX = re.compile(r""" |
|
12
|
|
|
(?x) |
|
13
|
|
|
((((git|hg)\+)?(git|ssh|https?):(//)?) # something like git:// ssh:// etc. |
|
14
|
|
|
| # or |
|
15
|
|
|
(\w+@[\w\.]+) # something like user@... |
|
16
|
|
|
) |
|
17
|
|
|
""") |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
def is_repo_url(value): |
|
21
|
|
|
"""Return True if value is a repository URL.""" |
|
22
|
|
|
return bool(REPO_REGEX.match(value)) |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
def expand_abbreviations(template, abbreviations): |
|
26
|
|
|
""" |
|
27
|
|
|
Expand abbreviations in a template name. |
|
28
|
|
|
|
|
29
|
|
|
:param template: The project template name. |
|
30
|
|
|
:param abbreviations: Abbreviation definitions. |
|
31
|
|
|
""" |
|
32
|
|
|
if template in abbreviations: |
|
33
|
|
|
return abbreviations[template] |
|
34
|
|
|
|
|
35
|
|
|
# Split on colon. If there is no colon, rest will be empty |
|
36
|
|
|
# and prefix will be the whole template |
|
37
|
|
|
prefix, sep, rest = template.partition(':') |
|
38
|
|
|
if prefix in abbreviations: |
|
39
|
|
|
return abbreviations[prefix].format(rest) |
|
40
|
|
|
|
|
41
|
|
|
return template |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
def determine_repo_dir(template, abbreviations, clone_to_dir, checkout, |
|
45
|
|
|
no_input): |
|
46
|
|
|
""" |
|
47
|
|
|
Locate the repository directory from a template reference. |
|
48
|
|
|
|
|
49
|
|
|
Applies repository abbreviations to the template reference. |
|
50
|
|
|
If the template refers to a repository URL, clone it. |
|
51
|
|
|
If the template is a path to a local repository, use it. |
|
52
|
|
|
|
|
53
|
|
|
:param template: A directory containing a project template directory, |
|
54
|
|
|
or a URL to a git repository. |
|
55
|
|
|
:param abbreviations: A dictionary of repository abbreviation |
|
56
|
|
|
definitions. |
|
57
|
|
|
:param clone_to_dir: The directory to clone the repository into. |
|
58
|
|
|
:param checkout: The branch, tag or commit ID to checkout after clone. |
|
59
|
|
|
:param no_input: Prompt the user at command line for manual configuration? |
|
60
|
|
|
:return: The cookiecutter template directory |
|
61
|
|
|
:raises: `RepositoryNotFound` if a repository directory could not be found. |
|
62
|
|
|
""" |
|
63
|
|
|
template = expand_abbreviations(template, abbreviations) |
|
64
|
|
|
|
|
65
|
|
|
if is_repo_url(template): |
|
66
|
|
|
repo_dir = clone( |
|
67
|
|
|
repo_url=template, |
|
68
|
|
|
checkout=checkout, |
|
69
|
|
|
clone_to_dir=clone_to_dir, |
|
70
|
|
|
no_input=no_input, |
|
71
|
|
|
) |
|
72
|
|
|
else: |
|
73
|
|
|
# If it's a local repo, no need to clone or copy to your |
|
74
|
|
|
# cookiecutters_dir |
|
75
|
|
|
repo_dir = template |
|
76
|
|
|
|
|
77
|
|
|
if not os.path.isdir(repo_dir): |
|
78
|
|
|
raise RepositoryNotFound( |
|
79
|
|
|
'The repository {0} could not be located.'.format(template) |
|
80
|
|
|
) |
|
81
|
|
|
return repo_dir |
|
82
|
|
|
|