-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsvelte_interface.py
162 lines (139 loc) Β· 6.04 KB
/
svelte_interface.py
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import hashlib
import json
import os
import sqlite3 as sql
import subprocess
import threading
import time
from pathlib import Path
import cherrypy
class SvelteInterface:
"""Automatically builds the game as necessary."""
_game_data = None
_admin_data = None
_db_global = None
def __init__(self, db_global):
self._db_global = db_global
def get_game(self):
"""Returns a dictionary with the game bundle data."""
return self._game_data
def get_admin(self):
"""Returns a dictionary with the admin bundle data."""
return self._admin_data
def _get_absolute_path(self, *path):
"""Returns the absolute path based on a path relative to this folder."""
joined_path = os.path.dirname(__file__)
for item in path:
joined_path = os.path.join(joined_path, item)
return os.path.abspath(joined_path)
def _build(self, is_game, game=""):
if is_game:
node = subprocess.Popen("npm run build -- \"" + game + "\"",
cwd=self._get_absolute_path("games"), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True)
else:
node = subprocess.Popen("npm run build",
cwd=self._get_absolute_path("admin"), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True)
code = node.wait()
if code == 0:
cherrypy.log(
"ππππππ " + ("Game" if is_game else "Admin") + " build succeeded ππππππ")
else:
cherrypy.log(
"π€¬π€¬π€¬π€¬π€¬π€¬π€¬ WARNING: " + ("Game" if is_game else "Admin") + " build failed π€¬π€¬π€¬π€¬π€¬π€¬")
if is_game:
self._game_data = {
"css": open(self._get_absolute_path("games", "build", "game.css"), "r").read(),
"js": open(self._get_absolute_path("games", "build", "game.js"), "r").read()
}
else:
self._admin_data = {
"css": open(self._get_absolute_path("admin", "build", "admin.css"), "r").read(),
"js": open(self._get_absolute_path("admin", "build", "admin.js"), "r").read()
}
def _get_game_name(self):
"""Returns the name of the selected game."""
conn_global = sql.connect(self._db_global)
cur_global = conn_global.cursor()
cur_global.execute("SELECT value FROM config WHERE key = 'game'")
game = str(cur_global.fetchall()[0][0])
conn_global.close()
return game
def _build_thread(self, is_game):
# Get the list of folders to monitor
if is_game:
monitor_folder = self._get_absolute_path(
"games", "config")
else:
monitor_folder = self._get_absolute_path(
"admin", "src")
last_modified_cache = {}
last_game = None
first_cycle = True
while True:
# Wait for file changes
has_changed = False
while not has_changed:
game = self._get_game_name()
if last_game != game:
last_game = game
has_changed = True
current_files = [str(x)
for x in Path(monitor_folder).rglob("*.*")]
for x in current_files:
if x not in last_modified_cache or last_modified_cache[x] != os.stat(x).st_mtime:
has_changed = True
last_modified_cache[x] = os.stat(x).st_mtime
for x in last_modified_cache.keys():
if x not in current_files:
has_changed = True
del last_modified_cache[x]
if not has_changed:
time.sleep(1)
# Build the app
if has_changed and not first_cycle:
cherrypy.log(
"π€ π€ π€ π€ π€ π€ " + ("Game" if is_game else "Admin") + " build starting π€ π€ π€ π€ π€ π€ ")
if is_game:
self._build(True, last_game)
else:
self._build(False)
first_cycle = False
def start(self):
# Check for "node_modules"
if not os.path.isdir(self._get_absolute_path("games", "node_modules")) or not os.path.isdir(self._get_absolute_path("admin", "node_modules")):
launch_allowed = True
response = input("Install Node modules for Svelte? (y-n) ")
if response == "y" or response == "yes":
node_1 = subprocess.Popen("npm install",
cwd=self._get_absolute_path("games"), shell=True)
code_1 = node_1.wait()
node_2 = subprocess.Popen("npm install",
cwd=self._get_absolute_path("admin"), shell=True)
code_2 = node_2.wait()
if code_1 == 0 and code_2 == 0:
print("Successfully installed Node modules.\n")
else:
print("Failed to install Node modules.\n")
launch_allowed = False
else:
print(
"Svelte builds will be skipped (expect things to be broken).\n")
launch_allowed = False
time.sleep(2)
if not launch_allowed:
return
# Build initial version
cherrypy.log(
"π€ π€ π€ π€ π€ π€ " + "Initial game and admin builds starting π€ π€ π€ π€ π€ π€ ")
self._build(True, self._get_game_name())
self._build(False)
# Start threads
threading.Thread(target=self._build_thread,
daemon=True, args=(True,)).start()
threading.Thread(target=self._build_thread,
daemon=True, args=(False,)).start()
if __name__ == "__main__":
svelte_interface = SvelteInterface()
svelte_interface.start()
while True:
time.sleep(1)