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