Home | History | Annotate | Download | only in devil
      1 # Copyright 2015 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 """Presubmit script for devil.
      6 
      7 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts for
      8 details on the presubmit API built into depot_tools.
      9 """
     10 
     11 
     12 def _RunPylint(input_api, output_api):
     13   return input_api.RunTests(input_api.canned_checks.RunPylint(
     14       input_api, output_api, pylintrc='pylintrc'))
     15 
     16 
     17 def _RunUnitTests(input_api, output_api):
     18   def J(*dirs):
     19     """Returns a path relative to presubmit directory."""
     20     return input_api.os_path.join(
     21         input_api.PresubmitLocalPath(), 'devil', *dirs)
     22 
     23   test_env = dict(input_api.environ)
     24   test_env.update({
     25     'PYTHONDONTWRITEBYTECODE': '1',
     26     'PYTHONPATH': ':'.join([J(), J('..')]),
     27   })
     28 
     29   return input_api.canned_checks.RunUnitTests(
     30       input_api,
     31       output_api,
     32       unit_tests=[
     33           J('devil_env_test.py'),
     34           J('android', 'battery_utils_test.py'),
     35           J('android', 'device_utils_test.py'),
     36           J('android', 'fastboot_utils_test.py'),
     37           J('android', 'md5sum_test.py'),
     38           J('android', 'logcat_monitor_test.py'),
     39           J('android', 'tools', 'script_common_test.py'),
     40           J('utils', 'cmd_helper_test.py'),
     41           J('utils', 'timeout_retry_unittest.py'),
     42       ],
     43       env=test_env)
     44 
     45 
     46 def _EnsureNoPylibUse(input_api, output_api):
     47   def other_python_files(f):
     48     this_presubmit_file = input_api.os_path.join(
     49         input_api.PresubmitLocalPath(), 'PRESUBMIT.py')
     50     return (f.LocalPath().endswith('.py')
     51             and not f.AbsoluteLocalPath() == this_presubmit_file)
     52 
     53   changed_files = input_api.AffectedSourceFiles(other_python_files)
     54   import_error_re = input_api.re.compile(
     55       r'(from pylib.* import)|(import pylib)')
     56 
     57   errors = []
     58   for f in changed_files:
     59     errors.extend(
     60         '%s:%d' % (f.LocalPath(), line_number)
     61         for line_number, line_text in f.ChangedContents()
     62         if import_error_re.search(line_text))
     63 
     64   if errors:
     65     return [output_api.PresubmitError(
     66         'pylib modules should not be imported from devil modules.',
     67         items=errors)]
     68   return []
     69 
     70 
     71 def CommonChecks(input_api, output_api):
     72   output = []
     73   output += _RunPylint(input_api, output_api)
     74   output += _RunUnitTests(input_api, output_api)
     75   output += _EnsureNoPylibUse(input_api, output_api)
     76   return output
     77 
     78 
     79 def CheckChangeOnUpload(input_api, output_api):
     80   return CommonChecks(input_api, output_api)
     81 
     82 
     83 def CheckChangeOnCommit(input_api, output_api):
     84   return CommonChecks(input_api, output_api)
     85 
     86