52 lines
1.7 KiB
Python
52 lines
1.7 KiB
Python
"""Inject secrets.ini into the build as -D flags.
|
|
|
|
Keeps WiFi credentials and the enrollment secret out of git and out of
|
|
platformio.ini. Copy secrets.ini.example to secrets.ini and fill it in; the
|
|
build works without the file, it just produces an unconfigured device that
|
|
cannot associate.
|
|
"""
|
|
|
|
import configparser
|
|
import os
|
|
|
|
Import("env") # noqa: F821 (provided by SCons/PlatformIO)
|
|
|
|
SECRETS = os.path.join(env.subst("$PROJECT_DIR"), "secrets.ini") # noqa: F821
|
|
|
|
# key in secrets.ini -> build flag. Strings are quoted, numbers are not.
|
|
STRING_KEYS = {
|
|
"wifi_ssid": "CFG_WIFI_SSID",
|
|
"wifi_pass": "CFG_WIFI_PASS",
|
|
"api_host": "CFG_API_HOST",
|
|
"enroll_secret": "CFG_ENROLL_SECRET",
|
|
"tz": "CFG_TZ",
|
|
}
|
|
NUMERIC_KEYS = {
|
|
"api_port": "CFG_API_PORT",
|
|
"deep_sleep": "CFG_DEEP_SLEEP",
|
|
}
|
|
|
|
if not os.path.isfile(SECRETS):
|
|
print("load_secrets: no secrets.ini found — building with defaults.")
|
|
print(" copy secrets.ini.example to secrets.ini before flashing.")
|
|
else:
|
|
parser = configparser.ConfigParser()
|
|
parser.read(SECRETS)
|
|
if not parser.has_section("secrets"):
|
|
raise SystemExit("load_secrets: secrets.ini has no [secrets] section")
|
|
|
|
flags = []
|
|
for key, macro in STRING_KEYS.items():
|
|
if parser.has_option("secrets", key):
|
|
value = parser.get("secrets", key)
|
|
flags.append(f'-D{macro}=\\"{value}\\"')
|
|
for key, macro in NUMERIC_KEYS.items():
|
|
if parser.has_option("secrets", key):
|
|
flags.append(f"-D{macro}={parser.get('secrets', key)}")
|
|
|
|
env.Append(CPPDEFINES=[]) # noqa: F821
|
|
env.Append(BUILD_FLAGS=flags) # noqa: F821
|
|
|
|
# Do not print the values themselves.
|
|
print(f"load_secrets: applied {len(flags)} flag(s) from secrets.ini")
|