Completed
Pull Request — master (#53)
by Jace
02:11
created

mine._default_top()   A

Complexity

Conditions 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3
Metric Value
cc 3
dl 0
loc 11
ccs 6
cts 6
cp 1
crap 3
rs 9.4286
1
"""Functions to interact with sharing services."""
2
3 1
import os
4 1
import re
5 1
import logging
6
7
8 1
ROOTS = (
9
    r"C:\Users",
10
    r"/Users",
11
    r"/home",
12
)
13 1
SERVICES = (
14
    'Dropbox',
15
    'Dropbox (Personal)',
16
)
17 1
CONFIG = 'mine.yml'
18 1
CONFLICT_BASE = r"{} \(.+'s conflicted copy \d+-\d+-\d+.*\).*"
19 1
CONFLICT_ANY = CONFLICT_BASE.format(".+")
20 1
CONFLICT_CONFIG = CONFLICT_BASE.format("mine")
21 1
DEPTH = 3  # number of levels to search for the settings file
22
23 1
log = logging.getLogger(__name__)
24
25
26 1
def find_root(top=None):
27
    """Get the root of the shared directory."""
28 1
    top = top or os.path.expanduser('~')
29
30 1
    log.debug("Looking for sharing service in '%s'...", top)
31 1
    for directory in os.listdir(top):
32 1
        if directory in SERVICES:
33 1
            path = os.path.join(top, directory)
34 1
            log.debug("Found sharing service: %s", path)
35 1
            return path
36
37 1
    msg = "no sharing service found"
38 1
    if os.getenv('CI'):
39 1
        log.warning(msg)
40 1
        return top
41
    else:
42 1
        raise EnvironmentError(msg)
43
44
45 1
def find_config_path(top=None, root=None):
46
    """Get the path to the settings file."""
47 1
    log.info("Looking for settings file...")
48 1
    top = top or os.path.expanduser('~')
49 1
    root = root or find_root(top=top)
50
51 1
    log.debug("Looking for '%s' in '%s'...", CONFIG, root)
52 1
    for dirpath, dirnames, _, in os.walk(root):
53 1
        depth = dirpath.count(os.path.sep) - root.count(os.path.sep)
54 1
        if depth >= DEPTH:
55 1
            del dirnames[:]
56 1
            continue
57 1
        path = os.path.join(dirpath, CONFIG)
58 1
        if os.path.isfile(path) and \
59
                not os.path.isfile(os.path.join(path, 'setup.py')):
60 1
            log.info("Found settings file: %s", path)
61 1
            return path
62
63 1
    raise EnvironmentError("No '{}' file found".format(CONFIG))
64
65
66 1
def delete_conflicts(root=None, config_only=False, force=False):
67
    """Delete all files with conflicted filenames."""
68 1
    root = root or find_root()
69
70 1
    log.info("%s conflicted files...", 'Deleting' if force else 'Displaying')
71 1
    pattern = CONFLICT_CONFIG if config_only else CONFLICT_ANY
72 1
    log.debug("Pattern: %r", pattern)
73 1
    regex = re.compile(pattern)
74 1
    count = 0
75 1
    for dirname, _, filenames in os.walk(root):
76 1
        for filename in filenames:
77 1
            if regex.match(filename):
78 1
                count += 1
79 1
                path = os.path.join(dirname, filename)
80 1
                if force:
81 1
                    os.remove(path)
82 1
                print(path)
83
84 1
    if count and not force:
85 1
        print()
86 1
        print("Run again with '--force' to delete these "
87
              "{} conflict(s)".format(count))
88 1
        return False
89
    else:
90
        return True
91