blob: 9305ed0b0f9717fdf7b7534133b0b411681bd58b [file] [log] [blame]
Brad Bishop977dc1a2019-02-06 16:01:43 -05001#!/usr/bin/env python3
Brad Bishop6e60e8b2018-02-01 10:27:11 -05002#
3# Helper script for committing data to git and pushing upstream
4#
5# Copyright (c) 2017, Intel Corporation.
6#
Brad Bishopc342db32019-05-15 21:57:59 -04007# SPDX-License-Identifier: GPL-2.0-only
Brad Bishop6e60e8b2018-02-01 10:27:11 -05008#
Brad Bishopc342db32019-05-15 21:57:59 -04009
Brad Bishop6e60e8b2018-02-01 10:27:11 -050010import argparse
Brad Bishop6e60e8b2018-02-01 10:27:11 -050011import logging
Brad Bishop6e60e8b2018-02-01 10:27:11 -050012import os
13import re
14import sys
Brad Bishop6e60e8b2018-02-01 10:27:11 -050015
16# Import oe and bitbake libs
17scripts_path = os.path.dirname(os.path.realpath(__file__))
18sys.path.append(os.path.join(scripts_path, 'lib'))
19import scriptpath
20scriptpath.add_bitbake_lib_path()
21scriptpath.add_oe_lib_path()
22
23from oeqa.utils.git import GitRepo, GitError
24from oeqa.utils.metadata import metadata_from_bb
Andrew Geissler99467da2019-02-25 18:54:23 -060025import oeqa.utils.gitarchive as gitarchive
Brad Bishop6e60e8b2018-02-01 10:27:11 -050026
27# Setup logging
28logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s")
29log = logging.getLogger()
30
31
Brad Bishop6e60e8b2018-02-01 10:27:11 -050032def 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 Bishop977dc1a2019-02-06 16:01:43 -050084def 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 Bishop6e60e8b2018-02-01 10:27:11 -050091
92def main(argv=None):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050093 args = parse_args(argv)
94 if args.debug:
95 log.setLevel(logging.DEBUG)
96
97 try:
Brad Bishop6e60e8b2018-02-01 10:27:11 -050098 # Get keywords to be used in tag and branch names and messages
99 metadata = metadata_from_bb()
Brad Bishop977dc1a2019-02-06 16:01:43 -0500100 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 Bishop6e60e8b2018-02-01 10:27:11 -0500105
Andrew Geissler99467da2019-02-25 18:54:23 -0600106 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 Bishop6e60e8b2018-02-01 10:27:11 -0500110
Andrew Geissler99467da2019-02-25 18:54:23 -0600111 except gitarchive.ArchiveError as err:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500112 log.error(str(err))
113 return 1
114
115 return 0
116
117if __name__ == "__main__":
118 sys.exit(main())