feat: print log & timer, change app dir
Browse files
app.py
CHANGED
@@ -1,15 +1,27 @@
|
|
1 |
import os
|
2 |
import shutil
|
|
|
3 |
import git
|
4 |
import subprocess
|
5 |
|
6 |
-
REPO_DIR = 'repo'
|
7 |
USER = os.getenv('HF_USER')
|
8 |
REPO_URL = os.getenv('HF_REPO_URL')
|
9 |
ACCESS_TOKEN = os.getenv('HF_ACCESS_TOKEN')
|
10 |
AUTH_REPO_URL = REPO_URL.replace('https://', f'https://{USER}:{ACCESS_TOKEN}@')
|
11 |
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
def check_envs():
|
14 |
envs = [USER, REPO_URL, ACCESS_TOKEN]
|
15 |
missing_vars = [var for var in envs if var is None]
|
@@ -21,7 +33,9 @@ def check_envs():
|
|
21 |
def pull() -> int:
|
22 |
try:
|
23 |
repo = git.Repo(REPO_DIR)
|
|
|
24 |
repo.remotes.origin.pull()
|
|
|
25 |
return 200
|
26 |
except git.exc.InvalidGitRepositoryError:
|
27 |
# 404
|
@@ -41,18 +55,22 @@ def clone(status_code):
|
|
41 |
if status_code == 500:
|
42 |
if os.path.exists(REPO_DIR):
|
43 |
shutil.rmtree(REPO_DIR)
|
|
|
44 |
|
45 |
try:
|
|
|
46 |
git.Repo.clone_from(AUTH_REPO_URL, REPO_DIR, branch='main')
|
|
|
47 |
except git.exc.GitCommandError as e:
|
48 |
print(f"克隆代碼時出錯:{e}")
|
49 |
raise e
|
50 |
|
51 |
|
52 |
def run():
|
53 |
-
os.chdir(os.path.join(
|
|
|
54 |
subprocess.run(["python", "app.py"], check=True)
|
55 |
-
|
56 |
|
57 |
def main():
|
58 |
check_envs()
|
|
|
1 |
import os
|
2 |
import shutil
|
3 |
+
import time
|
4 |
import git
|
5 |
import subprocess
|
6 |
|
7 |
+
REPO_DIR = '/data/repo/'
|
8 |
USER = os.getenv('HF_USER')
|
9 |
REPO_URL = os.getenv('HF_REPO_URL')
|
10 |
ACCESS_TOKEN = os.getenv('HF_ACCESS_TOKEN')
|
11 |
AUTH_REPO_URL = REPO_URL.replace('https://', f'https://{USER}:{ACCESS_TOKEN}@')
|
12 |
|
13 |
|
14 |
+
def timer(func):
|
15 |
+
def wrapper(*args, **kwargs):
|
16 |
+
start_time = time.time()
|
17 |
+
result = func(*args, **kwargs)
|
18 |
+
end_time = time.time()
|
19 |
+
elapsed_time = end_time - start_time
|
20 |
+
print(f'建置時間:{elapsed_time:.2f} 秒', )
|
21 |
+
return result
|
22 |
+
return wrapper
|
23 |
+
|
24 |
+
|
25 |
def check_envs():
|
26 |
envs = [USER, REPO_URL, ACCESS_TOKEN]
|
27 |
missing_vars = [var for var in envs if var is None]
|
|
|
33 |
def pull() -> int:
|
34 |
try:
|
35 |
repo = git.Repo(REPO_DIR)
|
36 |
+
print('嘗試拉取代代碼...')
|
37 |
repo.remotes.origin.pull()
|
38 |
+
print('拉取代代碼成功!')
|
39 |
return 200
|
40 |
except git.exc.InvalidGitRepositoryError:
|
41 |
# 404
|
|
|
55 |
if status_code == 500:
|
56 |
if os.path.exists(REPO_DIR):
|
57 |
shutil.rmtree(REPO_DIR)
|
58 |
+
print('狀態碼500,刪除舊repo...')
|
59 |
|
60 |
try:
|
61 |
+
print('嘗試CLONE REPO...')
|
62 |
git.Repo.clone_from(AUTH_REPO_URL, REPO_DIR, branch='main')
|
63 |
+
print('嘗試CLONE 成功!')
|
64 |
except git.exc.GitCommandError as e:
|
65 |
print(f"克隆代碼時出錯:{e}")
|
66 |
raise e
|
67 |
|
68 |
|
69 |
def run():
|
70 |
+
os.chdir(os.path.join(REPO_DIR))
|
71 |
+
print('嘗試運行主程式...')
|
72 |
subprocess.run(["python", "app.py"], check=True)
|
73 |
+
print('運行成功!')
|
74 |
|
75 |
def main():
|
76 |
check_envs()
|