Spaces:
Sleeping
Sleeping
File size: 5,032 Bytes
c2a8ece d5896b5 a37914c d5896b5 64bcfa7 d5896b5 64bcfa7 79c1109 b11cea5 64bcfa7 b11cea5 64bcfa7 b11cea5 64bcfa7 b11cea5 79c1109 b11cea5 79c1109 5ae0f46 b11cea5 5ae0f46 b11cea5 5ae0f46 d5896b5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
from flask import Flask, render_template, send_from_directory, abort
import schedule
import threading
import time
import subprocess
import os
import shutil
import stat
import logging
# ロギング設定をDEBUGに
logging.basicConfig(level=logging.DEBUG, format="%(asctime)s [%(levelname)s] %(message)s")
# FlaskのロガーもDEBUGに
app.logger.setLevel(logging.DEBUG)
# 定数
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",
static_url_path="")
@app.route("/")
def index():
return render_template("index.html")
@app.route("/<path:filepath>")
def serve_any(filepath):
app.logger.info(f"Request for file: {filepath}")
# static フォルダ内のファイルをチェック
static_path = os.path.join(app.static_folder, filepath)
app.logger.info(f"Checking static path: {static_path}")
if os.path.isfile(static_path):
app.logger.info(f"Serving from static: {filepath}")
return send_from_directory(app.static_folder, filepath)
# REPO_DIR内のファイルをチェック
full_path = os.path.join(REPO_DIR, filepath)
app.logger.info(f"Checking repo path: {full_path}")
if os.path.isfile(full_path):
directory, filename = os.path.split(full_path)
app.logger.info(f"Serving from repo: directory={directory}, filename={filename}")
return send_from_directory(directory, filename)
app.logger.warning(f"File not found: {filepath}")
abort(404)
# 読み取り専用ファイルを削除できるようにする
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)
src_target_file = os.path.join(REPO_DIR, "pages", "startup.js")
dest_pages = os.path.join("static", "pages")
if not os.path.exists(src_target_file):
logging.warning(f"{src_target_file} が存在しません。")
else:
if os.path.exists(dest_pages):
shutil.rmtree(dest_pages, onerror=remove_readonly)
shutil.copytree(os.path.join(REPO_DIR, "pages"), dest_pages)
target_file_copy = os.path.join(dest_pages, "startup.js")
with open(target_file_copy, "r", encoding="utf-8") as f:
content = f.read()
logging.info(f"コピー後のstartup.jsの内容抜粋:\n{content[:200]}")
logging.info("startup.js を修正中...")
new_content = content.replace(
'}, "https://studio.penguinmod.com");',
'}, "*");'
)
if content == new_content:
logging.warning("置換対象の文字列が見つかりませんでした。")
else:
with open(target_file_copy, "w", encoding="utf-8") as f:
f.write(new_content)
logging.info("置換が完了しました。")
# index.html をコピー
os.makedirs("templates", exist_ok=True)
shutil.copy(os.path.join(REPO_DIR, "index.html"), "templates/index.html")
# ここで <base href="..."> を削除
index_path = os.path.join("templates", "index.html")
with open(index_path, "r", encoding="utf-8") as f:
html = f.read()
import re
# <base href="https://sharkpools-extensions.vercel.app/"> を削除
new_html = re.sub(r'<base\s+href="https://sharkpools-extensions\.vercel\.app/"\s*/?>', '', html, flags=re.IGNORECASE)
if html == new_html:
logging.warning("<base href=...> タグが見つかりませんでした。")
else:
with open(index_path, "w", encoding="utf-8") as f:
f.write(new_html)
logging.info("<base href=...> タグの削除が完了しました。")
logging.info("リポジトリの更新が完了しました。")
# スケジューラを実行する関数(1分ごと)
def run_scheduler():
schedule.every(60).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)
|