patman.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #!/usr/bin/env python2
  2. #
  3. # Copyright (c) 2011 The Chromium OS Authors.
  4. #
  5. # SPDX-License-Identifier: GPL-2.0+
  6. #
  7. """See README for more information"""
  8. from optparse import OptionParser
  9. import os
  10. import re
  11. import sys
  12. import unittest
  13. # Our modules
  14. try:
  15. from patman import checkpatch, command, gitutil, patchstream, \
  16. project, settings, terminal, test
  17. except ImportError:
  18. import checkpatch
  19. import command
  20. import gitutil
  21. import patchstream
  22. import project
  23. import settings
  24. import terminal
  25. import test
  26. parser = OptionParser()
  27. parser.add_option('-H', '--full-help', action='store_true', dest='full_help',
  28. default=False, help='Display the README file')
  29. parser.add_option('-c', '--count', dest='count', type='int',
  30. default=-1, help='Automatically create patches from top n commits')
  31. parser.add_option('-i', '--ignore-errors', action='store_true',
  32. dest='ignore_errors', default=False,
  33. help='Send patches email even if patch errors are found')
  34. parser.add_option('-m', '--no-maintainers', action='store_false',
  35. dest='add_maintainers', default=True,
  36. help="Don't cc the file maintainers automatically")
  37. parser.add_option('-n', '--dry-run', action='store_true', dest='dry_run',
  38. default=False, help="Do a dry run (create but don't email patches)")
  39. parser.add_option('-p', '--project', default=project.DetectProject(),
  40. help="Project name; affects default option values and "
  41. "aliases [default: %default]")
  42. parser.add_option('-r', '--in-reply-to', type='string', action='store',
  43. help="Message ID that this series is in reply to")
  44. parser.add_option('-s', '--start', dest='start', type='int',
  45. default=0, help='Commit to start creating patches from (0 = HEAD)')
  46. parser.add_option('-t', '--ignore-bad-tags', action='store_true',
  47. default=False, help='Ignore bad tags / aliases')
  48. parser.add_option('--test', action='store_true', dest='test',
  49. default=False, help='run tests')
  50. parser.add_option('-v', '--verbose', action='store_true', dest='verbose',
  51. default=False, help='Verbose output of errors and warnings')
  52. parser.add_option('--cc-cmd', dest='cc_cmd', type='string', action='store',
  53. default=None, help='Output cc list for patch file (used by git)')
  54. parser.add_option('--no-check', action='store_false', dest='check_patch',
  55. default=True,
  56. help="Don't check for patch compliance")
  57. parser.add_option('--no-tags', action='store_false', dest='process_tags',
  58. default=True, help="Don't process subject tags as aliaes")
  59. parser.add_option('-T', '--thread', action='store_true', dest='thread',
  60. default=False, help='Create patches as a single thread')
  61. parser.usage += """
  62. Create patches from commits in a branch, check them and email them as
  63. specified by tags you place in the commits. Use -n to do a dry run first."""
  64. # Parse options twice: first to get the project and second to handle
  65. # defaults properly (which depends on project).
  66. (options, args) = parser.parse_args()
  67. settings.Setup(parser, options.project, '')
  68. (options, args) = parser.parse_args()
  69. if __name__ != "__main__":
  70. pass
  71. # Run our meagre tests
  72. elif options.test:
  73. import doctest
  74. import func_test
  75. sys.argv = [sys.argv[0]]
  76. result = unittest.TestResult()
  77. for module in (test.TestPatch, func_test.TestFunctional):
  78. suite = unittest.TestLoader().loadTestsFromTestCase(module)
  79. suite.run(result)
  80. for module in ['gitutil', 'settings']:
  81. suite = doctest.DocTestSuite(module)
  82. suite.run(result)
  83. # TODO: Surely we can just 'print' result?
  84. print(result)
  85. for test, err in result.errors:
  86. print(err)
  87. for test, err in result.failures:
  88. print(err)
  89. # Called from git with a patch filename as argument
  90. # Printout a list of additional CC recipients for this patch
  91. elif options.cc_cmd:
  92. fd = open(options.cc_cmd, 'r')
  93. re_line = re.compile('(\S*) (.*)')
  94. for line in fd.readlines():
  95. match = re_line.match(line)
  96. if match and match.group(1) == args[0]:
  97. for cc in match.group(2).split(', '):
  98. cc = cc.strip()
  99. if cc:
  100. print(cc)
  101. fd.close()
  102. elif options.full_help:
  103. pager = os.getenv('PAGER')
  104. if not pager:
  105. pager = 'more'
  106. fname = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])),
  107. 'README')
  108. command.Run(pager, fname)
  109. # Process commits, produce patches files, check them, email them
  110. else:
  111. gitutil.Setup()
  112. if options.count == -1:
  113. # Work out how many patches to send if we can
  114. options.count = gitutil.CountCommitsToBranch() - options.start
  115. col = terminal.Color()
  116. if not options.count:
  117. str = 'No commits found to process - please use -c flag'
  118. sys.exit(col.Color(col.RED, str))
  119. # Read the metadata from the commits
  120. if options.count:
  121. series = patchstream.GetMetaData(options.start, options.count)
  122. cover_fname, args = gitutil.CreatePatches(options.start, options.count,
  123. series)
  124. # Fix up the patch files to our liking, and insert the cover letter
  125. patchstream.FixPatches(series, args)
  126. if cover_fname and series.get('cover'):
  127. patchstream.InsertCoverLetter(cover_fname, series, options.count)
  128. # Do a few checks on the series
  129. series.DoChecks()
  130. # Check the patches, and run them through 'git am' just to be sure
  131. if options.check_patch:
  132. ok = checkpatch.CheckPatches(options.verbose, args)
  133. else:
  134. ok = True
  135. cc_file = series.MakeCcFile(options.process_tags, cover_fname,
  136. not options.ignore_bad_tags,
  137. options.add_maintainers)
  138. # Email the patches out (giving the user time to check / cancel)
  139. cmd = ''
  140. its_a_go = ok or options.ignore_errors
  141. if its_a_go:
  142. cmd = gitutil.EmailPatches(series, cover_fname, args,
  143. options.dry_run, not options.ignore_bad_tags, cc_file,
  144. in_reply_to=options.in_reply_to, thread=options.thread)
  145. else:
  146. print(col.Color(col.RED, "Not sending emails due to errors/warnings"))
  147. # For a dry run, just show our actions as a sanity check
  148. if options.dry_run:
  149. series.ShowActions(args, cmd, options.process_tags)
  150. if not its_a_go:
  151. print(col.Color(col.RED, "Email would not be sent"))
  152. os.remove(cc_file)