Completed
Push — master ( 1edd10...57e203 )
by
unknown
01:17
created

repository_has_cookiecutter_json()   A

Complexity

Conditions 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
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 repository_has_cookiecutter_json(repo_directory):
45
    """Determines if `repo_directory` contains a `cookiecutter.json` file.
46
47
    :param repo_directory: The candidate repository directory.
48
    :return: True if the `repo_directory` is valid, else False.
49
    """
50
    repo_directory_exists = os.path.isdir(repo_directory)
51
52
    repo_config_exists = os.path.isfile(
53
        os.path.join(repo_directory, 'cookiecutter.json')
54
    )
55
    return repo_directory_exists and repo_config_exists
56
57
58
def determine_repo_dir(template, abbreviations, clone_to_dir, checkout,
59
                       no_input):
60
    """
61
    Locate the repository directory from a template reference.
62
63
    Applies repository abbreviations to the template reference.
64
    If the template refers to a repository URL, clone it.
65
    If the template is a path to a local repository, use it.
66
67
    :param template: A directory containing a project template directory,
68
        or a URL to a git repository.
69
    :param abbreviations: A dictionary of repository abbreviation
70
        definitions.
71
    :param clone_to_dir: The directory to clone the repository into.
72
    :param checkout: The branch, tag or commit ID to checkout after clone.
73
    :param no_input: Prompt the user at command line for manual configuration?
74
    :return: The cookiecutter template directory
75
    :raises: `RepositoryNotFound` if a repository directory could not be found.
76
    """
77
    template = expand_abbreviations(template, abbreviations)
78
79
    if is_repo_url(template):
80
        repo_dir = clone(
81
            repo_url=template,
82
            checkout=checkout,
83
            clone_to_dir=clone_to_dir,
84
            no_input=no_input,
85
        )
86
    else:
87
        # If it's a local repo, no need to clone or copy to your
88
        # cookiecutters_dir
89
        repo_dir = template
90
91
    if repository_has_cookiecutter_json(repo_dir):
92
        return repo_dir
93
94
    raise RepositoryNotFound(
95
        'The repository {} could not be located or does not contain '
96
        'a "cookiecutter.json" file.'.format(repo_dir)
97
    )
98