diff options
author | Daniel Firth <dan.firth@codethink.co.uk> | 2016-12-05 11:08:59 +0000 |
---|---|---|
committer | Ben Brown <ben.brown@codethink.co.uk> | 2017-07-12 20:51:25 +0100 |
commit | 98f7dd8e055d34340fa87d742d8aecb722c7ca31 (patch) | |
tree | 9aaedd832c5cefe0bc7754bdb791752d2abd746c | |
parent | 3aa94b01386236f3c82729a55512ddb8a819894d (diff) | |
download | ybd-98f7dd8e055d34340fa87d742d8aecb722c7ca31.tar.gz |
Add get_transport_info helper method for normalizing repo urls, names and landing directory.lc/get_transport_info
-rw-r--r-- | ybd/repos.py | 32 |
1 files changed, 21 insertions, 11 deletions
diff --git a/ybd/repos.py b/ybd/repos.py index 643367e..0a24b6b 100644 --- a/ybd/repos.py +++ b/ybd/repos.py @@ -62,6 +62,16 @@ def get_repo_name(repo): return ''.join([transl(x) for x in url]) +def get_transport_info(repo): + '''Helper function to generate a dictionary of three useful values from + a given repository url: the normalized url, the normalized name, and + the full location of the repository in the gits directory.''' + i = {'url': get_repo_url(repo), + 'name': get_repo_name(repo)} + i.update({'dir': os.path.join(app.config['gits'], i['name'])}) + return i + + def get_version(gitdir, ref='HEAD'): try: with app.chdir(gitdir), open(os.devnull, "w") as fnull: @@ -89,13 +99,14 @@ def get_last_tag(gitdir): def get_tree(dn): + info = get_transport_info(dn['repo']) ref = str(dn.get('sha', str(dn.get('ref')))) track = app.config.get('track-branches') if dn.get('unpetrify-ref') and track: if track is True or dn['path'] in track: ref = str(dn['unpetrify-ref']) - gitdir = os.path.join(app.config['gits'], get_repo_name(dn['repo'])) + gitdir = info['dir'] if dn['repo'].startswith('file://') or dn['repo'].startswith('/'): gitdir = dn['repo'].replace('file://', '') if not os.path.isdir(gitdir): @@ -103,7 +114,7 @@ def get_tree(dn): if not os.path.exists(gitdir): try: - params = {'repo': get_repo_url(dn['repo']), 'ref': ref} + params = {'repo': info['url'], 'ref': ref} r = requests.get(url=app.config['tree-server'], params=params) return r.json()['tree'], r.json()['sha1'] except: @@ -134,9 +145,9 @@ def get_tree(dn): def mirror(name, repo): tempfile.tempdir = app.config['tmp'] - repo_url = get_repo_url(repo) + info = get_transport_info(repo) try: - tar_file = get_repo_name(repo_url) + '.tar' + tar_file = info['name'] + '.tar' app.log(name, 'Try fetching tarball %s' % tar_file) tmpdir = tempfile.mkdtemp() # try tarball first @@ -148,20 +159,19 @@ def mirror(name, repo): os.remove(tar_file) update_mirror(name, repo, tmpdir) except: - app.log(name, 'Try git clone from', repo_url) + app.log(name, 'Try git clone from', info['url']) tmpdir = tempfile.mkdtemp() with open(os.devnull, "w") as fnull: - if call(['git', 'clone', '--mirror', '-n', repo_url, tmpdir]): + if call(['git', 'clone', '--mirror', '-n', info['url'], tmpdir]): app.log(name, 'Failed to clone', repo, exit=True) with app.chdir(tmpdir): if call(['git', 'rev-parse']): app.log(name, 'Problem mirroring git repo at', tmpdir, exit=True) - gitdir = os.path.join(app.config['gits'], get_repo_name(repo)) try: - shutil.move(tmpdir, gitdir) - app.log(name, 'Git repo is mirrored at', gitdir) + shutil.move(tmpdir, info['dir']) + app.log(name, 'Git repo is mirrored at', info['dir']) except: pass @@ -217,7 +227,7 @@ def fetch_lfs_binaries(name, checkout): def _checkout(name, repo, ref, checkout): - gitdir = os.path.join(app.config['gits'], get_repo_name(repo)) + gitdir = get_transport_info(repo)['dir'] if not os.path.exists(gitdir): mirror(name, repo) elif not mirror_has_ref(gitdir, ref): @@ -261,7 +271,7 @@ def extract_commit(name, repo, ref, target_dir): function is much quicker when you don't need to copy the whole repo into target_dir. ''' - gitdir = os.path.join(app.config['gits'], get_repo_name(repo)) + gitdir = get_transport_info(repo)['dir'] if not os.path.exists(gitdir): mirror(name, repo) elif not mirror_has_ref(gitdir, ref): |