| Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 2 | # | 
|  | 3 | # Helper script for committing data to git and pushing upstream | 
|  | 4 | # | 
|  | 5 | # Copyright (c) 2017, Intel Corporation. | 
|  | 6 | # | 
| Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 7 | # SPDX-License-Identifier: GPL-2.0-only | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 8 | # | 
| Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 9 |  | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 10 | import argparse | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 11 | import logging | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 12 | import os | 
|  | 13 | import re | 
|  | 14 | import sys | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 15 |  | 
|  | 16 | # Import oe and bitbake libs | 
|  | 17 | scripts_path = os.path.dirname(os.path.realpath(__file__)) | 
|  | 18 | sys.path.append(os.path.join(scripts_path, 'lib')) | 
|  | 19 | import scriptpath | 
|  | 20 | scriptpath.add_bitbake_lib_path() | 
|  | 21 | scriptpath.add_oe_lib_path() | 
|  | 22 |  | 
|  | 23 | from oeqa.utils.git import GitRepo, GitError | 
|  | 24 | from oeqa.utils.metadata import metadata_from_bb | 
| Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 25 | import oeqa.utils.gitarchive as gitarchive | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 26 |  | 
|  | 27 | # Setup logging | 
|  | 28 | logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") | 
|  | 29 | log = logging.getLogger() | 
|  | 30 |  | 
|  | 31 |  | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 32 | def parse_args(argv): | 
|  | 33 | """Parse command line arguments""" | 
|  | 34 | parser = argparse.ArgumentParser( | 
|  | 35 | description="Commit data to git and push upstream", | 
|  | 36 | formatter_class=argparse.ArgumentDefaultsHelpFormatter) | 
|  | 37 |  | 
|  | 38 | parser.add_argument('--debug', '-D', action='store_true', | 
|  | 39 | help="Verbose logging") | 
|  | 40 | parser.add_argument('--git-dir', '-g', required=True, | 
|  | 41 | help="Local git directory to use") | 
|  | 42 | parser.add_argument('--no-create', action='store_true', | 
|  | 43 | help="If GIT_DIR is not a valid Git repository, do not " | 
|  | 44 | "try to create one") | 
|  | 45 | parser.add_argument('--bare', action='store_true', | 
|  | 46 | help="Initialize a bare repository when creating a " | 
|  | 47 | "new one") | 
|  | 48 | parser.add_argument('--push', '-p', nargs='?', default=False, const=True, | 
|  | 49 | help="Push to remote") | 
|  | 50 | parser.add_argument('--branch-name', '-b', | 
|  | 51 | default='{hostname}/{branch}/{machine}', | 
|  | 52 | help="Git branch name (pattern) to use") | 
|  | 53 | parser.add_argument('--no-tag', action='store_true', | 
|  | 54 | help="Do not create Git tag") | 
|  | 55 | parser.add_argument('--tag-name', '-t', | 
|  | 56 | default='{hostname}/{branch}/{machine}/{commit_count}-g{commit}/{tag_number}', | 
|  | 57 | help="Tag name (pattern) to use") | 
|  | 58 | parser.add_argument('--commit-msg-subject', | 
|  | 59 | default='Results of {branch}:{commit} on {hostname}', | 
|  | 60 | help="Subject line (pattern) to use in the commit message") | 
|  | 61 | parser.add_argument('--commit-msg-body', | 
|  | 62 | default='branch: {branch}\ncommit: {commit}\nhostname: {hostname}', | 
|  | 63 | help="Commit message body (pattern)") | 
|  | 64 | parser.add_argument('--tag-msg-subject', | 
|  | 65 | default='Test run #{tag_number} of {branch}:{commit} on {hostname}', | 
|  | 66 | help="Subject line (pattern) of the tag message") | 
|  | 67 | parser.add_argument('--tag-msg-body', | 
|  | 68 | default='', | 
|  | 69 | help="Tag message body (pattern)") | 
|  | 70 | parser.add_argument('--exclude', action='append', default=[], | 
|  | 71 | help="Glob to exclude files from the commit. Relative " | 
|  | 72 | "to DATA_DIR. May be specified multiple times") | 
|  | 73 | parser.add_argument('--notes', nargs=2, action='append', default=[], | 
|  | 74 | metavar=('GIT_REF', 'FILE'), | 
|  | 75 | help="Add a file as a note under refs/notes/GIT_REF. " | 
|  | 76 | "{branch_name} in GIT_REF will be expanded to the " | 
|  | 77 | "actual target branch name (specified by " | 
|  | 78 | "--branch-name). This option may be specified " | 
|  | 79 | "multiple times.") | 
|  | 80 | parser.add_argument('data_dir', metavar='DATA_DIR', | 
|  | 81 | help="Data to commit") | 
|  | 82 | return parser.parse_args(argv) | 
|  | 83 |  | 
| Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 84 | def get_nested(d, list_of_keys): | 
|  | 85 | try: | 
|  | 86 | for k in list_of_keys: | 
|  | 87 | d = d[k] | 
|  | 88 | return d | 
|  | 89 | except KeyError: | 
|  | 90 | return "" | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 91 |  | 
|  | 92 | def main(argv=None): | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 93 | args = parse_args(argv) | 
|  | 94 | if args.debug: | 
|  | 95 | log.setLevel(logging.DEBUG) | 
|  | 96 |  | 
|  | 97 | try: | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 98 | # Get keywords to be used in tag and branch names and messages | 
|  | 99 | metadata = metadata_from_bb() | 
| Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 100 | keywords = {'hostname': get_nested(metadata, ['hostname']), | 
|  | 101 | 'branch': get_nested(metadata, ['layers', 'meta', 'branch']), | 
|  | 102 | 'commit': get_nested(metadata, ['layers', 'meta', 'commit']), | 
|  | 103 | 'commit_count': get_nested(metadata, ['layers', 'meta', 'commit_count']), | 
|  | 104 | 'machine': get_nested(metadata, ['config', 'MACHINE'])} | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 105 |  | 
| Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 106 | gitarchive.gitarchive(args.data_dir, args.git_dir, args.no_create, args.bare, | 
|  | 107 | args.commit_msg_subject.strip(), args.commit_msg_body, args.branch_name, | 
|  | 108 | args.no_tag, args.tag_name, args.tag_msg_subject, args.tag_msg_body, | 
|  | 109 | args.exclude, args.notes, args.push, keywords, log) | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 110 |  | 
| Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 111 | except gitarchive.ArchiveError as err: | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 112 | log.error(str(err)) | 
|  | 113 | return 1 | 
|  | 114 |  | 
|  | 115 | return 0 | 
|  | 116 |  | 
|  | 117 | if __name__ == "__main__": | 
|  | 118 | sys.exit(main()) |