文档修改
This commit is contained in:
+35
-21
@@ -24,24 +24,7 @@ _TEMPLATE_DIR = os.path.join(_REPO_ROOT, "license_templates")
|
||||
BJ_TZ = timezone(timedelta(hours=8))
|
||||
|
||||
|
||||
def load_shared_env() -> None:
|
||||
"""从仓库根目录 .env 加载 LICENSE_*(子项目 .env 已存在同名变量时不覆盖)。"""
|
||||
path = os.path.join(_REPO_ROOT, ".env")
|
||||
if not os.path.isfile(path):
|
||||
return
|
||||
try:
|
||||
raw_bytes = open(path, "rb").read()
|
||||
except OSError:
|
||||
return
|
||||
text = ""
|
||||
for enc in ("utf-8-sig", "utf-16", "utf-16-le", "utf-16-be"):
|
||||
try:
|
||||
text = raw_bytes.decode(enc)
|
||||
break
|
||||
except Exception:
|
||||
continue
|
||||
if not text:
|
||||
text = raw_bytes.decode("utf-8", errors="ignore")
|
||||
def _apply_env_lines(text: str, *, license_only: bool, override: bool) -> None:
|
||||
text = text.replace("\x00", "")
|
||||
for line in text.splitlines():
|
||||
raw = line.strip()
|
||||
@@ -49,14 +32,45 @@ def load_shared_env() -> None:
|
||||
continue
|
||||
key, value = raw.split("=", 1)
|
||||
clean_key = key.strip().lstrip("\ufeff")
|
||||
if not clean_key.startswith("LICENSE_"):
|
||||
continue
|
||||
if clean_key in os.environ and (os.environ.get(clean_key) or "").strip():
|
||||
if license_only and not clean_key.startswith("LICENSE_"):
|
||||
continue
|
||||
clean_value = value.strip().strip('"').strip("'")
|
||||
if not clean_value:
|
||||
continue
|
||||
if not override and clean_key in os.environ and (os.environ.get(clean_key) or "").strip():
|
||||
continue
|
||||
os.environ[clean_key] = clean_value
|
||||
|
||||
|
||||
def _read_env_file(path: str) -> str:
|
||||
if not os.path.isfile(path):
|
||||
return ""
|
||||
try:
|
||||
raw_bytes = open(path, "rb").read()
|
||||
except OSError:
|
||||
return ""
|
||||
for enc in ("utf-8-sig", "utf-16", "utf-16-le", "utf-16-be"):
|
||||
try:
|
||||
return raw_bytes.decode(enc)
|
||||
except Exception:
|
||||
continue
|
||||
return raw_bytes.decode("utf-8", errors="ignore")
|
||||
|
||||
|
||||
def load_shared_env() -> None:
|
||||
"""
|
||||
加载顺序(后加载的可补空位):
|
||||
1. 仓库根 license.env — 默认授权地址 https://sq.bz121.com
|
||||
2. 仓库根 .env 中的 LICENSE_* — 可覆盖密钥等(勿提交 Git)
|
||||
子目录 .env 若已先加载且已有 LICENSE_*,则不被覆盖。
|
||||
"""
|
||||
license_path = os.path.join(_REPO_ROOT, "license.env")
|
||||
_apply_env_lines(_read_env_file(license_path), license_only=True, override=True)
|
||||
|
||||
root_env = os.path.join(_REPO_ROOT, ".env")
|
||||
_apply_env_lines(_read_env_file(root_env), license_only=True, override=True)
|
||||
|
||||
|
||||
def _env_bool(name: str, default: bool = False) -> bool:
|
||||
return (os.getenv(name) or "").strip().lower() in ("1", "true", "yes", "on")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user