Spaces:
Running
Running
Update startup.py
Browse files- startup.py +62 -25
startup.py
CHANGED
|
@@ -1,46 +1,83 @@
|
|
| 1 |
# startup.py
|
| 2 |
-
import os, pathlib, json, shutil
|
| 3 |
|
| 4 |
-
BASE = pathlib.Path(__file__).parent
|
| 5 |
-
|
|
|
|
| 6 |
|
| 7 |
-
# 选
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
try:
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
(RUNTIME / ".
|
| 16 |
-
(RUNTIME / ".write_test").unlink(missing_ok=True)
|
| 17 |
except Exception:
|
| 18 |
-
RUNTIME =
|
| 19 |
RUNTIME.mkdir(parents=True, exist_ok=True)
|
| 20 |
|
| 21 |
-
# 把
|
| 22 |
-
|
| 23 |
-
shutil.copytree(SRC_READONLY, RUNTIME, dirs_exist_ok=True)
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
|
| 27 |
-
|
| 28 |
|
| 29 |
-
if
|
| 30 |
-
(RUNTIME / "cookies.txt").write_text(
|
| 31 |
print(f"✅ cookies.txt -> {RUNTIME/'cookies.txt'}")
|
| 32 |
|
| 33 |
-
if
|
| 34 |
try:
|
| 35 |
-
data = json.loads(
|
| 36 |
(RUNTIME / "students.json").write_text(
|
| 37 |
json.dumps(data, ensure_ascii=False, indent=2), encoding="utf-8"
|
| 38 |
)
|
| 39 |
print(f"✅ students.json (parsed JSON) -> {RUNTIME/'students.json'}")
|
| 40 |
except Exception:
|
| 41 |
-
(RUNTIME / "students.json").write_text(
|
| 42 |
print(f"✅ students.json (raw text) -> {RUNTIME/'students.json'}")
|
| 43 |
|
| 44 |
-
#
|
| 45 |
os.environ["APP_RUNTIME_DIR_RESOLVED"] = str(RUNTIME)
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# startup.py
|
| 2 |
+
import os, pathlib, json, shutil
|
| 3 |
|
| 4 |
+
BASE = pathlib.Path(__file__).parent # 通常是 /app
|
| 5 |
+
# 可选显式指定源码目录(绝对路径),若不设会自动发现:
|
| 6 |
+
APP_SOURCE_DIR = os.getenv("APP_SOURCE_DIR")
|
| 7 |
|
| 8 |
+
# 入口文件候选名(可覆盖)
|
| 9 |
+
ENTRY_HINTS = [s.strip() for s in os.getenv(
|
| 10 |
+
"APP_ENTRY_HINTS", "script.py,app.py,main.py,server.py"
|
| 11 |
+
).split(",") if s.strip()]
|
| 12 |
|
| 13 |
+
def find_source_dir() -> pathlib.Path | None:
|
| 14 |
+
# 1) 环境变量优先
|
| 15 |
+
if APP_SOURCE_DIR:
|
| 16 |
+
p = pathlib.Path(APP_SOURCE_DIR)
|
| 17 |
+
if p.is_dir(): return p
|
| 18 |
+
# 2) 常见候选根
|
| 19 |
+
candidates = [BASE, BASE / "src"]
|
| 20 |
+
for d in BASE.iterdir():
|
| 21 |
+
if d.is_dir():
|
| 22 |
+
candidates.append(d)
|
| 23 |
+
if (d / "src").is_dir():
|
| 24 |
+
candidates.append(d / "src")
|
| 25 |
+
# 3) 在候选根里按入口名搜索
|
| 26 |
+
for root in candidates:
|
| 27 |
+
for entry in ENTRY_HINTS:
|
| 28 |
+
for path in root.rglob(entry):
|
| 29 |
+
if "site-packages" in str(path):
|
| 30 |
+
continue
|
| 31 |
+
return path.parent
|
| 32 |
+
return None
|
| 33 |
+
|
| 34 |
+
SRC_READONLY = find_source_dir()
|
| 35 |
+
if not SRC_READONLY:
|
| 36 |
+
# 打印部分目录树帮助定位
|
| 37 |
+
listing = "\n".join([str(p) for p in BASE.rglob("*")][:200])
|
| 38 |
+
raise FileNotFoundError(
|
| 39 |
+
f"找不到源码目录,请设置 APP_SOURCE_DIR。已尝试在 {BASE} 下自动发现。\n"
|
| 40 |
+
f"以下为部分目录树(截断):\n{listing}"
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
# 选择可写运行目录(优先 /data,失败回退 /tmp)
|
| 44 |
+
pref = pathlib.Path(os.getenv("APP_RUNTIME_DIR", "/data/grade_query_app"))
|
| 45 |
+
RUNTIME = pref
|
| 46 |
try:
|
| 47 |
+
RUNTIME.mkdir(parents=True, exist_ok=True)
|
| 48 |
+
(RUNTIME / ".probe").write_text("ok", encoding="utf-8")
|
| 49 |
+
(RUNTIME / ".probe").unlink(missing_ok=True)
|
|
|
|
| 50 |
except Exception:
|
| 51 |
+
RUNTIME = pathlib.Path("/tmp/grade_query_app")
|
| 52 |
RUNTIME.mkdir(parents=True, exist_ok=True)
|
| 53 |
|
| 54 |
+
# 把源码复制到可写目录,保持相对路径语义(templates 等)
|
| 55 |
+
shutil.copytree(SRC_READONLY, RUNTIME, dirs_exist_ok=True)
|
|
|
|
| 56 |
|
| 57 |
+
# 把 Secrets 写成运行期文件
|
| 58 |
+
cookies_txt = os.getenv("COOKIES_TXT", "").strip()
|
| 59 |
+
students_json = os.getenv("STUDENTS_JSON", "").strip()
|
| 60 |
|
| 61 |
+
if cookies_txt:
|
| 62 |
+
(RUNTIME / "cookies.txt").write_text(cookies_txt, encoding="utf-8")
|
| 63 |
print(f"✅ cookies.txt -> {RUNTIME/'cookies.txt'}")
|
| 64 |
|
| 65 |
+
if students_json:
|
| 66 |
try:
|
| 67 |
+
data = json.loads(students_json)
|
| 68 |
(RUNTIME / "students.json").write_text(
|
| 69 |
json.dumps(data, ensure_ascii=False, indent=2), encoding="utf-8"
|
| 70 |
)
|
| 71 |
print(f"✅ students.json (parsed JSON) -> {RUNTIME/'students.json'}")
|
| 72 |
except Exception:
|
| 73 |
+
(RUNTIME / "students.json").write_text(students_json, encoding="utf-8")
|
| 74 |
print(f"✅ students.json (raw text) -> {RUNTIME/'students.json'}")
|
| 75 |
|
| 76 |
+
# 暴露给 wsgi.py 使用
|
| 77 |
os.environ["APP_RUNTIME_DIR_RESOLVED"] = str(RUNTIME)
|
| 78 |
+
for hint in ENTRY_HINTS:
|
| 79 |
+
if (RUNTIME / hint).exists():
|
| 80 |
+
os.environ["APP_ENTRY_FILE_RESOLVED"] = hint
|
| 81 |
+
break
|
| 82 |
+
|
| 83 |
+
print(f"ℹ️ Using runtime dir: {RUNTIME}, entry: {os.environ.get('APP_ENTRY_FILE_RESOLVED','(auto)')}")
|