-
Notifications
You must be signed in to change notification settings - Fork 0
/
share.py
executable file
·61 lines (49 loc) · 1.73 KB
/
share.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python
"""
Classes and functions to interact with sharing services.
"""
import os
import getpass
import logging
ROOTS = (
r"C:\Users",
r"/Users",
r"/home",
)
SERVICES = (
'Dropbox',
'Google Drive',
'Drive',
)
SHARE = 'DropTheBeat'
SHARE_DEPTH = 3 # number of levels to search for share directory
def find(top=None):
"""Return the path to a sharing location."""
top = top or _default_top()
logging.debug("looking for service in {}...".format(top))
for directory in os.listdir(top):
if directory in SERVICES:
service = os.path.join(top, directory)
logging.debug("found service: {}".format(service))
logging.debug("looking for '{}' in {}...".format(SHARE, service))
for dirpath, dirnames, _, in os.walk(service):
depth = dirpath.count(os.path.sep) - service.count(os.path.sep)
if depth >= SHARE_DEPTH:
del dirnames[:]
continue
path = os.path.join(dirpath, SHARE)
if os.path.isdir(path) and \
not os.path.isfile(os.path.join(path, 'setup.py')):
logging.info("found share: {}".format(path))
return path
raise EnvironmentError("no '{}' folder found".format(SHARE))
def _default_top():
"""Return the default search path."""
username = getpass.getuser()
logging.debug("looking for home...")
for root in ROOTS:
path = os.path.join(root, username)
if os.path.isdir(path): # pragma: no cover - manual test
logging.debug("found home: {}".format(path))
return path
raise EnvironmentError("no home found for '{}'".format(username))