preupload: Depend on sourcing build/setup.sh

This eliminates the duplication of the rust toolchain version in the script, so
we can rely on one single source of truth for this value in setup.sh.

Change-Id: I145218062015f8df0984e523f1aea91ea4b1d154
diff --git a/preupload-hooks/rustfmt.py b/preupload-hooks/rustfmt.py
index 7922788..dac1dfa 100755
--- a/preupload-hooks/rustfmt.py
+++ b/preupload-hooks/rustfmt.py
@@ -22,9 +22,6 @@
 def get_parser():
     """Return a command line parser."""
     parser = argparse.ArgumentParser(description=__doc__)
-    parser.add_argument("--rustfmt_path",
-                        default="rustfmt",
-                        help="The path to the rustfmt binary.")
     parser.add_argument('files',
                         type=str,
                         nargs='*',
@@ -35,28 +32,21 @@
 
 def main(argv):
     """The main entry."""
+    if not os.getenv("ROOTDIR"):
+        print("source build/setup.sh first")
+        sys.exit(1)
+
     parser = get_parser()
     opts = parser.parse_args(argv)
 
-    # Check and set rustfmt path in case `source build/setup.sh` is not run in
-    # the shell session. In repo preupload, the path should be set in
-    # PREUPLOAD.cfg.
-    if opts.rustfmt_path != "rustfmt":
-        # Add rustfmt path to system PATH and set up RUSTUP_HOME at one level up
-        # rustfmt has to have both variables set up to work properly.
-        path = os.path.realpath(opts.rustfmt_path + "/..")
-        os.environ["PATH"] = path + ":" + os.getenv("PATH")
-        os.environ["RUSTUP_HOME"] = path + "/.."
-
     # Only process .rs files
     file_list = [f for f in opts.files if f.endswith("rs")]
     if not file_list:
         sys.exit(0)
 
-    nightly_flag = os.getenv("CANTRIP_RUST_VERSION") if os.getenv(
-        "CANTRIP_RUST_VERSION") else "nightly-2021-11-05"
+    nightly_flag = os.getenv("CANTRIP_RUST_VERSION")
 
-    cmd = [opts.rustfmt_path, f"+{nightly_flag}", "--check", "--color", "never"]
+    cmd = ["rustfmt", f"+{nightly_flag}", "--check", "--color", "never"]
 
     for f in file_list:
         cmd.append(f)