# Copyright (c) 2011 The Native Client Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# Documentation on PRESUBMIT.py can be found at:
# http://www.chromium.org/developers/how-tos/depottools/presubmit-scripts

import os.path
import sys


TOP_DIR = 'native_client'
MAIN_DEPS = os.path.join(TOP_DIR, 'DEPS')

def NaclTopDir():
  cwd = os.getcwd()
  pos = cwd.rfind(TOP_DIR)
  if pos < 0:
    print 'ERROR: expected to be called from with %s' % TOP_DIR
  return cwd[:pos + len(TOP_DIR)]


def _CommonChecks(input_api, output_api):
  """Checks for both upload and commit."""
  results = []
  results.extend(input_api.canned_checks.PanProjectChecks(
      input_api, output_api, project_name='Native Client'))
  return results


def CheckChangeOnUpload(input_api, output_api):
  """Verifies all changes in all files.
  Args:
    input_api: the limited set of input modules allowed in presubmit.
    output_api: the limited set of output modules allowed in presubmit.
  """
  report = []
  report.extend(_CommonChecks(input_api, output_api))

  # The commit queue assumes PRESUBMIT.py is standalone.
  # TODO(bradnelson): Migrate code_hygiene to a common location so that
  # it can be used by the commit queue.
  old_sys_path = list(sys.path)
  try:
    sys.path.append(os.path.join(NaclTopDir(), 'tools'))
    sys.path.append(os.path.join(NaclTopDir(), 'build'))
    import code_hygiene
    import validate_chrome_revision
  finally:
    sys.path = old_sys_path
    del old_sys_path

  affected_files = input_api.AffectedFiles(include_deletes=False)
  for filename in affected_files:
    filename = filename.AbsoluteLocalPath()
    errors, warnings = code_hygiene.CheckFile(filename, False)
    for e in errors:
      report.append(output_api.PresubmitError(e, items=errors[e]))
    for w in warnings:
      report.append(output_api.PresubmitPromptWarning(w, items=warnings[w]))
    if filename.endswith(MAIN_DEPS):
      try:
        e = validate_chrome_revision.ValidateChromeRevision(filename)
        if e is not None:
          report.append(output_api.PresubmitError(filename, items=[e]))
      except Exception, e:
        msg = "Could not validate Chrome revision: %s" % repr(e)
        report.append(output_api.PresubmitError(filename, items=[msg]))

  return report


def CheckChangeOnCommit(input_api, output_api):
  """Verifies all changes in all files and verifies that the
  tree is open and can accept a commit.
  Args:
    input_api: the limited set of input modules allowed in presubmit.
    output_api: the limited set of output modules allowed in presubmit.
  """
  report = []
  report.extend(CheckChangeOnUpload(input_api, output_api))
  report.extend(input_api.canned_checks.CheckTreeIsOpen(
      input_api, output_api,
      json_url='http://nativeclient-status.appspot.com/current?format=json'))
  return report


def GetPreferredTrySlaves():
  return [
      'nacl-lucid32_newlib_dbg',
      'nacl-lucid32_newlib_opt',
      'nacl-lucid32_glibc_opt',
      'nacl-lucid64_newlib_dbg',
      'nacl-lucid64_newlib_opt',
      'nacl-lucid64_glibc_opt',
      'nacl-lucid64-pnacl1',
      'nacl-lucid64-pnacl2',
      'nacl-mac10.5_newlib_opt',
      'nacl-mac10.5_glibc_opt',
      'nacl-mac10.6_newlib_opt',
      'nacl-mac10.6_glibc_opt',
      'nacl-win32_newlib_opt',
      'nacl-win32_glibc_opt',
      'nacl-win64_newlib_dbg',
      'nacl-win64_newlib_opt',
      'nacl-win64_glibc_opt',
      'nacl-arm_opt',
  ]

@@ -386,16 +386,243 @@
 DEPS')%0A%0A
+# List of directories to not apply presubmit project checks, relative%0A# to the NaCl top directory%0AEXCLUDE_PROJECT_CHECKS_DIRS = %5B%0A    'src/trusted/validator_x86/testdata/32',%0A    'src/trusted/validator_x86/testdata/64',%0A    %5D%0A%0A
 def Nacl
@@ -791,17 +791,16 @@
 _DIR)%5D%0A%0A
-%0A
 def _Com
@@ -1011,28 +1011,265 @@
 ent'
-))%0A  return results%0A
+,%0A      excluded_paths=tuple(EXCLUDE_PROJECT_CHECKS_DIRS)))%0A  return results%0A%0Adef IsFileInDirectories(f, dirs):%0A  %22%22%22 Returns true if f is in list of directories%22%22%22%0A  for d in dirs:%0A    if d is os.path.commonprefix(%5Bf , d%5D):%0A      return True%0A  return False
 %0A%0Ade
@@ -2088,16 +2088,122 @@
 =False)%0A
+  exclude_dirs = %5B NaclTopDir() + '/' + x + '/'%0A                   for x in EXCLUDE_PROJECT_CHECKS_DIRS %5D%0A
   for fi
@@ -2272,16 +2272,74 @@
 lPath()%0A
+    if not IsFileInDirectories(filename, exclude_dirs):%0A  
     erro
@@ -2393,24 +2393,26 @@
  False)%0A    
+  
 for e in err
@@ -2414,32 +2414,34 @@
 n errors:%0A      
+ 
+ 
 report.append(ou
@@ -2485,24 +2485,26 @@
 rs%5Be%5D))%0A    
+  
 for w in war
@@ -2506,24 +2506,26 @@
 n warnings:%0A
+  
       report

Add ability to turn off presubmit checks for data files of validator.