Files
jupyter-collection/.hub/jupyter_config_backup.py

32 lines
1004 B
Python
Raw Normal View History

2025-10-23 07:43:47 +08:00
import shutil, configparser
2025-10-22 11:07:27 +08:00
from pathlib import Path
2025-10-23 07:43:47 +08:00
# 仓库在host上的地址
SHARED_ROOT = Path("/home/zhangyi/jupyter-collection")
CTRL_FILE = SHARED_ROOT / ".hub" / "resource_map.ini"
2025-10-22 11:07:27 +08:00
2025-10-23 07:43:47 +08:00
def load_map():
2025-10-22 11:07:27 +08:00
cfg = configparser.ConfigParser()
cfg.read(CTRL_FILE)
2025-10-23 07:43:47 +08:00
# 用户名 -> [res1, res2, ...]
return {u: [r.strip() for r in v.split(",")]
for u, v in cfg.items("users")}
2025-10-22 11:07:27 +08:00
2025-10-23 07:43:47 +08:00
def copy_resources(spawner):
2025-10-22 11:07:27 +08:00
username = spawner.user.name
2025-10-23 07:43:47 +08:00
shared = Path(spawner.environment.get("HOME", f"/home/jupyter-{username}")) / "shared"
2025-10-22 11:07:27 +08:00
2025-10-23 07:43:47 +08:00
# 清空或新建 shared
2025-10-22 19:40:22 +08:00
if shared.exists():
shutil.rmtree(shared)
shared.mkdir(parents=True, exist_ok=True)
2025-10-22 11:07:27 +08:00
2025-10-23 07:43:47 +08:00
# 按 ini 拷贝资源
for res in load_map().get(username, []):
src = SHARED_ROOT / res
dst = shared / res
if src.is_dir():
shutil.copytree(src, dst, dirs_exist_ok=True)
2025-10-22 11:07:27 +08:00
2025-10-23 07:43:47 +08:00
# ========== 用户容器启动完成后执行 ==========
c.Spawner.post_spawn_hook = copy_resources