1*4882a593Smuzhiyun# 2*4882a593Smuzhiyun# Helper functions for committing data to git and pushing upstream 3*4882a593Smuzhiyun# 4*4882a593Smuzhiyun# Copyright (c) 2017, Intel Corporation. 5*4882a593Smuzhiyun# Copyright (c) 2019, Linux Foundation 6*4882a593Smuzhiyun# 7*4882a593Smuzhiyun# SPDX-License-Identifier: GPL-2.0-only 8*4882a593Smuzhiyun# 9*4882a593Smuzhiyun 10*4882a593Smuzhiyunimport os 11*4882a593Smuzhiyunimport re 12*4882a593Smuzhiyunimport sys 13*4882a593Smuzhiyunfrom operator import attrgetter 14*4882a593Smuzhiyunfrom collections import namedtuple 15*4882a593Smuzhiyunfrom oeqa.utils.git import GitRepo, GitError 16*4882a593Smuzhiyun 17*4882a593Smuzhiyunclass ArchiveError(Exception): 18*4882a593Smuzhiyun """Internal error handling of this script""" 19*4882a593Smuzhiyun 20*4882a593Smuzhiyundef format_str(string, fields): 21*4882a593Smuzhiyun """Format string using the given fields (dict)""" 22*4882a593Smuzhiyun try: 23*4882a593Smuzhiyun return string.format(**fields) 24*4882a593Smuzhiyun except KeyError as err: 25*4882a593Smuzhiyun raise ArchiveError("Unable to expand string '{}': unknown field {} " 26*4882a593Smuzhiyun "(valid fields are: {})".format( 27*4882a593Smuzhiyun string, err, ', '.join(sorted(fields.keys())))) 28*4882a593Smuzhiyun 29*4882a593Smuzhiyun 30*4882a593Smuzhiyundef init_git_repo(path, no_create, bare, log): 31*4882a593Smuzhiyun """Initialize local Git repository""" 32*4882a593Smuzhiyun path = os.path.abspath(path) 33*4882a593Smuzhiyun if os.path.isfile(path): 34*4882a593Smuzhiyun raise ArchiveError("Invalid Git repo at {}: path exists but is not a " 35*4882a593Smuzhiyun "directory".format(path)) 36*4882a593Smuzhiyun if not os.path.isdir(path) or not os.listdir(path): 37*4882a593Smuzhiyun if no_create: 38*4882a593Smuzhiyun raise ArchiveError("No git repo at {}, refusing to create " 39*4882a593Smuzhiyun "one".format(path)) 40*4882a593Smuzhiyun if not os.path.isdir(path): 41*4882a593Smuzhiyun try: 42*4882a593Smuzhiyun os.mkdir(path) 43*4882a593Smuzhiyun except (FileNotFoundError, PermissionError) as err: 44*4882a593Smuzhiyun raise ArchiveError("Failed to mkdir {}: {}".format(path, err)) 45*4882a593Smuzhiyun if not os.listdir(path): 46*4882a593Smuzhiyun log.info("Initializing a new Git repo at %s", path) 47*4882a593Smuzhiyun repo = GitRepo.init(path, bare) 48*4882a593Smuzhiyun try: 49*4882a593Smuzhiyun repo = GitRepo(path, is_topdir=True) 50*4882a593Smuzhiyun except GitError: 51*4882a593Smuzhiyun raise ArchiveError("Non-empty directory that is not a Git repository " 52*4882a593Smuzhiyun "at {}\nPlease specify an existing Git repository, " 53*4882a593Smuzhiyun "an empty directory or a non-existing directory " 54*4882a593Smuzhiyun "path.".format(path)) 55*4882a593Smuzhiyun return repo 56*4882a593Smuzhiyun 57*4882a593Smuzhiyun 58*4882a593Smuzhiyundef git_commit_data(repo, data_dir, branch, message, exclude, notes, log): 59*4882a593Smuzhiyun """Commit data into a Git repository""" 60*4882a593Smuzhiyun log.info("Committing data into to branch %s", branch) 61*4882a593Smuzhiyun tmp_index = os.path.join(repo.git_dir, 'index.oe-git-archive') 62*4882a593Smuzhiyun try: 63*4882a593Smuzhiyun # Create new tree object from the data 64*4882a593Smuzhiyun env_update = {'GIT_INDEX_FILE': tmp_index, 65*4882a593Smuzhiyun 'GIT_WORK_TREE': os.path.abspath(data_dir)} 66*4882a593Smuzhiyun repo.run_cmd('add .', env_update) 67*4882a593Smuzhiyun 68*4882a593Smuzhiyun # Remove files that are excluded 69*4882a593Smuzhiyun if exclude: 70*4882a593Smuzhiyun repo.run_cmd(['rm', '--cached'] + [f for f in exclude], env_update) 71*4882a593Smuzhiyun 72*4882a593Smuzhiyun tree = repo.run_cmd('write-tree', env_update) 73*4882a593Smuzhiyun 74*4882a593Smuzhiyun # Create new commit object from the tree 75*4882a593Smuzhiyun parent = repo.rev_parse(branch) 76*4882a593Smuzhiyun if not parent: 77*4882a593Smuzhiyun parent = repo.rev_parse("origin/" + branch) 78*4882a593Smuzhiyun git_cmd = ['commit-tree', tree, '-m', message] 79*4882a593Smuzhiyun if parent: 80*4882a593Smuzhiyun git_cmd += ['-p', parent] 81*4882a593Smuzhiyun commit = repo.run_cmd(git_cmd, env_update) 82*4882a593Smuzhiyun 83*4882a593Smuzhiyun # Create git notes 84*4882a593Smuzhiyun for ref, filename in notes: 85*4882a593Smuzhiyun ref = ref.format(branch_name=branch) 86*4882a593Smuzhiyun repo.run_cmd(['notes', '--ref', ref, 'add', 87*4882a593Smuzhiyun '-F', os.path.abspath(filename), commit]) 88*4882a593Smuzhiyun 89*4882a593Smuzhiyun # Update branch head 90*4882a593Smuzhiyun git_cmd = ['update-ref', 'refs/heads/' + branch, commit] 91*4882a593Smuzhiyun repo.run_cmd(git_cmd) 92*4882a593Smuzhiyun 93*4882a593Smuzhiyun # Update current HEAD, if we're on branch 'branch' 94*4882a593Smuzhiyun if not repo.bare and repo.get_current_branch() == branch: 95*4882a593Smuzhiyun log.info("Updating %s HEAD to latest commit", repo.top_dir) 96*4882a593Smuzhiyun repo.run_cmd('reset --hard') 97*4882a593Smuzhiyun 98*4882a593Smuzhiyun return commit 99*4882a593Smuzhiyun finally: 100*4882a593Smuzhiyun if os.path.exists(tmp_index): 101*4882a593Smuzhiyun os.unlink(tmp_index) 102*4882a593Smuzhiyun 103*4882a593Smuzhiyun 104*4882a593Smuzhiyundef expand_tag_strings(repo, name_pattern, msg_subj_pattern, msg_body_pattern, 105*4882a593Smuzhiyun keywords): 106*4882a593Smuzhiyun """Generate tag name and message, with support for running id number""" 107*4882a593Smuzhiyun keyws = keywords.copy() 108*4882a593Smuzhiyun # Tag number is handled specially: if not defined, we autoincrement it 109*4882a593Smuzhiyun if 'tag_number' not in keyws: 110*4882a593Smuzhiyun # Fill in all other fields than 'tag_number' 111*4882a593Smuzhiyun keyws['tag_number'] = '{tag_number}' 112*4882a593Smuzhiyun tag_re = format_str(name_pattern, keyws) 113*4882a593Smuzhiyun # Replace parentheses for proper regex matching 114*4882a593Smuzhiyun tag_re = tag_re.replace('(', '\(').replace(')', '\)') + '$' 115*4882a593Smuzhiyun # Inject regex group pattern for 'tag_number' 116*4882a593Smuzhiyun tag_re = tag_re.format(tag_number='(?P<tag_number>[0-9]{1,5})') 117*4882a593Smuzhiyun 118*4882a593Smuzhiyun keyws['tag_number'] = 0 119*4882a593Smuzhiyun for existing_tag in repo.run_cmd('tag').splitlines(): 120*4882a593Smuzhiyun match = re.match(tag_re, existing_tag) 121*4882a593Smuzhiyun 122*4882a593Smuzhiyun if match and int(match.group('tag_number')) >= keyws['tag_number']: 123*4882a593Smuzhiyun keyws['tag_number'] = int(match.group('tag_number')) + 1 124*4882a593Smuzhiyun 125*4882a593Smuzhiyun tag_name = format_str(name_pattern, keyws) 126*4882a593Smuzhiyun msg_subj= format_str(msg_subj_pattern.strip(), keyws) 127*4882a593Smuzhiyun msg_body = format_str(msg_body_pattern, keyws) 128*4882a593Smuzhiyun return tag_name, msg_subj + '\n\n' + msg_body 129*4882a593Smuzhiyun 130*4882a593Smuzhiyundef gitarchive(data_dir, git_dir, no_create, bare, commit_msg_subject, commit_msg_body, branch_name, no_tag, tagname, tag_msg_subject, tag_msg_body, exclude, notes, push, keywords, log): 131*4882a593Smuzhiyun 132*4882a593Smuzhiyun if not os.path.isdir(data_dir): 133*4882a593Smuzhiyun raise ArchiveError("Not a directory: {}".format(data_dir)) 134*4882a593Smuzhiyun 135*4882a593Smuzhiyun data_repo = init_git_repo(git_dir, no_create, bare, log) 136*4882a593Smuzhiyun 137*4882a593Smuzhiyun # Expand strings early in order to avoid getting into inconsistent 138*4882a593Smuzhiyun # state (e.g. no tag even if data was committed) 139*4882a593Smuzhiyun commit_msg = format_str(commit_msg_subject.strip(), keywords) 140*4882a593Smuzhiyun commit_msg += '\n\n' + format_str(commit_msg_body, keywords) 141*4882a593Smuzhiyun branch_name = format_str(branch_name, keywords) 142*4882a593Smuzhiyun tag_name = None 143*4882a593Smuzhiyun if not no_tag and tagname: 144*4882a593Smuzhiyun tag_name, tag_msg = expand_tag_strings(data_repo, tagname, 145*4882a593Smuzhiyun tag_msg_subject, 146*4882a593Smuzhiyun tag_msg_body, keywords) 147*4882a593Smuzhiyun 148*4882a593Smuzhiyun # Commit data 149*4882a593Smuzhiyun commit = git_commit_data(data_repo, data_dir, branch_name, 150*4882a593Smuzhiyun commit_msg, exclude, notes, log) 151*4882a593Smuzhiyun 152*4882a593Smuzhiyun # Create tag 153*4882a593Smuzhiyun if tag_name: 154*4882a593Smuzhiyun log.info("Creating tag %s", tag_name) 155*4882a593Smuzhiyun data_repo.run_cmd(['tag', '-a', '-m', tag_msg, tag_name, commit]) 156*4882a593Smuzhiyun 157*4882a593Smuzhiyun # Push data to remote 158*4882a593Smuzhiyun if push: 159*4882a593Smuzhiyun cmd = ['push', '--tags'] 160*4882a593Smuzhiyun # If no remote is given we push with the default settings from 161*4882a593Smuzhiyun # gitconfig 162*4882a593Smuzhiyun if push is not True: 163*4882a593Smuzhiyun notes_refs = ['refs/notes/' + ref.format(branch_name=branch_name) 164*4882a593Smuzhiyun for ref, _ in notes] 165*4882a593Smuzhiyun cmd.extend([push, branch_name] + notes_refs) 166*4882a593Smuzhiyun log.info("Pushing data to remote") 167*4882a593Smuzhiyun data_repo.run_cmd(cmd) 168*4882a593Smuzhiyun 169*4882a593Smuzhiyun# Container class for tester revisions 170*4882a593SmuzhiyunTestedRev = namedtuple('TestedRev', 'commit commit_number tags') 171*4882a593Smuzhiyun 172*4882a593Smuzhiyundef get_test_runs(log, repo, tag_name, **kwargs): 173*4882a593Smuzhiyun """Get a sorted list of test runs, matching given pattern""" 174*4882a593Smuzhiyun # First, get field names from the tag name pattern 175*4882a593Smuzhiyun field_names = [m.group(1) for m in re.finditer(r'{(\w+)}', tag_name)] 176*4882a593Smuzhiyun undef_fields = [f for f in field_names if f not in kwargs.keys()] 177*4882a593Smuzhiyun 178*4882a593Smuzhiyun # Fields for formatting tag name pattern 179*4882a593Smuzhiyun str_fields = dict([(f, '*') for f in field_names]) 180*4882a593Smuzhiyun str_fields.update(kwargs) 181*4882a593Smuzhiyun 182*4882a593Smuzhiyun # Get a list of all matching tags 183*4882a593Smuzhiyun tag_pattern = tag_name.format(**str_fields) 184*4882a593Smuzhiyun tags = repo.run_cmd(['tag', '-l', tag_pattern]).splitlines() 185*4882a593Smuzhiyun log.debug("Found %d tags matching pattern '%s'", len(tags), tag_pattern) 186*4882a593Smuzhiyun 187*4882a593Smuzhiyun # Parse undefined fields from tag names 188*4882a593Smuzhiyun str_fields = dict([(f, r'(?P<{}>[\w\-.()]+)'.format(f)) for f in field_names]) 189*4882a593Smuzhiyun str_fields['branch'] = r'(?P<branch>[\w\-.()/]+)' 190*4882a593Smuzhiyun str_fields['commit'] = '(?P<commit>[0-9a-f]{7,40})' 191*4882a593Smuzhiyun str_fields['commit_number'] = '(?P<commit_number>[0-9]{1,7})' 192*4882a593Smuzhiyun str_fields['tag_number'] = '(?P<tag_number>[0-9]{1,5})' 193*4882a593Smuzhiyun # escape parenthesis in fields in order to not messa up the regexp 194*4882a593Smuzhiyun fixed_fields = dict([(k, v.replace('(', r'\(').replace(')', r'\)')) for k, v in kwargs.items()]) 195*4882a593Smuzhiyun str_fields.update(fixed_fields) 196*4882a593Smuzhiyun tag_re = re.compile(tag_name.format(**str_fields)) 197*4882a593Smuzhiyun 198*4882a593Smuzhiyun # Parse fields from tags 199*4882a593Smuzhiyun revs = [] 200*4882a593Smuzhiyun for tag in tags: 201*4882a593Smuzhiyun m = tag_re.match(tag) 202*4882a593Smuzhiyun groups = m.groupdict() 203*4882a593Smuzhiyun revs.append([groups[f] for f in undef_fields] + [tag]) 204*4882a593Smuzhiyun 205*4882a593Smuzhiyun # Return field names and a sorted list of revs 206*4882a593Smuzhiyun return undef_fields, sorted(revs) 207*4882a593Smuzhiyun 208*4882a593Smuzhiyundef get_test_revs(log, repo, tag_name, **kwargs): 209*4882a593Smuzhiyun """Get list of all tested revisions""" 210*4882a593Smuzhiyun fields, runs = get_test_runs(log, repo, tag_name, **kwargs) 211*4882a593Smuzhiyun 212*4882a593Smuzhiyun revs = {} 213*4882a593Smuzhiyun commit_i = fields.index('commit') 214*4882a593Smuzhiyun commit_num_i = fields.index('commit_number') 215*4882a593Smuzhiyun for run in runs: 216*4882a593Smuzhiyun commit = run[commit_i] 217*4882a593Smuzhiyun commit_num = run[commit_num_i] 218*4882a593Smuzhiyun tag = run[-1] 219*4882a593Smuzhiyun if not commit in revs: 220*4882a593Smuzhiyun revs[commit] = TestedRev(commit, commit_num, [tag]) 221*4882a593Smuzhiyun else: 222*4882a593Smuzhiyun assert commit_num == revs[commit].commit_number, "Commit numbers do not match" 223*4882a593Smuzhiyun revs[commit].tags.append(tag) 224*4882a593Smuzhiyun 225*4882a593Smuzhiyun # Return in sorted table 226*4882a593Smuzhiyun revs = sorted(revs.values(), key=attrgetter('commit_number')) 227*4882a593Smuzhiyun log.debug("Found %d tested revisions:\n %s", len(revs), 228*4882a593Smuzhiyun "\n ".join(['{} ({})'.format(rev.commit_number, rev.commit) for rev in revs])) 229*4882a593Smuzhiyun return revs 230*4882a593Smuzhiyun 231*4882a593Smuzhiyundef rev_find(revs, attr, val): 232*4882a593Smuzhiyun """Search from a list of TestedRev""" 233*4882a593Smuzhiyun for i, rev in enumerate(revs): 234*4882a593Smuzhiyun if getattr(rev, attr) == val: 235*4882a593Smuzhiyun return i 236*4882a593Smuzhiyun raise ValueError("Unable to find '{}' value '{}'".format(attr, val)) 237*4882a593Smuzhiyun 238