X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=blobdiff_plain;f=apex%2Fbuild_utils.py;h=1c413dfdc56c2fd41f2510afc27a3c036bf21ef5;hb=f6dbb3929d904b4d5a9ee01f8270051e29ac1ec3;hp=66a63d37387ac3786ca68984ed0002ecb379086b;hpb=3aa975e7f5b73f9caa4f759926cc8710b3b0fd92;p=apex.git diff --git a/apex/build_utils.py b/apex/build_utils.py index 66a63d37..1c413dfd 100644 --- a/apex/build_utils.py +++ b/apex/build_utils.py @@ -16,9 +16,41 @@ import re import shutil import sys +from apex.common import constants as con from urllib.parse import quote_plus +def get_change(url, repo, branch, change_id): + """ + Fetches a change from upstream repo + :param url: URL of upstream gerrit + :param repo: name of repo + :param branch: branch of repo + :param change_id: SHA change id + :return: change if found and not abandoned, closed, or merged + """ + rest = GerritRestAPI(url=url) + change_path = "{}~{}~{}".format(quote_plus(repo), quote_plus(branch), + change_id) + change_str = "changes/{}?o=CURRENT_REVISION".format(change_path) + change = rest.get(change_str) + try: + assert change['status'] not in 'ABANDONED' 'CLOSED', \ + 'Change {} is in {} state'.format(change_id, change['status']) + if change['status'] == 'MERGED': + logging.info('Change {} is merged, ignoring...' + .format(change_id)) + return None + else: + return change + + except KeyError: + logging.error('Failed to get valid change data structure from url ' + '{}/{}, data returned: \n{}' + .format(change_id, change_str, change)) + raise + + def clone_fork(args): ref = None logging.info("Cloning {}".format(args.repo)) @@ -36,26 +68,11 @@ def clone_fork(args): if m: change_id = m.group(1) logging.info("Using change ID {} from {}".format(change_id, args.repo)) - rest = GerritRestAPI(url=args.url) - change_path = "{}~{}~{}".format(args.repo, quote_plus(args.branch), - change_id) - change_str = "changes/{}?o=CURRENT_REVISION".format(change_path) - change = rest.get(change_str) - try: - assert change['status'] not in 'ABANDONED' 'CLOSED',\ - 'Change {} is in {} state'.format(change_id, change['status']) - if change['status'] == 'MERGED': - logging.info('Change {} is merged, ignoring...' - .format(change_id)) - else: - current_revision = change['current_revision'] - ref = change['revisions'][current_revision]['ref'] - logging.info('setting ref to {}'.format(ref)) - except KeyError: - logging.error('Failed to get valid change data structure from url ' - '{}/{}, data returned: \n{}' - .format(change_id, change_str, change)) - raise + change = get_change(args.url, args.repo, args.branch, change_id) + if change: + current_revision = change['current_revision'] + ref = change['revisions'][current_revision]['ref'] + logging.info('setting ref to {}'.format(ref)) # remove existing file or directory named repo if os.path.exists(args.repo): @@ -73,6 +90,44 @@ def clone_fork(args): logging.info('Checked out commit:\n{}'.format(ws.head.commit.message)) +def strip_patch_sections(patch, sections=['releasenotes']): + """ + Removes patch sections from a diff which contain a file path + :param patch: patch to strip + :param sections: list of keywords to use to strip out of the patch file + :return: stripped patch + """ + + append_line = True + tmp_patch = [] + for line in patch.split("\n"): + if re.match('diff\s', line): + for section in sections: + if re.search(section, line): + logging.debug("Stripping {} from patch: {}".format( + section, line)) + append_line = False + break + else: + append_line = True + if append_line: + tmp_patch.append(line) + return '\n'.join(tmp_patch) + + +def get_patch(change_id, repo, branch, url=con.OPENSTACK_GERRIT): + logging.info("Fetching patch for change id {}".format(change_id)) + change = get_change(url, repo, branch, change_id) + if change: + current_revision = change['current_revision'] + rest = GerritRestAPI(url=url) + change_path = "{}~{}~{}".format(quote_plus(repo), quote_plus(branch), + change_id) + patch_url = "changes/{}/revisions/{}/patch".format(change_path, + current_revision) + return strip_patch_sections(rest.get(patch_url)) + + def get_parser(): parser = argparse.ArgumentParser() parser.add_argument('--debug', action='store_true', default=False,