soiz1 commited on
Commit
17dc697
·
verified ·
1 Parent(s): c06f3ad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -40
app.py CHANGED
@@ -1,43 +1,86 @@
1
- import os
 
 
 
2
  import subprocess
 
3
  import shutil
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
- # GitHubのリポジトリURL
6
- repo_url = "https://github.com/izum00/penguinmod.github.io"
7
-
8
- # 環境変数からトークンを取得
9
- token = os.getenv("github_token")
10
- if not token:
11
- raise RuntimeError("環境変数 'github_token' が設定されていません。")
12
-
13
- # 認証情報付きのURLを作成
14
- authed_url = repo_url.replace("https://", f"https://{token}@")
15
-
16
- # カレントディレクトリのパス
17
- current_dir = os.getcwd()
18
-
19
- # Gitユーザー情報の設定
20
- subprocess.run(["git", "config", "--global", "user.name", "izum00"], check=True)
21
- subprocess.run([
22
- "git", "config", "--global", "user.email",
23
- os.getenv("mail") or "[email protected]"
24
- ], check=True)
25
-
26
- # Gitの安全設定
27
- subprocess.run(["git", "config", "--global", "--add", "safe.directory", "/app"], check=True)
28
-
29
- # .gitが存在していれば削除(Gitオブジェクト壊れ防止)
30
- git_dir = os.path.join(current_dir, ".git")
31
- if os.path.exists(git_dir):
32
- shutil.rmtree(git_dir)
33
-
34
- # Gitの初期化とLFSの初期化(LFSが導入されていることが前提)
35
- subprocess.run(["git", "init"], check=True)
36
- subprocess.run(["git", "lfs", "install"], check=True) # LFSが使われているなら必要
37
- subprocess.run(["git", "remote", "add", "origin", authed_url], check=True)
38
-
39
- # Gitの操作
40
- subprocess.run(["git", "add", "."], check=True)
41
- subprocess.run(["git", "commit", "-m", "auto commit from script"], check=True)
42
- subprocess.run(["git", "branch", "-M", "main"], check=True)
43
- subprocess.run(["git", "push", "-u", "origin", "main", "--force"], check=True)
 
1
+ from flask import Flask, render_template, send_from_directory
2
+ import schedule
3
+ import threading
4
+ import time
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
+
61
+ dest_pages = os.path.join("static", "pages")
62
+ if os.path.exists(dest_pages):
63
+ shutil.rmtree(dest_pages, onerror=remove_readonly)
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)