|  | 
| 5 | 5 | Without any arguments builds docs for all active versions and | 
| 6 | 6 | languages. | 
| 7 | 7 | 
 | 
|  | 8 | +Environment variables for: | 
|  | 9 | +
 | 
|  | 10 | +- `SENTRY_DSN` (Error reporting) | 
|  | 11 | +- `FASTLY_SERVICE_ID` / `FASTLY_TOKEN` (CDN purges) | 
|  | 12 | +- `PYTHON_DOCS_ENABLE_ANALYTICS` (Enable Plausible for online docs) | 
|  | 13 | +
 | 
|  | 14 | +are read from the site configuration path for your platform | 
|  | 15 | +(/etc/xdg/docsbuild-scripts on linux) if available, | 
|  | 16 | +and can be overriden by writing a file to the user config dir | 
|  | 17 | +for your platform ($HOME/.config/docsbuild-scripts on linux). | 
|  | 18 | +The contents of the file is parsed as toml: | 
|  | 19 | +
 | 
|  | 20 | +```toml | 
|  | 21 | +[env] | 
|  | 22 | +SENTRY_DSN = "https://[email protected]/69420" | 
|  | 23 | +FASTLY_SERVICE_ID = "deadbeefdeadbeefdead" | 
|  | 24 | +FASTLY_TOKEN = "secureme!" | 
|  | 25 | +PYTHON_DOCS_ENABLE_ANALYTICS = "1" | 
|  | 26 | +``` | 
|  | 27 | +
 | 
| 8 | 28 | Languages are stored in `config.toml` while versions are discovered | 
| 9 | 29 | from the devguide. | 
| 10 | 30 | 
 | 
|  | 
| 48 | 68 | import tomlkit | 
| 49 | 69 | import urllib3 | 
| 50 | 70 | import zc.lockfile | 
|  | 71 | +from platformdirs import user_config_path, site_config_path | 
| 51 | 72 | 
 | 
| 52 | 73 | TYPE_CHECKING = False | 
| 53 | 74 | if TYPE_CHECKING: | 
| @@ -906,6 +927,7 @@ def main(): | 
| 906 | 927 |     """Script entry point.""" | 
| 907 | 928 |     args = parse_args() | 
| 908 | 929 |     setup_logging(args.log_directory, args.select_output) | 
|  | 930 | +    load_environment_variables() | 
| 909 | 931 | 
 | 
| 910 | 932 |     if args.select_output is None: | 
| 911 | 933 |         build_docs_with_lock(args, "build_docs.lock") | 
| @@ -1022,6 +1044,31 @@ def setup_logging(log_directory: Path, select_output: str | None): | 
| 1022 | 1044 |     logging.getLogger().setLevel(logging.DEBUG) | 
| 1023 | 1045 | 
 | 
| 1024 | 1046 | 
 | 
|  | 1047 | +def load_environment_variables() -> None: | 
|  | 1048 | +    _user_config_path = user_config_path("docsbuild-scripts") | 
|  | 1049 | +    _site_config_path = site_config_path("docsbuild-scripts") | 
|  | 1050 | +    if _user_config_path.is_file(): | 
|  | 1051 | +        ENV_CONF_FILE = _user_config_path | 
|  | 1052 | +    elif _site_config_path.is_file(): | 
|  | 1053 | +        ENV_CONF_FILE = _site_config_path | 
|  | 1054 | +    else: | 
|  | 1055 | +        logging.info( | 
|  | 1056 | +            "No environment variables configured. " | 
|  | 1057 | +            f"Configure in {_site_config_path} or {_user_config_path}." | 
|  | 1058 | +        ) | 
|  | 1059 | +        return | 
|  | 1060 | + | 
|  | 1061 | +    logging.info(f"Reading environment variables from {ENV_CONF_FILE}.") | 
|  | 1062 | +    if ENV_CONF_FILE == _site_config_path: | 
|  | 1063 | +        logging.info(f"You can override settings in {_user_config_path}.") | 
|  | 1064 | +    elif _site_config_path.is_file(): | 
|  | 1065 | +        logging.info(f"Overriding {_site_config_path}.") | 
|  | 1066 | +    with open(ENV_CONF_FILE, "r") as f: | 
|  | 1067 | +        for key, value in tomlkit.parse(f.read()).get("env", {}).items(): | 
|  | 1068 | +            logging.debug(f"Setting {key} in environment.") | 
|  | 1069 | +            os.environ[key] = value | 
|  | 1070 | + | 
|  | 1071 | + | 
| 1025 | 1072 | def build_docs_with_lock(args: argparse.Namespace, lockfile_name: str) -> int: | 
| 1026 | 1073 |     try: | 
| 1027 | 1074 |         lock = zc.lockfile.LockFile(HERE / lockfile_name) | 
|  | 
0 commit comments