"""Route every `std::env::var` / `var_os` read of a crate's production code through one `settings` module of that crate (STRA-338 debt (b), `env-read` ratchet). The read itself, its parsing and its place in the code do not change; the crate gets `src/settings.rs` with two inline functions that are the only readers of the process environment, so that a later step can replace them by a parsed `Settings` value without touching the call sites again. Usage: python scripts/dev/funnel_env_reads.py CRATE_DIR [--dry-run] Test files and the code after the first `#[cfg(test)] mod` are not touched. Offline refactoring aid; not part of the product. """ import pathlib import re import sys SETTINGS = '''//! The only place of this crate that reads the process environment (`env-read` ratchet of `architecture.json`). //! The reads keep their call sites and their parsing; a later step replaces these functions by a parsed settings value. use std::env::{self, VarError}; use std::ffi::{OsStr, OsString}; /// `std::env::var`: the text of an environment variable. #[inline] pub(crate) fn var>(key: K) -> Result { env::var(key) } /// `std::env::var_os`: an environment variable that may not be valid text. #[inline] pub(crate) fn var_os>(key: K) -> Option { env::var_os(key) } ''' READ = re.compile(r"(?