summaryrefslogtreecommitdiff
path: root/chromium/tools/metrics/histograms/PRESUBMIT.py
blob: 6cf0c11f298a1a73f6e136e05e2c90ac841c0e8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Copyright 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""
See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
for more details on the presubmit API built into depot_tools.
"""


def ValidationNeeded(input_api):
  """Check if validation of histograms.xml files are required."""
  for f in input_api.AffectedTextFiles():
    p = f.AbsoluteLocalPath()
    if (input_api.basename(p) in {'histograms.xml', 'enums.xml'} and
        input_api.os_path.dirname(p) == input_api.PresubmitLocalPath()):
      return True
  return False


def CheckChange(input_api, output_api):
  """Checks that histograms.xml is pretty-printed and well-formatted."""
  results = []
  if ValidationNeeded(input_api):
    cwd = input_api.PresubmitLocalPath()

    exit_code = input_api.subprocess.call(
        [input_api.python_executable, 'pretty_print.py', '--presubmit',
         '--non-interactive'],
        cwd=cwd)
    if exit_code != 0:
      results.append(output_api.PresubmitError(
          'histograms.xml is not formatted correctly; please run '
          'git cl format %s to fix.' % cwd))

    exit_code = input_api.subprocess.call(
        [input_api.python_executable, 'validate_format.py'], cwd=cwd)
    if exit_code != 0:
      results.append(output_api.PresubmitError(
          'histograms.xml is not well formatted; run %s/validate_format.py '
          'and fix the reported errors.' % cwd))

  return results


def CheckChangeOnUpload(input_api, output_api):
  return CheckChange(input_api, output_api)


def CheckChangeOnCommit(input_api, output_api):
  return CheckChange(input_api, output_api)