Mercurial > libervia-web
annotate libervia/web/server/tasks/implicit/task_brython.py @ 1519:01b8d68edd70
version: use standard Python's `dev0` instead of Libervia specific `D` for dev version
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 06 Jun 2023 17:40:16 +0200 |
parents | eb00d593801d |
children |
rev | line source |
---|---|
1247 | 1 #!/ur/bin/env python3 |
2 | |
1503
2796e73ed50c
tasks (brython): fix brython scripts loading.
Goffi <goffi@goffi.org>
parents:
1384
diff
changeset
|
3 from ast import literal_eval |
1247 | 4 import json |
5 from pathlib import Path | |
1503
2796e73ed50c
tasks (brython): fix brython scripts loading.
Goffi <goffi@goffi.org>
parents:
1384
diff
changeset
|
6 import shutil |
2796e73ed50c
tasks (brython): fix brython scripts loading.
Goffi <goffi@goffi.org>
parents:
1384
diff
changeset
|
7 from typing import Any, Dict |
2796e73ed50c
tasks (brython): fix brython scripts loading.
Goffi <goffi@goffi.org>
parents:
1384
diff
changeset
|
8 |
1518
eb00d593801d
refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents:
1515
diff
changeset
|
9 from libervia.backend.core import exceptions |
eb00d593801d
refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents:
1515
diff
changeset
|
10 from libervia.backend.core.i18n import _ |
eb00d593801d
refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents:
1515
diff
changeset
|
11 from libervia.backend.core.log import getLogger |
eb00d593801d
refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents:
1515
diff
changeset
|
12 from libervia.backend.tools.common import utils |
1503
2796e73ed50c
tasks (brython): fix brython scripts loading.
Goffi <goffi@goffi.org>
parents:
1384
diff
changeset
|
13 |
1518
eb00d593801d
refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents:
1515
diff
changeset
|
14 from libervia.web.server.classes import Script |
eb00d593801d
refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents:
1515
diff
changeset
|
15 from libervia.web.server.constants import Const as C |
eb00d593801d
refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents:
1515
diff
changeset
|
16 from libervia.web.server.tasks import task |
1247 | 17 |
18 | |
19 log = getLogger(__name__) | |
20 | |
21 | |
22 class Task(task.Task): | |
23 | |
24 def prepare(self): | |
25 if "brython" not in self.resource.browser_modules: | |
1326
089742e065e3
pages (files/list, photos/album): updated thumbnails size limits
Goffi <goffi@goffi.org>
parents:
1282
diff
changeset
|
26 raise exceptions.CancelError("No brython module found") |
1247 | 27 |
28 brython_js = self.build_path / "brython.js" | |
29 if not brython_js.is_file(): | |
30 installed_ver = None | |
31 else: | |
32 with brython_js.open() as f: | |
33 for line in f: | |
34 if line.startswith('// implementation ['): | |
35 installed_ver = literal_eval(line[18:])[:3] | |
36 log.debug( | |
37 f"brython v{'.'.join(str(v) for v in installed_ver)} already " | |
38 f"installed") | |
39 break | |
40 else: | |
41 log.warning( | |
42 f"brython file at {brython_js} doesn't has implementation " | |
43 f"version" | |
44 ) | |
45 installed_ver = None | |
46 | |
1250
821b6ce57f99
server (tasks/brython): log an error if brython is missing and needed
Goffi <goffi@goffi.org>
parents:
1247
diff
changeset
|
47 try: |
821b6ce57f99
server (tasks/brython): log an error if brython is missing and needed
Goffi <goffi@goffi.org>
parents:
1247
diff
changeset
|
48 import brython |
1515
73c848c2f41e
tasks (brython): fix version retrieval for Brython 3.11 (and 3.10?)
Goffi <goffi@goffi.org>
parents:
1509
diff
changeset
|
49 try: |
73c848c2f41e
tasks (brython): fix version retrieval for Brython 3.11 (and 3.10?)
Goffi <goffi@goffi.org>
parents:
1509
diff
changeset
|
50 from brython.__main__ import implementation |
73c848c2f41e
tasks (brython): fix version retrieval for Brython 3.11 (and 3.10?)
Goffi <goffi@goffi.org>
parents:
1509
diff
changeset
|
51 except ImportError: |
73c848c2f41e
tasks (brython): fix version retrieval for Brython 3.11 (and 3.10?)
Goffi <goffi@goffi.org>
parents:
1509
diff
changeset
|
52 from brython.version import implementation |
1250
821b6ce57f99
server (tasks/brython): log an error if brython is missing and needed
Goffi <goffi@goffi.org>
parents:
1247
diff
changeset
|
53 except ModuleNotFoundError as e: |
821b6ce57f99
server (tasks/brython): log an error if brython is missing and needed
Goffi <goffi@goffi.org>
parents:
1247
diff
changeset
|
54 log.error('"brython" module is missing, can\'t use browser code for Brython') |
821b6ce57f99
server (tasks/brython): log an error if brython is missing and needed
Goffi <goffi@goffi.org>
parents:
1247
diff
changeset
|
55 raise e |
1515
73c848c2f41e
tasks (brython): fix version retrieval for Brython 3.11 (and 3.10?)
Goffi <goffi@goffi.org>
parents:
1509
diff
changeset
|
56 ver = [int(v) for v in implementation.split('.')[:3]] |
1247 | 57 if ver != installed_ver: |
58 log.info(_("Installing Brython v{version}").format( | |
59 version='.'.join(str(v) for v in ver))) | |
60 data_path = Path(brython.__file__).parent / 'data' | |
61 # shutil has blocking method, but the task is run before we start | |
62 # the web server, so it's not a big deal | |
63 shutil.copyfile(data_path / "brython.js", brython_js) | |
1279
2d1ceb026d0e
tasks (brython): copy and load `brython_stdlib.js`
Goffi <goffi@goffi.org>
parents:
1268
diff
changeset
|
64 shutil.copy(data_path / "brython_stdlib.js", self.build_path) |
1247 | 65 else: |
66 log.debug("Brython is already installed") | |
67 | |
68 self.WATCH_DIRS = [] | |
1503
2796e73ed50c
tasks (brython): fix brython scripts loading.
Goffi <goffi@goffi.org>
parents:
1384
diff
changeset
|
69 self.set_common_scripts() |
1247 | 70 |
1503
2796e73ed50c
tasks (brython): fix brython scripts loading.
Goffi <goffi@goffi.org>
parents:
1384
diff
changeset
|
71 def set_common_scripts(self): |
1247 | 72 for dyn_data in self.resource.browser_modules["brython"]: |
73 url_hash = dyn_data['url_hash'] | |
74 import_url = f"/{C.BUILD_DIR}/{C.BUILD_DIR_DYN}/{url_hash}" | |
1282
0e4e413eb8db
server: user new OrderedSet to handle scripts:
Goffi <goffi@goffi.org>
parents:
1279
diff
changeset
|
75 dyn_data.setdefault('scripts', utils.OrderedSet()).update([ |
1268
e628724530ec
pages, tasks (brython): use set for scripts + common template data:
Goffi <goffi@goffi.org>
parents:
1262
diff
changeset
|
76 Script(src=f"/{C.BUILD_DIR}/brython.js"), |
1279
2d1ceb026d0e
tasks (brython): copy and load `brython_stdlib.js`
Goffi <goffi@goffi.org>
parents:
1268
diff
changeset
|
77 Script(src=f"/{C.BUILD_DIR}/brython_stdlib.js"), |
1282
0e4e413eb8db
server: user new OrderedSet to handle scripts:
Goffi <goffi@goffi.org>
parents:
1279
diff
changeset
|
78 ]) |
1503
2796e73ed50c
tasks (brython): fix brython scripts loading.
Goffi <goffi@goffi.org>
parents:
1384
diff
changeset
|
79 dyn_data.setdefault('template', {})['body_onload'] = self.get_body_onload( |
1268
e628724530ec
pages, tasks (brython): use set for scripts + common template data:
Goffi <goffi@goffi.org>
parents:
1262
diff
changeset
|
80 extra_path=[import_url]) |
1247 | 81 self.WATCH_DIRS.append(dyn_data['path'].resolve()) |
82 | |
1503
2796e73ed50c
tasks (brython): fix brython scripts loading.
Goffi <goffi@goffi.org>
parents:
1384
diff
changeset
|
83 def get_body_onload(self, debug=True, cache=True, extra_path=None): |
2796e73ed50c
tasks (brython): fix brython scripts loading.
Goffi <goffi@goffi.org>
parents:
1384
diff
changeset
|
84 on_load_opts: Dict[str, Any] = {"pythonpath": [f"/{C.BUILD_DIR}"]} |
2796e73ed50c
tasks (brython): fix brython scripts loading.
Goffi <goffi@goffi.org>
parents:
1384
diff
changeset
|
85 if debug: |
2796e73ed50c
tasks (brython): fix brython scripts loading.
Goffi <goffi@goffi.org>
parents:
1384
diff
changeset
|
86 on_load_opts["debug"] = 1 |
2796e73ed50c
tasks (brython): fix brython scripts loading.
Goffi <goffi@goffi.org>
parents:
1384
diff
changeset
|
87 if cache: |
2796e73ed50c
tasks (brython): fix brython scripts loading.
Goffi <goffi@goffi.org>
parents:
1384
diff
changeset
|
88 on_load_opts["cache"] = True |
2796e73ed50c
tasks (brython): fix brython scripts loading.
Goffi <goffi@goffi.org>
parents:
1384
diff
changeset
|
89 if extra_path is not None: |
2796e73ed50c
tasks (brython): fix brython scripts loading.
Goffi <goffi@goffi.org>
parents:
1384
diff
changeset
|
90 on_load_opts["pythonpath"].extend(extra_path) |
1268
e628724530ec
pages, tasks (brython): use set for scripts + common template data:
Goffi <goffi@goffi.org>
parents:
1262
diff
changeset
|
91 |
1503
2796e73ed50c
tasks (brython): fix brython scripts loading.
Goffi <goffi@goffi.org>
parents:
1384
diff
changeset
|
92 return f"brython({json.dumps(on_load_opts)})" |
1268
e628724530ec
pages, tasks (brython): use set for scripts + common template data:
Goffi <goffi@goffi.org>
parents:
1262
diff
changeset
|
93 |
1503
2796e73ed50c
tasks (brython): fix brython scripts loading.
Goffi <goffi@goffi.org>
parents:
1384
diff
changeset
|
94 def copy_files(self, files_paths, dest): |
1253
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1250
diff
changeset
|
95 for p in files_paths: |
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1250
diff
changeset
|
96 log.debug(f"copying {p}") |
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1250
diff
changeset
|
97 if p.is_dir(): |
1335
f2ce8072d89d
tasks (brython): don't copy `__pycache__` to build_dir + fixed recursive copy
Goffi <goffi@goffi.org>
parents:
1326
diff
changeset
|
98 if p.name == '__pycache__': |
f2ce8072d89d
tasks (brython): don't copy `__pycache__` to build_dir + fixed recursive copy
Goffi <goffi@goffi.org>
parents:
1326
diff
changeset
|
99 continue |
f2ce8072d89d
tasks (brython): don't copy `__pycache__` to build_dir + fixed recursive copy
Goffi <goffi@goffi.org>
parents:
1326
diff
changeset
|
100 shutil.copytree(p, dest / p.name) |
1253
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1250
diff
changeset
|
101 else: |
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1250
diff
changeset
|
102 shutil.copy(p, dest) |
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1250
diff
changeset
|
103 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1503
diff
changeset
|
104 async def on_dir_event(self, host, filepath, flags): |
1503
2796e73ed50c
tasks (brython): fix brython scripts loading.
Goffi <goffi@goffi.org>
parents:
1384
diff
changeset
|
105 self.set_common_scripts() |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1503
diff
changeset
|
106 await self.manager.run_task_instance(self) |
1384
b61509c47cac
tasks (brython): fixed Brython scripts during dev mode reload
Goffi <goffi@goffi.org>
parents:
1335
diff
changeset
|
107 |
1247 | 108 def start(self): |
109 dyn_path = self.build_path / C.BUILD_DIR_DYN | |
110 for dyn_data in self.resource.browser_modules["brython"]: | |
111 url_hash = dyn_data['url_hash'] | |
1253
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1250
diff
changeset
|
112 if url_hash is None: |
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1250
diff
changeset
|
113 # root modules |
1256
08cd652dea14
server, pages, tasks (brython): common_scripts:
Goffi <goffi@goffi.org>
parents:
1253
diff
changeset
|
114 url_prefix = dyn_data.get('url_prefix') |
08cd652dea14
server, pages, tasks (brython): common_scripts:
Goffi <goffi@goffi.org>
parents:
1253
diff
changeset
|
115 if url_prefix is None: |
08cd652dea14
server, pages, tasks (brython): common_scripts:
Goffi <goffi@goffi.org>
parents:
1253
diff
changeset
|
116 dest = self.build_path |
08cd652dea14
server, pages, tasks (brython): common_scripts:
Goffi <goffi@goffi.org>
parents:
1253
diff
changeset
|
117 init_dest_url = f"/{C.BUILD_DIR}/__init__.py" |
08cd652dea14
server, pages, tasks (brython): common_scripts:
Goffi <goffi@goffi.org>
parents:
1253
diff
changeset
|
118 else: |
08cd652dea14
server, pages, tasks (brython): common_scripts:
Goffi <goffi@goffi.org>
parents:
1253
diff
changeset
|
119 dest = self.build_path / url_prefix |
08cd652dea14
server, pages, tasks (brython): common_scripts:
Goffi <goffi@goffi.org>
parents:
1253
diff
changeset
|
120 dest.mkdir(exist_ok = True) |
08cd652dea14
server, pages, tasks (brython): common_scripts:
Goffi <goffi@goffi.org>
parents:
1253
diff
changeset
|
121 init_dest_url = f"/{C.BUILD_DIR}/{url_prefix}/__init__.py" |
08cd652dea14
server, pages, tasks (brython): common_scripts:
Goffi <goffi@goffi.org>
parents:
1253
diff
changeset
|
122 |
1503
2796e73ed50c
tasks (brython): fix brython scripts loading.
Goffi <goffi@goffi.org>
parents:
1384
diff
changeset
|
123 self.copy_files(dyn_data['path'].glob('*py'), dest) |
1256
08cd652dea14
server, pages, tasks (brython): common_scripts:
Goffi <goffi@goffi.org>
parents:
1253
diff
changeset
|
124 |
08cd652dea14
server, pages, tasks (brython): common_scripts:
Goffi <goffi@goffi.org>
parents:
1253
diff
changeset
|
125 init_file = dyn_data['path'] / '__init__.py' |
08cd652dea14
server, pages, tasks (brython): common_scripts:
Goffi <goffi@goffi.org>
parents:
1253
diff
changeset
|
126 if init_file.is_file(): |
1282
0e4e413eb8db
server: user new OrderedSet to handle scripts:
Goffi <goffi@goffi.org>
parents:
1279
diff
changeset
|
127 self.resource.dyn_data_common['scripts'].update([ |
1268
e628724530ec
pages, tasks (brython): use set for scripts + common template data:
Goffi <goffi@goffi.org>
parents:
1262
diff
changeset
|
128 Script(src=f"/{C.BUILD_DIR}/brython.js"), |
1503
2796e73ed50c
tasks (brython): fix brython scripts loading.
Goffi <goffi@goffi.org>
parents:
1384
diff
changeset
|
129 Script(src=f"/{C.BUILD_DIR}/brython_stdlib.js"), |
1268
e628724530ec
pages, tasks (brython): use set for scripts + common template data:
Goffi <goffi@goffi.org>
parents:
1262
diff
changeset
|
130 Script(type='text/python', src=init_dest_url) |
1282
0e4e413eb8db
server: user new OrderedSet to handle scripts:
Goffi <goffi@goffi.org>
parents:
1279
diff
changeset
|
131 ]) |
1268
e628724530ec
pages, tasks (brython): use set for scripts + common template data:
Goffi <goffi@goffi.org>
parents:
1262
diff
changeset
|
132 self.resource.dyn_data_common.setdefault( |
1503
2796e73ed50c
tasks (brython): fix brython scripts loading.
Goffi <goffi@goffi.org>
parents:
1384
diff
changeset
|
133 "template", {})['body_onload'] = self.get_body_onload() |
1253
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1250
diff
changeset
|
134 else: |
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1250
diff
changeset
|
135 page_dyn_path = dyn_path / url_hash |
1335
f2ce8072d89d
tasks (brython): don't copy `__pycache__` to build_dir + fixed recursive copy
Goffi <goffi@goffi.org>
parents:
1326
diff
changeset
|
136 log.debug(f"using dynamic path at {page_dyn_path}") |
1253
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1250
diff
changeset
|
137 if page_dyn_path.exists(): |
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1250
diff
changeset
|
138 log.debug("cleaning existing path") |
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1250
diff
changeset
|
139 shutil.rmtree(page_dyn_path) |
1247 | 140 |
1253
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1250
diff
changeset
|
141 page_dyn_path.mkdir(parents=True, exist_ok=True) |
6d49fae517ba
pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents:
1250
diff
changeset
|
142 log.debug("copying browser python files") |
1503
2796e73ed50c
tasks (brython): fix brython scripts loading.
Goffi <goffi@goffi.org>
parents:
1384
diff
changeset
|
143 self.copy_files(dyn_data['path'].iterdir(), page_dyn_path) |
1247 | 144 |
1268
e628724530ec
pages, tasks (brython): use set for scripts + common template data:
Goffi <goffi@goffi.org>
parents:
1262
diff
changeset
|
145 script = Script( |
e628724530ec
pages, tasks (brython): use set for scripts + common template data:
Goffi <goffi@goffi.org>
parents:
1262
diff
changeset
|
146 type='text/python', |
e628724530ec
pages, tasks (brython): use set for scripts + common template data:
Goffi <goffi@goffi.org>
parents:
1262
diff
changeset
|
147 src=f"/{C.BUILD_DIR}/{C.BUILD_DIR_DYN}/{url_hash}/__init__.py" |
e628724530ec
pages, tasks (brython): use set for scripts + common template data:
Goffi <goffi@goffi.org>
parents:
1262
diff
changeset
|
148 ) |
1384
b61509c47cac
tasks (brython): fixed Brython scripts during dev mode reload
Goffi <goffi@goffi.org>
parents:
1335
diff
changeset
|
149 dyn_data.setdefault('scripts', utils.OrderedSet()).add(script) |