Add script to run with Pyodide

This commit is contained in:
Valentin Lorentz 2021-04-14 13:47:16 +02:00
parent ed4d5d60e7
commit ca422d007a
4 changed files with 102 additions and 0 deletions

1
.gitignore vendored
View File

@ -31,6 +31,7 @@ data/
logs/
*.conf
*.conf.bak
!pyodide/limnoria.conf
# Intellij PyCharm / IDEA related files
*.iml

48
pyodide/index.html Normal file
View File

@ -0,0 +1,48 @@
<html>
<head>
<script type="text/javascript" src="https://cdn.jsdelivr.net/pyodide/dev/full/pyodide.js"></script>
<script type="text/javascript">
async function main(){
await loadPyodide({ indexURL : 'https://cdn.jsdelivr.net/pyodide/dev/full/' });
await pyodide.runPythonAsync(`
import pyodide
import micropip
# Use micropip to install a Limnoria wheel
await micropip.install(
'http://[fdfe:6f1:908a:1fbe:c713:7c74:d786:8f15]:8081/%(WHEEL_PATH)s'
)
# Check we can indeed import Limnoria
import supybot.version
print(supybot.version.version)
# Load config file
config = pyodide.open_url('/limnoria.conf').read()
with open('limnoria.conf', 'a') as fd:
fd.write(config)
# Inject arguments to pretend it's called from the CLI
import sys
sys.argv = ['supybot', '--allow-root', '--single-loop', 'limnoria.conf']
# Initialize the bot
import supybot._main
supybot._main.main()
`);
while (1) {
// Hacky!!! We're taking the main loop away from Limnoria
// so the Python code releases control to the JS interpreter
await pyodide.runPythonAsync(`
import supybot._main
supybot._main._main(singleLoop=True)
`);
}
}
main();
</script>
</head>
<body>
</body>
</html>

5
pyodide/limnoria.conf Normal file
View File

@ -0,0 +1,5 @@
supybot.networks: testnet
supybot.networks.testnet.servers: [fdfe:4421:f18d:fe24:d51c:3fb4:b255:4f09]:8097
supybot.networks.testnet.channels: #limnoria-bots
supybot.drivers.module: PyodideWebsocket
supybot.log.stdout.level: DEBUG

48
pyodide/serve.py Normal file
View File

@ -0,0 +1,48 @@
import sys
import glob
import socket
import http.server
import socketserver
WHEEL_PATH = max(glob.glob('dist/limnoria-*-py3-none-any.whl'))
CONF_PATH = 'pyodide/limnoria.conf'
class Handler(http.server.BaseHTTPRequestHandler):
def end_headers(self):
# Enable Cross-Origin Resource Sharing (CORS)
self.send_header('Access-Control-Allow-Origin', '*')
super().end_headers()
def do_GET(self):
if self.path == '/':
self.send_response(200)
self.send_header('Content-Type', 'text/html;charset=UTF-8')
self.end_headers()
with open('pyodide/index.html', 'rb') as fd:
self.wfile.write(fd.read() % {b'WHEEL_PATH': WHEEL_PATH.encode()})
elif self.path == '/' + WHEEL_PATH:
self.send_response(200)
self.send_header('Content-Type', 'application/wasm')
with open(WHEEL_PATH, 'rb') as fd:
self.wfile.write(fd.read())
elif self.path == '/limnoria.conf':
self.send_response(200)
with open(CONF_PATH, 'rb') as fd:
self.wfile.write(fd.read())
elif self.path == '/favicon.ico':
pass
else:
print('Unexpected URL', self.path)
class TCPv6Server(socketserver.TCPServer):
address_family = socket.AF_INET6
allow_reuse_address = True
if __name__ == '__main__':
port = 8081
with TCPv6Server(('::', port), Handler) as httpd:
print('Serving at: http://[::1]:{}'.format(port))
httpd.serve_forever()