稍微妥协一下得了

This commit is contained in:
shenjack 2024-04-20 22:24:20 +08:00
parent 7f96a37934
commit f94057cc3b
Signed by: shenjack
GPG Key ID: 7B1134A979775551

View File

@ -1,26 +1,54 @@
import os
from subprocess import run from subprocess import run
from pathlib import Path from pathlib import Path
ON_CF = os.getenv("CF_PAGES") == "1"
def get_env_info() -> dict[str, str]: def get_env_info() -> dict[str, str]:
# 读取环境变量 # 读取环境变量
env_info = {} env_info = {}
# git branch # git branch
branch = run( # if on cf, read it from env: CF_PAGES_BRANCH
["git", "branch", "--show-current"], capture_output=True, text=True, encoding="utf-8" if ON_CF:
) branch = os.getenv("CF_PAGES_BRANCH") or "unknown"
env_info["branch"] = branch.stdout.strip() else:
branch = run(
["git", "branch", "--show-current"],
capture_output=True,
text=True,
encoding="utf-8",
).stdout
env_info["branch"] = branch.strip()
# git commit hash # git commit hash
commit = run(["git", "rev-parse", "HEAD"], capture_output=True, text=True, encoding="utf-8") # on cf -> get from CF_PAGES_COMMIT_SHA
env_info["commit"] = commit.stdout.strip() if ON_CF:
commit = os.getenv("CF_PAGES_COMMIT_SHA") or "unknown"
else:
commit = run(
["git", "rev-parse", "HEAD"],
capture_output=True,
text=True,
encoding="utf-8",
).stdout
env_info["commit"] = commit.strip()
# git commit message # git commit message
message = run( message = run(
["git", "log", "-1", "--pretty=%B"], capture_output=True, text=True, encoding="utf-8" ["git", "log", "-1", "--pretty=%B"],
capture_output=True,
text=True,
encoding="utf-8",
) )
env_info["message"] = message.stdout.strip() env_info["message"] = message.stdout.strip()
# git tag # git tag
tag = run(["git", "describe", "--tags"], capture_output=True, text=True, encoding="utf-8") if ON_CF:
env_info["tag"] = tag.stdout.strip() tag = os.getenv("CF_PAGES_COMMIT_TAG") or "cf"
else:
tag = run(
["git", "describe", "--tags"], capture_output=True, text=True, encoding="utf-8"
).stdout
env_info["tag"] = tag.strip()
return env_info return env_info
@ -70,7 +98,9 @@ if __name__ == "__main__":
marker = marker_template.format(version_info) marker = marker_template.format(version_info)
print(f"Master: {border}\n{marker}\n") print(f"Master: {border}\n{marker}\n")
raw_content = raw_content.replace(border_raw, border).replace(marker_raw, marker) raw_content = raw_content.replace(border_raw, border).replace(
marker_raw, marker
)
# 写入文件 # 写入文件
try: try: