Run pydeps check only in src.

Bug: 803245
Change-Id: I645e82663991cd012ced5f19a056d08bbc2c797d
Reviewed-on: https://chromium-review.googlesource.com/952281
Reviewed-by: Daniel Cheng <[email protected]>
Commit-Queue: Zhiling Huang <[email protected]>
Cr-Commit-Position: refs/heads/master@{#542297}
diff --git a/PRESUBMIT.py b/PRESUBMIT.py
index fafc895..d9435634 100644
--- a/PRESUBMIT.py
+++ b/PRESUBMIT.py
@@ -2180,15 +2180,21 @@
   results = []
   # First, check for new / deleted .pydeps.
   for f in input_api.AffectedFiles(include_deletes=True):
-    if f.LocalPath().endswith('.pydeps'):
-      if f.Action() == 'D' and f.LocalPath() in _ALL_PYDEPS_FILES:
-        results.append(output_api.PresubmitError(
-            'Please update _ALL_PYDEPS_FILES within //PRESUBMIT.py to '
-            'remove %s' % f.LocalPath()))
-      elif f.Action() != 'D' and f.LocalPath() not in _ALL_PYDEPS_FILES:
-        results.append(output_api.PresubmitError(
-            'Please update _ALL_PYDEPS_FILES within //PRESUBMIT.py to '
-            'include %s' % f.LocalPath()))
+    # Check whether we are running the presubmit check for a file in src.
+    # f.LocalPath is relative to repo (src, or internal repo).
+    # os_path.exists is relative to src repo.
+    # Therefore if os_path.exists is true, it means f.LocalPath is relative
+    # to src and we can conclude that the pydeps is in src.
+    if input_api.os_path.exists(f.LocalPath()):
+      if f.LocalPath().endswith('.pydeps'):
+        if f.Action() == 'D' and f.LocalPath() in _ALL_PYDEPS_FILES:
+          results.append(output_api.PresubmitError(
+              'Please update _ALL_PYDEPS_FILES within //PRESUBMIT.py to '
+              'remove %s' % f.LocalPath()))
+        elif f.Action() != 'D' and f.LocalPath() not in _ALL_PYDEPS_FILES:
+          results.append(output_api.PresubmitError(
+              'Please update _ALL_PYDEPS_FILES within //PRESUBMIT.py to '
+              'include %s' % f.LocalPath()))
 
   if results:
     return results
diff --git a/PRESUBMIT_test.py b/PRESUBMIT_test.py
index bcca909f..b83d58ad 100755
--- a/PRESUBMIT_test.py
+++ b/PRESUBMIT_test.py
@@ -656,10 +656,21 @@
       MockAffectedFile('new.pydeps', [], action='A'),
     ]
 
+    self.mock_input_api.CreateMockFileInPath(
+        [x.LocalPath() for x in self.mock_input_api.AffectedFiles(
+            include_deletes=True)])
     results = self._RunCheck()
     self.assertEqual(1, len(results))
     self.assertTrue('PYDEPS_FILES' in str(results[0]))
 
+  def testPydepNotInSrc(self):
+    self.mock_input_api.files = [
+      MockAffectedFile('new.pydeps', [], action='A'),
+    ]
+    self.mock_input_api.CreateMockFileInPath([])
+    results = self._RunCheck()
+    self.assertEqual(0, len(results))
+
   def testRemovedPydep(self):
     # PRESUBMIT._CheckPydepsNeedsUpdating is only implemented for Android.
     if self.mock_input_api.platform != 'linux2':
@@ -668,7 +679,9 @@
     self.mock_input_api.files = [
       MockAffectedFile(PRESUBMIT._ALL_PYDEPS_FILES[0], [], action='D'),
     ]
-
+    self.mock_input_api.CreateMockFileInPath(
+        [x.LocalPath() for x in self.mock_input_api.AffectedFiles(
+            include_deletes=True)])
     results = self._RunCheck()
     self.assertEqual(1, len(results))
     self.assertTrue('PYDEPS_FILES' in str(results[0]))
diff --git a/PRESUBMIT_test_mocks.py b/PRESUBMIT_test_mocks.py
index 732da0e..846f8413 100644
--- a/PRESUBMIT_test_mocks.py
+++ b/PRESUBMIT_test_mocks.py
@@ -51,6 +51,7 @@
 
     return errors
 
+
 class MockInputApi(object):
   """Mock class for the InputApi class.
 
@@ -75,6 +76,9 @@
     self.change = MockChange([])
     self.presubmit_local_path = os.path.dirname(__file__)
 
+  def CreateMockFileInPath(self, f_list):
+    self.os_path.exists = lambda x: x in f_list
+
   def AffectedFiles(self, file_filter=None, include_deletes=False):
     for file in self.files:
       if file_filter and not file_filter(file):