goome-cli 0.5.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
goome_cli.py
ADDED
|
@@ -0,0 +1,589 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
import argparse
|
|
3
|
+
import functools
|
|
4
|
+
import http.server
|
|
5
|
+
import io
|
|
6
|
+
import json
|
|
7
|
+
import os
|
|
8
|
+
import re
|
|
9
|
+
import shutil
|
|
10
|
+
import subprocess
|
|
11
|
+
import sys
|
|
12
|
+
import tarfile
|
|
13
|
+
import tempfile
|
|
14
|
+
import time
|
|
15
|
+
import urllib.error
|
|
16
|
+
import urllib.request
|
|
17
|
+
|
|
18
|
+
__version__ = "0.5.3"
|
|
19
|
+
|
|
20
|
+
ORG = "goome-ui"
|
|
21
|
+
PYPI_URL = "https://pypi.org/pypi/goome-cli/json"
|
|
22
|
+
RELEASES_URL = "https://api.github.com/repos/goome-ui/goome-cli/releases/latest"
|
|
23
|
+
ASSET_URL = "https://github.com/goome-ui/goome-cli/releases/latest/download/goome.py"
|
|
24
|
+
LANGS = {
|
|
25
|
+
"c": "C, one goome.h, builds with any C compiler",
|
|
26
|
+
"csharp": "C#, .NET 8 or newer",
|
|
27
|
+
"python": "Python 3.8 or newer",
|
|
28
|
+
"java": "Java 22 or newer, Gradle",
|
|
29
|
+
"godot": "Godot 4.4 or newer, GDScript",
|
|
30
|
+
"rust": "Rust 1.77 or newer, cargo",
|
|
31
|
+
"node": "Node 18 or newer, npm",
|
|
32
|
+
"go": "Go 1.23 or newer, cgo with a C compiler",
|
|
33
|
+
"zig": "Zig 0.15.2",
|
|
34
|
+
"lua": "Lua 5.4 or LuaJIT, LuaRocks and a C compiler",
|
|
35
|
+
}
|
|
36
|
+
TEMPLATES = {
|
|
37
|
+
"app": "a normal window, with a toggle that turns it into an overlay",
|
|
38
|
+
"overlay": "a see-through overlay, only the window takes clicks",
|
|
39
|
+
"mod-menu": "a menu over the game that a hotkey opens and closes",
|
|
40
|
+
"hud": "a little info panel in the corner, clicks go through it",
|
|
41
|
+
}
|
|
42
|
+
TEMPLATE_ONLY = ["variants", "tests", "CONTRIBUTING.md", ".github/workflows/pr-rules.yml"]
|
|
43
|
+
TOKENS = ("My Goome App", "my-goome-app", "my_goome_app", "MyGoomeApp", "mygoomeapp")
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def names(name):
|
|
47
|
+
words = [w for w in re.split(r"[^A-Za-z0-9]+", name) if w]
|
|
48
|
+
if not words:
|
|
49
|
+
raise SystemExit("goome: %r isnt a usable name, use letters and numbers" % name)
|
|
50
|
+
lower = [w.lower() for w in words]
|
|
51
|
+
pascal = "".join(w[:1].upper() + w[1:] for w in words)
|
|
52
|
+
flat = "".join(lower)
|
|
53
|
+
if pascal[0].isdigit():
|
|
54
|
+
pascal = "App" + pascal
|
|
55
|
+
if flat[0].isdigit():
|
|
56
|
+
flat = "app" + flat
|
|
57
|
+
title = " ".join(w[:1].upper() + w[1:] for w in words)
|
|
58
|
+
return dict(zip(TOKENS, (title, "-".join(lower), "_".join(lower), pascal, flat)))
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def next_steps(lang, folder, kebab):
|
|
62
|
+
cd = "cd " + (folder if " " not in folder else '"%s"' % folder)
|
|
63
|
+
steps = {
|
|
64
|
+
"c": [cd, "cc -O2 -o %s main.c goome.c" % kebab, "./" + kebab, "",
|
|
65
|
+
"or with cmake (windows too):", "cmake -S . -B build", "cmake --build build"],
|
|
66
|
+
"csharp": [cd, "dotnet run"],
|
|
67
|
+
"python": [cd, "python -m venv .venv", "source .venv/bin/activate (.venv\\Scripts\\activate on windows)",
|
|
68
|
+
"pip install -r requirements.txt", "python main.py"],
|
|
69
|
+
"java": [cd, "./gradlew run (gradlew.bat run on windows)"],
|
|
70
|
+
"godot": [cd, "./get-goome.sh (or get-goome.ps1 on windows)", "godot -e --path ."],
|
|
71
|
+
"rust": [cd, "cargo run"],
|
|
72
|
+
"node": [cd, "npm install", "node main.js"],
|
|
73
|
+
"go": [cd, "go run ."],
|
|
74
|
+
"zig": [cd, "zig build run"],
|
|
75
|
+
"lua": [cd, "luarocks install --local goome-0.5.0-1.rockspec",
|
|
76
|
+
"eval \"$(luarocks path)\" (on windows run luarocks path and do what it prints)", "lua main.lua"],
|
|
77
|
+
}
|
|
78
|
+
return steps[lang]
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def fetch(url, timeout=60):
|
|
82
|
+
req = urllib.request.Request(url, headers={"User-Agent": "goome-cli/" + __version__})
|
|
83
|
+
with urllib.request.urlopen(req, timeout=timeout) as r:
|
|
84
|
+
return r.read()
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def download(lang, ref):
|
|
88
|
+
repo = "%s/goome-app-template-%s" % (ORG, lang)
|
|
89
|
+
if ref:
|
|
90
|
+
tries = [ref]
|
|
91
|
+
else:
|
|
92
|
+
tries = ["refs/tags/v" + __version__, "refs/heads/main"]
|
|
93
|
+
for i, r in enumerate(tries):
|
|
94
|
+
url = "https://codeload.github.com/%s/tar.gz/%s" % (repo, r)
|
|
95
|
+
try:
|
|
96
|
+
return fetch(url), r
|
|
97
|
+
except urllib.error.HTTPError as e:
|
|
98
|
+
if e.code != 404 or i == len(tries) - 1:
|
|
99
|
+
raise SystemExit("goome: couldnt download %s (%s)" % (url, e))
|
|
100
|
+
except urllib.error.URLError as e:
|
|
101
|
+
raise SystemExit("goome: couldnt reach github: %s" % e.reason)
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def safe_extract(data, dest):
|
|
105
|
+
with tarfile.open(fileobj=io.BytesIO(data), mode="r:gz") as tar:
|
|
106
|
+
root = os.path.realpath(dest)
|
|
107
|
+
for m in tar.getmembers():
|
|
108
|
+
if not (m.isfile() or m.isdir()):
|
|
109
|
+
continue
|
|
110
|
+
parts = m.name.replace("\\", "/").split("/")[1:]
|
|
111
|
+
if not parts or not parts[0] or ".." in parts or m.name.startswith("/"):
|
|
112
|
+
continue
|
|
113
|
+
out = os.path.realpath(os.path.join(root, *parts))
|
|
114
|
+
if not out.startswith(root + os.sep):
|
|
115
|
+
continue
|
|
116
|
+
if m.isdir():
|
|
117
|
+
os.makedirs(out, exist_ok=True)
|
|
118
|
+
continue
|
|
119
|
+
os.makedirs(os.path.dirname(out), exist_ok=True)
|
|
120
|
+
src = tar.extractfile(m)
|
|
121
|
+
with open(out, "wb") as f:
|
|
122
|
+
shutil.copyfileobj(src, f)
|
|
123
|
+
if m.mode & 0o111:
|
|
124
|
+
os.chmod(out, 0o755)
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def copy_local(src, dest):
|
|
128
|
+
skip = {".git", ".venv", "build", "bin", "obj", ".gradle", ".godot", "__pycache__", "target", "node_modules", ".zig-cache", "zig-out", "lua_modules"}
|
|
129
|
+
for top, dirs, files in os.walk(src):
|
|
130
|
+
dirs[:] = [d for d in dirs if d not in skip]
|
|
131
|
+
rel = os.path.relpath(top, src)
|
|
132
|
+
os.makedirs(os.path.join(dest, rel), exist_ok=True)
|
|
133
|
+
for f in files:
|
|
134
|
+
shutil.copy2(os.path.join(top, f), os.path.join(dest, rel, f))
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def apply_variant(dest, template):
|
|
138
|
+
if template == "app":
|
|
139
|
+
return
|
|
140
|
+
var = os.path.join(dest, "variants", template)
|
|
141
|
+
if not os.path.isdir(var):
|
|
142
|
+
raise SystemExit("goome: this template doesnt have a %s version" % template)
|
|
143
|
+
for top, dirs, files in os.walk(var):
|
|
144
|
+
rel = os.path.relpath(top, var)
|
|
145
|
+
os.makedirs(os.path.join(dest, rel), exist_ok=True)
|
|
146
|
+
for f in files:
|
|
147
|
+
shutil.copy2(os.path.join(top, f), os.path.join(dest, rel, f))
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def strip_template(dest):
|
|
151
|
+
for p in TEMPLATE_ONLY:
|
|
152
|
+
full = os.path.join(dest, *p.split("/"))
|
|
153
|
+
if os.path.isdir(full):
|
|
154
|
+
shutil.rmtree(full)
|
|
155
|
+
elif os.path.exists(full):
|
|
156
|
+
os.remove(full)
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
def rename(dest, table):
|
|
160
|
+
for top, dirs, files in os.walk(dest):
|
|
161
|
+
for f in files:
|
|
162
|
+
path = os.path.join(top, f)
|
|
163
|
+
try:
|
|
164
|
+
with open(path, "rb") as fh:
|
|
165
|
+
raw = fh.read()
|
|
166
|
+
text = raw.decode("utf-8")
|
|
167
|
+
except (UnicodeDecodeError, OSError):
|
|
168
|
+
continue
|
|
169
|
+
new = text
|
|
170
|
+
for old, value in table.items():
|
|
171
|
+
new = new.replace(old, value)
|
|
172
|
+
if new != text:
|
|
173
|
+
with open(path, "wb") as fh:
|
|
174
|
+
fh.write(new.encode("utf-8"))
|
|
175
|
+
for top, dirs, files in os.walk(dest, topdown=False):
|
|
176
|
+
for f in dirs + files:
|
|
177
|
+
new = f
|
|
178
|
+
for old, value in table.items():
|
|
179
|
+
new = new.replace(old, value)
|
|
180
|
+
if new != f:
|
|
181
|
+
os.rename(os.path.join(top, f), os.path.join(top, new))
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
def write_readme(dest, lang, template, table):
|
|
185
|
+
title = table["My Goome App"]
|
|
186
|
+
lines = ["# " + title, "",
|
|
187
|
+
"Made with [goome](https://goome-ui.github.io/docs/), from the %s %s template." % (lang, template), "",
|
|
188
|
+
"```sh"] + [s for s in next_steps(lang, ".", table["my-goome-app"])[1:] if s and not s.endswith(":")] + ["```", ""]
|
|
189
|
+
with open(os.path.join(dest, "README.md"), "w") as f:
|
|
190
|
+
f.write("\n".join(lines))
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
def cmd_new(a):
|
|
194
|
+
folder = a.dir or a.name
|
|
195
|
+
dest = os.path.abspath(folder)
|
|
196
|
+
if os.path.exists(dest) and (not os.path.isdir(dest) or os.listdir(dest)):
|
|
197
|
+
raise SystemExit("goome: %s already exists and isnt empty" % folder)
|
|
198
|
+
table = names(a.name)
|
|
199
|
+
parent = os.path.dirname(dest)
|
|
200
|
+
os.makedirs(parent, exist_ok=True)
|
|
201
|
+
work = tempfile.mkdtemp(prefix=".goome-new-", dir=parent)
|
|
202
|
+
try:
|
|
203
|
+
if a.source:
|
|
204
|
+
if os.path.isdir(a.source):
|
|
205
|
+
copy_local(a.source, work)
|
|
206
|
+
else:
|
|
207
|
+
with open(a.source, "rb") as f:
|
|
208
|
+
safe_extract(f.read(), work)
|
|
209
|
+
got = a.source
|
|
210
|
+
else:
|
|
211
|
+
data, got = download(a.lang, a.ref)
|
|
212
|
+
safe_extract(data, work)
|
|
213
|
+
apply_variant(work, a.template)
|
|
214
|
+
strip_template(work)
|
|
215
|
+
rename(work, table)
|
|
216
|
+
write_readme(work, a.lang, a.template, table)
|
|
217
|
+
if os.path.isdir(dest):
|
|
218
|
+
os.rmdir(dest)
|
|
219
|
+
os.rename(work, dest)
|
|
220
|
+
except BaseException:
|
|
221
|
+
shutil.rmtree(work, ignore_errors=True)
|
|
222
|
+
raise
|
|
223
|
+
print("made %s (%s, %s template, from %s)" % (folder, a.lang, a.template, got))
|
|
224
|
+
print("")
|
|
225
|
+
print("next:")
|
|
226
|
+
for s in next_steps(a.lang, folder, table["my-goome-app"]):
|
|
227
|
+
print(" " + s if s else "")
|
|
228
|
+
return 0
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
def cmd_list(a):
|
|
232
|
+
print("languages (--lang):")
|
|
233
|
+
for k, v in LANGS.items():
|
|
234
|
+
print(" %-9s %s" % (k, v))
|
|
235
|
+
print("")
|
|
236
|
+
print("templates (--template):")
|
|
237
|
+
for k, v in TEMPLATES.items():
|
|
238
|
+
print(" %-9s %s" % (k, v))
|
|
239
|
+
print("")
|
|
240
|
+
print("template repos: https://github.com/%s/goome-app-template-<lang>" % ORG)
|
|
241
|
+
return 0
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
CLANGS = ["clang"] + ["clang-%d" % v for v in range(25, 13, -1)]
|
|
245
|
+
LLVM_DIRS = ["/opt/homebrew/opt/llvm/bin", "/usr/local/opt/llvm/bin", "/usr/lib/llvm/bin", "C:\\Program Files\\LLVM\\bin"]
|
|
246
|
+
INSTALL_HINT = (
|
|
247
|
+
"install clang and lld with the webassembly target:\n"
|
|
248
|
+
" debian/ubuntu sudo apt install clang lld\n"
|
|
249
|
+
" fedora sudo dnf install clang lld\n"
|
|
250
|
+
" arch sudo pacman -S clang lld\n"
|
|
251
|
+
" mac brew install llvm lld (apples own clang cant do wasm)\n"
|
|
252
|
+
" windows winget install LLVM.LLVM\n"
|
|
253
|
+
"or point CLANG and WASM_LD at ones you have"
|
|
254
|
+
)
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
def which_in(name, extra=()):
|
|
258
|
+
found = shutil.which(name)
|
|
259
|
+
if found:
|
|
260
|
+
return found
|
|
261
|
+
for d in extra:
|
|
262
|
+
for ext in ("", ".exe"):
|
|
263
|
+
p = os.path.join(d, name + ext)
|
|
264
|
+
if os.path.isfile(p) and os.access(p, os.X_OK):
|
|
265
|
+
return p
|
|
266
|
+
return None
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
def can_wasm(clang):
|
|
270
|
+
try:
|
|
271
|
+
r = subprocess.run([clang, "--print-targets"], capture_output=True, text=True, timeout=30)
|
|
272
|
+
except (OSError, subprocess.SubprocessError):
|
|
273
|
+
return False
|
|
274
|
+
return "wasm32" in r.stdout
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
def find_tools():
|
|
278
|
+
want = os.environ.get("CLANG")
|
|
279
|
+
names = [want] if want else CLANGS
|
|
280
|
+
clang = None
|
|
281
|
+
seen = False
|
|
282
|
+
for n in names:
|
|
283
|
+
c = which_in(n, LLVM_DIRS)
|
|
284
|
+
if not c:
|
|
285
|
+
continue
|
|
286
|
+
seen = True
|
|
287
|
+
if can_wasm(c):
|
|
288
|
+
clang = c
|
|
289
|
+
break
|
|
290
|
+
if not clang:
|
|
291
|
+
if seen:
|
|
292
|
+
raise SystemExit("goome: found clang but it cant target wasm32.\n" + INSTALL_HINT)
|
|
293
|
+
raise SystemExit("goome: no clang found, building for the web needs one.\n" + INSTALL_HINT)
|
|
294
|
+
ld = os.environ.get("WASM_LD")
|
|
295
|
+
if ld and not os.path.isfile(ld):
|
|
296
|
+
ld = which_in(ld, LLVM_DIRS)
|
|
297
|
+
if not ld:
|
|
298
|
+
beside = os.path.join(os.path.dirname(os.path.realpath(clang)), "wasm-ld")
|
|
299
|
+
ld = beside if os.path.isfile(beside) else which_in("wasm-ld", LLVM_DIRS)
|
|
300
|
+
if not ld:
|
|
301
|
+
raise SystemExit("goome: no wasm-ld found, its in the lld package.\n" + INSTALL_HINT)
|
|
302
|
+
return clang, ld
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
def is_goome_tree(d):
|
|
306
|
+
return os.path.isfile(os.path.join(d, "wasm", "web-build.sh")) and os.path.isfile(os.path.join(d, "src", "goome.c"))
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
def cache_dir():
|
|
310
|
+
home = os.path.expanduser("~")
|
|
311
|
+
base = os.environ.get("XDG_CACHE_HOME")
|
|
312
|
+
if not base and os.name == "nt":
|
|
313
|
+
base = os.environ.get("LOCALAPPDATA")
|
|
314
|
+
if not base and sys.platform == "darwin":
|
|
315
|
+
base = os.path.join(home, "Library", "Caches")
|
|
316
|
+
base = base or os.path.join(home, ".cache")
|
|
317
|
+
return os.path.join(base, "goome")
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
def find_source(app, given, ref):
|
|
321
|
+
if given:
|
|
322
|
+
if not is_goome_tree(given):
|
|
323
|
+
raise SystemExit("goome: %s isnt a c-goome checkout (no wasm/web-build.sh), use a newer one" % given)
|
|
324
|
+
return given
|
|
325
|
+
env = os.environ.get("GOOME_SRC")
|
|
326
|
+
if env:
|
|
327
|
+
if not is_goome_tree(env):
|
|
328
|
+
raise SystemExit("goome: GOOME_SRC=%s isnt a c-goome checkout with wasm/web-build.sh" % env)
|
|
329
|
+
return env
|
|
330
|
+
here = os.path.dirname(os.path.abspath(app))
|
|
331
|
+
for start in (here, os.getcwd()):
|
|
332
|
+
d = start
|
|
333
|
+
while True:
|
|
334
|
+
for cand in (d, os.path.join(d, "c-goome"), os.path.join(d, "goome"), os.path.join(d, "vendor", "c-goome")):
|
|
335
|
+
if is_goome_tree(cand):
|
|
336
|
+
return cand
|
|
337
|
+
up = os.path.dirname(d)
|
|
338
|
+
if up == d:
|
|
339
|
+
break
|
|
340
|
+
d = up
|
|
341
|
+
tries = [ref] if ref else ["refs/tags/v" + __version__, "refs/heads/main"]
|
|
342
|
+
last = None
|
|
343
|
+
for i, r in enumerate(tries):
|
|
344
|
+
dest = os.path.join(cache_dir(), "c-goome-" + r.replace("/", "-"))
|
|
345
|
+
if is_goome_tree(dest) and r.startswith("refs/tags/"):
|
|
346
|
+
return dest
|
|
347
|
+
url = "https://codeload.github.com/%s/c-goome/tar.gz/%s" % (ORG, r)
|
|
348
|
+
try:
|
|
349
|
+
data = fetch(url)
|
|
350
|
+
except urllib.error.HTTPError as e:
|
|
351
|
+
if e.code != 404:
|
|
352
|
+
raise SystemExit("goome: couldnt download %s (%s)" % (url, e))
|
|
353
|
+
continue
|
|
354
|
+
except urllib.error.URLError as e:
|
|
355
|
+
raise SystemExit("goome: couldnt reach github: %s" % e.reason)
|
|
356
|
+
shutil.rmtree(dest, ignore_errors=True)
|
|
357
|
+
os.makedirs(dest)
|
|
358
|
+
safe_extract(data, dest)
|
|
359
|
+
if is_goome_tree(dest):
|
|
360
|
+
return dest
|
|
361
|
+
last = r
|
|
362
|
+
shutil.rmtree(dest, ignore_errors=True)
|
|
363
|
+
if last:
|
|
364
|
+
raise SystemExit("goome: %s of c-goome has no web build yet. use --ref dev or --goome <checkout of c-goome>" % last.split("/")[-1])
|
|
365
|
+
raise SystemExit("goome: couldnt find c-goome %s, use --goome <checkout of c-goome>" % (ref or "v" + __version__))
|
|
366
|
+
|
|
367
|
+
|
|
368
|
+
def find_app(path):
|
|
369
|
+
if path:
|
|
370
|
+
if not os.path.isfile(path):
|
|
371
|
+
raise SystemExit("goome: no such file: %s" % path)
|
|
372
|
+
return path
|
|
373
|
+
for c in ("main.c", os.path.join("src", "main.c"), "app.c", os.path.join("src", "app.c")):
|
|
374
|
+
if os.path.isfile(c):
|
|
375
|
+
return c
|
|
376
|
+
raise SystemExit("goome: which file? give it as goome build --web app.c (looked for main.c, src/main.c, app.c)")
|
|
377
|
+
|
|
378
|
+
|
|
379
|
+
def cmd_build(a):
|
|
380
|
+
if not a.web:
|
|
381
|
+
raise SystemExit("goome: build needs a target, only --web so far")
|
|
382
|
+
app = find_app(a.file)
|
|
383
|
+
clang, ld = find_tools()
|
|
384
|
+
src = find_source(app, a.goome, a.ref)
|
|
385
|
+
bash = shutil.which("bash")
|
|
386
|
+
if not bash:
|
|
387
|
+
raise SystemExit("goome: building for the web needs bash (git bash or wsl on windows)")
|
|
388
|
+
out = os.path.abspath(a.out)
|
|
389
|
+
env = dict(os.environ, CLANG=clang, WASM_LD=ld)
|
|
390
|
+
cmd = [bash, os.path.join(src, "wasm", "web-build.sh"), app, out] + list(a.more)
|
|
391
|
+
r = subprocess.run(cmd, env=env, stderr=subprocess.PIPE, stdout=subprocess.PIPE, text=True)
|
|
392
|
+
if r.returncode:
|
|
393
|
+
msg = r.stderr.strip() or r.stdout.strip() or "the build failed"
|
|
394
|
+
if msg.startswith("web-build: "):
|
|
395
|
+
msg = msg[len("web-build: "):]
|
|
396
|
+
raise SystemExit("goome: " + msg)
|
|
397
|
+
print(r.stdout.strip())
|
|
398
|
+
print("")
|
|
399
|
+
print("serve it with: goome serve %s" % a.out)
|
|
400
|
+
return 0
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
class Quiet(http.server.SimpleHTTPRequestHandler):
|
|
404
|
+
extensions_map = dict(http.server.SimpleHTTPRequestHandler.extensions_map, **{".wasm": "application/wasm", ".js": "text/javascript"})
|
|
405
|
+
|
|
406
|
+
def log_message(self, fmt, *args):
|
|
407
|
+
pass
|
|
408
|
+
|
|
409
|
+
def end_headers(self):
|
|
410
|
+
self.send_header("Cache-Control", "no-store")
|
|
411
|
+
super().end_headers()
|
|
412
|
+
|
|
413
|
+
|
|
414
|
+
def make_server(folder, bind, port):
|
|
415
|
+
if not os.path.isfile(os.path.join(folder, "index.html")):
|
|
416
|
+
raise SystemExit("goome: no index.html in %s, build it first with goome build --web" % folder)
|
|
417
|
+
handler = functools.partial(Quiet, directory=os.path.abspath(folder))
|
|
418
|
+
try:
|
|
419
|
+
return http.server.ThreadingHTTPServer((bind, port), handler)
|
|
420
|
+
except OSError as e:
|
|
421
|
+
raise SystemExit("goome: cant listen on %s:%d (%s)" % (bind, port, e.strerror))
|
|
422
|
+
|
|
423
|
+
|
|
424
|
+
def cmd_serve(a):
|
|
425
|
+
srv = make_server(a.dir, a.bind, a.port)
|
|
426
|
+
print("serving %s at http://%s:%d/ (ctrl+c to stop)" % (a.dir, "localhost" if a.bind in ("127.0.0.1", "0.0.0.0") else a.bind, srv.server_address[1]))
|
|
427
|
+
sys.stdout.flush()
|
|
428
|
+
try:
|
|
429
|
+
srv.serve_forever()
|
|
430
|
+
except KeyboardInterrupt:
|
|
431
|
+
print("")
|
|
432
|
+
finally:
|
|
433
|
+
srv.server_close()
|
|
434
|
+
return 0
|
|
435
|
+
|
|
436
|
+
|
|
437
|
+
def parse_version(text):
|
|
438
|
+
parts = []
|
|
439
|
+
for piece in str(text).strip().lstrip("vV").split("."):
|
|
440
|
+
m = re.match(r"\d+", piece)
|
|
441
|
+
if not m:
|
|
442
|
+
break
|
|
443
|
+
parts.append(int(m.group()))
|
|
444
|
+
return tuple(parts)
|
|
445
|
+
|
|
446
|
+
|
|
447
|
+
def is_newer(latest, current):
|
|
448
|
+
a, b = parse_version(latest), parse_version(current)
|
|
449
|
+
return bool(a) and a > b
|
|
450
|
+
|
|
451
|
+
|
|
452
|
+
def installed_with_pip():
|
|
453
|
+
return os.path.basename(os.path.abspath(__file__)) == "goome_cli.py"
|
|
454
|
+
|
|
455
|
+
|
|
456
|
+
def latest_version(timeout=60):
|
|
457
|
+
sources = [(PYPI_URL, lambda d: d["info"]["version"]), (RELEASES_URL, lambda d: d["tag_name"])]
|
|
458
|
+
if not installed_with_pip():
|
|
459
|
+
sources.reverse()
|
|
460
|
+
for url, pick in sources:
|
|
461
|
+
try:
|
|
462
|
+
found = str(pick(json.loads(fetch(url, timeout).decode("utf-8")))).lstrip("vV")
|
|
463
|
+
except (OSError, ValueError, KeyError, TypeError):
|
|
464
|
+
continue
|
|
465
|
+
if parse_version(found):
|
|
466
|
+
return found
|
|
467
|
+
return None
|
|
468
|
+
|
|
469
|
+
|
|
470
|
+
def pip_upgrade_command():
|
|
471
|
+
if os.sep + "pipx" + os.sep in sys.prefix.replace("\\", os.sep) and shutil.which("pipx"):
|
|
472
|
+
return ["pipx", "upgrade", "goome-cli"]
|
|
473
|
+
return [sys.executable, "-m", "pip", "install", "-U", "goome-cli"]
|
|
474
|
+
|
|
475
|
+
|
|
476
|
+
def replace_self(path, data):
|
|
477
|
+
folder = os.path.dirname(path)
|
|
478
|
+
if not os.access(folder, os.W_OK) or not os.access(path, os.W_OK):
|
|
479
|
+
raise SystemExit("goome: cant write to %s, so it wasnt updated. Run the update with the rights that file needs, or download goome.py again by hand." % path)
|
|
480
|
+
if hasattr(os, "geteuid") and os.stat(path).st_uid != os.geteuid():
|
|
481
|
+
raise SystemExit("goome: %s belongs to another user (a system install?), so it wasnt updated. Update it the way it was installed." % path)
|
|
482
|
+
shutil.copy2(path, path + ".bak")
|
|
483
|
+
fd, tmp = tempfile.mkstemp(dir=folder, prefix=".goome-update-")
|
|
484
|
+
try:
|
|
485
|
+
with os.fdopen(fd, "wb") as f:
|
|
486
|
+
f.write(data)
|
|
487
|
+
shutil.copymode(path, tmp)
|
|
488
|
+
os.replace(tmp, path)
|
|
489
|
+
finally:
|
|
490
|
+
if os.path.exists(tmp):
|
|
491
|
+
os.remove(tmp)
|
|
492
|
+
|
|
493
|
+
|
|
494
|
+
def cmd_update(a):
|
|
495
|
+
latest = latest_version(15)
|
|
496
|
+
if not latest:
|
|
497
|
+
print("goome: couldnt reach PyPI or GitHub to look for a new version", file=sys.stderr)
|
|
498
|
+
return 1
|
|
499
|
+
if a.check:
|
|
500
|
+
print(latest)
|
|
501
|
+
return 0
|
|
502
|
+
if not is_newer(latest, __version__):
|
|
503
|
+
print("goome %s is the newest" % __version__)
|
|
504
|
+
return 0
|
|
505
|
+
print("goome %s -> %s" % (__version__, latest))
|
|
506
|
+
if installed_with_pip():
|
|
507
|
+
cmd = pip_upgrade_command()
|
|
508
|
+
print(" ".join(cmd))
|
|
509
|
+
return subprocess.call(cmd)
|
|
510
|
+
path = os.path.realpath(os.path.abspath(__file__))
|
|
511
|
+
try:
|
|
512
|
+
data = fetch(ASSET_URL, 60)
|
|
513
|
+
except OSError as e:
|
|
514
|
+
print("goome: couldnt download goome.py (%s)" % e, file=sys.stderr)
|
|
515
|
+
return 1
|
|
516
|
+
if ('__version__ = "%s"' % latest).encode() not in data:
|
|
517
|
+
print("goome: the downloaded goome.py isnt version %s, not replacing anything" % latest, file=sys.stderr)
|
|
518
|
+
return 1
|
|
519
|
+
try:
|
|
520
|
+
compile(data, "goome.py", "exec")
|
|
521
|
+
except (SyntaxError, ValueError):
|
|
522
|
+
print("goome: the downloaded goome.py is broken, not replacing anything", file=sys.stderr)
|
|
523
|
+
return 1
|
|
524
|
+
replace_self(path, data)
|
|
525
|
+
print("updated %s, the old one is next to it as goome.py.bak" % path)
|
|
526
|
+
return 0
|
|
527
|
+
|
|
528
|
+
|
|
529
|
+
def update_note():
|
|
530
|
+
if os.environ.get("GOOME_NO_UPDATE_CHECK") or not sys.stdout.isatty():
|
|
531
|
+
return
|
|
532
|
+
try:
|
|
533
|
+
stamp = os.path.join(cache_dir(), "update-check")
|
|
534
|
+
try:
|
|
535
|
+
with open(stamp) as f:
|
|
536
|
+
last = float(f.read().strip())
|
|
537
|
+
except (OSError, ValueError):
|
|
538
|
+
last = 0
|
|
539
|
+
now = time.time()
|
|
540
|
+
if 0 <= now - last < 86400:
|
|
541
|
+
return
|
|
542
|
+
os.makedirs(cache_dir(), exist_ok=True)
|
|
543
|
+
with open(stamp, "w") as f:
|
|
544
|
+
f.write(str(now))
|
|
545
|
+
latest = latest_version(2)
|
|
546
|
+
if latest and is_newer(latest, __version__):
|
|
547
|
+
print("goome %s is out (you have %s), run goome update" % (latest, __version__), file=sys.stderr)
|
|
548
|
+
except Exception:
|
|
549
|
+
return
|
|
550
|
+
|
|
551
|
+
|
|
552
|
+
def main(argv=None):
|
|
553
|
+
p = argparse.ArgumentParser(prog="goome", description="makes a new goome project from a template, builds apps for the web")
|
|
554
|
+
p.add_argument("--version", action="version", version="goome " + __version__)
|
|
555
|
+
sub = p.add_subparsers(dest="cmd")
|
|
556
|
+
n = sub.add_parser("new", help="make a new project")
|
|
557
|
+
n.add_argument("name")
|
|
558
|
+
n.add_argument("--lang", required=True, choices=list(LANGS))
|
|
559
|
+
n.add_argument("--template", default="app", choices=list(TEMPLATES))
|
|
560
|
+
n.add_argument("--dir", help="folder to make it in, the name by default")
|
|
561
|
+
n.add_argument("--ref", help="branch, tag or commit of the template repo")
|
|
562
|
+
n.add_argument("--from", dest="source", help="use a local template folder or .tar.gz instead of downloading")
|
|
563
|
+
b = sub.add_parser("build", help="build your app, --web makes a folder you can host")
|
|
564
|
+
b.add_argument("file", nargs="?", help="the app, main.c by default")
|
|
565
|
+
b.add_argument("--web", action="store_true", help="compile it to webassembly with a page around it")
|
|
566
|
+
b.add_argument("--out", default="web", help="folder to put it in, web by default")
|
|
567
|
+
b.add_argument("--goome", help="a c-goome checkout to build with instead of downloading one")
|
|
568
|
+
b.add_argument("--ref", help="branch or tag of c-goome to download")
|
|
569
|
+
b.add_argument("--more", nargs="*", default=[], help="more .c files that belong to the app")
|
|
570
|
+
sv = sub.add_parser("serve", help="host a folder you built with --web on localhost")
|
|
571
|
+
sv.add_argument("dir", nargs="?", default="web")
|
|
572
|
+
sv.add_argument("--port", type=int, default=8000)
|
|
573
|
+
sv.add_argument("--bind", default="127.0.0.1")
|
|
574
|
+
sub.add_parser("list", help="show the languages and templates")
|
|
575
|
+
u = sub.add_parser("update", help="update goome-cli to the newest version")
|
|
576
|
+
u.add_argument("--check", action="store_true", help="only print the newest version")
|
|
577
|
+
a = p.parse_args(argv)
|
|
578
|
+
cmds = {"new": cmd_new, "build": cmd_build, "serve": cmd_serve, "list": cmd_list, "update": cmd_update}
|
|
579
|
+
if a.cmd not in cmds:
|
|
580
|
+
p.print_help()
|
|
581
|
+
return 1
|
|
582
|
+
code = cmds[a.cmd](a)
|
|
583
|
+
if a.cmd in ("new", "build", "list"):
|
|
584
|
+
update_note()
|
|
585
|
+
return code
|
|
586
|
+
|
|
587
|
+
|
|
588
|
+
if __name__ == "__main__":
|
|
589
|
+
sys.exit(main())
|