"""Create ignored local-development secrets without printing their values.""" import os import secrets from pathlib import Path root = Path(__file__).resolve().parents[1] text = (root / ".env.example").read_text() text = text.replace( "IDENTITY_SECRET=\n", "IDENTITY_SECRET=" + secrets.token_urlsafe(48) + "\n" ) text = text.replace( "SERVICE_TOKEN=\n", "SERVICE_TOKEN=" + secrets.token_urlsafe(48) + "\n" ) try: descriptor = os.open(root / ".env", os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600) except FileExistsError: raise SystemExit(".env already exists; left unchanged") from None with os.fdopen(descriptor, "w") as output: output.write(text) print("Created local .env with generated secrets. Values are not printed.")