main.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #!/usr/bin/env python3
  2. # SPDX-License-Identifier: GPL-2.0+
  3. #
  4. # Copyright (c) 2011 The Chromium OS Authors.
  5. #
  6. """See README for more information"""
  7. from argparse import ArgumentParser
  8. import os
  9. import re
  10. import sys
  11. import unittest
  12. if __name__ == "__main__":
  13. # Allow 'from patman import xxx to work'
  14. our_path = os.path.dirname(os.path.realpath(__file__))
  15. sys.path.append(os.path.join(our_path, '..'))
  16. # Our modules
  17. from patman import command
  18. from patman import control
  19. from patman import gitutil
  20. from patman import project
  21. from patman import settings
  22. from patman import terminal
  23. from patman import test_util
  24. from patman import test_checkpatch
  25. def AddCommonArgs(parser):
  26. parser.add_argument('-b', '--branch', type=str,
  27. help="Branch to process (by default, the current branch)")
  28. parser.add_argument('-c', '--count', dest='count', type=int,
  29. default=-1, help='Automatically create patches from top n commits')
  30. parser.add_argument('-e', '--end', type=int, default=0,
  31. help='Commits to skip at end of patch list')
  32. parser.add_argument('-s', '--start', dest='start', type=int,
  33. default=0, help='Commit to start creating patches from (0 = HEAD)')
  34. epilog = '''Create patches from commits in a branch, check them and email them
  35. as specified by tags you place in the commits. Use -n to do a dry run first.'''
  36. parser = ArgumentParser(epilog=epilog)
  37. subparsers = parser.add_subparsers(dest='cmd')
  38. send = subparsers.add_parser('send')
  39. send.add_argument('-H', '--full-help', action='store_true', dest='full_help',
  40. default=False, help='Display the README file')
  41. send.add_argument('-i', '--ignore-errors', action='store_true',
  42. dest='ignore_errors', default=False,
  43. help='Send patches email even if patch errors are found')
  44. send.add_argument('-l', '--limit-cc', dest='limit', type=int, default=None,
  45. help='Limit the cc list to LIMIT entries [default: %(default)s]')
  46. send.add_argument('-m', '--no-maintainers', action='store_false',
  47. dest='add_maintainers', default=True,
  48. help="Don't cc the file maintainers automatically")
  49. send.add_argument('-n', '--dry-run', action='store_true', dest='dry_run',
  50. default=False, help="Do a dry run (create but don't email patches)")
  51. send.add_argument('-p', '--project', default=project.DetectProject(),
  52. help="Project name; affects default option values and "
  53. "aliases [default: %(default)s]")
  54. send.add_argument('-r', '--in-reply-to', type=str, action='store',
  55. help="Message ID that this series is in reply to")
  56. send.add_argument('-t', '--ignore-bad-tags', action='store_true',
  57. default=False, help='Ignore bad tags / aliases')
  58. send.add_argument('-v', '--verbose', action='store_true', dest='verbose',
  59. default=False, help='Verbose output of errors and warnings')
  60. send.add_argument('-T', '--thread', action='store_true', dest='thread',
  61. default=False, help='Create patches as a single thread')
  62. send.add_argument('--cc-cmd', dest='cc_cmd', type=str, action='store',
  63. default=None, help='Output cc list for patch file (used by git)')
  64. send.add_argument('--no-binary', action='store_true', dest='ignore_binary',
  65. default=False,
  66. help="Do not output contents of changes in binary files")
  67. send.add_argument('--no-check', action='store_false', dest='check_patch',
  68. default=True,
  69. help="Don't check for patch compliance")
  70. send.add_argument('--no-tags', action='store_false', dest='process_tags',
  71. default=True, help="Don't process subject tags as aliases")
  72. send.add_argument('--smtp-server', type=str,
  73. help="Specify the SMTP server to 'git send-email'")
  74. AddCommonArgs(send)
  75. send.add_argument('patchfiles', nargs='*')
  76. test_parser = subparsers.add_parser('test', help='Run tests')
  77. AddCommonArgs(test_parser)
  78. # Parse options twice: first to get the project and second to handle
  79. # defaults properly (which depends on project).
  80. argv = sys.argv[1:]
  81. if len(argv) < 1 or argv[0].startswith('-'):
  82. argv = ['send'] + argv
  83. args = parser.parse_args(argv)
  84. if hasattr(args, 'project'):
  85. settings.Setup(gitutil, send, args.project, '')
  86. args = parser.parse_args(argv)
  87. if __name__ != "__main__":
  88. pass
  89. # Run our meagre tests
  90. if args.cmd == 'test':
  91. import doctest
  92. from patman import func_test
  93. sys.argv = [sys.argv[0]]
  94. result = unittest.TestResult()
  95. for module in (test_checkpatch.TestPatch, func_test.TestFunctional):
  96. suite = unittest.TestLoader().loadTestsFromTestCase(module)
  97. suite.run(result)
  98. for module in ['gitutil', 'settings', 'terminal']:
  99. suite = doctest.DocTestSuite(module)
  100. suite.run(result)
  101. sys.exit(test_util.ReportResult('patman', None, result))
  102. # Process commits, produce patches files, check them, email them
  103. elif args.cmd == 'send':
  104. # Called from git with a patch filename as argument
  105. # Printout a list of additional CC recipients for this patch
  106. if args.cc_cmd:
  107. fd = open(args.cc_cmd, 'r')
  108. re_line = re.compile('(\S*) (.*)')
  109. for line in fd.readlines():
  110. match = re_line.match(line)
  111. if match and match.group(1) == args.patchfiles[0]:
  112. for cc in match.group(2).split('\0'):
  113. cc = cc.strip()
  114. if cc:
  115. print(cc)
  116. fd.close()
  117. elif args.full_help:
  118. pager = os.getenv('PAGER')
  119. if not pager:
  120. pager = 'more'
  121. fname = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])),
  122. 'README')
  123. command.Run(pager, fname)
  124. else:
  125. control.send(args)