environment: Limit recursive setup.py search

Don't recursively search for setup.py files within an already found
Python package.

Compare

https://cr-buildbucket.appspot.com/build/8867608848692668560

and

$ led get-build 8867608848692668560 | led edit-recipe-bundle | led launch
https://ci.chromium.org/swarming/task/4efedd9236098110

See also pwrev/19441.

Bug: 275
Change-Id: Ifcb82f497ab9e16fe2828a260768cef837117ea5
Reviewed-on: https://pigweed-review.googlesource.com/c/infra/recipes/+/19442
Reviewed-by: Marc-Antoine Ruel <maruel@google.com>
Commit-Queue: Rob Mohr <mohrr@google.com>
diff --git a/recipe_modules/environment/api.py b/recipe_modules/environment/api.py
index 8783a11..0d84572 100644
--- a/recipe_modules/environment/api.py
+++ b/recipe_modules/environment/api.py
@@ -129,8 +129,17 @@
                     for x in files
                     if self.m.path.basename(x) == 'setup.py'
                 )
+
+            original_matches = matches[:]
+            for match in original_matches:
+                # Remove all matches that have another match as a strict prefix.
+                matches = [
+                    x for x in matches if x == match or not x.startswith(match)
+                ]
+
             with self.m.step.nest('packages') as step:
-                step.logs['matches'] = [str(x) for x in matches]
+                step.logs['raw matches'] = [str(x) for x in original_matches]
+                step.logs['filtered matches'] = [str(x) for x in matches]
         return matches
 
     def _init_python(self, checkout_root):
diff --git a/recipe_modules/environment/test_api.py b/recipe_modules/environment/test_api.py
index 584138c..6b7f677 100644
--- a/recipe_modules/environment/test_api.py
+++ b/recipe_modules/environment/test_api.py
@@ -62,7 +62,9 @@
                 root=root
             ),
             stdout=self.m.raw_io.output(
-                'pw_cli/py/setup.py\n' 'pw_presubmit/py/setup.py\n'
+                'pw_cli/py/setup.py\n'
+                'pw_presubmit/py/setup.py\n'
+                'pw_presubmit/py/skipped_because_of_previous_match/setup.py\n'
             ),
         )
 
diff --git a/recipe_modules/environment/tests/full.expected/windows.json b/recipe_modules/environment/tests/full.expected/windows.json
index 0ef3436..884de14 100644
--- a/recipe_modules/environment/tests/full.expected/windows.json
+++ b/recipe_modules/environment/tests/full.expected/windows.json
@@ -96,6 +96,7 @@
       "@@@STEP_NEST_LEVEL@3@@@",
       "@@@STEP_LOG_LINE@listdir@[START_DIR]\\python\\pw_cli/py/setup.py@@@",
       "@@@STEP_LOG_LINE@listdir@[START_DIR]\\python\\pw_presubmit/py/setup.py@@@",
+      "@@@STEP_LOG_LINE@listdir@[START_DIR]\\python\\pw_presubmit/py/skipped_because_of_previous_match/setup.py@@@",
       "@@@STEP_LOG_END@listdir@@@"
     ]
   },
@@ -104,9 +105,13 @@
     "name": "environment.setup python.find_python_packages.packages",
     "~followup_annotations": [
       "@@@STEP_NEST_LEVEL@3@@@",
-      "@@@STEP_LOG_LINE@matches@[START_DIR]\\python\\pw_cli/py@@@",
-      "@@@STEP_LOG_LINE@matches@[START_DIR]\\python\\pw_presubmit/py@@@",
-      "@@@STEP_LOG_END@matches@@@"
+      "@@@STEP_LOG_LINE@raw matches@[START_DIR]\\python\\pw_cli/py@@@",
+      "@@@STEP_LOG_LINE@raw matches@[START_DIR]\\python\\pw_presubmit/py@@@",
+      "@@@STEP_LOG_LINE@raw matches@[START_DIR]\\python\\pw_presubmit/py/skipped_because_of_previous_match@@@",
+      "@@@STEP_LOG_END@raw matches@@@",
+      "@@@STEP_LOG_LINE@filtered matches@[START_DIR]\\python\\pw_cli/py@@@",
+      "@@@STEP_LOG_LINE@filtered matches@[START_DIR]\\python\\pw_presubmit/py@@@",
+      "@@@STEP_LOG_END@filtered matches@@@"
     ]
   },
   {
diff --git a/recipe_modules/environment/tests/full.expected/with_cargo.json b/recipe_modules/environment/tests/full.expected/with_cargo.json
index 487ba15..047fb2c 100644
--- a/recipe_modules/environment/tests/full.expected/with_cargo.json
+++ b/recipe_modules/environment/tests/full.expected/with_cargo.json
@@ -233,6 +233,7 @@
       "@@@STEP_NEST_LEVEL@3@@@",
       "@@@STEP_LOG_LINE@listdir@[START_DIR]/python/pw_cli/py/setup.py@@@",
       "@@@STEP_LOG_LINE@listdir@[START_DIR]/python/pw_presubmit/py/setup.py@@@",
+      "@@@STEP_LOG_LINE@listdir@[START_DIR]/python/pw_presubmit/py/skipped_because_of_previous_match/setup.py@@@",
       "@@@STEP_LOG_END@listdir@@@"
     ]
   },
@@ -241,9 +242,13 @@
     "name": "environment.setup python.find_python_packages.packages",
     "~followup_annotations": [
       "@@@STEP_NEST_LEVEL@3@@@",
-      "@@@STEP_LOG_LINE@matches@[START_DIR]/python/pw_cli/py@@@",
-      "@@@STEP_LOG_LINE@matches@[START_DIR]/python/pw_presubmit/py@@@",
-      "@@@STEP_LOG_END@matches@@@"
+      "@@@STEP_LOG_LINE@raw matches@[START_DIR]/python/pw_cli/py@@@",
+      "@@@STEP_LOG_LINE@raw matches@[START_DIR]/python/pw_presubmit/py@@@",
+      "@@@STEP_LOG_LINE@raw matches@[START_DIR]/python/pw_presubmit/py/skipped_because_of_previous_match@@@",
+      "@@@STEP_LOG_END@raw matches@@@",
+      "@@@STEP_LOG_LINE@filtered matches@[START_DIR]/python/pw_cli/py@@@",
+      "@@@STEP_LOG_LINE@filtered matches@[START_DIR]/python/pw_presubmit/py@@@",
+      "@@@STEP_LOG_END@filtered matches@@@"
     ]
   },
   {
diff --git a/recipes/docs_builder.expected/docs.json b/recipes/docs_builder.expected/docs.json
index 03a98c6..cb3cbe8 100644
--- a/recipes/docs_builder.expected/docs.json
+++ b/recipes/docs_builder.expected/docs.json
@@ -342,7 +342,8 @@
     "name": "environment.setup python.find_python_packages.packages",
     "~followup_annotations": [
       "@@@STEP_NEST_LEVEL@3@@@",
-      "@@@STEP_LOG_END@matches@@@"
+      "@@@STEP_LOG_END@raw matches@@@",
+      "@@@STEP_LOG_END@filtered matches@@@"
     ]
   },
   {
diff --git a/recipes/docs_builder.expected/docs_dry_run.json b/recipes/docs_builder.expected/docs_dry_run.json
index c4b4686..249fde9 100644
--- a/recipes/docs_builder.expected/docs_dry_run.json
+++ b/recipes/docs_builder.expected/docs_dry_run.json
@@ -342,7 +342,8 @@
     "name": "environment.setup python.find_python_packages.packages",
     "~followup_annotations": [
       "@@@STEP_NEST_LEVEL@3@@@",
-      "@@@STEP_LOG_END@matches@@@"
+      "@@@STEP_LOG_END@raw matches@@@",
+      "@@@STEP_LOG_END@filtered matches@@@"
     ]
   },
   {
diff --git a/recipes/env_setup_builder.expected/dry-run.json b/recipes/env_setup_builder.expected/dry-run.json
index 5efd22a..a9a8941 100644
--- a/recipes/env_setup_builder.expected/dry-run.json
+++ b/recipes/env_setup_builder.expected/dry-run.json
@@ -486,7 +486,8 @@
     "name": "environment.setup python.find_python_packages.packages",
     "~followup_annotations": [
       "@@@STEP_NEST_LEVEL@3@@@",
-      "@@@STEP_LOG_END@matches@@@"
+      "@@@STEP_LOG_END@raw matches@@@",
+      "@@@STEP_LOG_END@filtered matches@@@"
     ]
   },
   {
diff --git a/recipes/env_setup_builder.expected/pigweed.json b/recipes/env_setup_builder.expected/pigweed.json
index 728ac34..ca1f3fe 100644
--- a/recipes/env_setup_builder.expected/pigweed.json
+++ b/recipes/env_setup_builder.expected/pigweed.json
@@ -486,7 +486,8 @@
     "name": "environment.setup python.find_python_packages.packages",
     "~followup_annotations": [
       "@@@STEP_NEST_LEVEL@3@@@",
-      "@@@STEP_LOG_END@matches@@@"
+      "@@@STEP_LOG_END@raw matches@@@",
+      "@@@STEP_LOG_END@filtered matches@@@"
     ]
   },
   {
diff --git a/recipes/pw_presubmit.expected/pigweed.json b/recipes/pw_presubmit.expected/pigweed.json
index 6e50b89..af4d537 100644
--- a/recipes/pw_presubmit.expected/pigweed.json
+++ b/recipes/pw_presubmit.expected/pigweed.json
@@ -486,7 +486,8 @@
     "name": "environment.setup python.find_python_packages.packages",
     "~followup_annotations": [
       "@@@STEP_NEST_LEVEL@3@@@",
-      "@@@STEP_LOG_END@matches@@@"
+      "@@@STEP_LOG_END@raw matches@@@",
+      "@@@STEP_LOG_END@filtered matches@@@"
     ]
   },
   {
diff --git a/recipes/pw_presubmit.expected/repo.json b/recipes/pw_presubmit.expected/repo.json
index fc859d5..ed6b6ce 100644
--- a/recipes/pw_presubmit.expected/repo.json
+++ b/recipes/pw_presubmit.expected/repo.json
@@ -804,7 +804,8 @@
     "name": "environment.setup python.find_python_packages.packages",
     "~followup_annotations": [
       "@@@STEP_NEST_LEVEL@3@@@",
-      "@@@STEP_LOG_END@matches@@@"
+      "@@@STEP_LOG_END@raw matches@@@",
+      "@@@STEP_LOG_END@filtered matches@@@"
     ]
   },
   {
diff --git a/recipes/pw_presubmit.expected/step.json b/recipes/pw_presubmit.expected/step.json
index 004dbb5..4b1bf14 100644
--- a/recipes/pw_presubmit.expected/step.json
+++ b/recipes/pw_presubmit.expected/step.json
@@ -531,7 +531,8 @@
     "name": "environment.setup python.find_python_packages.packages",
     "~followup_annotations": [
       "@@@STEP_NEST_LEVEL@3@@@",
-      "@@@STEP_LOG_END@matches@@@"
+      "@@@STEP_LOG_END@raw matches@@@",
+      "@@@STEP_LOG_END@filtered matches@@@"
     ]
   },
   {
diff --git a/recipes/target_to_cipd.expected/success.json b/recipes/target_to_cipd.expected/success.json
index 38db397..905995a 100644
--- a/recipes/target_to_cipd.expected/success.json
+++ b/recipes/target_to_cipd.expected/success.json
@@ -804,7 +804,8 @@
     "name": "environment.setup python.find_python_packages.packages",
     "~followup_annotations": [
       "@@@STEP_NEST_LEVEL@3@@@",
-      "@@@STEP_LOG_END@matches@@@"
+      "@@@STEP_LOG_END@raw matches@@@",
+      "@@@STEP_LOG_END@filtered matches@@@"
     ]
   },
   {
diff --git a/recipes/xrefs.expected/dry_run.json b/recipes/xrefs.expected/dry_run.json
index 963f120..09efdc4 100644
--- a/recipes/xrefs.expected/dry_run.json
+++ b/recipes/xrefs.expected/dry_run.json
@@ -342,7 +342,8 @@
     "name": "environment.setup python.find_python_packages.packages",
     "~followup_annotations": [
       "@@@STEP_NEST_LEVEL@3@@@",
-      "@@@STEP_LOG_END@matches@@@"
+      "@@@STEP_LOG_END@raw matches@@@",
+      "@@@STEP_LOG_END@filtered matches@@@"
     ]
   },
   {
diff --git a/recipes/xrefs.expected/kythe.json b/recipes/xrefs.expected/kythe.json
index eaba595..d7d9435 100644
--- a/recipes/xrefs.expected/kythe.json
+++ b/recipes/xrefs.expected/kythe.json
@@ -342,7 +342,8 @@
     "name": "environment.setup python.find_python_packages.packages",
     "~followup_annotations": [
       "@@@STEP_NEST_LEVEL@3@@@",
-      "@@@STEP_LOG_END@matches@@@"
+      "@@@STEP_LOG_END@raw matches@@@",
+      "@@@STEP_LOG_END@filtered matches@@@"
     ]
   },
   {