fontlab-python-instamix 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- fontlab_instamix/__init__.py +38 -0
- fontlab_instamix/__main__.py +8 -0
- fontlab_instamix/__version__.py +24 -0
- fontlab_instamix/bootstrap.py +187 -0
- fontlab_instamix/cli.py +161 -0
- fontlab_instamix/dest.py +71 -0
- fontlab_instamix/env.py +98 -0
- fontlab_instamix/extension.py +408 -0
- fontlab_instamix/gui.py +74 -0
- fontlab_instamix/ledger.py +102 -0
- fontlab_instamix/manifest.py +115 -0
- fontlab_instamix/packages.py +122 -0
- fontlab_instamix/platformdirs.py +91 -0
- fontlab_instamix/uv.py +133 -0
- fontlab_instamix/venv.py +34 -0
- fontlab_instamix/wrap.py +71 -0
- fontlab_python_instamix-1.0.0.dist-info/METADATA +134 -0
- fontlab_python_instamix-1.0.0.dist-info/RECORD +21 -0
- fontlab_python_instamix-1.0.0.dist-info/WHEEL +4 -0
- fontlab_python_instamix-1.0.0.dist-info/entry_points.txt +2 -0
- fontlab_python_instamix-1.0.0.dist-info/licenses/LICENSE +201 -0
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# this_file: src/fontlab_instamix/__init__.py
|
|
2
|
+
"""Stdlib-only installer toolkit for FontLab 9's embedded Python 3.11.
|
|
3
|
+
|
|
4
|
+
Submodules load lazily: ``fontlab_instamix.install(...)`` imports
|
|
5
|
+
:mod:`fontlab_instamix.extension` on first use.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import importlib
|
|
11
|
+
from typing import TYPE_CHECKING
|
|
12
|
+
|
|
13
|
+
if TYPE_CHECKING:
|
|
14
|
+
from .bootstrap import install_project
|
|
15
|
+
from .env import data_dir
|
|
16
|
+
from .extension import install, installed, uninstall
|
|
17
|
+
|
|
18
|
+
try:
|
|
19
|
+
from .__version__ import __version__
|
|
20
|
+
except ImportError: # running from a checkout: hatch-vcs writes this file on build
|
|
21
|
+
__version__ = "0.0.0+checkout"
|
|
22
|
+
|
|
23
|
+
_LAZY = {
|
|
24
|
+
"install": ("extension", "install"),
|
|
25
|
+
"uninstall": ("extension", "uninstall"),
|
|
26
|
+
"installed": ("extension", "installed"),
|
|
27
|
+
"install_project": ("bootstrap", "install_project"),
|
|
28
|
+
"data_dir": ("env", "data_dir"),
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
__all__ = ["__version__", "data_dir", "install", "install_project", "installed", "uninstall"]
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def __getattr__(name: str):
|
|
35
|
+
if name in _LAZY:
|
|
36
|
+
module, attr = _LAZY[name]
|
|
37
|
+
return getattr(importlib.import_module(f"{__name__}.{module}"), attr)
|
|
38
|
+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# file generated by vcs-versioning
|
|
2
|
+
# don't change, don't track in version control
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
__all__ = [
|
|
6
|
+
"__version__",
|
|
7
|
+
"__version_tuple__",
|
|
8
|
+
"version",
|
|
9
|
+
"version_tuple",
|
|
10
|
+
"__commit_id__",
|
|
11
|
+
"commit_id",
|
|
12
|
+
]
|
|
13
|
+
|
|
14
|
+
version: str
|
|
15
|
+
__version__: str
|
|
16
|
+
__version_tuple__: tuple[int | str, ...]
|
|
17
|
+
version_tuple: tuple[int | str, ...]
|
|
18
|
+
commit_id: str | None
|
|
19
|
+
__commit_id__: str | None
|
|
20
|
+
|
|
21
|
+
__version__ = version = '1.0.0'
|
|
22
|
+
__version_tuple__ = version_tuple = (1, 0, 0)
|
|
23
|
+
|
|
24
|
+
__commit_id__ = commit_id = None
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
# this_file: src/fontlab_instamix/bootstrap.py
|
|
2
|
+
"""Install a project checkout into FontLab from its ``install.vfpy``.
|
|
3
|
+
|
|
4
|
+
A project's ``install.vfpy`` needs two lines::
|
|
5
|
+
|
|
6
|
+
import fontlab_instamix.bootstrap as b
|
|
7
|
+
b.install_project(__file__)
|
|
8
|
+
|
|
9
|
+
It reads ``[tool.fontlab-install]`` from the project's ``pyproject.toml``
|
|
10
|
+
(keys: ``id``, ``name``, ``packages``, ``scripts``, ``extensions``,
|
|
11
|
+
``dependencies``) and turns it into a ``.fontlabext`` installed by
|
|
12
|
+
:mod:`fontlab_instamix.extension`, so the ledger and uninstall are shared.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
17
|
+
import json
|
|
18
|
+
import shutil
|
|
19
|
+
import tempfile
|
|
20
|
+
import tomllib
|
|
21
|
+
from dataclasses import dataclass, field
|
|
22
|
+
from pathlib import Path
|
|
23
|
+
|
|
24
|
+
from . import env, extension, manifest
|
|
25
|
+
|
|
26
|
+
DEFAULT_SCRIPTS = "Scripts"
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class BootstrapError(RuntimeError):
|
|
30
|
+
"""The project has no usable ``[tool.fontlab-install]`` table."""
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
@dataclass(frozen=True)
|
|
34
|
+
class Project:
|
|
35
|
+
root: Path
|
|
36
|
+
id: str
|
|
37
|
+
name: str
|
|
38
|
+
version: str
|
|
39
|
+
packages: tuple[str, ...] = ()
|
|
40
|
+
scripts: tuple[str, ...] = ()
|
|
41
|
+
extensions: str = ""
|
|
42
|
+
dependencies: tuple[str, ...] = ()
|
|
43
|
+
extra: dict = field(default_factory=dict)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _list(value, key: str) -> tuple[str, ...]:
|
|
47
|
+
if value is None:
|
|
48
|
+
return ()
|
|
49
|
+
if isinstance(value, str):
|
|
50
|
+
return (value,)
|
|
51
|
+
if isinstance(value, list) and all(isinstance(v, str) for v in value):
|
|
52
|
+
return tuple(value)
|
|
53
|
+
raise BootstrapError(f"[tool.fontlab-install] {key} must be a string or list of strings")
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def find_root(start: Path) -> Path:
|
|
57
|
+
"""Folder holding ``pyproject.toml`` at or above ``start``."""
|
|
58
|
+
start = Path(start).resolve()
|
|
59
|
+
for folder in (start, *start.parents) if start.is_dir() else start.parents:
|
|
60
|
+
if (folder / "pyproject.toml").is_file():
|
|
61
|
+
return folder
|
|
62
|
+
raise BootstrapError(f"no pyproject.toml at or above {start}")
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def read_project(start: Path) -> Project:
|
|
66
|
+
"""Parse ``[tool.fontlab-install]`` from the nearest ``pyproject.toml``."""
|
|
67
|
+
root = find_root(start)
|
|
68
|
+
data = tomllib.loads((root / "pyproject.toml").read_text(encoding="utf-8"))
|
|
69
|
+
table = data.get("tool", {}).get("fontlab-install")
|
|
70
|
+
if not isinstance(table, dict):
|
|
71
|
+
raise BootstrapError(f"{root / 'pyproject.toml'} has no [tool.fontlab-install] table")
|
|
72
|
+
ext_id = table.get("id")
|
|
73
|
+
if not isinstance(ext_id, str) or not manifest.ID_PATTERN.match(ext_id):
|
|
74
|
+
raise BootstrapError("[tool.fontlab-install] id must be a reverse-DNS id like com.example.tool")
|
|
75
|
+
project = data.get("project", {})
|
|
76
|
+
scripts = _list(table.get("scripts"), "scripts")
|
|
77
|
+
if not scripts and (root / DEFAULT_SCRIPTS).is_dir():
|
|
78
|
+
scripts = (DEFAULT_SCRIPTS,)
|
|
79
|
+
extensions = table.get("extensions", "")
|
|
80
|
+
if not isinstance(extensions, str):
|
|
81
|
+
raise BootstrapError("[tool.fontlab-install] extensions must be a folder name")
|
|
82
|
+
known = {"id", "name", "packages", "scripts", "extensions", "dependencies"}
|
|
83
|
+
return Project(
|
|
84
|
+
root=root,
|
|
85
|
+
id=ext_id,
|
|
86
|
+
name=str(table.get("name") or project.get("name") or ext_id),
|
|
87
|
+
version=str(project.get("version") or "0"),
|
|
88
|
+
packages=_list(table.get("packages"), "packages"),
|
|
89
|
+
scripts=scripts,
|
|
90
|
+
extensions=extensions,
|
|
91
|
+
dependencies=_list(table.get("dependencies"), "dependencies"),
|
|
92
|
+
extra={k: v for k, v in table.items() if k not in known},
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def manifest_data(project: Project) -> dict:
|
|
97
|
+
"""Manifest describing the project checkout as an extension."""
|
|
98
|
+
rules = []
|
|
99
|
+
for folder in project.scripts:
|
|
100
|
+
to = "Scripts/{}" if folder == DEFAULT_SCRIPTS else f"Scripts/{Path(folder).name}"
|
|
101
|
+
rules.append({"from": folder, "to": to})
|
|
102
|
+
for package in project.packages:
|
|
103
|
+
if not (project.root / package).is_dir():
|
|
104
|
+
raise BootstrapError(f"package folder {package!r} not found in {project.root}")
|
|
105
|
+
data = {
|
|
106
|
+
"id": project.id,
|
|
107
|
+
"name": project.name,
|
|
108
|
+
"version": project.version,
|
|
109
|
+
"python": {"packages": list(project.packages), "requires": []},
|
|
110
|
+
"install": rules,
|
|
111
|
+
}
|
|
112
|
+
manifest.parse(data)
|
|
113
|
+
return data
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def sibling_dependencies(project: Project) -> tuple[list[Path], list[str]]:
|
|
117
|
+
"""Split dependencies into sibling checkouts (with install.vfpy) and uv specs."""
|
|
118
|
+
siblings, specs = [], []
|
|
119
|
+
for spec in project.dependencies:
|
|
120
|
+
name = spec.split(";")[0].strip()
|
|
121
|
+
for sep in "<>=!~[ ":
|
|
122
|
+
name = name.split(sep)[0]
|
|
123
|
+
sibling = project.root.parent / name
|
|
124
|
+
if (sibling / "install.vfpy").is_file() and (sibling / "pyproject.toml").is_file():
|
|
125
|
+
siblings.append(sibling)
|
|
126
|
+
else:
|
|
127
|
+
specs.append(spec)
|
|
128
|
+
return siblings, specs
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def install_project(
|
|
132
|
+
start,
|
|
133
|
+
data: Path | None = None,
|
|
134
|
+
*,
|
|
135
|
+
copy: bool | None = None,
|
|
136
|
+
requirements=None,
|
|
137
|
+
_seen: set | None = None,
|
|
138
|
+
) -> list[dict]:
|
|
139
|
+
"""Install the project around ``start`` (usually ``__file__``).
|
|
140
|
+
|
|
141
|
+
Packages are linked from the checkout (an editable install), scripts are
|
|
142
|
+
linked into ``Scripts/<id>``, sibling dependency checkouts are installed
|
|
143
|
+
first, other dependencies go through uv. Returns the ledger records.
|
|
144
|
+
"""
|
|
145
|
+
data = env.data_dir(data)
|
|
146
|
+
project = read_project(Path(start))
|
|
147
|
+
seen = _seen if _seen is not None else set()
|
|
148
|
+
if project.root in seen:
|
|
149
|
+
return []
|
|
150
|
+
seen.add(project.root)
|
|
151
|
+
records: list[dict] = []
|
|
152
|
+
siblings, specs = sibling_dependencies(project)
|
|
153
|
+
for sibling in siblings:
|
|
154
|
+
records += install_project(sibling, data, copy=copy, requirements=requirements, _seen=seen)
|
|
155
|
+
data_manifest = manifest_data(project)
|
|
156
|
+
data_manifest["python"]["requires"] = specs
|
|
157
|
+
records.append(_install_checkout(project, data_manifest, data, copy, requirements))
|
|
158
|
+
if project.extensions and (project.root / project.extensions).is_dir():
|
|
159
|
+
for folder in sorted((project.root / project.extensions).rglob("*")):
|
|
160
|
+
nested = any(extension.is_ext(part) for part in folder.relative_to(project.root).parts[:-1])
|
|
161
|
+
if folder.is_dir() and extension.is_ext(folder.name) and not nested:
|
|
162
|
+
records += extension.install(folder, data, copy=copy, requirements=requirements)
|
|
163
|
+
return records
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
def _install_checkout(project: Project, data_manifest: dict, data: Path, copy, requirements) -> dict:
|
|
167
|
+
"""Install the checkout through a tiny generated ``.fontlabext`` of links."""
|
|
168
|
+
with tempfile.TemporaryDirectory(prefix="instamix-project-") as temp:
|
|
169
|
+
folder = Path(temp) / f"{project.id}.fontlabext"
|
|
170
|
+
folder.mkdir()
|
|
171
|
+
copied = False
|
|
172
|
+
for rel in [r["from"] for r in data_manifest["install"]] + data_manifest["python"]["packages"]:
|
|
173
|
+
target = folder / rel
|
|
174
|
+
target.parent.mkdir(parents=True, exist_ok=True)
|
|
175
|
+
source = project.root / rel
|
|
176
|
+
try:
|
|
177
|
+
target.symlink_to(source, target_is_directory=source.is_dir())
|
|
178
|
+
except OSError: # Windows without symlink rights: copy (not live)
|
|
179
|
+
copied = True
|
|
180
|
+
if source.is_dir():
|
|
181
|
+
shutil.copytree(source, target)
|
|
182
|
+
else:
|
|
183
|
+
shutil.copy2(source, target)
|
|
184
|
+
(folder / manifest.MANIFEST).write_text(json.dumps(data_manifest), encoding="utf-8")
|
|
185
|
+
# Files copied into the temp folder vanish afterwards, so never link to them.
|
|
186
|
+
copy = True if copied else copy
|
|
187
|
+
return extension.install_folder(folder, data, copy=copy, requirements=requirements, editable=True)
|
fontlab_instamix/cli.py
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
# this_file: src/fontlab_instamix/cli.py
|
|
2
|
+
"""``fontlab-instamix`` command line (also ``python -m fontlab_instamix``)."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
import argparse
|
|
7
|
+
import json
|
|
8
|
+
import sys
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
|
|
11
|
+
from . import __version__, bootstrap, env, extension, packages, uv, venv, wrap
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def _data(args) -> Path:
|
|
15
|
+
return env.data_dir(args.data_dir)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def cmd_env(args) -> int:
|
|
19
|
+
print(json.dumps(env.describe(_data(args)), indent=2))
|
|
20
|
+
return 0
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def cmd_ext_install(args) -> int:
|
|
24
|
+
data = _data(args)
|
|
25
|
+
for path in args.paths:
|
|
26
|
+
for record in extension.install(Path(path), data, copy=args.copy or None, run_hooks=args.run_hooks):
|
|
27
|
+
print(f"installed {record['id']} {record['version']} ({len(record['entries'])} paths)")
|
|
28
|
+
return 0
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def cmd_ext_uninstall(args) -> int:
|
|
32
|
+
data = _data(args)
|
|
33
|
+
for ext_id in args.ids:
|
|
34
|
+
kept = extension.uninstall(ext_id, data)
|
|
35
|
+
print(f"uninstalled {ext_id}")
|
|
36
|
+
for path in kept:
|
|
37
|
+
print(f" kept changed path: {path}")
|
|
38
|
+
return 0
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def cmd_ext_list(args) -> int:
|
|
42
|
+
for record in extension.installed(_data(args)):
|
|
43
|
+
print(f"{record['id']}\t{record['version']}\t{record['name']}")
|
|
44
|
+
return 0
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def cmd_pkg_install(args) -> int:
|
|
48
|
+
data = _data(args)
|
|
49
|
+
result = packages.install(data, args.specs, upgrade=args.upgrade)
|
|
50
|
+
print((result.stdout or "").strip() or "done")
|
|
51
|
+
return 0
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def cmd_pkg_uninstall(args) -> int:
|
|
55
|
+
data = _data(args)
|
|
56
|
+
for name in args.names:
|
|
57
|
+
removed = packages.uninstall(data, name)
|
|
58
|
+
print(f"removed {name} ({len(removed)} files)")
|
|
59
|
+
return 0
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def cmd_pkg_list(args) -> int:
|
|
63
|
+
for name, version in packages.listed(_data(args)):
|
|
64
|
+
print(f"{name}\t{version}")
|
|
65
|
+
return 0
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def cmd_wrap(args) -> int:
|
|
69
|
+
source = Path(args.source)
|
|
70
|
+
ext_id = args.id or f"com.example.{wrap.slug(source.stem)}"
|
|
71
|
+
folder = wrap.make_folder(source, Path(args.out), ext_id, name=args.name, version=args.version)
|
|
72
|
+
print(folder)
|
|
73
|
+
if args.zip:
|
|
74
|
+
print(wrap.make_zip([folder], folder.with_suffix(".fontlabextz")))
|
|
75
|
+
return 0
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def cmd_bootstrap(args) -> int:
|
|
79
|
+
for record in bootstrap.install_project(Path(args.project), args.data_dir, copy=args.copy or None):
|
|
80
|
+
print(f"installed {record['id']} ({len(record['entries'])} paths)")
|
|
81
|
+
return 0
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def cmd_uv_fetch(args) -> int:
|
|
85
|
+
data = _data(args)
|
|
86
|
+
print(uv.download(data, args.version) if args.force else uv.ensure(data))
|
|
87
|
+
return 0
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def cmd_venv(args) -> int:
|
|
91
|
+
data = _data(args)
|
|
92
|
+
print(venv.create(data, uv.ensure(data)))
|
|
93
|
+
return 0
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def parser() -> argparse.ArgumentParser:
|
|
97
|
+
p = argparse.ArgumentParser(prog="fontlab-instamix", description="Install extensions and packages into FontLab 9.")
|
|
98
|
+
p.add_argument("--version", action="version", version=__version__)
|
|
99
|
+
p.add_argument("--data-dir", help=f"FontLab data folder (default: ${env.DATA_ENV} or FontLab's own)")
|
|
100
|
+
sub = p.add_subparsers(dest="command", required=True)
|
|
101
|
+
|
|
102
|
+
sub.add_parser("env", help="show Python, platform and FontLab folders").set_defaults(func=cmd_env)
|
|
103
|
+
|
|
104
|
+
ext = sub.add_parser("ext", help="manage .fontlabext extensions").add_subparsers(dest="action", required=True)
|
|
105
|
+
s = ext.add_parser("install", help="install .fontlabext folders or .fontlabextz zips")
|
|
106
|
+
s.add_argument("paths", nargs="+")
|
|
107
|
+
s.add_argument("--copy", action="store_true", help="copy files instead of symlinking")
|
|
108
|
+
s.add_argument("--run-hooks", action="store_true", help="run install*.vfpy hooks found in zips")
|
|
109
|
+
s.set_defaults(func=cmd_ext_install)
|
|
110
|
+
s = ext.add_parser("uninstall", help="uninstall extensions by id")
|
|
111
|
+
s.add_argument("ids", nargs="+")
|
|
112
|
+
s.set_defaults(func=cmd_ext_uninstall)
|
|
113
|
+
ext.add_parser("list", help="list installed extensions").set_defaults(func=cmd_ext_list)
|
|
114
|
+
|
|
115
|
+
pkg = sub.add_parser("pkg", help="manage Python packages in FontLab's site-packages").add_subparsers(
|
|
116
|
+
dest="action", required=True
|
|
117
|
+
)
|
|
118
|
+
s = pkg.add_parser("install", help="install PyPI or git+ packages with uv")
|
|
119
|
+
s.add_argument("specs", nargs="+")
|
|
120
|
+
s.add_argument("--upgrade", action="store_true")
|
|
121
|
+
s.set_defaults(func=cmd_pkg_install)
|
|
122
|
+
s = pkg.add_parser("uninstall", help="remove packages installed with pkg install")
|
|
123
|
+
s.add_argument("names", nargs="+")
|
|
124
|
+
s.set_defaults(func=cmd_pkg_uninstall)
|
|
125
|
+
pkg.add_parser("list", help="list installed distributions").set_defaults(func=cmd_pkg_list)
|
|
126
|
+
|
|
127
|
+
s = sub.add_parser("wrap", help="wrap a script or folder into a .fontlabext")
|
|
128
|
+
s.add_argument("source")
|
|
129
|
+
s.add_argument("--id")
|
|
130
|
+
s.add_argument("--name")
|
|
131
|
+
s.add_argument("--version", default="0.1.0")
|
|
132
|
+
s.add_argument("--out", default=".")
|
|
133
|
+
s.add_argument("--zip", action="store_true", help="also write a .fontlabextz")
|
|
134
|
+
s.set_defaults(func=cmd_wrap)
|
|
135
|
+
|
|
136
|
+
s = sub.add_parser("bootstrap", help="install a project checkout using its [tool.fontlab-install]")
|
|
137
|
+
s.add_argument("project", nargs="?", default=".")
|
|
138
|
+
s.add_argument("--copy", action="store_true")
|
|
139
|
+
s.set_defaults(func=cmd_bootstrap)
|
|
140
|
+
|
|
141
|
+
uvp = sub.add_parser("uv", help="manage instamix's uv").add_subparsers(dest="action", required=True)
|
|
142
|
+
s = uvp.add_parser("fetch", help="find uv or download it into <data>/python/uv")
|
|
143
|
+
s.add_argument("--version", help="uv release tag, e.g. 0.9.0 (default: latest)")
|
|
144
|
+
s.add_argument("--force", action="store_true", help="download even if uv is found")
|
|
145
|
+
s.set_defaults(func=cmd_uv_fetch)
|
|
146
|
+
|
|
147
|
+
sub.add_parser("venv", help="create <data>/.venv for Python 3.11").set_defaults(func=cmd_venv)
|
|
148
|
+
return p
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def main(argv: list[str] | None = None) -> int:
|
|
152
|
+
args = parser().parse_args(argv)
|
|
153
|
+
try:
|
|
154
|
+
return args.func(args)
|
|
155
|
+
except (OSError, ValueError, RuntimeError) as error:
|
|
156
|
+
print(f"fontlab-instamix: {error}", file=sys.stderr)
|
|
157
|
+
return 1
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
if __name__ == "__main__":
|
|
161
|
+
sys.exit(main())
|
fontlab_instamix/dest.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# this_file: src/fontlab_instamix/dest.py
|
|
2
|
+
"""Resolve manifest ``to`` rules into paths inside the FontLab data folder.
|
|
3
|
+
|
|
4
|
+
Rules (see PLAN.md): ``.`` and ``./`` mean the target root, ``{}`` is the
|
|
5
|
+
extension name without ``.fontlabext``, ``{var}`` comes from manifest
|
|
6
|
+
``vars``, any other text is a subfolder. Unknown variables, ``..`` and
|
|
7
|
+
``\\`` or ``:`` inside a part (after expansion, on every platform) are
|
|
8
|
+
errors. The first path part names the target, matched case-insensitively.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import re
|
|
14
|
+
from pathlib import PurePosixPath
|
|
15
|
+
|
|
16
|
+
TARGETS = ("Scripts", "Actions", "Codepage", "Data", "Encoding", "site-packages")
|
|
17
|
+
SITE_PACKAGES = "python/3.11/site-packages"
|
|
18
|
+
_VAR = re.compile(r"\{([^{}]*)\}")
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class DestError(ValueError):
|
|
22
|
+
"""A ``to`` rule that cannot be resolved safely."""
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def target_name(name: str) -> str:
|
|
26
|
+
"""Return the canonical target for ``name`` (case-insensitive)."""
|
|
27
|
+
for target in TARGETS:
|
|
28
|
+
if target.lower() == name.lower():
|
|
29
|
+
return target
|
|
30
|
+
raise DestError(f"unknown target {name!r}; use one of {', '.join(TARGETS)}")
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def target_root(target: str) -> str:
|
|
34
|
+
"""Return the data-folder-relative directory of a canonical target."""
|
|
35
|
+
return SITE_PACKAGES if target == "site-packages" else target
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def expand(text: str, basename: str, variables: dict[str, str]) -> str:
|
|
39
|
+
"""Replace ``{}`` and ``{var}`` placeholders in ``text``."""
|
|
40
|
+
|
|
41
|
+
def sub(match: re.Match[str]) -> str:
|
|
42
|
+
key = match.group(1)
|
|
43
|
+
if key == "":
|
|
44
|
+
return basename
|
|
45
|
+
if key not in variables:
|
|
46
|
+
raise DestError(f"unknown variable {{{key}}} in {text!r}")
|
|
47
|
+
value = str(variables[key])
|
|
48
|
+
if "\\" in value or ":" in value or ".." in PurePosixPath(value).parts:
|
|
49
|
+
raise DestError(f"variable {{{key}}} may not contain '\\', ':' or '..': {value!r}")
|
|
50
|
+
return value
|
|
51
|
+
|
|
52
|
+
return _VAR.sub(sub, text)
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def resolve(rule: str, basename: str, variables: dict[str, str] | None = None) -> str:
|
|
56
|
+
"""Resolve ``rule`` to a POSIX path relative to the data folder.
|
|
57
|
+
|
|
58
|
+
The result is a directory: sources are placed inside it.
|
|
59
|
+
"""
|
|
60
|
+
if not rule or rule.startswith(("/", "\\")) or ":" in rule.split("/")[0][1:2]:
|
|
61
|
+
raise DestError(f"destination must be relative: {rule!r}")
|
|
62
|
+
expanded = expand(rule.replace("\\", "/"), basename, variables or {})
|
|
63
|
+
parts = [p for p in expanded.split("/") if p not in ("", ".")]
|
|
64
|
+
if not parts:
|
|
65
|
+
raise DestError(f"destination has no target: {rule!r}")
|
|
66
|
+
if any(p == ".." for p in parts):
|
|
67
|
+
raise DestError(f"destination escapes its target: {rule!r}")
|
|
68
|
+
if any("\\" in p or ":" in p for p in parts): # Windows separators or drives
|
|
69
|
+
raise DestError(f"destination parts may not contain '\\' or ':': {expanded!r}")
|
|
70
|
+
root = target_root(target_name(parts[0]))
|
|
71
|
+
return str(PurePosixPath(root, *parts[1:]))
|
fontlab_instamix/env.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
# this_file: src/fontlab_instamix/env.py
|
|
2
|
+
"""Facts about the running Python and the FontLab data folder.
|
|
3
|
+
|
|
4
|
+
The data folder is chosen in this order: an explicit argument, the
|
|
5
|
+
``FONTLAB_DATA_DIR`` environment variable, ``fontlab.flPreferences()
|
|
6
|
+
.dataPathDefault`` when running inside FontLab, then the platform default.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import os
|
|
12
|
+
import platform
|
|
13
|
+
import sys
|
|
14
|
+
from pathlib import Path
|
|
15
|
+
|
|
16
|
+
from . import platformdirs
|
|
17
|
+
|
|
18
|
+
DATA_ENV = "FONTLAB_DATA_DIR"
|
|
19
|
+
STANDARD_FOLDERS = ("Scripts", "Actions", "Codepage", "Data", "Encoding")
|
|
20
|
+
FONTLAB_PYTHON = "3.11" # FontLab 9 embeds Python 3.11 whatever runs the CLI
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def python_version() -> str:
|
|
24
|
+
"""``major.minor`` of the running Python, e.g. ``3.11``."""
|
|
25
|
+
return f"{sys.version_info.major}.{sys.version_info.minor}"
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def machine() -> str:
|
|
29
|
+
"""Normalised CPU architecture: ``arm64`` or ``x86_64``."""
|
|
30
|
+
name = platform.machine().lower()
|
|
31
|
+
if name in ("arm64", "aarch64"):
|
|
32
|
+
return "arm64"
|
|
33
|
+
if name in ("amd64", "x86_64", "x64"):
|
|
34
|
+
return "x86_64"
|
|
35
|
+
return name
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def uv_platform(plat: str = sys.platform, arch: str | None = None) -> str:
|
|
39
|
+
"""Target triple for ``uv pip install --python-platform``."""
|
|
40
|
+
arch = arch or machine()
|
|
41
|
+
cpu = "aarch64" if arch == "arm64" else "x86_64"
|
|
42
|
+
if plat == "win32":
|
|
43
|
+
return f"{cpu}-pc-windows-msvc"
|
|
44
|
+
if plat == "darwin":
|
|
45
|
+
return f"{cpu}-apple-darwin"
|
|
46
|
+
return f"{cpu}-unknown-linux-gnu"
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def fontlab_data_dir() -> Path | None:
|
|
50
|
+
"""Ask FontLab for its data folder; ``None`` outside FontLab."""
|
|
51
|
+
try:
|
|
52
|
+
import fontlab # type: ignore[import-not-found]
|
|
53
|
+
except ImportError:
|
|
54
|
+
return None
|
|
55
|
+
try:
|
|
56
|
+
# No-arg flPreferences() is in established use (fontlabapp plugins/boot.py).
|
|
57
|
+
value = fontlab.flPreferences().dataPathDefault
|
|
58
|
+
if callable(value):
|
|
59
|
+
value = value()
|
|
60
|
+
except Exception: # noqa: BLE001 - any FontLab failure means "unknown"
|
|
61
|
+
return None
|
|
62
|
+
return Path(str(value)) if value else None
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def data_dir(explicit: str | os.PathLike | None = None) -> Path:
|
|
66
|
+
"""Return the FontLab data folder (not created)."""
|
|
67
|
+
if explicit:
|
|
68
|
+
return Path(explicit).expanduser()
|
|
69
|
+
if os.environ.get(DATA_ENV):
|
|
70
|
+
return Path(os.environ[DATA_ENV]).expanduser()
|
|
71
|
+
return fontlab_data_dir() or platformdirs.default_data_dir()
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def site_packages(data: Path) -> Path:
|
|
75
|
+
"""``<data>/python/3.11/site-packages`` used by FontLab's sys.path."""
|
|
76
|
+
return Path(data) / "python" / FONTLAB_PYTHON / "site-packages"
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def ensure_folders(data: Path) -> None:
|
|
80
|
+
"""Create the standard data subfolders instamix links into."""
|
|
81
|
+
for name in (*STANDARD_FOLDERS, "Extensions/Library"):
|
|
82
|
+
(Path(data) / name).mkdir(parents=True, exist_ok=True)
|
|
83
|
+
site_packages(data).mkdir(parents=True, exist_ok=True)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def describe(data: Path | None = None) -> dict:
|
|
87
|
+
"""Summary used by ``fontlab-instamix env``."""
|
|
88
|
+
data = data or data_dir()
|
|
89
|
+
return {
|
|
90
|
+
"python": python_version(),
|
|
91
|
+
"implementation": sys.version.split()[0],
|
|
92
|
+
"platform": sys.platform,
|
|
93
|
+
"machine": machine(),
|
|
94
|
+
"uv_platform": uv_platform(),
|
|
95
|
+
"data_dir": str(data),
|
|
96
|
+
"site_packages": str(site_packages(data)),
|
|
97
|
+
"in_fontlab": "fontlab" in sys.modules,
|
|
98
|
+
}
|