This repo contains code to mirror other repos. It also contains the code that is getting mirrored.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

106 rader
2.6 KiB

  1. class app {
  2. constructor(modules, invocation){
  3. languagePluginLoader.then(() => {
  4. // If you don't require for pre-loaded Python packages, remove this promise below.
  5. window.pyodide.runPythonAsync("import setuptools, micropip").then(()=>{
  6. window.pyodide.runPythonAsync("micropip.install('lark-parser')").then(()=>{
  7. this.fetchSources(modules).then(() => {
  8. window.pyodide.runPythonAsync("import " + Object.keys(modules).join("\nimport ") + "\n" + invocation + "\n").then(() => this.initializingComplete());
  9. });
  10. });
  11. });
  12. });
  13. }
  14. loadSources(module, baseURL, files) {
  15. let promises = [];
  16. for (let f in files) {
  17. promises.push(
  18. new Promise((resolve, reject) => {
  19. let file = files[f];
  20. let url = (baseURL ? baseURL + "/" : "") + file;
  21. fetch(url, {}).then((response) => {
  22. if (response.status === 200)
  23. return response.text().then((code) => {
  24. let path = ("/lib/python3.7/site-packages/" + module + "/" + file).split("/");
  25. let lookup = "";
  26. for (let i in path) {
  27. if (!path[i]) {
  28. continue;
  29. }
  30. lookup += (lookup ? "/" : "") + path[i];
  31. if (parseInt(i) === path.length - 1) {
  32. window.pyodide._module.FS.writeFile(lookup, code);
  33. console.debug(`fetched ${lookup}`);
  34. } else {
  35. try {
  36. window.pyodide._module.FS.lookupPath(lookup);
  37. } catch {
  38. window.pyodide._module.FS.mkdir(lookup);
  39. console.debug(`created ${lookup}`);
  40. }
  41. }
  42. }
  43. resolve();
  44. });
  45. else
  46. reject();
  47. });
  48. })
  49. );
  50. }
  51. return Promise.all(promises);
  52. }
  53. fetchSources(modules) {
  54. let promises = [];
  55. for( let module of Object.keys(modules) )
  56. {
  57. promises.push(
  58. new Promise((resolve, reject) => {
  59. fetch(`${modules[module]}/files.json`, {}).then((response) => {
  60. if (response.status === 200) {
  61. response.text().then((list) => {
  62. let files = JSON.parse(list);
  63. this.loadSources(module, modules[module], files).then(() => {
  64. resolve();
  65. })
  66. })
  67. } else {
  68. reject();
  69. }
  70. })
  71. }));
  72. }
  73. return Promise.all(promises).then(() => {
  74. for( let module of Object.keys(modules) ) {
  75. window.pyodide.loadedPackages[module] = "default channel";
  76. }
  77. window.pyodide.runPython(
  78. 'import importlib as _importlib\n' +
  79. '_importlib.invalidate_caches()\n'
  80. );
  81. });
  82. }
  83. initializingComplete() {
  84. document.body.classList.remove("is-loading")
  85. }
  86. }
  87. (function () {
  88. window.top.app = new app({"app": "app"}, "import app.app; app.app.start()");
  89. })();