]> git.apps.os.sepia.ceph.com Git - teuthology.git/commitdiff
repo_utils: Give cloned repos more unique names
authorZack Cerza <zack@redhat.com>
Thu, 1 Dec 2016 18:17:20 +0000 (11:17 -0700)
committerZack Cerza <zack@redhat.com>
Fri, 2 Dec 2016 17:27:10 +0000 (10:27 -0700)
Signed-off-by: Zack Cerza <zack@redhat.com>
teuthology/repo_utils.py
teuthology/test/test_repo_utils.py

index 93ac41bf22c693afaf8308067f200f24ad3aea93..82a76455b8b9c978d8c94d31bc0611a4be18256e 100644 (file)
@@ -240,12 +240,11 @@ def fetch_repo(url, branch, bootstrap=None, lock=True):
     :param branch:     The branch we want
     :returns:          The destination path
     """
-    # 'url/to/project.git' -> 'project'
-    name = url.split('/')[-1].split('.git')[0]
     src_base_path = config.src_base_path
     if not os.path.exists(src_base_path):
         os.mkdir(src_base_path)
-    dest_path = os.path.join(src_base_path, '%s_%s' % (name, branch))
+    dirname = '%s_%s' % (url_to_dirname(url), branch)
+    dest_path = os.path.join(src_base_path, dirname)
     # only let one worker create/update the checkout at a time
     lock_path = dest_path.rstrip('/') + '.lock'
     with FileLock(lock_path, noop=not lock):
@@ -267,6 +266,29 @@ def fetch_repo(url, branch, bootstrap=None, lock=True):
     return dest_path
 
 
+def url_to_dirname(url):
+    """
+    Given a URL, returns a string that's safe to use as a directory name.
+    Examples:
+
+        git://git.ceph.com/ceph-qa-suite.git -> git.ceph.com_ceph-qa-suite
+        https://github.com/ceph/ceph -> github.com_ceph_ceph
+        https://github.com/liewegas/ceph.git -> github.com_liewegas_ceph
+        file:///my/dir/has/ceph.git -> my_dir_has_ceph
+    """
+    # Strip protocol from left-hand side
+    string = re.match('.*://(.*)', url).groups()[0]
+    # Strip '.git' from the right-hand side
+    string = string.rstrip('.git')
+    # Replace certain characters with underscores
+    string = re.sub('[:/]', '_', string)
+    # Remove duplicate underscores
+    string = re.sub('_+', '_', string)
+    # Remove leading or trailing underscore
+    string = string.strip('_')
+    return string
+
+
 def fetch_qa_suite(branch, lock=True):
     """
     Make sure ceph-qa-suite is checked out.
index 75def4ced554fd5ee951c4c5bee589e50b8be60f..41542e0b0fa85c42f4153f416969899238f206b7 100644 (file)
@@ -1,7 +1,7 @@
 import logging
 import os
 import os.path
-from pytest import raises
+from pytest import raises, mark
 import shutil
 import subprocess
 
@@ -167,3 +167,14 @@ class TestRepoUtils(object):
                     )
             for result in p:
                 pass
+
+    URLS_AND_DIRNAMES = [
+        ('git://git.ceph.com/ceph-qa-suite.git', 'git.ceph.com_ceph-qa-suite'),
+        ('https://github.com/ceph/ceph', 'github.com_ceph_ceph'),
+        ('https://github.com/liewegas/ceph.git', 'github.com_liewegas_ceph'),
+        ('file:///my/dir/has/ceph.git', 'my_dir_has_ceph'),
+    ]
+
+    @mark.parametrize("input_, expected", URLS_AND_DIRNAMES)
+    def test_url_to_dirname(self, input_, expected):
+        assert repo_utils.url_to_dirname(input_) == expected