Add check for virtual env to doctor

Change-Id: If2aca9af65809828a8070603c618100856c6580f
diff --git a/pw_doctor/py/pw_doctor/doctor.py b/pw_doctor/py/pw_doctor/doctor.py
index d45fe05..ae0ff38 100755
--- a/pw_doctor/py/pw_doctor/doctor.py
+++ b/pw_doctor/py/pw_doctor/doctor.py
@@ -18,6 +18,7 @@
 import logging
 import json
 import os
+import pathlib
 import shutil
 import subprocess
 import sys
@@ -105,6 +106,27 @@
 
 
 @register_into(CHECKS)
+def virtualenv(ctx: DoctorContext):
+    """Check that we're in the correct virtualenv."""
+    try:
+        venv_path = pathlib.Path(os.environ['VIRTUAL_ENV']).resolve()
+    except KeyError:
+        ctx.error('VIRTUAL_ENV not set')
+        return
+
+    # When running in LUCI we might not have gone through the normal environment
+    # setup process, so we need to skip the rest of this step.
+    if 'LUCI_CONTEXT' in os.environ:
+        return
+
+    root = pathlib.Path(os.environ['PW_ROOT']).resolve()
+
+    if root not in venv_path.parents:
+        ctx.error('VIRTUAL_ENV (%s) not inside PW_ROOT (%s)', venv_path, root)
+        ctx.error('\n'.join(os.environ.keys()))
+
+
+@register_into(CHECKS)
 def cipd(ctx: DoctorContext):
     """Check cipd is set up correctly and in use."""
     cipd_path = 'pigweed'