jzyg123 commited on
Commit
46319d0
·
verified ·
1 Parent(s): 556a3e5

Update startup.py

Browse files
Files changed (1) hide show
  1. startup.py +62 -25
startup.py CHANGED
@@ -1,46 +1,83 @@
1
  # startup.py
2
- import os, pathlib, json, shutil, stat
3
 
4
- BASE = pathlib.Path(__file__).parent # /app(只读)
5
- SRC_READONLY = BASE / "src" / "成绩查询"
 
6
 
7
- # 选一个写的运行目录:优先 /data,其次 /tmp
8
- preferred = pathlib.Path(os.getenv("APP_RUNTIME_DIR", "/data/grade_query_app"))
9
- fallback = pathlib.Path("/tmp/grade_query_app")
10
- RUNTIME = preferred
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  try:
13
- (RUNTIME).mkdir(parents=True, exist_ok=True)
14
- # 试写一个探针文件,判断是否可写
15
- (RUNTIME / ".write_test").write_text("ok", encoding="utf-8")
16
- (RUNTIME / ".write_test").unlink(missing_ok=True)
17
  except Exception:
18
- RUNTIME = fallback
19
  RUNTIME.mkdir(parents=True, exist_ok=True)
20
 
21
- # 把只读源码复制到可写运行目录留模板等相对路径)
22
- if not (RUNTIME / "script.py").exists():
23
- shutil.copytree(SRC_READONLY, RUNTIME, dirs_exist_ok=True)
24
 
25
- # Secrets 写 cookies 与学生清单到可写目录
26
- cookies_txt_env = os.environ.get("COOKIES_TXT", "").strip()
27
- students_json_env = os.environ.get("STUDENTS_JSON", "").strip()
28
 
29
- if cookies_txt_env:
30
- (RUNTIME / "cookies.txt").write_text(cookies_txt_env, encoding="utf-8")
31
  print(f"✅ cookies.txt -> {RUNTIME/'cookies.txt'}")
32
 
33
- if students_json_env:
34
  try:
35
- data = json.loads(students_json_env)
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(students_json_env, encoding="utf-8")
42
  print(f"✅ students.json (raw text) -> {RUNTIME/'students.json'}")
43
 
44
- # 为后续 wsgi.py 使用暴露路径
45
  os.environ["APP_RUNTIME_DIR_RESOLVED"] = str(RUNTIME)
46
- print(f"ℹ️ Using runtime dir: {RUNTIME}")
 
 
 
 
 
 
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)')}")