download.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. # Copyright (c) 2009, 2011 Google Inc. All rights reserved.
  2. # Copyright (c) 2009 Apple Inc. All rights reserved.
  3. #
  4. # Redistribution and use in source and binary forms, with or without
  5. # modification, are permitted provided that the following conditions are
  6. # met:
  7. #
  8. # * Redistributions of source code must retain the above copyright
  9. # notice, this list of conditions and the following disclaimer.
  10. # * Redistributions in binary form must reproduce the above
  11. # copyright notice, this list of conditions and the following disclaimer
  12. # in the documentation and/or other materials provided with the
  13. # distribution.
  14. # * Neither the name of Google Inc. nor the names of its
  15. # contributors may be used to endorse or promote products derived from
  16. # this software without specific prior written permission.
  17. #
  18. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. import logging
  30. from webkitpy.tool import steps
  31. from webkitpy.common.checkout.changelog import ChangeLog
  32. from webkitpy.common.config import urls
  33. from webkitpy.common.system.executive import ScriptError
  34. from webkitpy.tool.commands.abstractsequencedcommand import AbstractSequencedCommand
  35. from webkitpy.tool.commands.stepsequence import StepSequence
  36. from webkitpy.tool.comments import bug_comment_from_commit_text
  37. from webkitpy.tool.grammar import pluralize
  38. from webkitpy.tool.multicommandtool import Command
  39. _log = logging.getLogger(__name__)
  40. class Clean(AbstractSequencedCommand):
  41. name = "clean"
  42. help_text = "Clean the working copy"
  43. steps = [
  44. steps.DiscardLocalChanges,
  45. ]
  46. def _prepare_state(self, options, args, tool):
  47. options.force_clean = True
  48. class Update(AbstractSequencedCommand):
  49. name = "update"
  50. help_text = "Update working copy (used internally)"
  51. steps = [
  52. steps.DiscardLocalChanges,
  53. steps.Update,
  54. ]
  55. class Build(AbstractSequencedCommand):
  56. name = "build"
  57. help_text = "Update working copy and build"
  58. steps = [
  59. steps.DiscardLocalChanges,
  60. steps.Update,
  61. steps.Build,
  62. ]
  63. def _prepare_state(self, options, args, tool):
  64. options.build = True
  65. class BuildAndTest(AbstractSequencedCommand):
  66. name = "build-and-test"
  67. help_text = "Update working copy, build, and run the tests"
  68. steps = [
  69. steps.DiscardLocalChanges,
  70. steps.Update,
  71. steps.Build,
  72. steps.RunTests,
  73. ]
  74. class Land(AbstractSequencedCommand):
  75. name = "land"
  76. help_text = "Land the current working directory diff and updates the associated bug if any"
  77. argument_names = "[BUGID]"
  78. show_in_main_help = True
  79. steps = [
  80. steps.AddSvnMimetypeForPng,
  81. steps.UpdateChangeLogsWithReviewer,
  82. steps.ValidateReviewer,
  83. steps.ValidateChangeLogs, # We do this after UpdateChangeLogsWithReviewer to avoid not having to cache the diff twice.
  84. steps.Build,
  85. steps.RunTests,
  86. steps.Commit,
  87. steps.CloseBugForLandDiff,
  88. ]
  89. long_help = """land commits the current working copy diff (just as svn or git commit would).
  90. land will NOT build and run the tests before committing, but you can use the --build option for that.
  91. If a bug id is provided, or one can be found in the ChangeLog land will update the bug after committing."""
  92. def _prepare_state(self, options, args, tool):
  93. changed_files = self._tool.scm().changed_files(options.git_commit)
  94. return {
  95. "changed_files": changed_files,
  96. "bug_id": (args and args[0]) or tool.checkout().bug_id_for_this_commit(options.git_commit, changed_files),
  97. }
  98. class LandCowhand(AbstractSequencedCommand):
  99. # Gender-blind term for cowboy, see: http://en.wiktionary.org/wiki/cowhand
  100. name = "land-cowhand"
  101. help_text = "Prepares a ChangeLog and lands the current working directory diff."
  102. steps = [
  103. steps.PrepareChangeLog,
  104. steps.EditChangeLog,
  105. steps.CheckStyle,
  106. steps.ConfirmDiff,
  107. steps.Build,
  108. steps.RunTests,
  109. steps.Commit,
  110. steps.CloseBugForLandDiff,
  111. ]
  112. def _prepare_state(self, options, args, tool):
  113. options.check_style_filter = "-changelog"
  114. class LandCowboy(LandCowhand):
  115. name = "land-cowboy"
  116. def _prepare_state(self, options, args, tool):
  117. _log.warning("land-cowboy is deprecated, use land-cowhand instead.")
  118. LandCowhand._prepare_state(self, options, args, tool)
  119. class CheckStyleLocal(AbstractSequencedCommand):
  120. name = "check-style-local"
  121. help_text = "Run check-webkit-style on the current working directory diff"
  122. steps = [
  123. steps.CheckStyle,
  124. ]
  125. class AbstractPatchProcessingCommand(Command):
  126. # Subclasses must implement the methods below. We don't declare them here
  127. # because we want to be able to implement them with mix-ins.
  128. #
  129. # pylint: disable=E1101
  130. # def _fetch_list_of_patches_to_process(self, options, args, tool):
  131. # def _prepare_to_process(self, options, args, tool):
  132. # def _process_patch(self, options, args, tool):
  133. @staticmethod
  134. def _collect_patches_by_bug(patches):
  135. bugs_to_patches = {}
  136. for patch in patches:
  137. bugs_to_patches[patch.bug_id()] = bugs_to_patches.get(patch.bug_id(), []) + [patch]
  138. return bugs_to_patches
  139. def execute(self, options, args, tool):
  140. self._prepare_to_process(options, args, tool)
  141. patches = self._fetch_list_of_patches_to_process(options, args, tool)
  142. # It's nice to print out total statistics.
  143. bugs_to_patches = self._collect_patches_by_bug(patches)
  144. _log.info("Processing %s from %s." % (pluralize("patch", len(patches)), pluralize("bug", len(bugs_to_patches))))
  145. for patch in patches:
  146. self._process_patch(patch, options, args, tool)
  147. class AbstractPatchSequencingCommand(AbstractPatchProcessingCommand):
  148. prepare_steps = None
  149. main_steps = None
  150. def __init__(self):
  151. options = []
  152. self._prepare_sequence = StepSequence(self.prepare_steps)
  153. self._main_sequence = StepSequence(self.main_steps)
  154. options = sorted(set(self._prepare_sequence.options() + self._main_sequence.options()))
  155. AbstractPatchProcessingCommand.__init__(self, options)
  156. def _prepare_to_process(self, options, args, tool):
  157. self._prepare_sequence.run_and_handle_errors(tool, options)
  158. def _process_patch(self, patch, options, args, tool):
  159. state = { "patch" : patch }
  160. self._main_sequence.run_and_handle_errors(tool, options, state)
  161. class ProcessAttachmentsMixin(object):
  162. def _fetch_list_of_patches_to_process(self, options, args, tool):
  163. return map(lambda patch_id: tool.bugs.fetch_attachment(patch_id), args)
  164. class ProcessBugsMixin(object):
  165. def _fetch_list_of_patches_to_process(self, options, args, tool):
  166. all_patches = []
  167. for bug_id in args:
  168. patches = tool.bugs.fetch_bug(bug_id).reviewed_patches()
  169. _log.info("%s found on bug %s." % (pluralize("reviewed patch", len(patches)), bug_id))
  170. all_patches += patches
  171. if not all_patches:
  172. _log.info("No reviewed patches found, looking for unreviewed patches.")
  173. for bug_id in args:
  174. patches = tool.bugs.fetch_bug(bug_id).patches()
  175. _log.info("%s found on bug %s." % (pluralize("patch", len(patches)), bug_id))
  176. all_patches += patches
  177. return all_patches
  178. class ProcessURLsMixin(object):
  179. def _fetch_list_of_patches_to_process(self, options, args, tool):
  180. all_patches = []
  181. for url in args:
  182. bug_id = urls.parse_bug_id(url)
  183. if bug_id:
  184. patches = tool.bugs.fetch_bug(bug_id).patches()
  185. _log.info("%s found on bug %s." % (pluralize("patch", len(patches)), bug_id))
  186. all_patches += patches
  187. attachment_id = urls.parse_attachment_id(url)
  188. if attachment_id:
  189. all_patches += tool.bugs.fetch_attachment(attachment_id)
  190. return all_patches
  191. class CheckStyle(AbstractPatchSequencingCommand, ProcessAttachmentsMixin):
  192. name = "check-style"
  193. help_text = "Run check-webkit-style on the specified attachments"
  194. argument_names = "ATTACHMENT_ID [ATTACHMENT_IDS]"
  195. main_steps = [
  196. steps.DiscardLocalChanges,
  197. steps.Update,
  198. steps.ApplyPatch,
  199. steps.CheckStyle,
  200. ]
  201. class BuildAttachment(AbstractPatchSequencingCommand, ProcessAttachmentsMixin):
  202. name = "build-attachment"
  203. help_text = "Apply and build patches from bugzilla"
  204. argument_names = "ATTACHMENT_ID [ATTACHMENT_IDS]"
  205. main_steps = [
  206. steps.DiscardLocalChanges,
  207. steps.Update,
  208. steps.ApplyPatch,
  209. steps.Build,
  210. ]
  211. class BuildAndTestAttachment(AbstractPatchSequencingCommand, ProcessAttachmentsMixin):
  212. name = "build-and-test-attachment"
  213. help_text = "Apply, build, and test patches from bugzilla"
  214. argument_names = "ATTACHMENT_ID [ATTACHMENT_IDS]"
  215. main_steps = [
  216. steps.DiscardLocalChanges,
  217. steps.Update,
  218. steps.ApplyPatch,
  219. steps.Build,
  220. steps.RunTests,
  221. ]
  222. class AbstractPatchApplyingCommand(AbstractPatchSequencingCommand):
  223. prepare_steps = [
  224. steps.EnsureLocalCommitIfNeeded,
  225. steps.CleanWorkingDirectory,
  226. steps.Update,
  227. ]
  228. main_steps = [
  229. steps.ApplyPatchWithLocalCommit,
  230. ]
  231. long_help = """Updates the working copy.
  232. Downloads and applies the patches, creating local commits if necessary."""
  233. class ApplyAttachment(AbstractPatchApplyingCommand, ProcessAttachmentsMixin):
  234. name = "apply-attachment"
  235. help_text = "Apply an attachment to the local working directory"
  236. argument_names = "ATTACHMENT_ID [ATTACHMENT_IDS]"
  237. show_in_main_help = True
  238. class ApplyFromBug(AbstractPatchApplyingCommand, ProcessBugsMixin):
  239. name = "apply-from-bug"
  240. help_text = "Apply reviewed patches from provided bugs to the local working directory"
  241. argument_names = "BUGID [BUGIDS]"
  242. show_in_main_help = True
  243. class ApplyWatchList(AbstractPatchSequencingCommand, ProcessAttachmentsMixin):
  244. name = "apply-watchlist"
  245. help_text = "Applies the watchlist to the specified attachments"
  246. argument_names = "ATTACHMENT_ID [ATTACHMENT_IDS]"
  247. main_steps = [
  248. steps.DiscardLocalChanges,
  249. steps.Update,
  250. steps.ApplyPatch,
  251. steps.ApplyWatchList,
  252. ]
  253. long_help = """"Applies the watchlist to the specified attachments.
  254. Downloads the attachment, applies it locally, runs the watchlist against it, and updates the bug with the result."""
  255. class AbstractPatchLandingCommand(AbstractPatchSequencingCommand):
  256. main_steps = [
  257. steps.DiscardLocalChanges,
  258. steps.Update,
  259. steps.ApplyPatch,
  260. steps.ValidateChangeLogs,
  261. steps.ValidateReviewer,
  262. steps.Build,
  263. steps.RunTests,
  264. steps.Commit,
  265. steps.ClosePatch,
  266. steps.CloseBug,
  267. ]
  268. long_help = """Checks to make sure builders are green.
  269. Updates the working copy.
  270. Applies the patch.
  271. Builds.
  272. Runs the layout tests.
  273. Commits the patch.
  274. Clears the flags on the patch.
  275. Closes the bug if no patches are marked for review."""
  276. class LandAttachment(AbstractPatchLandingCommand, ProcessAttachmentsMixin):
  277. name = "land-attachment"
  278. help_text = "Land patches from bugzilla, optionally building and testing them first"
  279. argument_names = "ATTACHMENT_ID [ATTACHMENT_IDS]"
  280. show_in_main_help = True
  281. class LandFromBug(AbstractPatchLandingCommand, ProcessBugsMixin):
  282. name = "land-from-bug"
  283. help_text = "Land all patches on the given bugs, optionally building and testing them first"
  284. argument_names = "BUGID [BUGIDS]"
  285. show_in_main_help = True
  286. class LandFromURL(AbstractPatchLandingCommand, ProcessURLsMixin):
  287. name = "land-from-url"
  288. help_text = "Land all patches on the given URLs, optionally building and testing them first"
  289. argument_names = "URL [URLS]"
  290. class ValidateChangelog(AbstractSequencedCommand):
  291. name = "validate-changelog"
  292. help_text = "Validate that the ChangeLogs and reviewers look reasonable"
  293. long_help = """Examines the current diff to see whether the ChangeLogs
  294. and the reviewers listed in the ChangeLogs look reasonable.
  295. """
  296. steps = [
  297. steps.ValidateChangeLogs,
  298. steps.ValidateReviewer,
  299. ]
  300. class AbstractRolloutPrepCommand(AbstractSequencedCommand):
  301. argument_names = "REVISION [REVISIONS] REASON"
  302. def _commit_info(self, revision):
  303. commit_info = self._tool.checkout().commit_info_for_revision(revision)
  304. if commit_info and commit_info.bug_id():
  305. # Note: Don't print a bug URL here because it will confuse the
  306. # SheriffBot because the SheriffBot just greps the output
  307. # of create-rollout for bug URLs. It should do better
  308. # parsing instead.
  309. _log.info("Preparing rollout for bug %s." % commit_info.bug_id())
  310. else:
  311. _log.info("Unable to parse bug number from diff.")
  312. return commit_info
  313. def _prepare_state(self, options, args, tool):
  314. revision_list = []
  315. for revision in str(args[0]).split():
  316. if revision.isdigit():
  317. revision_list.append(int(revision))
  318. else:
  319. raise ScriptError(message="Invalid svn revision number: " + revision)
  320. revision_list.sort()
  321. # We use the earliest revision for the bug info
  322. earliest_revision = revision_list[0]
  323. state = {
  324. "revision": earliest_revision,
  325. "revision_list": revision_list,
  326. "reason": args[1],
  327. }
  328. commit_info = self._commit_info(earliest_revision)
  329. if commit_info:
  330. state["bug_id"] = commit_info.bug_id()
  331. cc_list = sorted([party.bugzilla_email()
  332. for party in commit_info.responsible_parties()
  333. if party.bugzilla_email()])
  334. # FIXME: We should used the list as the canonical representation.
  335. state["bug_cc"] = ",".join(cc_list)
  336. return state
  337. class PrepareRollout(AbstractRolloutPrepCommand):
  338. name = "prepare-rollout"
  339. help_text = "Revert the given revision(s) in the working copy and prepare ChangeLogs with revert reason"
  340. long_help = """Updates the working copy.
  341. Applies the inverse diff for the provided revision(s).
  342. Creates an appropriate rollout ChangeLog, including a trac link and bug link.
  343. """
  344. steps = [
  345. steps.DiscardLocalChanges,
  346. steps.Update,
  347. steps.RevertRevision,
  348. steps.PrepareChangeLogForRevert,
  349. ]
  350. class CreateRollout(AbstractRolloutPrepCommand):
  351. name = "create-rollout"
  352. help_text = "Creates a bug to track the broken SVN revision(s) and uploads a rollout patch."
  353. steps = [
  354. steps.DiscardLocalChanges,
  355. steps.Update,
  356. steps.RevertRevision,
  357. steps.CreateBug,
  358. steps.PrepareChangeLogForRevert,
  359. steps.PostDiffForRevert,
  360. ]
  361. def _prepare_state(self, options, args, tool):
  362. state = AbstractRolloutPrepCommand._prepare_state(self, options, args, tool)
  363. # Currently, state["bug_id"] points to the bug that caused the
  364. # regression. We want to create a new bug that blocks the old bug
  365. # so we move state["bug_id"] to state["bug_blocked"] and delete the
  366. # old state["bug_id"] so that steps.CreateBug will actually create
  367. # the new bug that we want (and subsequently store its bug id into
  368. # state["bug_id"])
  369. state["bug_blocked"] = state["bug_id"]
  370. del state["bug_id"]
  371. state["bug_title"] = "REGRESSION(r%s): %s" % (state["revision"], state["reason"])
  372. state["bug_description"] = "%s broke the build:\n%s" % (urls.view_revision_url(state["revision"]), state["reason"])
  373. # FIXME: If we had more context here, we could link to other open bugs
  374. # that mention the test that regressed.
  375. if options.parent_command == "sheriff-bot":
  376. state["bug_description"] += """
  377. This is an automatic bug report generated by the sheriff-bot. If this bug
  378. report was created because of a flaky test, please file a bug for the flaky
  379. test (if we don't already have one on file) and dup this bug against that bug
  380. so that we can track how often these flaky tests case pain.
  381. "Only you can prevent forest fires." -- Smokey the Bear
  382. """
  383. return state
  384. class Rollout(AbstractRolloutPrepCommand):
  385. name = "rollout"
  386. show_in_main_help = True
  387. help_text = "Revert the given revision(s) in the working copy and optionally commit the revert and re-open the original bug"
  388. long_help = """Updates the working copy.
  389. Applies the inverse diff for the provided revision.
  390. Creates an appropriate rollout ChangeLog, including a trac link and bug link.
  391. Opens the generated ChangeLogs in $EDITOR.
  392. Shows the prepared diff for confirmation.
  393. Commits the revert and updates the bug (including re-opening the bug if necessary)."""
  394. steps = [
  395. steps.DiscardLocalChanges,
  396. steps.Update,
  397. steps.RevertRevision,
  398. steps.PrepareChangeLogForRevert,
  399. steps.EditChangeLog,
  400. steps.ConfirmDiff,
  401. steps.Build,
  402. steps.Commit,
  403. steps.ReopenBugAfterRollout,
  404. ]