Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -5,26 +5,56 @@ import time
|
|
5 |
import subprocess
|
6 |
import os
|
7 |
import shutil
|
8 |
-
|
9 |
import stat
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
|
|
11 |
def remove_readonly(func, path, excinfo):
|
12 |
os.chmod(path, stat.S_IWRITE)
|
13 |
func(path)
|
14 |
|
|
|
15 |
def clone_or_update_repo():
|
|
|
|
|
16 |
if not os.path.exists(REPO_DIR):
|
|
|
17 |
subprocess.run(["git", "clone", REPO_URL])
|
18 |
else:
|
19 |
-
|
|
|
|
|
|
|
|
|
20 |
|
21 |
if os.path.exists(TARGET_FILE):
|
|
|
22 |
with open(TARGET_FILE, "r", encoding="utf-8") as f:
|
23 |
content = f.read()
|
24 |
new_content = content.replace("https://studio.penguinmod.com", "*")
|
25 |
with open(TARGET_FILE, "w", encoding="utf-8") as f:
|
26 |
f.write(new_content)
|
27 |
|
|
|
28 |
os.makedirs("templates", exist_ok=True)
|
29 |
shutil.copy(os.path.join(REPO_DIR, "index.html"), "templates/index.html")
|
30 |
|
@@ -34,31 +64,23 @@ def clone_or_update_repo():
|
|
34 |
os.makedirs("static", exist_ok=True)
|
35 |
shutil.copytree(os.path.join(REPO_DIR, "pages"), dest_pages)
|
36 |
|
|
|
37 |
|
38 |
-
|
39 |
-
REPO_DIR = "SharkPools-Extensions"
|
40 |
-
TARGET_FILE = os.path.join(REPO_DIR, "pages", "startup.js")
|
41 |
-
|
42 |
-
app = Flask(__name__, template_folder="templates", static_folder="static")
|
43 |
-
|
44 |
-
@app.route("/")
|
45 |
-
def index():
|
46 |
-
return render_template("index.html")
|
47 |
-
|
48 |
-
@app.route('/static/<path:filename>')
|
49 |
-
def serve_static(filename):
|
50 |
-
return send_from_directory(app.static_folder, filename)
|
51 |
-
|
52 |
def run_scheduler():
|
53 |
-
schedule.every().
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
|
|
|
|
57 |
|
58 |
-
#
|
59 |
clone_or_update_repo()
|
60 |
scheduler_thread = threading.Thread(target=run_scheduler, daemon=True)
|
61 |
scheduler_thread.start()
|
62 |
|
|
|
63 |
if __name__ == "__main__":
|
64 |
app.run(debug=True, host="0.0.0.0", port=7860)
|
|
|
5 |
import subprocess
|
6 |
import os
|
7 |
import shutil
|
|
|
8 |
import stat
|
9 |
+
import logging
|
10 |
+
|
11 |
+
# ロギング設定
|
12 |
+
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
|
13 |
+
|
14 |
+
# 定数
|
15 |
+
REPO_URL = "https://github.com/SharkPool-SP/SharkPools-Extensions.git"
|
16 |
+
REPO_DIR = "SharkPools-Extensions"
|
17 |
+
TARGET_FILE = os.path.join(REPO_DIR, "pages", "startup.js")
|
18 |
+
|
19 |
+
# Flask アプリケーション設定
|
20 |
+
app = Flask(__name__, template_folder="templates", static_folder="static")
|
21 |
+
|
22 |
+
@app.route("/")
|
23 |
+
def index():
|
24 |
+
return render_template("index.html")
|
25 |
+
|
26 |
+
@app.route('/static/<path:filename>')
|
27 |
+
def serve_static(filename):
|
28 |
+
return send_from_directory(app.static_folder, filename)
|
29 |
|
30 |
+
# 読み取り専用ファイルを削除できるようにする
|
31 |
def remove_readonly(func, path, excinfo):
|
32 |
os.chmod(path, stat.S_IWRITE)
|
33 |
func(path)
|
34 |
|
35 |
+
# リポジトリをクローンまたは更新し、ファイルを修正・コピーする関数
|
36 |
def clone_or_update_repo():
|
37 |
+
logging.info("リポジトリの更新を開始します。")
|
38 |
+
|
39 |
if not os.path.exists(REPO_DIR):
|
40 |
+
logging.info("リポジトリをクローン中...")
|
41 |
subprocess.run(["git", "clone", REPO_URL])
|
42 |
else:
|
43 |
+
logging.info("リポジトリをプル中...")
|
44 |
+
result = subprocess.run(["git", "-C", REPO_DIR, "pull"], capture_output=True, text=True)
|
45 |
+
logging.info(result.stdout)
|
46 |
+
if result.stderr:
|
47 |
+
logging.error(result.stderr)
|
48 |
|
49 |
if os.path.exists(TARGET_FILE):
|
50 |
+
logging.info("startup.js を修正中...")
|
51 |
with open(TARGET_FILE, "r", encoding="utf-8") as f:
|
52 |
content = f.read()
|
53 |
new_content = content.replace("https://studio.penguinmod.com", "*")
|
54 |
with open(TARGET_FILE, "w", encoding="utf-8") as f:
|
55 |
f.write(new_content)
|
56 |
|
57 |
+
logging.info("テンプレートと静的ファイルをコピー中...")
|
58 |
os.makedirs("templates", exist_ok=True)
|
59 |
shutil.copy(os.path.join(REPO_DIR, "index.html"), "templates/index.html")
|
60 |
|
|
|
64 |
os.makedirs("static", exist_ok=True)
|
65 |
shutil.copytree(os.path.join(REPO_DIR, "pages"), dest_pages)
|
66 |
|
67 |
+
logging.info("リポジトリの更新が完了しました。")
|
68 |
|
69 |
+
# スケジューラを実行する関数(1分ごと)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
def run_scheduler():
|
71 |
+
schedule.every(1).minutes.do(clone_or_update_repo)
|
72 |
+
try:
|
73 |
+
while True:
|
74 |
+
schedule.run_pending()
|
75 |
+
time.sleep(1)
|
76 |
+
except Exception as e:
|
77 |
+
logging.error(f"スケジューラでエラーが発生しました: {e}")
|
78 |
|
79 |
+
# 初回実行およびスケジューラ起動
|
80 |
clone_or_update_repo()
|
81 |
scheduler_thread = threading.Thread(target=run_scheduler, daemon=True)
|
82 |
scheduler_thread.start()
|
83 |
|
84 |
+
# アプリ起動
|
85 |
if __name__ == "__main__":
|
86 |
app.run(debug=True, host="0.0.0.0", port=7860)
|