Home | History | Annotate | Download | only in memory_inspector
      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 
      5 """Top-level presubmit script for memory_inspector.
      6 
      7 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts for
      8 details on the presubmit API built into gcl.
      9 """
     10 
     11 
     12 def _CommonChecks(input_api, output_api):
     13   output = []
     14   blacklist = [r'classification_rules.*']
     15   output.extend(input_api.canned_checks.RunPylint(
     16       input_api, output_api, black_list=blacklist,
     17       extra_paths_list=[
     18           input_api.os_path.join(input_api.PresubmitLocalPath(), '..', '..',
     19                                  'build', 'android')
     20       ]))
     21   output.extend(input_api.canned_checks.RunUnitTests(
     22       input_api,
     23       output_api,
     24       [input_api.os_path.join(input_api.PresubmitLocalPath(), 'run_tests')]))
     25 
     26   if input_api.is_committing:
     27     output.extend(input_api.canned_checks.PanProjectChecks(input_api,
     28                                                            output_api,
     29                                                            owners_check=False))
     30   return output
     31 
     32 
     33 def _CheckPrebuiltsAreUploaded(input_api, output_api):
     34   """Checks that the SHA1 files in prebuilts/ reference existing objects on GCS.
     35 
     36   This is to avoid that somebody accidentally checks in some new XXX.sha1 files
     37   into prebuilts/ without having previously uploaded the corresponding binaries
     38   to the cloud storage bucket. This can happen if the developer has a consistent
     39   local copy of the binary. This check verifies (through a HTTP HEAD request)
     40   that the GCS bucket has an object for each .sha1 file in prebuilts and raises
     41   a presubmit error, listing the missing files, if not.
     42   """
     43   import sys
     44   import urllib2
     45   old_sys_path = sys.path
     46   try:
     47     sys.path = [input_api.PresubmitLocalPath()] + sys.path
     48     from memory_inspector import constants
     49   finally:
     50     sys.path = old_sys_path
     51   missing_files = []
     52   for f in input_api.os_listdir(constants.PREBUILTS_PATH):
     53     if not f.endswith('.sha1'):
     54       continue
     55     prebuilt_sha_path = input_api.os_path.join(constants.PREBUILTS_PATH, f)
     56     with open(prebuilt_sha_path) as sha_file:
     57       sha = sha_file.read().strip()
     58     url = constants.PREBUILTS_BASE_URL + sha
     59     request = urllib2.Request(url)
     60     request.get_method = lambda : 'HEAD'
     61     try:
     62       urllib2.urlopen(request, timeout=5)
     63     except Exception as e:
     64       if isinstance(e, urllib2.HTTPError) and e.code == 404:
     65         missing_files += [prebuilt_sha_path]
     66       else:
     67         return [output_api.PresubmitError('HTTP Error while checking %s' % url,
     68                                           long_text=str(e))]
     69   if missing_files:
     70     return [output_api.PresubmitError(
     71         'Some prebuilts have not been uploaded. Perhaps you forgot to '
     72         'upload_to_google_storage.py?', missing_files)]
     73   return []
     74 
     75 
     76 def CheckChangeOnUpload(input_api, output_api):
     77   results = []
     78   results.extend(_CommonChecks(input_api, output_api))
     79   results.extend(_CheckPrebuiltsAreUploaded(input_api, output_api))
     80   return results
     81 
     82 
     83 def CheckChangeOnCommit(input_api, output_api):
     84   return _CommonChecks(input_api, output_api)
     85