Skip to content

Commit bd0e222

Browse files
ewdurbinAA-Turner
andauthored
Move environment variables to a configuration file (#269)
Co-authored-by: Adam Turner <[email protected]>
1 parent 2113fd7 commit bd0e222

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

build_docs.py

+47
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,26 @@
55
Without any arguments builds docs for all active versions and
66
languages.
77
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+
828
Languages are stored in `config.toml` while versions are discovered
929
from the devguide.
1030
@@ -48,6 +68,7 @@
4868
import tomlkit
4969
import urllib3
5070
import zc.lockfile
71+
from platformdirs import user_config_path, site_config_path
5172

5273
TYPE_CHECKING = False
5374
if TYPE_CHECKING:
@@ -906,6 +927,7 @@ def main():
906927
"""Script entry point."""
907928
args = parse_args()
908929
setup_logging(args.log_directory, args.select_output)
930+
load_environment_variables()
909931

910932
if args.select_output is None:
911933
build_docs_with_lock(args, "build_docs.lock")
@@ -1022,6 +1044,31 @@ def setup_logging(log_directory: Path, select_output: str | None):
10221044
logging.getLogger().setLevel(logging.DEBUG)
10231045

10241046

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+
10251072
def build_docs_with_lock(args: argparse.Namespace, lockfile_name: str) -> int:
10261073
try:
10271074
lock = zc.lockfile.LockFile(HERE / lockfile_name)

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
jinja2
2+
platformdirs
23
sentry-sdk>=2
34
tomlkit>=0.13
45
urllib3>=2

0 commit comments

Comments
 (0)