Repository navigation
Expand file tree
/
Copy pathmodules.py
More file actions
249 lines (228 loc) · 8.86 KB
/
Copy pathmodules.py
File metadata and controls
249 lines (228 loc) · 8.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# Copyright (c) DataLab Platform Developers, BSD 3-Clause license, see LICENSE file.
"""Inspect single-file plugins without importing or executing their code.
The public catalog keeps a verbatim copy of this Qt-free module. Only literal
strings and references to previously declared string constants are read.
"""
from __future__ import annotations
import ast
import hashlib
import os.path as osp
from typing import Any
from packaging.version import InvalidVersion, Version
MAX_MODULE_BYTES = 4 * 1024 * 1024
_FIELDS = {
"id": "plugin_id",
"name": "display_name",
"version": "version",
"description": "summary",
}
class ModuleInspectionError(ValueError):
"""A source file is not an acceptable single-file plugin artifact."""
def module_name(filename: str) -> str:
"""Return the importable name of a ``datalab_*.py`` file."""
name, extension = osp.splitext(filename)
if (
"/" in filename
or "\\" in filename
or extension != ".py"
or not name.startswith("datalab_")
or len(name) <= len("datalab_")
or not name.isidentifier()
):
raise ModuleInspectionError(
"A plugin module file name must look like datalab_<name>.py"
)
return name
def _string(node: ast.expr, constants: dict[str, str]) -> str | None:
if isinstance(node, ast.Constant) and isinstance(node.value, str):
return node.value
if isinstance(node, ast.Name):
return constants.get(node.id)
return None
def _assignment(node: ast.stmt) -> tuple[str, ast.expr] | None:
if (
isinstance(node, ast.Assign)
and len(node.targets) == 1
and isinstance(node.targets[0], ast.Name)
):
return node.targets[0].id, node.value
if (
isinstance(node, ast.AnnAssign)
and isinstance(node.target, ast.Name)
and node.value is not None
):
return node.target.id, node.value
return None
def _metadata(tree: ast.Module) -> tuple[dict[str, str], list[str]]:
constants: dict[str, str] = {}
constructors: set[str] = set()
namespaces: set[str] = set()
records: list[dict[str, str]] = []
for statement in tree.body:
if isinstance(statement, (ast.Import, ast.ImportFrom)):
for alias in statement.names:
bound = alias.asname or alias.name.split(".", maxsplit=1)[0]
constants.pop(bound, None)
constructors.discard(bound)
namespaces.discard(bound)
if isinstance(statement, ast.ImportFrom) and statement.module in (
"datalab.plugins",
"datalab.plugins.base",
):
constructors.update(
alias.asname or alias.name
for alias in statement.names
if alias.name == "PluginInfo"
)
elif isinstance(statement, ast.Import):
namespaces.update(
alias.asname or alias.name
for alias in statement.names
if alias.name == "datalab.plugins" and alias.asname
)
assignment = _assignment(statement)
if assignment is not None:
name, value = assignment
text = _string(value, constants)
constants.pop(name, None)
constructors.discard(name)
namespaces.discard(name)
if text is not None:
constants[name] = text
if not isinstance(statement, ast.ClassDef):
if assignment is None and not isinstance(
statement, (ast.Import, ast.ImportFrom)
):
for node in ast.walk(statement):
if isinstance(node, ast.Name) and isinstance(node.ctx, ast.Store):
constants.pop(node.id, None)
constructors.discard(node.id)
namespaces.discard(node.id)
continue
local = dict(constants)
local_constructors = set(constructors)
local_namespaces = set(namespaces)
for member in statement.body:
assignment = _assignment(member)
if assignment is None:
for node in ast.walk(member):
if isinstance(node, ast.Name) and isinstance(node.ctx, ast.Store):
local.pop(node.id, None)
local_constructors.discard(node.id)
local_namespaces.discard(node.id)
continue
name, value = assignment
if name != "PLUGIN_INFO":
text = _string(value, local)
local.pop(name, None)
local_constructors.discard(name)
local_namespaces.discard(name)
if text is not None:
local[name] = text
continue
record: dict[str, str] = {}
if isinstance(value, ast.Call):
function = value.func
recognized = (
isinstance(function, ast.Name) and function.id in local_constructors
) or (
isinstance(function, ast.Attribute)
and function.attr == "PluginInfo"
and isinstance(function.value, ast.Name)
and function.value.id in local_namespaces
)
if recognized and not any(
keyword.arg is None for keyword in value.keywords
):
for keyword in value.keywords:
if keyword.arg in _FIELDS:
text = _string(keyword.value, local)
if text is not None:
record[_FIELDS[keyword.arg]] = text
records.append(record)
warnings: list[str] = []
if len(records) != 1:
warnings.append(
"Plugin metadata is not unique or not statically readable; "
"the catalog describes the module as a whole"
)
return {}, warnings
record = records[0]
if "version" in record and record["version"]:
try:
Version(record["version"])
except InvalidVersion:
warnings.append("The declared version is not comparable; using the digest")
record.pop("version")
if not all(record.get(field) for field in ("plugin_id", "display_name", "version")):
warnings.append(
"Some plugin metadata is unavailable without executing the file"
)
return record, warnings
def inspect_module(path_or_bytes: str | bytes, *, filename: str) -> dict[str, Any]:
"""Return file facts and safely readable plugin metadata.
Valid Python is not necessarily a working or trustworthy plugin. Missing
metadata stays unknown; callers may supply it explicitly in a catalog.
"""
name = module_name(filename)
if isinstance(path_or_bytes, str):
with open(path_or_bytes, "rb") as stream:
data = stream.read(MAX_MODULE_BYTES + 1)
else:
data = path_or_bytes
if not data:
raise ModuleInspectionError("Plugin module is empty")
if len(data) > MAX_MODULE_BYTES:
raise ModuleInspectionError(
f"Plugin module exceeds the {MAX_MODULE_BYTES} byte size limit"
)
try:
tree = ast.parse(data, filename=filename)
# Parsing alone accepts e.g. a return outside a function.
compile(tree, filename, "exec")
except (SyntaxError, ValueError, UnicodeError, RecursionError) as exc:
raise ModuleInspectionError(f"Invalid Python plugin {filename}: {exc}") from exc
metadata, warnings = _metadata(tree)
return {
"filename": filename,
"name": name,
"sha256": hashlib.sha256(data).hexdigest(),
"size_bytes": len(data),
"version": metadata.get("version", ""),
"plugin_id": metadata.get("plugin_id", ""),
"display_name": metadata.get("display_name", ""),
"summary": metadata.get("summary", ""),
"warnings": warnings,
}
def apply_catalog_metadata(
manifest: dict[str, Any],
*,
plugin_id: str,
name: str,
version: str,
summary: str = "",
) -> dict[str, Any]:
"""Supplement unknown metadata, refusing a known identity/version conflict."""
if version:
Version(version)
for field, expected in (("plugin_id", plugin_id), ("version", version)):
actual = manifest[field]
if (
actual
and expected
and (
Version(actual) != Version(expected)
if field == "version"
else actual != expected
)
):
raise ModuleInspectionError(
f"Catalog {field} {expected!r} differs from the file: {actual!r}"
)
return {
**manifest,
"plugin_id": plugin_id,
"display_name": name,
"version": version or manifest["version"],
"summary": summary or manifest["summary"],
}