pw_env_setup: Skip downloading overridden packages

Bug: b/246971220
Change-Id: I31db4cad2519289ecc80e87448536a46a279dfd6
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/110476
Reviewed-by: Wyatt Hepler <hepler@google.com>
Reviewed-by: Anthony DiGirolamo <tonymd@google.com>
Commit-Queue: Rob Mohr <mohrr@google.com>
diff --git a/pw_env_setup/py/pw_env_setup/cipd_setup/update.py b/pw_env_setup/py/pw_env_setup/cipd_setup/update.py
index 73140a4..8f4d411 100755
--- a/pw_env_setup/py/pw_env_setup/cipd_setup/update.py
+++ b/pw_env_setup/py/pw_env_setup/cipd_setup/update.py
@@ -21,6 +21,7 @@
 
 from __future__ import print_function
 
+import collections
 import hashlib
 import json
 import os
@@ -191,9 +192,8 @@
     return result
 
 
-def write_ensure_file(package_files, ensure_file, platform):  # pylint: disable=redefined-outer-name
+def all_packages(package_files):
     packages = []
-
     for package_file in package_files:
         name = package_file_name(package_file)
         with open(package_file, 'r') as ins:
@@ -204,13 +204,28 @@
                 else:
                     package['subdir'] = name
             packages.extend(file_packages)
+    return packages
+
+
+def deduplicate_packages(packages):
+    deduped = collections.OrderedDict()
+    for package in reversed(packages):
+        if package['path'] in deduped:
+            del deduped[package['path']]
+        deduped[package['path']] = package
+    return reversed(deduped.values())
+
+
+def write_ensure_file(package_files, ensure_file, platform):  # pylint: disable=redefined-outer-name
+    packages = all_packages(package_files)
+    deduped_packages = deduplicate_packages(packages)
 
     with open(ensure_file, 'w') as outs:
         outs.write('$VerifiedPlatform linux-amd64\n'
                    '$VerifiedPlatform mac-amd64\n'
                    '$ParanoidMode CheckPresence\n')
 
-        for pkg in packages:
+        for pkg in deduped_packages:
             # If this is a new-style package manifest platform handling must
             # be done here instead of by the cipd executable.
             if 'platforms' in pkg and platform not in pkg['platforms']: