: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):
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.
import logging
import os
import os.path
-from pytest import raises
+from pytest import raises, mark
import shutil
import subprocess
)
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