From 99eeba1f44b8452e23457f77b86134972672bdf8 Mon Sep 17 00:00:00 2001 From: Sam Thursfield Date: Mon, 11 Apr 2016 12:10:11 +0100 Subject: migrations/008: Make it work for chunks whose ref is not a commit SHA1 Reported here: Change-Id: I08aa2f2f205f805661dfd2043eb100baaf2eb72b --- migrations/008-submodules-in-strata.py | 51 ++++++++++++++++++++++++++++++---- 1 file changed, 45 insertions(+), 6 deletions(-) diff --git a/migrations/008-submodules-in-strata.py b/migrations/008-submodules-in-strata.py index 33404f2..73e57e0 100755 --- a/migrations/008-submodules-in-strata.py +++ b/migrations/008-submodules-in-strata.py @@ -81,14 +81,47 @@ def get_repo_name(repo): ## End of code based on ybd repos.py -def get_toplevel_file_list_from_repo(url, ref): + +# This function is taken from morphlib/git.py. +def is_valid_sha1(ref): + '''Checks whether a string is a valid SHA1.''' + return len(ref) == 40 and all(x in string.hexdigits for x in ref) + + +def resolve_ref_to_commit(url, ref): + '''Resolve ref to a SHA1, using the remote morph-cache server. + + This is only useful for repos hosted on a Baserock Trove, but it does work + without needing to clone the whole repo. + + ''' + response = requests.get( + GIT_CACHE_SERVER_URL + '1.0/sha1s', + params={'repo': url, 'ref': ref}, + headers={'Accept': 'application/json'}, + timeout=9) + logging.debug("Got response: %s" % response) + try: + response.raise_for_status() + commit = response.json()['sha1'] + except Exception as e: + raise RuntimeError( + "Unexpected response from server %s for repo %s: %s" % + (GIT_CACHE_SERVER_URL, url, e.message)) + return commit + + +def get_toplevel_file_list_from_repo(url, commit): '''Try to list the set of files in the root directory of the repo at 'url'. + This is only useful for repos hosted on a Baserock Trove, but it does work + without needing to clone the whole repo. + ''' try: response = requests.get( GIT_CACHE_SERVER_URL + '1.0/trees', - params={'repo': url, 'ref': ref}, + params={'repo': url, 'ref': commit}, headers={'Accept': 'application/json'}, timeout=9) logging.debug("Got response: %s" % response) @@ -185,12 +218,18 @@ def add_submodules_to_strata(contents, filename): if 'submodules' in chunk_ref: continue try: + if is_valid_sha1(chunk_git_ref): + chunk_git_commit = chunk_git_ref + else: + chunk_git_commit = resolve_ref_to_commit (chunk_git_url, chunk_git_ref) + toplevel_file_list = get_toplevel_file_list_from_repo( - chunk_git_url, chunk_git_ref) + chunk_git_url, chunk_git_commit) except Exception as e: + logging.debug(e) message = ( - "Unable to look up repo %s on remote Git server %s. Check that " - "the repo URL is correct." % (chunk_git_url, TROVE_HOST)) + "Unable to look up repo %s on remote Git server %s. Check " + "that the repo URL is correct." % (chunk_git_url, TROVE_HOST)) warning = ( "If you are using a Trove that is not %s, please edit the " "TROVE_HOST constant in this script and run it again." % @@ -206,7 +245,7 @@ def add_submodules_to_strata(contents, filename): "%s: got file list %s", chunk_git_url, toplevel_file_list) if u'.gitmodules' in toplevel_file_list: - submodules = submodules_to_dict(chunk_git_url, chunk_git_ref) + submodules = submodules_to_dict(chunk_git_url, chunk_git_commit) if submodules: chunk_ref['submodules'] = submodules changed = True -- cgit v1.2.1