File size: 2,731 Bytes
057a205
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
patch_index.py -- adds the sky dome to index.html with ONE block of script tags before </body>.

It changes nothing else in the page. The dome mounts itself as a full-screen layer on top of the
existing app and calls your existing functions (showSkyMap, setSearchCenter, runSearch, applySky).

Usage (from the project folder):
    python3 tools/patch_index.py             # patch ./index.html (keeps index.html.bak)
    python3 tools/patch_index.py --check     # report only
    python3 tools/patch_index.py --remove    # take the dome block out again

Open the app with  ?dome=0  to skip the dome without removing anything.
"""
import re
import shutil
import sys
from pathlib import Path

BEGIN = "<!-- sky dome (patch_index.py) -->"
END = "<!-- /sky dome -->"
FILES = ["dome-coords.js", "dome.js", "dome-look.js", "dome-layers.js", "dome-landscape.js",
         "app-state.js", "dome-select.js", "dome-hud.js", "dome-app.js"]
BLOCK = BEGIN + "\n" + "\n".join('<script src="/%s"></script>' % f for f in FILES) + \
    "\n<script>if (window.DomeApp) { DomeApp.start(); }</script>\n" + END + "\n"


def main():
    args = [a for a in sys.argv[1:] if not a.startswith("--")]
    target = Path(args[0] if args else "index.html")
    if not target.is_file():
        sys.exit("Cannot find %s. Run this from the folder that contains index.html." % target)
    text = target.read_text(encoding="utf-8")
    patched = BEGIN in text

    if "--check" in sys.argv:
        print(("Dome is installed in %s" if patched else "Dome is NOT installed in %s") % target)
        return

    if "--remove" in sys.argv:
        if not patched:
            print("Nothing to remove.")
            return
        new = re.sub(re.escape(BEGIN) + r".*?" + re.escape(END) + r"\n?", "", text, flags=re.S)
        shutil.copy2(target, target.with_name(target.name + ".before-remove"))
        target.write_text(new, encoding="utf-8")
        print("Removed the dome block from %s." % target)
        return

    if patched:
        print("Already patched: %s" % target)
        return
    if text.count("</body>") != 1:
        sys.exit("Nothing changed: expected exactly one </body> in %s (found %d)." % (target, text.count("</body>")))
    if "three@0.128.0" not in text and "three.min.js" not in text:
        print("WARNING: index.html does not appear to load Three.js r128. The dome needs it; the page will skip the dome.")

    backup = target.with_name(target.name + ".bak")
    shutil.copy2(target, backup)
    target.write_text(text.replace("</body>", BLOCK + "</body>", 1), encoding="utf-8")
    print("Patched %s (backup: %s). Restart the server and reload." % (target, backup))


if __name__ == "__main__":
    main()