PRESUBMIT.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. """Presubmit script.
  5. See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
  6. for more details about the presubmit API built into depot_tools.
  7. """
  8. PRESUBMIT_VERSION = '2.0.0'
  9. USE_PYTHON3 = True
  10. _BANNED_CPP_FUNCTIONS = (
  11. (
  12. 'ScopedLightModeAsDefault',
  13. (
  14. 'Assistant code should use ScopedAssistantLightModeAsDefault instead of',
  15. 'ScopedLightModeAsDefault.',
  16. 'See //ash/public/cpp/scoped_light_mode_as_default.h for details.',
  17. ),
  18. ),
  19. )
  20. def CheckNoBannedFunctions(input_api, output_api):
  21. """Make sure that banned functions are not used."""
  22. warnings = []
  23. def GetMessageForFunction(input_api, affected_file, line_num, line, func_name,
  24. message):
  25. result = []
  26. if input_api.re.search(r"^ *//", line): # Ignore comments.
  27. return result
  28. if line.endswith(" nocheck"): # Ignore lines with nocheck comments.
  29. return result
  30. if func_name in line:
  31. result.append(' %s:%d:' % (affected_file.LocalPath(), line_num))
  32. for message_line in message:
  33. result.append(' %s' % message_line)
  34. return result
  35. file_filter = lambda f: f.LocalPath().endswith(('.cc', '.mm', '.h'))
  36. for f in input_api.AffectedFiles(file_filter=file_filter):
  37. for line_num, line in f.ChangedContents():
  38. for func_name, message in _BANNED_CPP_FUNCTIONS:
  39. problems = GetMessageForFunction(input_api, f, line_num, line,
  40. func_name, message)
  41. if problems:
  42. warnings.extend(problems)
  43. result = []
  44. if (warnings):
  45. result.append(output_api.PresubmitPromptWarning(
  46. 'Banned functions were used.\n' + '\n'.join(warnings)))
  47. return result
  48. def CheckChangeOnUpload(input_api, output_api):
  49. results = []
  50. results += CheckNoBannedFunctions(input_api, output_api)
  51. return results