pw_env_setup: Add "system_packages" option to the config file

Add a new optional boolean value virtualenv.system_packages
to the environment configuration file, giving the Python
virtualenv access to the system packages. It's achieved by
passing --system-site-packages flag when the virtualenv is
created.

The option is useful when Pigweed is used in the context
of existing Python-based infrastructure such as a test
framework.

Signed-off-by: Damian Krolik <damian.krolik@nordicsemi.no>
Change-Id: I137148afcffc0a3ec30426df6dc725fd3eea156e
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/49040
Reviewed-by: Rob Mohr <mohrr@google.com>
Reviewed-by: Michael Spang <spang@google.com>
Commit-Queue: Rob Mohr <mohrr@google.com>
diff --git a/pw_env_setup/docs.rst b/pw_env_setup/docs.rst
index 2f02a0f..e999b6c 100644
--- a/pw_env_setup/docs.rst
+++ b/pw_env_setup/docs.rst
@@ -227,6 +227,10 @@
   only installing Pigweed Python packages, use the location of the Pigweed
   submodule.
 
+``virtualenv.system_packages``
+  A boolean value that can be used the give the Python virtual environment
+  access to the system site packages. Defaults to ``false``.
+
 ``optional_submodules``
   By default environment setup will check that all submodules are present in
   the checkout. Any submodules in this list are excluded from that check.
@@ -245,7 +249,8 @@
       "gn_root": ".",
       "gn_targets": [
         ":python.install",
-      ]
+      ],
+      "system_packages": false
     },
     "optional_submodules": [
       "optional/submodule/one",
diff --git a/pw_env_setup/py/pw_env_setup/env_setup.py b/pw_env_setup/py/pw_env_setup/env_setup.py
index 21e53fa..02e3ffa 100755
--- a/pw_env_setup/py/pw_env_setup/env_setup.py
+++ b/pw_env_setup/py/pw_env_setup/env_setup.py
@@ -198,6 +198,7 @@
         self._virtualenv_requirements = []
         self._virtualenv_gn_targets = []
         self._optional_submodules = []
+        self._virtualenv_system_packages = False
 
         self._config_file_name = getattr(config_file, 'name', 'config file')
         if config_file:
@@ -264,6 +265,9 @@
             self._virtualenv_gn_targets.append(
                 virtualenv_setup.GnTarget('{}#{}'.format(root, target)))
 
+        self._virtualenv_system_packages = virtualenv.pop(
+            'system_packages', False)
+
         if virtualenv:
             raise ConfigFileError(
                 'unrecognized option in {}: "virtualenv.{}"'.format(
@@ -501,6 +505,7 @@
                 gn_out_dir=self._virtualenv_gn_out_dir,
                 python=new_python3,
                 env=self._env,
+                system_packages=self._virtualenv_system_packages,
         ):
             return result(_Result.Status.FAILED)
 
diff --git a/pw_env_setup/py/pw_env_setup/virtualenv_setup/install.py b/pw_env_setup/py/pw_env_setup/virtualenv_setup/install.py
index c6161e7..7d03fe3 100644
--- a/pw_env_setup/py/pw_env_setup/virtualenv_setup/install.py
+++ b/pw_env_setup/py/pw_env_setup/virtualenv_setup/install.py
@@ -123,14 +123,15 @@
 
 
 def install(
-        project_root,
-        venv_path,
-        full_envsetup=True,
-        requirements=(),
-        gn_targets=(),
-        gn_out_dir=None,
-        python=sys.executable,
-        env=None,
+    project_root,
+    venv_path,
+    full_envsetup=True,
+    requirements=(),
+    gn_targets=(),
+    gn_out_dir=None,
+    python=sys.executable,
+    env=None,
+    system_packages=False,
 ):
     """Creates a venv and installs all packages in this Git repo."""
 
@@ -167,7 +168,9 @@
         if '__PYVENV_LAUNCHER__' in envcopy:
             del envcopy['__PYVENV_LAUNCHER__']
 
-        cmd = (python, '-m', 'venv', '--upgrade', venv_path)
+        cmd = [python, '-m', 'venv', '--upgrade']
+        cmd += ['--system-site-packages'] if system_packages else []
+        cmd += [venv_path]
         _check_call(cmd, env=envcopy)
 
     venv_python = os.path.join(venv_bin, 'python')