Skip to content

Commit

Permalink
Added Google Drive support and depth limit.
Browse files Browse the repository at this point in the history
  • Loading branch information
jacebrowning committed Apr 25, 2014
1 parent f959df2 commit bf3c467
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
12 changes: 10 additions & 2 deletions dtb/share.py
Expand Up @@ -15,8 +15,11 @@
)
SERVICES = (
'Dropbox',
'Google Drive',
'Drive',
)
SHARE = 'DropTheBeat'
SHARE_DEPTH = 3 # number of levels to search for share directory


def find(top=None):
Expand All @@ -30,9 +33,14 @@ def find(top=None):
service = os.path.join(top, directory)
logging.debug("found service: {}".format(service))
logging.debug("looking for '{}' in {}...".format(SHARE, service))
for dirpath, _, _, in os.walk(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):
if os.path.isdir(path) and \
not os.path.isfile(os.path.join(path, 'setup.py')):
logging.info("found share: {}".format(path))
return path

Expand Down
24 changes: 22 additions & 2 deletions dtb/test/test_share.py
Expand Up @@ -19,8 +19,8 @@
class TestFunctions(unittest.TestCase): # pylint: disable=R0904
"""Unit tests for the sharing functions class.""" # pylint: disable=C0103,W0212

def test_find(self):
"""Verify a sharing folder can be found."""
def test_find_dropbox(self):
"""Verify a sharing folder can be found for Dropbox."""
temp = tempfile.mkdtemp()
os.makedirs(os.path.join(temp, 'Dropbox', 'DropTheBeat'))
try:
Expand All @@ -29,6 +29,26 @@ def test_find(self):
finally:
shutil.rmtree(temp)

def test_find_drive(self):
"""Verify a sharing folder can be found for Google Drive."""
temp = tempfile.mkdtemp()
os.makedirs(os.path.join(temp, 'Drive', 'DropTheBeat'))
try:
path = share.find(top=temp)
self.assertTrue(os.path.isdir(path))
finally:
shutil.rmtree(temp)

@patch('dtb.share.SHARE_DEPTH', 2)
def test_find_depth(self):
"""Verify a sharing folder is not found below the depth."""
temp = tempfile.mkdtemp()
os.makedirs(os.path.join(temp, 'Drive', 'a', 'b', 'DropTheBeat'))
try:
self.assertRaises(OSError, share.find, top=temp)
finally:
shutil.rmtree(temp)

@patch('os.path.isdir', Mock(return_value=False))
def test_find_no_home(self):
"""Verify an error occurs when no home directory is found."""
Expand Down

0 comments on commit bf3c467

Please sign in to comment.