githooks: ensure pip + scipy are installed

Now that we always enter the chroot for presubmits, we might try to run
tests in a world where we run `crosperf` tests without `numpy` or
`scipy` installed.

There's probably a better solution here (this one is kinda bad because
presubmits are 'quiet' until we fail, and all of this setup can take up
to a minute), but until we find that, stop the bleeding (aka people
doing `--no-verify`)

BUG=chromium:980719
TEST=Ran crosperf tests with a fresh chroot.

Change-Id: I71275f6be0e38bf086b14d35b8bc4bfffcd7ab18
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/2199644
Reviewed-by: Manoj Gupta <manojgupta@chromium.org>
Tested-by: George Burgess <gbiv@chromium.org>
diff --git a/toolchain_utils_githooks/check-presubmit.py b/toolchain_utils_githooks/check-presubmit.py
index 8f20405..2637334 100755
--- a/toolchain_utils_githooks/check-presubmit.py
+++ b/toolchain_utils_githooks/check-presubmit.py
@@ -469,8 +469,12 @@
   return None
 
 
+def is_in_chroot() -> bool:
+  return os.path.exists('/etc/cros_chroot_version')
+
+
 def maybe_reexec_inside_chroot(autofix: bool, files: t.List[str]) -> None:
-  if os.path.exists('/etc/cros_chroot_version'):
+  if is_in_chroot():
     return
 
   enter_chroot = True
@@ -511,6 +515,25 @@
   os.execvp(args[0], args)
 
 
+# FIXME(crbug.com/980719): we probably want a better way of handling this. For
+# now, as a workaround, ensure we have all dependencies installed as a part of
+# presubmits. pip and scipy are fast enough to install (they take <1min
+# combined on my machine), so hoooopefully users won't get too impatient.
+def ensure_scipy_installed() -> None:
+  if not has_executable_on_path('pip'):
+    print('Autoinstalling `pip`...')
+    subprocess.check_call(['sudo', 'emerge', 'dev-python/pip'])
+
+  exit_code = subprocess.call(
+      ['python3', '-c', 'import scipy'],
+      stdout=subprocess.DEVNULL,
+      stderr=subprocess.DEVNULL,
+  )
+  if exit_code != 0:
+    print('Autoinstalling `scipy`...')
+    subprocess.check_call(['pip', 'install', '--user', 'scipy'])
+
+
 def main(argv):
   parser = argparse.ArgumentParser(description=__doc__)
   parser.add_argument(
@@ -533,6 +556,11 @@
   if opts.enter_chroot:
     maybe_reexec_inside_chroot(opts.autofix, opts.files)
 
+  # If you ask for --no_enter_chroot, you're on your own for installing these
+  # things.
+  if is_in_chroot():
+    ensure_scipy_installed()
+
   files = [os.path.abspath(f) for f in files]
 
   # Note that we extract .__name__s from these, so please name them in a