annotate libervia/server/tasks/implicit/task_js_modules.py @ 1273:10748aa888a9

tasks (js_modules, sass): check for `yarnpkg` before `yarn`, as it is the name used in Debian
author Goffi <goffi@goffi.org>
date Tue, 26 May 2020 12:33:09 +0200
parents 0b269d4a46a3
children 2f6dac783c8e
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
1273
10748aa888a9 tasks (js_modules, sass): check for `yarnpkg` before `yarn`, as it is the name used in Debian
Goffi <goffi@goffi.org>
parents: 1259
diff changeset
28 cmd = self.findCommand('yarnpkg', 'yarn', 'npm')
1254
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 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
30
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 try:
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 brython_map = js_data['brython_map']
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 except KeyError:
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 pass
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 else:
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 log.info(_("creating JS modules mapping for Brython"))
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 js_modules_path = self.build_path / 'js_modules'
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 js_modules_path.mkdir(exist_ok=True)
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 init_path = js_modules_path / '__init__.py'
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 init_path.touch()
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
41
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 for module_name, module_data in brython_map.items():
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 log.debug(f"generating mapping for {module_name}")
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 if ' ' in module_name:
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 raise ValueError(
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 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
47 module_path = js_modules_path / f"{module_name}.py"
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 if isinstance(module_data, str):
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 module_data = {'path': module_data}
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 try:
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 js_path = module_data.pop('path')
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 except KeyError:
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 raise ValueError(
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 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
55 module_data['path'] = Path('node_modules') / js_path.strip(' /')
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 export = module_data.get('export') or [module_name]
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 export_objects = '\n'.join(f'{e} = window.{e}' for e in export)
1259
0b269d4a46a3 task (js_modules): handle `extra_init`:
Goffi <goffi@goffi.org>
parents: 1254
diff changeset
58 extra_kwargs = {"build_dir": C.BUILD_DIR}
1254
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
59
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 with module_path.open('w') as f:
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 f.write(f"""\
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 #!/usr/bin/env python3
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 from browser import window, load
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 {module_data.get('extra_import', '')}
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
65
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 load("{Path('/').joinpath(C.BUILD_DIR, module_data['path'])}")
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 {export_objects}
1259
0b269d4a46a3 task (js_modules): handle `extra_init`:
Goffi <goffi@goffi.org>
parents: 1254
diff changeset
68 {module_data.get('extra_init', '').format(**extra_kwargs)}
1254
780dbc2f4853 server (tasks): new `js_modules` implicit task:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 """)