Completed
Push — identify-cookiecutter-template ( be2324...86477a )
by Michael
01:12
created

determine_repo_dir()   B

Complexity

Conditions 3

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
c 2
b 0
f 0
dl 0
loc 37
rs 8.8571
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
BUILTIN_ABBREVIATIONS = {
12
    'gh': 'https://github.com/{0}.git',
13
    'bb': 'https://bitbucket.org/{0}',
14
}
15
16
REPO_REGEX = re.compile(r"""
17
(?x)
18
((((git|hg)\+)?(git|ssh|https?):(//)?)  # something like git:// ssh:// etc.
19
 |                                      # or
20
 (\w+@[\w\.]+)                          # something like user@...
21
)
22
""")
23
24
25
def is_repo_url(value):
26
    """Return True if value is a repository URL."""
27
    return bool(REPO_REGEX.match(value))
28
29
30
def expand_abbreviations(template, user_abbreviations):
31
    """
32
    Expand abbreviations in a template name.
33
34
    :param template: The project template name.
35
    :param user_abbreviations: The user config, which will contain abbreviation
36
        definitions.
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, clone_to_dir, checkout,
54
                       no_input):
55
    """
56
    Locate the repository directory from a template reference.
57
58
    Applies repository abbreviations to the template reference.
59
    If the template refers to a repository URL, clone it.
60
    If the template is a path to a local repository, use it.
61
62
    :param template: A directory containing a project template directory,
63
        or a URL to a git repository.
64
    :param abbreviations: A dictionary of repository abbreviation
65
        definitions.
66
    :param clone_to_dir: The directory to clone the repository into.
67
    :param checkout: The branch, tag or commit ID to checkout after clone.
68
    :param no_input: Prompt the user at command line for manual configuration?
69
    :return:
70
    """
71
    template = expand_abbreviations(template, abbreviations)
72
73
    if is_repo_url(template):
74
        repo_dir = clone(
75
            repo_url=template,
76
            checkout=checkout,
77
            clone_to_dir=clone_to_dir,
78
            no_input=no_input,
79
        )
80
    else:
81
        # If it's a local repo, no need to clone or copy to your
82
        # cookiecutters_dir
83
        repo_dir = template
84
85
    if not os.path.isdir(repo_dir):
86
        raise RepositoryNotFound(
87
            'The repository {0} could not be located.'.format(template)
88
        )
89
    return repo_dir
90