]>
Commit | Line | Data |
---|---|---|
9a3cc9b8 | 1 | #!/usr/bin/env python3 |
801e612b ML |
2 | # |
3 | # Helper script for committing data to git and pushing upstream | |
4 | # | |
5 | # Copyright (c) 2017, Intel Corporation. | |
6 | # | |
f8c9c511 | 7 | # SPDX-License-Identifier: GPL-2.0-only |
801e612b | 8 | # |
f8c9c511 | 9 | |
801e612b | 10 | import argparse |
801e612b | 11 | import logging |
801e612b ML |
12 | import os |
13 | import re | |
14 | import sys | |
801e612b ML |
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 | |
4820ca2b | 25 | import oeqa.utils.gitarchive as gitarchive |
801e612b ML |
26 | |
27 | # Setup logging | |
28 | logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") | |
29 | log = logging.getLogger() | |
30 | ||
31 | ||
801e612b ML |
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") | |
995cb3bf ML |
45 | parser.add_argument('--bare', action='store_true', |
46 | help="Initialize a bare repository when creating a " | |
47 | "new one") | |
801e612b ML |
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)") | |
fd125cf6 ML |
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") | |
0ef7c143 ML |
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.") | |
801e612b ML |
80 | parser.add_argument('data_dir', metavar='DATA_DIR', |
81 | help="Data to commit") | |
82 | return parser.parse_args(argv) | |
83 | ||
9a3cc9b8 YEP |
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 "" | |
801e612b ML |
91 | |
92 | def main(argv=None): | |
801e612b ML |
93 | args = parse_args(argv) |
94 | if args.debug: | |
95 | log.setLevel(logging.DEBUG) | |
96 | ||
97 | try: | |
801e612b ML |
98 | # Get keywords to be used in tag and branch names and messages |
99 | metadata = metadata_from_bb() | |
9a3cc9b8 YEP |
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'])} | |
801e612b | 105 | |
4820ca2b RP |
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) | |
801e612b | 110 | |
4820ca2b | 111 | except gitarchive.ArchiveError as err: |
801e612b ML |
112 | log.error(str(err)) |
113 | return 1 | |
114 | ||
115 | return 0 | |
116 | ||
117 | if __name__ == "__main__": | |
118 | sys.exit(main()) |