import re import subprocess import os import bz2 import tarfile import requests import base122 # Download and extract Pyodide if not os.path.isfile('pyodide-0.27.2.tar.bz2'): pyodide = requests.get('https://github.com/pyodide/pyodide/releases/download/0.27.2/pyodide-0.27.2.tar.bz2').content with open('pyodide-0.27.2.tar.bz2', 'wb') as f: f.write(pyodide) # Extract the bz2 archive with bz2.BZ2File('pyodide-0.27.2.tar.bz2') as bz_file: with tarfile.TarFile(fileobj=bz_file) as tar_file: tar_file.extractall() # Install prettier and format pyodide.asm.js and pyodide.js print(subprocess.check_output('npm install prettier'.split(' '))) print(subprocess.check_output('./node_modules/.bin/prettier -w pyodide/pyodide.asm.js'.split(' '))) print(subprocess.check_output('./node_modules/.bin/prettier -w pyodide/pyodide.js'.split(' '))) print(subprocess.check_output('./node_modules/.bin/prettier -w pyodide/pyodide.mjs'.split(' '))) # Get base122 representation of pyodide.asm.wasm with open('pyodide/pyodide.asm.wasm', 'rb') as f: pyodide_asm_wasm = base122.b122encode(f.read()).decode('utf8') # Get base122 representation of python_stdlib.zip with open('pyodide/python_stdlib.zip', 'rb') as f: python_stdlib = base122.b122encode(f.read()).decode('utf8') with open('pyodide/pyodide-lock.json', 'r') as f: pyodide_lock = f.read() # Replace asm mentions with a reference to window.pyodideAsmWasm with open('pyodide/pyodide.js', 'r') as f: pyodide_js = f.read() wasm_instantiate = """ let n; i ? (n = await WebAssembly.instantiateStreaming(i, r)) : (n = await WebAssembly.instantiate(await t, r));""" wasm_inlined = f'let n = await WebAssembly.instantiate(Uint8Array.from(decode("{pyodide_asm_wasm}")), r);' pyodide_js = pyodide_js.replace(wasm_instantiate, wasm_inlined) pyodide_js = pyodide_js.replace('let { binary: t, response: i } = P(e + "pyodide.asm.wasm");', '') stdlib_fetcher = 'async function G(e, t) {' early_return = f"""{stdlib_fetcher} if (e.endsWith('python_stdlib.zip')) {{ return Uint8Array.from(decode("{python_stdlib}")); }} """ pyodide_js = pyodide_js.replace(stdlib_fetcher, early_return) lock_json_fetcher = 'async function X(e) {' early_lock_return = f"""{lock_json_fetcher} if (e.endsWith('pyodide-lock.json')) {{ return {pyodide_lock}; }} """ pyodide_js = pyodide_js.replace(lock_json_fetcher, early_lock_return) with open('pyodide/pyodide.asm.js', 'r') as f: pyodide_asm_js = f.read() wasm_binary_file = 'wasmBinaryFile = "pyodide.asm.wasm";' wasm_binary_literal = f'wasmBinaryFile = dataURIPrefix + "{pyodide_asm_wasm}";' pyodide_asm_js = pyodide_asm_js.replace(wasm_binary_file, wasm_binary_literal) with open('pyodide/pyodide.asm.js', 'w') as w: w.write(pyodide_asm_js) execute_asm_js = """let f = `${r.indexURL}pyodide.asm.js`; await O(f);""" pyodide_js = pyodide_js.replace(execute_asm_js, pyodide_asm_js) with open('pyodide/pyodide.js', 'w') as w: w.write(pyodide_js) with open('base122.js', 'r') as f: base122_js = f.read() with open('pyodide/console.html', 'r') as f: console_html = f.read() load_pyodide_js = 'const { loadPyodide } = await import(indexURL + "pyodide.mjs");' console_html = console_html.replace(load_pyodide_js, '') console_html = console_html.replace('', console_html ) with open('pyodide/new_console.html', 'w') as w: w.write(console_html)