Completed
Push — develop ( 6c75dd...cd0240 )
by Jace
05:37
created

gdm.Source.__str__()   A

Complexity

Conditions 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2
Metric Value
dl 0
loc 5
rs 9.4286
ccs 5
cts 5
cp 1
cc 2
crap 2
1
"""Wrappers for the dependency configuration files."""
2
3 1
import os
4 1
import logging
5
6 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...
7
8 1
from . import common
9 1
from . import git
10 1
from . import shell
11
12
13 1
log = logging.getLogger(__name__)
14
15
16 1
@yorm.attr(repo=yorm.converters.String)
17 1
@yorm.attr(dir=yorm.converters.String)
18 1
@yorm.attr(rev=yorm.converters.String)
19 1
@yorm.attr(link=yorm.converters.String)
20 1
class Source(yorm.converters.AttributeDictionary):
21
    """A dictionary of `git` and `ln` arguments."""
22
23 1
    def __init__(self, repo, name, rev='master', link=None):
24 1
        super().__init__()
25 1
        self.repo = repo
26 1
        self.dir = name
27 1
        self.rev = rev
28 1
        self.link = link
29 1
        if not self.repo:
30 1
            raise ValueError("'repo' missing on {}".format(repr(self)))
31 1
        if not self.dir:
32 1
            raise ValueError("'dir' missing on {}".format(repr(self)))
33
34 1
    def __repr__(self):
35 1
        return "<source {}>".format(self)
36
37 1
    def __str__(self):
38 1
        fmt = "'{r}' @ '{v}' in '{d}'"
39 1
        if self.link:
40 1
            fmt += " <- '{s}'"
41 1
        return fmt.format(r=self.repo, v=self.rev, d=self.dir, s=self.link)
42
43 1
    def __eq__(self, other):
44 1
        return self.dir == other.dir
45
46 1
    def __ne__(self, other):
47 1
        return self.dir != other.dir
48
49 1
    def __lt__(self, other):
50 1
        return self.dir < other.dir
51
52 1
    def update_files(self, force=False, fetch=False, clean=True):
53
        """Ensure the source matches the specified revision."""
54 1
        log.info("Updating source files...")
55
56
        # Enter the working tree
57 1
        if not os.path.exists(self.dir):
58 1
            log.debug("Creating a new repository...")
59 1
            git.clone(self.repo, self.dir)
60 1
        shell.cd(self.dir)
61
62
        # Check for uncommitted changes
63 1
        if not force:
64 1
            log.debug("Confirming there are no uncommitted changes...")
65 1
            if git.changes(include_untracked=clean):
66
                common.show()
67
                msg = "Uncommitted changes: {}".format(os.getcwd())
68
                raise RuntimeError(msg)
69
70
        # Fetch the desired revision
71 1
        if fetch or self.rev not in (git.get_branch(),
72
                                     git.get_hash(),
73
                                     git.get_tag()):
74 1
            git.fetch(self.repo, self.rev)
75
76
        # Update the working tree to the desired revision
77 1
        git.update(self.rev, fetch=fetch, clean=clean)
78
79 1
    def create_link(self, root, force=False):
80
        """Create a link from the target name to the current directory."""
81 1
        if self.link:
82 1
            log.info("Creating a symbolic link...")
83 1
            target = os.path.join(root, self.link)
84 1
            source = os.path.relpath(os.getcwd(), os.path.dirname(target))
85 1
            if os.path.islink(target):
86 1
                os.remove(target)
87 1
            elif os.path.exists(target):
88 1
                if force:
89 1
                    shell.rm(target)
90
                else:
91 1
                    common.show()
92 1
                    msg = "Preexisting link location: {}".format(target)
93 1
                    raise RuntimeError(msg)
94 1
            shell.ln(source, target)
95
96 1
    def identify(self, allow_dirty=True):
97
        """Get the path and current repository URL and hash."""
98 1
        if os.path.isdir(self.dir):
99
100 1
            shell.cd(self.dir)
101
102 1
            path = os.getcwd()
103 1
            url = git.get_url()
104 1
            if git.changes(_show=True):
105
                revision = '<dirty>'
106
                if not allow_dirty:
107
                    common.show()
108
                    msg = "Uncommitted changes: {}".format(os.getcwd())
109
                    raise RuntimeError(msg)
110
            else:
111 1
                revision = git.get_hash(_show=True)
112 1
            common.show(revision, log=False)
113
114 1
            return path, url, revision
115
116
        else:
117
118 1
            return os.getcwd(), '<missing>', '<unknown>'
119
120 1
    def lock(self):
121
        """Return a locked version of the current source."""
122 1
        _, _, revision = self.identify()
123 1
        source = self.__class__(self.repo, self.dir, revision, self.link)
124
        return source
125