PRESUBMIT.py revision c5cede9ae108bb15f6b7a8aea21c7e1fefa2834c
1# Copyright 2014 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"""Chromium presubmit script for third_party/polymer.
6
7See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
8for more details on the presubmit API built into gcl.
9"""
10
11def _CheckVulcanizeWasRun(input_api, output_api):
12  """Checks to make sure vulcanize has been run on polymer when it is
13     changed."""
14  version_tag = '// @version: '
15  separator = input_api.os_path.sep
16  cwd = input_api.PresubmitLocalPath() + separator
17  polymer_js = cwd + 'polymer' + separator + 'polymer.js'
18  polymer_vulcanized_js = cwd + 'vulcanized' + separator + 'polymer-elements.js'
19
20  version = ''
21  with open(polymer_js, 'r') as f:
22    for line in f:
23      if version_tag in line:
24        version = line.split(' ')[2].strip()
25  if (not version):
26    return [output_api.PresubmitError('Expected to find an @version tag in: ' +
27                                      polymer_js + ' but did not.')]
28  vulcanized_version = ''
29  with open(polymer_vulcanized_js, 'r') as f:
30    for line in f:
31      if version_tag in line:
32        vulcanized_version = line.split(' ')[2].strip()
33
34  if (not vulcanized_version):
35    return [output_api.PresubmitError('Expected to find an @version tag in: ' +
36                                      polymer_vulcanized_js + ' but did not.')]
37  if (vulcanized_version != version):
38    return [output_api.PresubmitError('The vulcanized version of polymer (' +
39        polymer_vulcanized_js + ') has version ' + vulcanized_version +
40        ' while the original version (' + polymer_js + ') has version ' +
41        version + '. You probably need to run vulcanize.py in ' + cwd + '.')]
42
43  return []
44
45def _CommonChecks(input_api, output_api):
46  """Checks common to both upload and commit."""
47  results = []
48  results.extend(_CheckVulcanizeWasRun(input_api, output_api))
49  return results
50
51def CheckChangeOnUpload(input_api, output_api):
52  results = []
53  results.extend(_CommonChecks(input_api, output_api))
54  return results
55
56
57def CheckChangeOnCommit(input_api, output_api):
58  results = []
59  results.extend(_CommonChecks(input_api, output_api))
60  return results
61