Karan Adhikari commited on
fix update issue
Browse files
update.py
CHANGED
|
@@ -1,11 +1,12 @@
|
|
| 1 |
-
from logging import FileHandler, StreamHandler, INFO,
|
| 2 |
from os import path as ospath, environ
|
| 3 |
from pathlib import Path
|
| 4 |
-
from subprocess import run as srun
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
from datetime import datetime
|
| 7 |
import pytz
|
| 8 |
import shutil
|
|
|
|
| 9 |
IST = pytz.timezone("Asia/Kolkata")
|
| 10 |
|
| 11 |
class ISTFormatter(Formatter):
|
|
@@ -17,21 +18,52 @@ log_file = "log.txt"
|
|
| 17 |
if ospath.exists(log_file):
|
| 18 |
with open(log_file, "w") as f:
|
| 19 |
f.truncate(0)
|
| 20 |
-
if Path(".git").exists():
|
|
|
|
|
|
|
| 21 |
file_handler = FileHandler(log_file)
|
| 22 |
stream_handler = StreamHandler()
|
| 23 |
-
|
| 24 |
formatter = ISTFormatter("[%(asctime)s] [%(levelname)s] - %(message)s", "%d-%b-%y %I:%M:%S %p")
|
| 25 |
file_handler.setFormatter(formatter)
|
| 26 |
stream_handler.setFormatter(formatter)
|
| 27 |
-
|
| 28 |
basicConfig(handlers=[file_handler, stream_handler], level=INFO)
|
| 29 |
|
|
|
|
| 30 |
load_dotenv("config.env")
|
| 31 |
|
| 32 |
-
UPSTREAM_REPO = environ.get("UPSTREAM_REPO", "").strip() or None
|
| 33 |
-
UPSTREAM_BRANCH = environ.get("UPSTREAM_BRANCH", "").strip() or "master"
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
if UPSTREAM_REPO:
|
| 36 |
if Path(".git").exists():
|
| 37 |
srun(["rm", "-rf", ".git"])
|
|
@@ -55,7 +87,6 @@ if UPSTREAM_REPO:
|
|
| 55 |
log_info("Successfully updated with latest commits!!")
|
| 56 |
commit_check = srun(["git", "rev-parse", "HEAD"], capture_output=True, text=True)
|
| 57 |
if commit_check.returncode == 0:
|
| 58 |
-
|
| 59 |
-
log_info(f"Latest commit ID: {commit_id}")
|
| 60 |
else:
|
| 61 |
log_error("β Update failed! Retry or ask for support.")
|
|
|
|
| 1 |
+
from logging import FileHandler, StreamHandler, INFO, Formatter, basicConfig, error as log_error, info as log_info
|
| 2 |
from os import path as ospath, environ
|
| 3 |
from pathlib import Path
|
| 4 |
+
from subprocess import run as srun
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
from datetime import datetime
|
| 7 |
import pytz
|
| 8 |
import shutil
|
| 9 |
+
|
| 10 |
IST = pytz.timezone("Asia/Kolkata")
|
| 11 |
|
| 12 |
class ISTFormatter(Formatter):
|
|
|
|
| 18 |
if ospath.exists(log_file):
|
| 19 |
with open(log_file, "w") as f:
|
| 20 |
f.truncate(0)
|
| 21 |
+
if Path(".git").exists():
|
| 22 |
+
shutil.rmtree(".git")
|
| 23 |
+
|
| 24 |
file_handler = FileHandler(log_file)
|
| 25 |
stream_handler = StreamHandler()
|
|
|
|
| 26 |
formatter = ISTFormatter("[%(asctime)s] [%(levelname)s] - %(message)s", "%d-%b-%y %I:%M:%S %p")
|
| 27 |
file_handler.setFormatter(formatter)
|
| 28 |
stream_handler.setFormatter(formatter)
|
|
|
|
| 29 |
basicConfig(handlers=[file_handler, stream_handler], level=INFO)
|
| 30 |
|
| 31 |
+
# ββ Load config.env as the base (for DATABASE URI, etc.) βββββββββββββββββββββ
|
| 32 |
load_dotenv("config.env")
|
| 33 |
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
def _fetch_upstream_from_db() -> tuple[str | None, str]:
|
| 36 |
+
try:
|
| 37 |
+
from pymongo import MongoClient
|
| 38 |
+
raw_uris = environ.get("DATABASE", "")
|
| 39 |
+
uris = [u.strip() for u in raw_uris.replace(",", " ").split() if u.strip()]
|
| 40 |
+
if not uris:
|
| 41 |
+
log_info("update.py: No DATABASE found β skipping DB settings lookup.")
|
| 42 |
+
return None, "master"
|
| 43 |
+
|
| 44 |
+
tracking_uri = uris[0]
|
| 45 |
+
client = MongoClient(tracking_uri, serverSelectionTimeoutMS=5000)
|
| 46 |
+
doc = client["dbFyvio"]["settings"].find_one({"_id": "app_settings"})
|
| 47 |
+
client.close()
|
| 48 |
+
|
| 49 |
+
if doc:
|
| 50 |
+
repo = (doc.get("upstream_repo") or "").strip() or None
|
| 51 |
+
branch = (doc.get("upstream_branch") or "").strip() or "master"
|
| 52 |
+
return repo, branch
|
| 53 |
+
|
| 54 |
+
except Exception as exc:
|
| 55 |
+
log_error(f"update.py: DB lookup failed ({exc}) β falling back to config.env.")
|
| 56 |
+
|
| 57 |
+
return None, "master"
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
# ββ Priority: DB value > config.env value ββββββββββββββββββββββββββββββββββ
|
| 61 |
+
db_repo, db_branch = _fetch_upstream_from_db()
|
| 62 |
+
|
| 63 |
+
UPSTREAM_REPO = db_repo or environ.get("UPSTREAM_REPO", "").strip() or None
|
| 64 |
+
UPSTREAM_BRANCH = db_branch or environ.get("UPSTREAM_BRANCH", "").strip() or "master"
|
| 65 |
+
|
| 66 |
+
# ββ Git update ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 67 |
if UPSTREAM_REPO:
|
| 68 |
if Path(".git").exists():
|
| 69 |
srun(["rm", "-rf", ".git"])
|
|
|
|
| 87 |
log_info("Successfully updated with latest commits!!")
|
| 88 |
commit_check = srun(["git", "rev-parse", "HEAD"], capture_output=True, text=True)
|
| 89 |
if commit_check.returncode == 0:
|
| 90 |
+
log_info(f"Latest commit ID: {commit_check.stdout.strip()}")
|
|
|
|
| 91 |
else:
|
| 92 |
log_error("β Update failed! Retry or ask for support.")
|