146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)# Copyright 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)import os
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)import sys
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)PYLINT_BLACKLIST = []
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)PYLINT_DISABLED_WARNINGS = ['R0923', 'R0201', 'E1101']
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)def _CommonChecks(input_api, output_api):
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  results = []
12eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
13eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  # TODO(nduca): This should call update_docs.IsUpdateDocsNeeded().
14eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  # Disabled due to crbug.com/255326.
15eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if False:
16eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    update_docs_path = os.path.join(
17eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      input_api.PresubmitLocalPath(), 'update_docs')
18eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    assert os.path.exists(update_docs_path)
19eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    results.append(output_api.PresubmitError(
20eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      'Docs are stale. Please run:\n' +
21eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      '$ %s' % os.path.abspath(update_docs_path)))
22eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
23a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  # Importing telemetry.web_components actually brings tvcm into the path.
24a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  import telemetry.web_components # pylint: disable=W0612
25a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  from tvcm import presubmit_checker
26a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  checker = presubmit_checker.PresubmitChecker(input_api, output_api)
27a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  results += checker.RunChecks()
28a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
29eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  results.extend(input_api.canned_checks.RunPylint(
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        input_api, output_api,
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        black_list=PYLINT_BLACKLIST,
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        disabled_warnings=PYLINT_DISABLED_WARNINGS))
33eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  return results
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
35eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochdef GetPathsToPrepend(input_api):
36eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  return [input_api.PresubmitLocalPath()]
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
38eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochdef RunWithPrependedPath(prepended_path, fn, *args):
39eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  old_path = sys.path
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
41eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  try:
42eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    sys.path = prepended_path + old_path
43eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    return fn(*args)
44eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  finally:
45eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    sys.path = old_path
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)def CheckChangeOnUpload(input_api, output_api):
48eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  def go():
49eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    results = []
50eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    results.extend(_CommonChecks(input_api, output_api))
51eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    return results
52eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  return RunWithPrependedPath(GetPathsToPrepend(input_api), go)
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)def CheckChangeOnCommit(input_api, output_api):
55eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  def go():
56eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    results = []
57eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    results.extend(_CommonChecks(input_api, output_api))
58eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    return results
59eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  return RunWithPrependedPath(GetPathsToPrepend(input_api), go)
60