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
5import os
6
7_EXCLUDED_PATHS = []
8
9_LICENSE_HEADER = (
10  r".*? Copyright \(c\) 20\d\d The Chromium Authors\. All rights reserved\."
11    "\n"
12  r".*? Use of this source code is governed by a BSD-style license that can "
13    "be\n"
14  r".*? found in the LICENSE file\."
15    "\n"
16)
17
18
19def _CommonChecks(input_api, output_api):
20  results = []
21  results.extend(input_api.canned_checks.PanProjectChecks(
22      input_api, output_api, excluded_paths=_EXCLUDED_PATHS))
23
24  src_dir = os.path.join(input_api.change.RepositoryRoot(), "src")
25
26  def IsResource(maybe_resource):
27    f = maybe_resource.AbsoluteLocalPath()
28    if not f.endswith(('.css', '.html', '.js')):
29      return False
30    return True
31
32  from web_dev_style import css_checker, js_checker
33  results.extend(css_checker.CSSChecker(input_api, output_api,
34                                        file_filter=IsResource).RunChecks())
35  results.extend(js_checker.JSChecker(input_api, output_api,
36                                      file_filter=IsResource).RunChecks())
37
38  from build import check_gyp
39  gyp_result = check_gyp.GypCheck()
40  if len(gyp_result) > 0:
41    results.extend([output_api.PresubmitError(gyp_result)])
42
43  from build import check_grit
44  grit_result = check_grit.GritCheck()
45  if len(grit_result) > 0:
46    results.extend([output_api.PresubmitError(grit_result)])
47
48  black_list = input_api.DEFAULT_BLACK_LIST
49  sources = lambda x: input_api.FilterSourceFile(x, black_list=black_list)
50  results.extend(input_api.canned_checks.CheckLicense(
51                 input_api, output_api, _LICENSE_HEADER,
52                 source_file_filter=sources))
53  return results
54
55
56def GetPathsToPrepend(input_api):
57  web_dev_style_path = input_api.os_path.join(
58    input_api.change.RepositoryRoot(),
59    "third_party",
60    "web_dev_style")
61  return [input_api.PresubmitLocalPath(), web_dev_style_path]
62
63
64def RunWithPrependedPath(prepended_path, fn, *args):
65  import sys
66  old_path = sys.path
67
68  try:
69    sys.path = prepended_path + old_path
70    return fn(*args)
71  finally:
72    sys.path = old_path
73
74
75def CheckChangeOnUpload(input_api, output_api):
76  def go():
77    results = []
78    results.extend(_CommonChecks(input_api, output_api))
79    return results
80  return RunWithPrependedPath(GetPathsToPrepend(input_api), go)
81
82
83def CheckChangeOnCommit(input_api, output_api):
84  def go():
85    results = []
86    results.extend(_CommonChecks(input_api, output_api))
87    return results
88  return RunWithPrependedPath(GetPathsToPrepend(input_api), go)
89