vendor.py: add an are-we-in-the-chroot check

This script is only meant to be run from within the chroot. This check
makes this requirement clearer.

Also move arg parsing into main, as this is more consistent with cros
style.

BUG=None
TEST=./vendor.py inside and outside of the chroot.

Change-Id: Ie24aacdcb1269922f0a279e660b795085ea20d4d
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/rust_crates/+/4225531
Tested-by: George Burgess <gbiv@chromium.org>
Reviewed-by: Allen Webb <allenwebb@google.com>
Commit-Queue: George Burgess <gbiv@chromium.org>
diff --git a/vendor.py b/vendor.py
index 1ce8e3c..fd68d5e 100755
--- a/vendor.py
+++ b/vendor.py
@@ -17,8 +17,10 @@
 import subprocess
 import sys
 import textwrap
+
 import toml
 
+
 # We only care about crates we're actually going to use and that's usually
 # limited to ones with cfg(linux). For running `cargo metadata`, limit results
 # to only these platforms.
@@ -825,7 +827,20 @@
         )
 
 
-def main(args):
+def main():
+    if not pathlib.Path("/etc/cros_chroot_version").exists():
+        sys.exit("This script can only be run within the chroot.")
+
+    parser = argparse.ArgumentParser(description="Vendor packages properly")
+    parser.add_argument(
+        "--skip-license-check",
+        "-s",
+        help="Skip the license check on a specific package",
+        action="append",
+    )
+    parser.add_argument("--license-map", help="Write license map to this file")
+    args = parser.parse_args()
+
     current_path = pathlib.Path(__file__).parent.absolute()
     patches = os.path.join(current_path, "patches")
     vendor = os.path.join(current_path, "vendor")
@@ -866,14 +881,4 @@
 
 
 if __name__ == "__main__":
-    parser = argparse.ArgumentParser(description="Vendor packages properly")
-    parser.add_argument(
-        "--skip-license-check",
-        "-s",
-        help="Skip the license check on a specific package",
-        action="append",
-    )
-    parser.add_argument("--license-map", help="Write license map to this file")
-    args = parser.parse_args()
-
-    main(args)
+    main()