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