1#!/usr/bin/env python
2# Copyright 2014 the V8 project authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6import argparse
7import json
8import os
9import sys
10import urllib
11
12from common_includes import *
13import chromium_roll
14
15
16class CheckActiveRoll(Step):
17  MESSAGE = "Check active roll."
18
19  @staticmethod
20  def ContainsChromiumRoll(changes):
21    for change in changes:
22      if change["subject"].startswith("Update V8 to"):
23        return True
24    return False
25
26  def RunStep(self):
27    params = {
28      "closed": 3,
29      "owner": self._options.author,
30      "limit": 30,
31      "format": "json",
32    }
33    params = urllib.urlencode(params)
34    search_url = "https://codereview.chromium.org/search"
35    result = self.ReadURL(search_url, params, wait_plan=[5, 20])
36    if self.ContainsChromiumRoll(json.loads(result)["results"]):
37      print "Stop due to existing Chromium roll."
38      return True
39
40
41class DetectLastPush(Step):
42  MESSAGE = "Detect commit ID of the last push to trunk."
43
44  def RunStep(self):
45    push_hash = self.FindLastTrunkPush(
46        branch="origin/master", include_patches=True)
47    self["last_push"] = self.GetCommitPositionNumber(push_hash)
48
49
50class DetectLastRoll(Step):
51  MESSAGE = "Detect commit ID of the last Chromium roll."
52
53  def RunStep(self):
54    # Interpret the DEPS file to retrieve the v8 revision.
55    # TODO(machenbach): This should be part or the roll-deps api of
56    # depot_tools.
57    Var = lambda var: '%s'
58    exec(FileToText(os.path.join(self._options.chromium, "DEPS")))
59    last_roll = self.GetCommitPositionNumber(vars['v8_revision'])
60    # FIXME(machenbach): When rolling from bleeding edge and from trunk there
61    # be different commit numbers here. Better use version?
62    if int(last_roll) >= int(self["last_push"]):
63      print("There is no newer v8 revision than the one in Chromium (%s)."
64            % last_roll)
65      return True
66
67
68class CheckClusterFuzz(Step):
69  MESSAGE = "Check ClusterFuzz api for new problems."
70
71  def RunStep(self):
72    if not os.path.exists(self.Config("CLUSTERFUZZ_API_KEY_FILE")):
73      print "Skipping ClusterFuzz check. No api key file found."
74      return False
75    api_key = FileToText(self.Config("CLUSTERFUZZ_API_KEY_FILE"))
76    # Check for open, reproducible issues that have no associated bug.
77    result = self._side_effect_handler.ReadClusterFuzzAPI(
78        api_key, job_type="linux_asan_d8_dbg", reproducible="True",
79        open="True", bug_information="",
80        revision_greater_or_equal=str(self["last_push"]))
81    if result:
82      print "Stop due to pending ClusterFuzz issues."
83      return True
84
85
86class RollChromium(Step):
87  MESSAGE = "Roll V8 into Chromium."
88
89  def RunStep(self):
90    if self._options.roll:
91      args = [
92        "--author", self._options.author,
93        "--reviewer", self._options.reviewer,
94        "--chromium", self._options.chromium,
95        "--use-commit-queue",
96      ]
97      if self._options.sheriff:
98        args.extend([
99            "--sheriff", "--googlers-mapping", self._options.googlers_mapping])
100      if self._options.dry_run:
101        args.extend(["--dry-run"])
102      self._side_effect_handler.Call(chromium_roll.ChromiumRoll().Run, args)
103
104
105class AutoRoll(ScriptsBase):
106  def _PrepareOptions(self, parser):
107    parser.add_argument("-c", "--chromium", required=True,
108                        help=("The path to your Chromium src/ "
109                              "directory to automate the V8 roll."))
110    parser.add_argument("--roll", help="Call Chromium roll script.",
111                        default=False, action="store_true")
112
113  def _ProcessOptions(self, options):  # pragma: no cover
114    if not options.reviewer:
115      print "A reviewer (-r) is required."
116      return False
117    if not options.author:
118      print "An author (-a) is required."
119      return False
120    return True
121
122  def _Config(self):
123    return {
124      "PERSISTFILE_BASENAME": "/tmp/v8-auto-roll-tempfile",
125      "CLUSTERFUZZ_API_KEY_FILE": ".cf_api_key",
126    }
127
128  def _Steps(self):
129    return [
130      CheckActiveRoll,
131      DetectLastPush,
132      DetectLastRoll,
133      CheckClusterFuzz,
134      RollChromium,
135    ]
136
137
138if __name__ == "__main__":  # pragma: no cover
139  sys.exit(AutoRoll().Run())
140