pw_build: copy_from_cipd updates packages

Also create a pool so we only run one copy_from_cipd process at a time.

Fixed: 334
Change-Id: I629da2fa875659d8fe3ac92e980102be3b2c919d
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/36242
Commit-Queue: Rob Mohr <mohrr@google.com>
Reviewed-by: Armando Montanez <amontanez@google.com>
diff --git a/pw_build/BUILD.gn b/pw_build/BUILD.gn
index 7668dd0..c9399fb 100644
--- a/pw_build/BUILD.gn
+++ b/pw_build/BUILD.gn
@@ -125,6 +125,10 @@
   depth = 1
 }
 
+pool("copy_from_cipd_pool") {
+  depth = 1
+}
+
 # Requirements for the pw_python_package lint targets.
 pw_python_requirements("python_lint") {
   requirements = [
diff --git a/pw_build/copy_from_cipd.gni b/pw_build/copy_from_cipd.gni
index a98b76b..bb9502b 100644
--- a/pw_build/copy_from_cipd.gni
+++ b/pw_build/copy_from_cipd.gni
@@ -67,6 +67,10 @@
       "--out-dir=${_out_relative}",
     ]
 
+    # Parallel calls might both be invoking CIPD on the same directory which
+    # might work but is redundant, so serialize calls.
+    pool = "$dir_pw_build:copy_from_cipd_pool"
+
     # TODO(pwbug/335): This should somehow track the actual .a for changes as
     # well.
     inputs = [ invoker.manifest ]
diff --git a/pw_build/py/BUILD.gn b/pw_build/py/BUILD.gn
index 4e15723..2355db7 100644
--- a/pw_build/py/BUILD.gn
+++ b/pw_build/py/BUILD.gn
@@ -39,6 +39,7 @@
   ]
   python_deps = [
     "$dir_pw_cli/py",
+    "$dir_pw_env_setup/py",
     "$dir_pw_presubmit/py",
   ]
   pylintrc = "$dir_pigweed/.pylintrc"
diff --git a/pw_build/py/pw_build/copy_from_cipd.py b/pw_build/py/pw_build/copy_from_cipd.py
index 9535845..63921ee 100755
--- a/pw_build/py/pw_build/copy_from_cipd.py
+++ b/pw_build/py/pw_build/copy_from_cipd.py
@@ -47,6 +47,8 @@
 import sys
 from pathlib import Path
 
+import pw_env_setup.cipd_setup.update
+
 logger = logging.getLogger(__name__)
 
 
@@ -104,9 +106,9 @@
     cmd = ['cipd', 'describe', path, '-version', instance_id]
     output = subprocess.check_output(cmd).decode()
     if expected_version not in output:
-        # TODO(pwbug/334) Update package if it's out of date.
-        raise ValueError(
-            f'{package_name} is out of date, please rerun bootstrap')
+        pw_env_setup.cipd_setup.update.update(
+            'cipd', (manifest, ), os.environ['PW_CIPD_INSTALL_DIR'],
+            os.environ['CIPD_CACHE_DIR'])
 
 
 def main():
diff --git a/pw_build/py/setup.py b/pw_build/py/setup.py
index 7682dd5..50388a5 100644
--- a/pw_build/py/setup.py
+++ b/pw_build/py/setup.py
@@ -27,6 +27,7 @@
     install_requires=[
         'wheel',
         'pw_cli',
+        'pw_env_setup',
         'pw_presubmit',
     ],
     entry_points={