s4s-editor-beta / app.py
soiz1's picture
Update app.py
17dc697 verified
raw
history blame
3.02 kB
from flask import Flask, render_template, send_from_directory
import schedule
import threading
import time
import subprocess
import os
import shutil
import stat
import logging
# ロギング設定
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
# 定数
REPO_URL = "https://github.com/SharkPool-SP/SharkPools-Extensions.git"
REPO_DIR = "SharkPools-Extensions"
TARGET_FILE = os.path.join(REPO_DIR, "pages", "startup.js")
# Flask アプリケーション設定
app = Flask(__name__, template_folder="templates", static_folder="static")
@app.route("/")
def index():
return render_template("index.html")
@app.route('/static/<path:filename>')
def serve_static(filename):
return send_from_directory(app.static_folder, filename)
# 読み取り専用ファイルを削除できるようにする
def remove_readonly(func, path, excinfo):
os.chmod(path, stat.S_IWRITE)
func(path)
# リポジトリをクローンまたは更新し、ファイルを修正・コピーする関数
def clone_or_update_repo():
logging.info("リポジトリの更新を開始します。")
if not os.path.exists(REPO_DIR):
logging.info("リポジトリをクローン中...")
subprocess.run(["git", "clone", REPO_URL])
else:
logging.info("リポジトリをプル中...")
result = subprocess.run(["git", "-C", REPO_DIR, "pull"], capture_output=True, text=True)
logging.info(result.stdout)
if result.stderr:
logging.error(result.stderr)
if os.path.exists(TARGET_FILE):
logging.info("startup.js を修正中...")
with open(TARGET_FILE, "r", encoding="utf-8") as f:
content = f.read()
new_content = content.replace("https://studio.penguinmod.com", "*")
with open(TARGET_FILE, "w", encoding="utf-8") as f:
f.write(new_content)
logging.info("テンプレートと静的ファイルをコピー中...")
os.makedirs("templates", exist_ok=True)
shutil.copy(os.path.join(REPO_DIR, "index.html"), "templates/index.html")
dest_pages = os.path.join("static", "pages")
if os.path.exists(dest_pages):
shutil.rmtree(dest_pages, onerror=remove_readonly)
os.makedirs("static", exist_ok=True)
shutil.copytree(os.path.join(REPO_DIR, "pages"), dest_pages)
logging.info("リポジトリの更新が完了しました。")
# スケジューラを実行する関数(1分ごと)
def run_scheduler():
schedule.every(1).minutes.do(clone_or_update_repo)
try:
while True:
schedule.run_pending()
time.sleep(1)
except Exception as e:
logging.error(f"スケジューラでエラーが発生しました: {e}")
# 初回実行およびスケジューラ起動
clone_or_update_repo()
scheduler_thread = threading.Thread(target=run_scheduler, daemon=True)
scheduler_thread.start()
# アプリ起動
if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0", port=7860)