blob: ab1c2b9ad4cf294ab27bced28e597c8de8988ca3 [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#
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#
16import argparse
Brad Bishop6e60e8b2018-02-01 10:27:11 -050017import logging
Brad Bishop6e60e8b2018-02-01 10:27:11 -050018import os
19import re
20import sys
Brad Bishop6e60e8b2018-02-01 10:27:11 -050021
22# Import oe and bitbake libs
23scripts_path = os.path.dirname(os.path.realpath(__file__))
24sys.path.append(os.path.join(scripts_path, 'lib'))
25import scriptpath
26scriptpath.add_bitbake_lib_path()
27scriptpath.add_oe_lib_path()
28
29from oeqa.utils.git import GitRepo, GitError
30from oeqa.utils.metadata import metadata_from_bb
Andrew Geissler99467da2019-02-25 18:54:23 -060031import oeqa.utils.gitarchive as gitarchive
Brad Bishop6e60e8b2018-02-01 10:27:11 -050032
33# Setup logging
34logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s")
35log = logging.getLogger()
36
37
Brad Bishop6e60e8b2018-02-01 10:27:11 -050038def 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 Bishop977dc1a2019-02-06 16:01:43 -050090def 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 Bishop6e60e8b2018-02-01 10:27:11 -050097
98def main(argv=None):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050099 args = parse_args(argv)
100 if args.debug:
101 log.setLevel(logging.DEBUG)
102
103 try:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500104 # Get keywords to be used in tag and branch names and messages
105 metadata = metadata_from_bb()
Brad Bishop977dc1a2019-02-06 16:01:43 -0500106 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 Bishop6e60e8b2018-02-01 10:27:11 -0500111
Andrew Geissler99467da2019-02-25 18:54:23 -0600112 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 Bishop6e60e8b2018-02-01 10:27:11 -0500116
Andrew Geissler99467da2019-02-25 18:54:23 -0600117 except gitarchive.ArchiveError as err:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500118 log.error(str(err))
119 return 1
120
121 return 0
122
123if __name__ == "__main__":
124 sys.exit(main())