Spaces:
Sleeping
Sleeping
Update src/utils.py
Browse files- src/utils.py +68 -26
src/utils.py
CHANGED
@@ -14,11 +14,78 @@ ulog = logging.getLogger("utils")
|
|
14 |
|
15 |
# Repo root (/home/user/app)
|
16 |
APP_DIR = Path(__file__).resolve().parents[1]
|
|
|
|
|
|
|
17 |
|
18 |
# Single source of truth for DB location:
|
19 |
# - Default to Hugging Face persistent volume at /data
|
20 |
# - Can be overridden via env var DB_PATH if needed
|
21 |
-
DB_PATH = Path(os.environ.get("DB_PATH", "/data/olist.sqlite"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
def _ensure_db_file():
|
24 |
"""
|
@@ -44,28 +111,3 @@ def _ensure_db_file():
|
|
44 |
else:
|
45 |
ulog.warning("db: no seed DB found at %s; will create empty DB at %s", seed, DB_PATH)
|
46 |
|
47 |
-
def get_connection():
|
48 |
-
"""
|
49 |
-
Open a connection to the persistent DB.
|
50 |
-
Logs path and PRAGMA database_list so you can verify which file is used.
|
51 |
-
"""
|
52 |
-
_ensure_db_file()
|
53 |
-
try:
|
54 |
-
conn = sqlite3.connect(DB_PATH, check_same_thread=False, timeout=30)
|
55 |
-
conn.execute("PRAGMA foreign_keys = ON;")
|
56 |
-
# Diagnostics
|
57 |
-
try:
|
58 |
-
size = DB_PATH.stat().st_size if DB_PATH.exists() else 0
|
59 |
-
except Exception:
|
60 |
-
size = -1
|
61 |
-
ulog.debug("db: open path=%s exists=%s size=%s bytes",
|
62 |
-
DB_PATH, DB_PATH.exists(), size)
|
63 |
-
try:
|
64 |
-
dblist = conn.execute("PRAGMA database_list").fetchall()
|
65 |
-
ulog.debug("db: PRAGMA database_list => %s", dblist)
|
66 |
-
except Exception as e:
|
67 |
-
ulog.debug("db: PRAGMA database_list failed: %s", e)
|
68 |
-
return conn
|
69 |
-
except Exception as e:
|
70 |
-
ulog.exception("db: connect failed for %s: %s", DB_PATH, e)
|
71 |
-
raise
|
|
|
14 |
|
15 |
# Repo root (/home/user/app)
|
16 |
APP_DIR = Path(__file__).resolve().parents[1]
|
17 |
+
PERSIST = Path("/data/olist.sqlite") # preferred persistent path
|
18 |
+
FALLBACK = APP_DIR / "olist.sqlite" # non-persistent fallback
|
19 |
+
ENV_DB = os.environ.get("DB_PATH")
|
20 |
|
21 |
# Single source of truth for DB location:
|
22 |
# - Default to Hugging Face persistent volume at /data
|
23 |
# - Can be overridden via env var DB_PATH if needed
|
24 |
+
#DB_PATH = Path(os.environ.get("DB_PATH", "/data/olist.sqlite"))
|
25 |
+
|
26 |
+
def _can_write_dir(d: Path) -> bool:
|
27 |
+
try:
|
28 |
+
d.mkdir(parents=True, exist_ok=True)
|
29 |
+
test = d / ".perm_test"
|
30 |
+
with open(test, "wb") as f:
|
31 |
+
f.write(b"ok")
|
32 |
+
test.unlink()
|
33 |
+
return True
|
34 |
+
except Exception as e:
|
35 |
+
ulog.warning("perm: cannot write to %s: %s", d, e)
|
36 |
+
return False
|
37 |
+
|
38 |
+
def _choose_path() -> Path:
|
39 |
+
if ENV_DB:
|
40 |
+
p = Path(ENV_DB).resolve()
|
41 |
+
# if file exists OR parent is writable, we’ll use it
|
42 |
+
if p.exists() or _can_write_dir(p.parent):
|
43 |
+
return p
|
44 |
+
ulog.warning("db: DB_PATH=%s not writable; falling back…", p)
|
45 |
+
# prefer /data if writable or already exists
|
46 |
+
if PERSIST.exists() or _can_write_dir(PERSIST.parent):
|
47 |
+
return PERSIST
|
48 |
+
# last resort: fallback in app dir
|
49 |
+
ulog.warning("db: /data not writable; using non-persistent %s", FALLBACK)
|
50 |
+
return FALLBACK
|
51 |
+
|
52 |
+
def _seed_if_missing(path: Path):
|
53 |
+
if path.exists():
|
54 |
+
return
|
55 |
+
seed = APP_DIR / "olist.sqlite" # optional repo copy
|
56 |
+
try:
|
57 |
+
path.parent.mkdir(parents=True, exist_ok=True)
|
58 |
+
except Exception as e:
|
59 |
+
ulog.exception("db: mkdir failed for %s: %s", path.parent, e)
|
60 |
+
if seed.exists():
|
61 |
+
try:
|
62 |
+
shutil.copy2(seed, path)
|
63 |
+
ulog.info("db: seeded %s from %s", path, seed)
|
64 |
+
except Exception as e:
|
65 |
+
ulog.exception("db: seed copy failed %s -> %s: %s", seed, path, e)
|
66 |
+
else:
|
67 |
+
ulog.warning("db: no seed found at %s; creating empty DB at %s", seed, path)
|
68 |
+
|
69 |
+
def get_connection():
|
70 |
+
path = _choose_path()
|
71 |
+
_seed_if_missing(path)
|
72 |
+
try:
|
73 |
+
conn = sqlite3.connect(path, check_same_thread=False, timeout=30)
|
74 |
+
conn.execute("PRAGMA foreign_keys = ON;")
|
75 |
+
try:
|
76 |
+
size = path.stat().st_size if path.exists() else 0
|
77 |
+
except Exception:
|
78 |
+
size = -1
|
79 |
+
ulog.debug("db: open path=%s exists=%s size=%s bytes", path, path.exists(), size)
|
80 |
+
try:
|
81 |
+
dblist = conn.execute("PRAGMA database_list").fetchall()
|
82 |
+
ulog.debug("db: PRAGMA database_list => %s", dblist)
|
83 |
+
except Exception as e:
|
84 |
+
ulog.debug("db: PRAGMA database_list failed: %s", e)
|
85 |
+
return conn
|
86 |
+
except Exception as e:
|
87 |
+
ulog.exception("db: connect failed for %s: %s", path, e)
|
88 |
+
raise
|
89 |
|
90 |
def _ensure_db_file():
|
91 |
"""
|
|
|
111 |
else:
|
112 |
ulog.warning("db: no seed DB found at %s; will create empty DB at %s", seed, DB_PATH)
|
113 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|