annotate libervia/server/tasks/implicit/task_js_modules.py @ 1254:780dbc2f4853

server (tasks): new `js_modules` implicit task: /!\ either `yarn` (preferred) or `npm` (fallback) is a new dependency This task will install JS modules specified in `browser_meta.js` using either `yarn` or `npm`. A mapping for Brython can also be specified in browser metadata, in this case Python modules will be created and put in the `js_modules` python package.
author Goffi <goffi@goffi.org>
date Wed, 29 Apr 2020 17:39:11 +0200
parents
children 0b269d4a46a3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1254
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/ur/bin/env python3
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
2
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
3 import json
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 from pathlib import Path
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
5 from sat.core.i18n import _
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
6 from sat.core.log import getLogger
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 from sat.core import exceptions
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 from libervia.server.constants import Const as C
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 from libervia.server.tasks import task
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
10
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
11
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 log = getLogger(__name__)
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
13
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
14
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 class Task(task.Task):
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
16
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 async def prepare(self):
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 if "js" not in self.resource.browser_modules:
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
19 raise exceptions.CancelError(f"No JS module needed")
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
20
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 async def start(self):
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 js_data = self.resource.browser_modules['js']
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 package = js_data.get('package', {})
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 package_path = self.build_path / 'package.json'
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 with package_path.open('w') as f:
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 json.dump(package, f)
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
27
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 try:
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 cmd = self.findCommand('yarn')
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 except exceptions.NotFound:
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 cmd = self.findCommand('npm')
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 await self.runCommand(cmd, 'install', path=str(self.build_path))
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
33
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 try:
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 brython_map = js_data['brython_map']
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 except KeyError:
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 pass
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 else:
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 log.info(_("creating JS modules mapping for Brython"))
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 js_modules_path = self.build_path / 'js_modules'
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 js_modules_path.mkdir(exist_ok=True)
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 init_path = js_modules_path / '__init__.py'
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 init_path.touch()
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
44
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 for module_name, module_data in brython_map.items():
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 log.debug(f"generating mapping for {module_name}")
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 if ' ' in module_name:
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 raise ValueError(
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 f"module {module_name!r} has space(s), it must not!")
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 module_path = js_modules_path / f"{module_name}.py"
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 if isinstance(module_data, str):
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 module_data = {'path': module_data}
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 try:
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 js_path = module_data.pop('path')
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 except KeyError:
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 raise ValueError(
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 f'module data for {module_name} must have a "path" key')
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 module_data['path'] = Path('node_modules') / js_path.strip(' /')
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 export = module_data.get('export') or [module_name]
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 export_objects = '\n'.join(f'{e} = window.{e}' for e in export)
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
61
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 with module_path.open('w') as f:
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 f.write(f"""\
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 #!/usr/bin/env python3
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 from browser import window, load
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 {module_data.get('extra_import', '')}
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
67
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 load("{Path('/').joinpath(C.BUILD_DIR, module_data['path'])}")
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 {export_objects}
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 {module_data.get('extra_init', '')}
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 """)