diff --git a/dtb/share.py b/dtb/share.py index 253e498..97efbec 100755 --- a/dtb/share.py +++ b/dtb/share.py @@ -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): @@ -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 diff --git a/dtb/test/test_share.py b/dtb/test/test_share.py index afb1dac..bc766be 100644 --- a/dtb/test/test_share.py +++ b/dtb/test/test_share.py @@ -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: @@ -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."""