1#!/usr/bin/env python
2# Copyright 2013 the V8 project authors. All rights reserved.
3# Redistribution and use in source and binary forms, with or without
4# modification, are permitted provided that the following conditions are
5# met:
6#
7#     * Redistributions of source code must retain the above copyright
8#       notice, this list of conditions and the following disclaimer.
9#     * Redistributions in binary form must reproduce the above
10#       copyright notice, this list of conditions and the following
11#       disclaimer in the documentation and/or other materials provided
12#       with the distribution.
13#     * Neither the name of Google Inc. nor the names of its
14#       contributors may be used to endorse or promote products derived
15#       from this software without specific prior written permission.
16#
17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29import argparse
30import json
31import os
32import re
33import sys
34import urllib
35
36from common_includes import *
37import push_to_trunk
38
39PUSH_MESSAGE_RE = re.compile(r".* \(based on bleeding_edge revision r(\d+)\)$")
40
41class Preparation(Step):
42  MESSAGE = "Preparation."
43
44  def RunStep(self):
45    self.InitialEnvironmentChecks(self.default_cwd)
46    self.CommonPrepare()
47
48
49class CheckAutoPushSettings(Step):
50  MESSAGE = "Checking settings file."
51
52  def RunStep(self):
53    settings_file = os.path.realpath(self.Config("SETTINGS_LOCATION"))
54    if os.path.exists(settings_file):
55      settings_dict = json.loads(FileToText(settings_file))
56      if settings_dict.get("enable_auto_roll") is False:
57        self.Die("Push to trunk disabled by auto-roll settings file: %s"
58                 % settings_file)
59
60
61class CheckTreeStatus(Step):
62  MESSAGE = "Checking v8 tree status message."
63
64  def RunStep(self):
65    status_url = "https://v8-status.appspot.com/current?format=json"
66    status_json = self.ReadURL(status_url, wait_plan=[5, 20, 300, 300])
67    self["tree_message"] = json.loads(status_json)["message"]
68    if re.search(r"nopush|no push", self["tree_message"], flags=re.I):
69      self.Die("Push to trunk disabled by tree state: %s"
70               % self["tree_message"])
71
72
73class FetchLKGR(Step):
74  MESSAGE = "Fetching V8 LKGR."
75
76  def RunStep(self):
77    lkgr_url = "https://v8-status.appspot.com/lkgr"
78    # Retry several times since app engine might have issues.
79    self["lkgr"] = self.ReadURL(lkgr_url, wait_plan=[5, 20, 300, 300])
80
81
82class CheckLastPush(Step):
83  MESSAGE = "Checking last V8 push to trunk."
84
85  def RunStep(self):
86    last_push = self.FindLastTrunkPush()
87
88    # Retrieve the bleeding edge revision of the last push from the text in
89    # the push commit message.
90    last_push_title = self.GitLog(n=1, format="%s", git_hash=last_push)
91    last_push_be = PUSH_MESSAGE_RE.match(last_push_title).group(1)
92
93    if not last_push_be:  # pragma: no cover
94      self.Die("Could not retrieve bleeding edge revision for trunk push %s"
95               % last_push)
96
97    # TODO(machenbach): This metric counts all revisions. It could be
98    # improved by counting only the revisions on bleeding_edge.
99    if int(self["lkgr"]) - int(last_push_be) < 10:  # pragma: no cover
100      # This makes sure the script doesn't push twice in a row when the cron
101      # job retries several times.
102      self.Die("Last push too recently: %s" % last_push_be)
103
104
105class PushToTrunk(Step):
106  MESSAGE = "Pushing to trunk if specified."
107
108  def RunStep(self):
109    print "Pushing lkgr %s to trunk." % self["lkgr"]
110
111    # TODO(machenbach): Update the script before calling it.
112    if self._options.push:
113      self._side_effect_handler.Call(
114          push_to_trunk.PushToTrunk().Run,
115          ["--author", self._options.author,
116           "--reviewer", self._options.reviewer,
117           "--revision", self["lkgr"],
118           "--force"])
119
120
121class AutoPush(ScriptsBase):
122  def _PrepareOptions(self, parser):
123    parser.add_argument("-p", "--push",
124                        help="Push to trunk. Dry run if unspecified.",
125                        default=False, action="store_true")
126
127  def _ProcessOptions(self, options):
128    if not options.author or not options.reviewer:  # pragma: no cover
129      print "You need to specify author and reviewer."
130      return False
131    options.requires_editor = False
132    return True
133
134  def _Config(self):
135    return {
136      "PERSISTFILE_BASENAME": "/tmp/v8-auto-push-tempfile",
137      "SETTINGS_LOCATION": "~/.auto-roll",
138    }
139
140  def _Steps(self):
141    return [
142      Preparation,
143      CheckAutoPushSettings,
144      CheckTreeStatus,
145      FetchLKGR,
146      CheckLastPush,
147      PushToTrunk,
148    ]
149
150
151if __name__ == "__main__":  # pragma: no cover
152  sys.exit(AutoPush().Run())
153