Completed
Push — develop ( f22294...0e77e7 )
by Jace
10s
created

Source.create_link()   B

Complexity

Conditions 6

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 6.0493

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
c 2
b 0
f 0
dl 0
loc 25
ccs 16
cts 18
cp 0.8889
crap 6.0493
rs 7.5384
1
"""Wrappers for the dependency configuration files."""
2
3 1
import os
4 1
import logging
5 1
import warnings
6
7 1
import yorm
0 ignored issues
show
Configuration introduced by
The import yorm could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
8 1
from yorm.types import String, AttributeDictionary
0 ignored issues
show
Configuration introduced by
The import yorm.types could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
9
10 1
from .. import common
11 1
from .. import git
12 1
from .. import shell
13 1
from ..exceptions import InvalidConfig, InvalidRepository, UncommittedChanges
14
15
16 1
log = logging.getLogger(__name__)
0 ignored issues
show
Coding Style Naming introduced by
The name log does not conform to the constant naming conventions ((([A-Z_][A-Z0-9_]*)|(__.*__))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
17
18
19 1
@yorm.attr(name=String)
20 1
@yorm.attr(link=String)
21 1
@yorm.attr(repo=String)
22 1
@yorm.attr(rev=String)
23 1
class Source(AttributeDictionary):
24
    """A dictionary of `git` and `ln` arguments."""
25
26 1
    DIRTY = '<dirty>'
27 1
    UNKNOWN = '<unknown>'
28
29 1
    def __init__(self, repo, name, rev='master', link=None):
30 1
        super().__init__()
31 1
        self.repo = repo
32 1
        self.name = name
33 1
        self.rev = rev
34 1
        self.link = link
35 1
        if not self.repo:
36 1
            raise InvalidConfig("'repo' missing on {}".format(repr(self)))
37 1
        if not self.name:
38 1
            raise InvalidConfig("'name' missing on {}".format(repr(self)))
39
40 1
    def __repr__(self):
41 1
        return "<source {}>".format(self)
42
43 1
    def __str__(self):
44 1
        pattern = "'{r}' @ '{v}' in '{d}'"
45 1
        if self.link:
46 1
            pattern += " <- '{s}'"
47 1
        return pattern.format(r=self.repo, v=self.rev, d=self.name, s=self.link)
48
49 1
    def __eq__(self, other):
50 1
        return self.name == other.name
51
52 1
    def __ne__(self, other):
53 1
        return self.name != other.name
54
55 1
    def __lt__(self, other):
56 1
        return self.name < other.name
57
58 1
    def update_files(self, force=False, fetch=False, clean=True):
59
        """Ensure the source matches the specified revision."""
60 1
        log.info("Updating source files...")
61
62
        # Enter the working tree
63 1
        if not os.path.exists(self.name):
64 1
            log.debug("Creating a new repository...")
65 1
            git.clone(self.repo, self.name)
66 1
        shell.cd(self.name)
67
68
        # Check for uncommitted changes
69 1
        if not force:
70 1
            log.debug("Confirming there are no uncommitted changes...")
71 1
            if git.changes(include_untracked=clean):
72
                common.show()
73
                msg = "Uncommitted changes in {}".format(os.getcwd())
74
                raise UncommittedChanges(msg)
75
76
        # Fetch the desired revision
77 1
        if fetch or self.rev not in (git.get_branch(),
78
                                     git.get_hash(),
79
                                     git.get_tag()):
80 1
            git.fetch(self.repo, self.rev)
81
82
        # Update the working tree to the desired revision
83 1
        git.update(self.rev, fetch=fetch, clean=clean)
84
85 1
    def create_link(self, root, force=False):
86
        """Create a link from the target name to the current directory."""
87 1
        if not self.link:
88 1
            return
89
90 1
        log.info("Creating a symbolic link...")
91
92 1
        if os.name == 'nt':
93
            warnings.warn("Symbolic links are not supported on Windows")
94
            return
95
96 1
        target = os.path.join(root, self.link)
97 1
        source = os.path.relpath(os.getcwd(), os.path.dirname(target))
98
99 1
        if os.path.islink(target):
100 1
            os.remove(target)
101 1
        elif os.path.exists(target):
102 1
            if force:
103 1
                shell.rm(target)
104
            else:
105 1
                common.show()
106 1
                msg = "Preexisting link location at {}".format(target)
107 1
                raise UncommittedChanges(msg)
108
109 1
        shell.ln(source, target)
110
111 1
    def identify(self, allow_dirty=True, allow_missing=True):
112
        """Get the path and current repository URL and hash."""
113 1
        if os.path.isdir(self.name):
114
115 1
            shell.cd(self.name)
116
117 1
            path = os.getcwd()
118 1
            url = git.get_url()
119 1
            if git.changes(display_status=not allow_dirty, _show=True):
120 1
                if not allow_dirty:
121 1
                    common.show()
122 1
                    msg = "Uncommitted changes in {}".format(os.getcwd())
123 1
                    raise UncommittedChanges(msg)
124
125
                common.show(self.DIRTY, color='dirty', log=False)
126
                return path, url, self.DIRTY
127
            else:
128 1
                revision = git.get_hash(_show=True)
129 1
                common.show(revision, color='revision', log=False)
130 1
                return path, url, revision
131
132 1
        elif allow_missing:
133
134 1
            return os.getcwd(), '<missing>', self.UNKNOWN
135
136
        else:
137
138 1
            path = os.path.join(os.getcwd(), self.name)
139 1
            msg = "Not a valid repository: {}".format(path)
140 1
            raise InvalidRepository(msg)
141
142 1
    def lock(self):
143
        """Return a locked version of the current source."""
144 1
        _, _, revision = self.identify(allow_dirty=False, allow_missing=False)
145 1
        source = self.__class__(self.repo, self.name, revision, self.link)
146
        return source
147