From a834565c816ba0a108de349812c76d417e8ab9f7 Mon Sep 17 00:00:00 2001 From: Zack Cerza Date: Thu, 1 Dec 2016 11:17:20 -0700 Subject: [PATCH] repo_utils: Give cloned repos more unique names Signed-off-by: Zack Cerza --- teuthology/repo_utils.py | 28 +++++++++++++++++++++++++--- teuthology/test/test_repo_utils.py | 13 ++++++++++++- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/teuthology/repo_utils.py b/teuthology/repo_utils.py index 93ac41bf22..82a76455b8 100644 --- a/teuthology/repo_utils.py +++ b/teuthology/repo_utils.py @@ -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. diff --git a/teuthology/test/test_repo_utils.py b/teuthology/test/test_repo_utils.py index 75def4ced5..41542e0b0f 100644 --- a/teuthology/test/test_repo_utils.py +++ b/teuthology/test/test_repo_utils.py @@ -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 -- 2.39.5