1
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2
|
|
3
|
"""
|
4
|
Loading of parts of Haketilo source for testing in browser
|
5
|
"""
|
6
|
|
7
|
# This file is part of Haketilo.
|
8
|
#
|
9
|
# Copyright (C) 2021 Wojtek Kosior <koszko@koszko.org>
|
10
|
#
|
11
|
# This program is free software: you can redistribute it and/or modify
|
12
|
# it under the terms of the GNU General Public License as published by
|
13
|
# the Free Software Foundation, either version 3 of the License, or
|
14
|
# (at your option) any later version.
|
15
|
#
|
16
|
# This program is distributed in the hope that it will be useful,
|
17
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
18
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
19
|
# GNU General Public License for more details.
|
20
|
#
|
21
|
# You should have received a copy of the GNU General Public License
|
22
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
23
|
#
|
24
|
# I, Wojtek Kosior, thereby promise not to sue for violation of this file's
|
25
|
# license. Although I request that you do not make use this code in a
|
26
|
# proprietary program, I am not going to enforce this in court.
|
27
|
|
28
|
from pathlib import Path
|
29
|
import subprocess, re
|
30
|
|
31
|
from .misc_constants import *
|
32
|
|
33
|
script_root = here.parent
|
34
|
awk_script = script_root / 'compute_scripts.awk'
|
35
|
|
36
|
def make_relative_path(path):
|
37
|
path = Path(path)
|
38
|
|
39
|
if path.is_absolute():
|
40
|
path = path.relative_to(script_root)
|
41
|
|
42
|
return path
|
43
|
|
44
|
"""Used to ignore hidden files and emacs auto-save files."""
|
45
|
script_name_regex = re.compile(r'^[^.#].*\.js$')
|
46
|
|
47
|
def available_scripts(directory):
|
48
|
for script in directory.rglob('*.js'):
|
49
|
if script_name_regex.match(script.name):
|
50
|
yield script
|
51
|
|
52
|
def get_wrapped_script(script_path):
|
53
|
if script_path == 'exports_init.js':
|
54
|
with open(script_root / 'MOZILLA_exports_init.js') as script:
|
55
|
return script.read()
|
56
|
|
57
|
awk = subprocess.run(['awk', '-f', str(awk_script), 'wrapped_code',
|
58
|
str(script_path)],
|
59
|
stdout=subprocess.PIPE, cwd=script_root, check=True)
|
60
|
|
61
|
return awk.stdout.decode()
|
62
|
|
63
|
def load_script(path, import_dirs):
|
64
|
"""
|
65
|
`path` and `import_dirs` are .js file path and a list of directory paths,
|
66
|
respectively. They may be absolute or specified relative to Haketilo's
|
67
|
project directory.
|
68
|
|
69
|
Return a string containing script from `path` together with all other
|
70
|
scripts it depends on, wrapped in the same way Haketilo's build system wraps
|
71
|
them, with imports properly satisfied.
|
72
|
"""
|
73
|
path = make_relative_path(path)
|
74
|
|
75
|
import_dirs = [make_relative_path(dir) for dir in import_dirs]
|
76
|
available = [s for dir in import_dirs for s in available_scripts(dir)]
|
77
|
|
78
|
awk = subprocess.run(['awk', '-f', str(awk_script), 'script_dependencies',
|
79
|
str(path), *[str(s) for s in available]],
|
80
|
stdout=subprocess.PIPE, cwd=script_root, check=True)
|
81
|
|
82
|
output = awk.stdout.decode()
|
83
|
|
84
|
return '\n'.join([get_wrapped_script(path) for path in output.split()])
|