oe-git-archive 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #!/usr/bin/env python3
  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
  17. import logging
  18. import os
  19. import re
  20. import sys
  21. # Import oe and bitbake libs
  22. scripts_path = os.path.dirname(os.path.realpath(__file__))
  23. sys.path.append(os.path.join(scripts_path, 'lib'))
  24. import scriptpath
  25. scriptpath.add_bitbake_lib_path()
  26. scriptpath.add_oe_lib_path()
  27. from oeqa.utils.git import GitRepo, GitError
  28. from oeqa.utils.metadata import metadata_from_bb
  29. import oeqa.utils.gitarchive as gitarchive
  30. # Setup logging
  31. logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s")
  32. log = logging.getLogger()
  33. def parse_args(argv):
  34. """Parse command line arguments"""
  35. parser = argparse.ArgumentParser(
  36. description="Commit data to git and push upstream",
  37. formatter_class=argparse.ArgumentDefaultsHelpFormatter)
  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. def get_nested(d, list_of_keys):
  84. try:
  85. for k in list_of_keys:
  86. d = d[k]
  87. return d
  88. except KeyError:
  89. return ""
  90. def main(argv=None):
  91. args = parse_args(argv)
  92. if args.debug:
  93. log.setLevel(logging.DEBUG)
  94. try:
  95. # Get keywords to be used in tag and branch names and messages
  96. metadata = metadata_from_bb()
  97. keywords = {'hostname': get_nested(metadata, ['hostname']),
  98. 'branch': get_nested(metadata, ['layers', 'meta', 'branch']),
  99. 'commit': get_nested(metadata, ['layers', 'meta', 'commit']),
  100. 'commit_count': get_nested(metadata, ['layers', 'meta', 'commit_count']),
  101. 'machine': get_nested(metadata, ['config', 'MACHINE'])}
  102. gitarchive.gitarchive(args.data_dir, args.git_dir, args.no_create, args.bare,
  103. args.commit_msg_subject.strip(), args.commit_msg_body, args.branch_name,
  104. args.no_tag, args.tag_name, args.tag_msg_subject, args.tag_msg_body,
  105. args.exclude, args.notes, args.push, keywords, log)
  106. except gitarchive.ArchiveError as err:
  107. log.error(str(err))
  108. return 1
  109. return 0
  110. if __name__ == "__main__":
  111. sys.exit(main())