1# Copyright (c) 2012 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Presubmit script for changes affecting extensions docs server
6
7See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
8for more details about the presubmit API built into gcl.
9"""
10
11# Run build_server so that files needed by tests are copied to the local
12# third_party directory.
13import os
14import sys
15
16WHITELIST = [ r'.+_test.py$' ]
17# The integration tests are selectively run from the PRESUBMIT in
18# chrome/common/extensions.
19BLACKLIST = [ r'integration_test.py$' ]
20
21def _BuildServer(input_api):
22  try:
23    sys.path.insert(0, input_api.PresubmitLocalPath())
24    import build_server
25    build_server.main()
26  finally:
27    sys.path.pop(0)
28
29def _ImportAppYamlHelper(input_api):
30  try:
31    sys.path.insert(0, input_api.PresubmitLocalPath())
32    from app_yaml_helper import AppYamlHelper
33    return AppYamlHelper
34  finally:
35    sys.path.pop(0)
36
37def _WarnIfAppYamlHasntChanged(input_api, output_api):
38  app_yaml_path = os.path.join(input_api.PresubmitLocalPath(), 'app.yaml')
39  if app_yaml_path in input_api.AbsoluteLocalPaths():
40    return []
41  return [output_api.PresubmitPromptOrNotify('''
42**************************************************
43CHANGE DETECTED IN SERVER2 WITHOUT APP.YAML UPDATE
44**************************************************
45Maybe this is ok? Follow this simple guide:
46
47Q: Does this change any data that might get stored?
48  * Did you add/remove/update a field to a data source?
49  * Did you add/remove/update some data that gets sent to templates?
50  * Is this change to support a new feature in the templates?
51  * Does this change include changes to templates?
52Yes? Bump the middle version and zero out the end version, i.e. 2-5-2 -> 2-6-0.
53     THIS WILL CAUSE THE CURRENTLY RUNNING SERVER TO STOP UPDATING.
54     PUSH THE NEW VERSION ASAP.
55No? Continue.
56
57Q: Is this a non-trivial change to the server?
58Yes? Bump the end version.
59     Unlike above, the server will *not* stop updating.
60No? Are you sure? How much do you bet? This can't be rolled back...
61
62Q: Is this a spelling correction? New test? Better comments?
63Yes? Ok fine. Ignore this warning.
64No? I guess this presubmit check doesn't work.
65''')]
66
67def _CheckYamlConsistency(input_api, output_api):
68  app_yaml_path = os.path.join(input_api.PresubmitLocalPath(), 'app.yaml')
69  cron_yaml_path = os.path.join(input_api.PresubmitLocalPath(), 'cron.yaml')
70  if not (app_yaml_path in input_api.AbsoluteLocalPaths() or
71          cron_yaml_path in input_api.AbsoluteLocalPaths()):
72    return []
73
74  AppYamlHelper = _ImportAppYamlHelper(input_api)
75  app_yaml_version = AppYamlHelper.ExtractVersion(
76      input_api.ReadFile(app_yaml_path))
77  cron_yaml_version = AppYamlHelper.ExtractVersion(
78      input_api.ReadFile(cron_yaml_path), key='target')
79
80  if app_yaml_version == cron_yaml_version:
81    return []
82  return [output_api.PresubmitError(
83      'Versions of app.yaml (%s) and cron.yaml (%s) must match' % (
84          app_yaml_version, cron_yaml_version))]
85
86def _RunPresubmit(input_api, output_api):
87  _BuildServer(input_api)
88  return (
89      _WarnIfAppYamlHasntChanged(input_api, output_api) +
90      _CheckYamlConsistency(input_api, output_api) +
91      input_api.canned_checks.RunUnitTestsInDirectory(
92          input_api, output_api, '.', whitelist=WHITELIST, blacklist=BLACKLIST)
93  )
94
95def CheckChangeOnUpload(input_api, output_api):
96  return _RunPresubmit(input_api, output_api)
97
98def CheckChangeOnCommit(input_api, output_api):
99  return _RunPresubmit(input_api, output_api)
100