abysshub 1.0.1.dev10__tar.gz → 1.0.1.dev12__tar.gz
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.
- {abysshub-1.0.1.dev10 → abysshub-1.0.1.dev12}/PKG-INFO +1 -1
- {abysshub-1.0.1.dev10 → abysshub-1.0.1.dev12}/pyproject.toml +1 -1
- {abysshub-1.0.1.dev10 → abysshub-1.0.1.dev12}/src/abysshub/_command.py +282 -36
- {abysshub-1.0.1.dev10 → abysshub-1.0.1.dev12}/tests/test_command.py +13 -2
- {abysshub-1.0.1.dev10 → abysshub-1.0.1.dev12}/.gitignore +0 -0
- {abysshub-1.0.1.dev10 → abysshub-1.0.1.dev12}/LICENSE +0 -0
- {abysshub-1.0.1.dev10 → abysshub-1.0.1.dev12}/README.md +0 -0
- {abysshub-1.0.1.dev10 → abysshub-1.0.1.dev12}/src/abysshub/__init__.py +0 -0
- {abysshub-1.0.1.dev10 → abysshub-1.0.1.dev12}/src/abysshub/__main__.py +0 -0
- {abysshub-1.0.1.dev10 → abysshub-1.0.1.dev12}/src/abysshub/_async_client.py +0 -0
- {abysshub-1.0.1.dev10 → abysshub-1.0.1.dev12}/src/abysshub/_client.py +0 -0
- {abysshub-1.0.1.dev10 → abysshub-1.0.1.dev12}/src/abysshub/_error.py +0 -0
- {abysshub-1.0.1.dev10 → abysshub-1.0.1.dev12}/src/abysshub/_files.py +0 -0
- {abysshub-1.0.1.dev10 → abysshub-1.0.1.dev12}/src/abysshub/_http.py +0 -0
- {abysshub-1.0.1.dev10 → abysshub-1.0.1.dev12}/src/abysshub/_run.py +0 -0
- {abysshub-1.0.1.dev10 → abysshub-1.0.1.dev12}/src/abysshub/_types.py +0 -0
- {abysshub-1.0.1.dev10 → abysshub-1.0.1.dev12}/src/abysshub/py.typed +0 -0
- {abysshub-1.0.1.dev10 → abysshub-1.0.1.dev12}/tests/caller.py +0 -0
- {abysshub-1.0.1.dev10 → abysshub-1.0.1.dev12}/tests/conftest.py +0 -0
- {abysshub-1.0.1.dev10 → abysshub-1.0.1.dev12}/tests/fake_server.py +0 -0
- {abysshub-1.0.1.dev10 → abysshub-1.0.1.dev12}/tests/test_client.py +0 -0
- {abysshub-1.0.1.dev10 → abysshub-1.0.1.dev12}/tests/test_files.py +0 -0
- {abysshub-1.0.1.dev10 → abysshub-1.0.1.dev12}/tests/test_http.py +0 -0
- {abysshub-1.0.1.dev10 → abysshub-1.0.1.dev12}/tests/test_package.py +0 -0
- {abysshub-1.0.1.dev10 → abysshub-1.0.1.dev12}/tests/test_replay.py +0 -0
- {abysshub-1.0.1.dev10 → abysshub-1.0.1.dev12}/tests/test_timeout.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: abysshub
|
|
3
|
-
Version: 1.0.1.
|
|
3
|
+
Version: 1.0.1.dev12
|
|
4
4
|
Summary: Run a hosted tool, an Abyss Widget, from your own Python code and get its result and files. The official library for the Abyss API.
|
|
5
5
|
Project-URL: Homepage, https://abysshub.com/docs#api
|
|
6
6
|
Project-URL: Documentation, https://abysshub.com/docs
|
|
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "abysshub"
|
|
7
|
-
version = "1.0.1.
|
|
7
|
+
version = "1.0.1.dev12"
|
|
8
8
|
description = "Run a hosted tool, an Abyss Widget, from your own Python code and get its result and files. The official library for the Abyss API."
|
|
9
9
|
keywords = [
|
|
10
10
|
"abyss",
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
"""The ``abysshub`` command. ``abysshub publish <folder>`` does what a person does on the
|
|
2
|
-
website's Publish page, through `/v1`'s routes that change a Widget
|
|
3
|
-
|
|
2
|
+
website's Publish page, through `/v1`'s routes that change a Widget; ``abysshub status``
|
|
3
|
+
shows the Widget on Abyss beside the folder, and ``abysshub pull`` takes its listing. It
|
|
4
|
+
asks nothing at a prompt: it stops or it goes, and every stop exits non-zero with its ``code``."""
|
|
4
5
|
from __future__ import annotations
|
|
5
6
|
|
|
6
7
|
import argparse
|
|
8
|
+
import hashlib
|
|
7
9
|
import json
|
|
8
10
|
import os
|
|
9
11
|
import sys
|
|
@@ -15,6 +17,7 @@ from typing import Any
|
|
|
15
17
|
from ._client import Abyss, _expect, _has_ended, _is_key, _is_upload, _is_widget
|
|
16
18
|
from ._error import AbyssError
|
|
17
19
|
from ._files import file
|
|
20
|
+
from ._run import _downloaded
|
|
18
21
|
from ._types import parse_upload
|
|
19
22
|
|
|
20
23
|
PUBLISH_FILE = "abyss.json"
|
|
@@ -27,6 +30,8 @@ LISTING_FIELDS = ("title", "description", "tags", "visibility", "price", "free_r
|
|
|
27
30
|
IMAGES = (".png", ".jpg", ".jpeg", ".webp", ".gif")
|
|
28
31
|
"""The extensions a thumbnail may have: an image's."""
|
|
29
32
|
JSON = {"Content-Type": "application/json"}
|
|
33
|
+
SOURCES = {"widget": "the Widget", "account": "your account"}
|
|
34
|
+
"""Where an Environment name comes from, in words."""
|
|
30
35
|
|
|
31
36
|
|
|
32
37
|
def main(argv: list[str] | None = None) -> int:
|
|
@@ -40,18 +45,48 @@ def main(argv: list[str] | None = None) -> int:
|
|
|
40
45
|
description=f"Publish a Widget folder, with its {PUBLISH_FILE} and {TEST_INPUTS}, and print its link.",
|
|
41
46
|
)
|
|
42
47
|
publish_verb.add_argument("folder", type=Path)
|
|
48
|
+
publish_verb.add_argument(
|
|
49
|
+
"--force", action="store_true", help="publish over a Widget that moved on Abyss: the folder wins"
|
|
50
|
+
)
|
|
51
|
+
status_verb = verbs.add_parser(
|
|
52
|
+
"status",
|
|
53
|
+
help="show the Widget on Abyss beside the folder, changing nothing",
|
|
54
|
+
description="Show the Widget on Abyss beside the folder: its link, its version, each Listing Field that "
|
|
55
|
+
"differs, where each Environment name comes from, and whether it moved. Changes nothing.",
|
|
56
|
+
)
|
|
57
|
+
status_verb.add_argument("folder", type=Path)
|
|
58
|
+
pull_verb = verbs.add_parser(
|
|
59
|
+
"pull",
|
|
60
|
+
help="take the Widget's listing and thumbnail from Abyss into the folder",
|
|
61
|
+
description=f"Write the Widget's Listing Fields into {PUBLISH_FILE} and its thumbnail into the folder. "
|
|
62
|
+
"Never touches code.",
|
|
63
|
+
)
|
|
64
|
+
pull_verb.add_argument("folder", type=Path)
|
|
43
65
|
args = parser.parse_args(argv)
|
|
44
|
-
|
|
66
|
+
if args.verb == "publish":
|
|
67
|
+
widget_folder, faults = _read_folder(args.folder)
|
|
68
|
+
else:
|
|
69
|
+
publish_file, faults = _read_publish_file(args.folder)
|
|
70
|
+
if faults:
|
|
71
|
+
return _stop(faults)
|
|
72
|
+
if args.verb == "status" and "version" not in (publish_file.get("widget") or {}):
|
|
73
|
+
print(f"{args.folder} was never published: `abysshub publish {args.folder}` publishes it.")
|
|
74
|
+
return 0
|
|
45
75
|
try:
|
|
46
76
|
abyss = Abyss()
|
|
47
77
|
except AbyssError as error:
|
|
48
78
|
return _stop([*faults, (error.code, error.message)])
|
|
49
79
|
with abyss:
|
|
50
|
-
faults.extend(_key_faults(abyss))
|
|
51
|
-
if faults:
|
|
52
|
-
return _stop(faults)
|
|
53
80
|
try:
|
|
54
|
-
|
|
81
|
+
if args.verb == "publish":
|
|
82
|
+
faults.extend(_key_faults(abyss))
|
|
83
|
+
if faults:
|
|
84
|
+
return _stop(faults)
|
|
85
|
+
print(publish(abyss, args.folder, widget_folder, args.force))
|
|
86
|
+
elif args.verb == "status":
|
|
87
|
+
status(abyss, args.folder, publish_file)
|
|
88
|
+
else:
|
|
89
|
+
pull(abyss, args.folder, publish_file)
|
|
55
90
|
except AbyssError as error:
|
|
56
91
|
print(f"abysshub: stopped: {error.code}: {error.message}", file=sys.stderr)
|
|
57
92
|
if error.request_id:
|
|
@@ -101,6 +136,15 @@ def _read_folder(folder: Path) -> tuple[_Folder, list[tuple[str, str]]]:
|
|
|
101
136
|
return _Folder(publish_file or {}, interface, test_input), faults
|
|
102
137
|
|
|
103
138
|
|
|
139
|
+
def _read_publish_file(folder: Path) -> tuple[dict[str, Any], list[tuple[str, str]]]:
|
|
140
|
+
"""Reads the Publish File alone, as ``status`` and ``pull`` need it, with its faults."""
|
|
141
|
+
faults: list[tuple[str, str]] = []
|
|
142
|
+
publish_file = _read_json(folder / PUBLISH_FILE, "publish_file", dict, faults)
|
|
143
|
+
if publish_file is not None and not isinstance(publish_file.get("widget") or {}, dict):
|
|
144
|
+
faults.append(("publish_file_invalid", f"The widget block of {PUBLISH_FILE} is not a JSON object."))
|
|
145
|
+
return publish_file or {}, faults
|
|
146
|
+
|
|
147
|
+
|
|
104
148
|
def _check_listing(folder: Path, publish_file: dict[str, Any], faults: list[tuple[str, str]]) -> None:
|
|
105
149
|
"""The Publish File's faults: no title, no tag, and a thumbnail that is missing on a
|
|
106
150
|
first publish or names no image file in the folder."""
|
|
@@ -169,42 +213,51 @@ def _key_faults(abyss: Abyss) -> list[tuple[str, str]]:
|
|
|
169
213
|
|
|
170
214
|
def _names_file(folder: Path, path: Any) -> bool:
|
|
171
215
|
"""Whether ``path`` names a file inside the folder."""
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
target = (folder / path).resolve()
|
|
175
|
-
return target.is_file() and folder.resolve() in target.parents
|
|
216
|
+
return isinstance(path, str) and _inside(folder, path) and (folder / path).is_file()
|
|
217
|
+
|
|
176
218
|
|
|
219
|
+
def _inside(folder: Path, path: str) -> bool:
|
|
220
|
+
"""Whether ``path`` is inside the folder, whether or not a file is there."""
|
|
221
|
+
return bool(path) and folder.resolve() in (folder / path).resolve().parents
|
|
177
222
|
|
|
178
|
-
|
|
179
|
-
|
|
223
|
+
|
|
224
|
+
def publish(abyss: Abyss, folder: Path, widget_folder: _Folder, force: bool = False) -> str:
|
|
225
|
+
"""Publishes a folder, and answers the Widget's link: a new Widget for a folder that was
|
|
226
|
+
never published, a new version of the same Widget for one that was.
|
|
180
227
|
|
|
181
228
|
A Draft's id goes into the Publish File as soon as it is made, and its Candidate's
|
|
182
229
|
number once committed, so a publish that stops is taken up again on the same Draft.
|
|
230
|
+
A published Widget is first compared with the folder, and a Widget that moved on
|
|
231
|
+
Abyss stops the publish before any upload, unless ``force``.
|
|
183
232
|
"""
|
|
184
233
|
publish_file = widget_folder.publish_file
|
|
185
234
|
interface = widget_folder.interface
|
|
186
|
-
block = publish_file.get("widget") or {}
|
|
187
|
-
if "version" in block:
|
|
188
|
-
raise AbyssError(
|
|
189
|
-
"widget_published",
|
|
190
|
-
f"This folder is {block.get('id')}, published already; this abysshub publishes a new Widget only.",
|
|
191
|
-
)
|
|
192
|
-
|
|
235
|
+
block = dict(publish_file.get("widget") or {})
|
|
193
236
|
widget = block.get("id")
|
|
194
|
-
|
|
237
|
+
# A Widget published already saves only what differs; a new one saves every Listing Field.
|
|
238
|
+
listing: dict[str, Any] | None = None
|
|
239
|
+
changes = _listing_fields(publish_file)
|
|
240
|
+
send_thumbnail = bool(publish_file.get("thumbnail"))
|
|
241
|
+
if "version" in block:
|
|
242
|
+
listing, changes, send_thumbnail = _compare(abyss, folder, block, publish_file, force)
|
|
243
|
+
base_version = listing["live_version"]
|
|
244
|
+
elif widget is None:
|
|
195
245
|
draft = _expect(
|
|
196
246
|
abyss._send(abyss._start(None), "POST", f"{abyss.base_url}/v1/widgets", retries=0),
|
|
197
247
|
_is_widget,
|
|
198
248
|
"The Draft answered without an id.",
|
|
199
249
|
)
|
|
200
250
|
widget = draft["id"]
|
|
201
|
-
|
|
251
|
+
block["id"] = widget
|
|
252
|
+
_write_block(folder, publish_file, block)
|
|
202
253
|
print(f"Made the Draft {widget}.")
|
|
254
|
+
base_version = None
|
|
203
255
|
else:
|
|
204
256
|
print(f"Publishing the Draft {widget}.")
|
|
257
|
+
base_version = block.get("candidate")
|
|
205
258
|
widget_url = abyss._widget_url(widget)
|
|
206
259
|
|
|
207
|
-
base = json.dumps({"base_version":
|
|
260
|
+
base = json.dumps({"base_version": base_version}).encode()
|
|
208
261
|
opened = _expect(
|
|
209
262
|
abyss._send(abyss._start(None), "POST", f"{widget_url}/versions", JSON, base),
|
|
210
263
|
_is_project_version,
|
|
@@ -227,7 +280,8 @@ def publish(abyss: Abyss, folder: Path, widget_folder: _Folder) -> str:
|
|
|
227
280
|
_is_candidate,
|
|
228
281
|
"The commit answered without a Candidate.",
|
|
229
282
|
)
|
|
230
|
-
|
|
283
|
+
block["candidate"] = candidate["version"]
|
|
284
|
+
_write_block(folder, publish_file, block)
|
|
231
285
|
|
|
232
286
|
print(f"Test Run of version {candidate['version']}...")
|
|
233
287
|
run = _test_run(abyss, folder, widget, candidate["version"], widget_folder.test_input, interface)
|
|
@@ -240,20 +294,190 @@ def publish(abyss: Abyss, folder: Path, widget_folder: _Folder) -> str:
|
|
|
240
294
|
run_id=run["id"],
|
|
241
295
|
)
|
|
242
296
|
|
|
243
|
-
|
|
297
|
+
thumbnail = publish_file.get("thumbnail") if send_thumbnail else None
|
|
298
|
+
if listing is None or changes or thumbnail:
|
|
299
|
+
listing = _save_listing(abyss, folder, widget, changes, thumbnail)
|
|
300
|
+
if thumbnail:
|
|
301
|
+
block["thumbnail_sha256"] = _sha256(folder / thumbnail)
|
|
302
|
+
block["listing_revision"] = listing["listing_revision"]
|
|
303
|
+
_write_block(folder, publish_file, block)
|
|
244
304
|
published = _expect(
|
|
245
305
|
abyss._send(abyss._start(None), "POST", f"{widget_url}/publish"),
|
|
246
306
|
_is_widget,
|
|
247
307
|
"The publish answered without the Widget.",
|
|
248
308
|
)
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
{"id": widget, "version": candidate["version"], "listing_revision": listing["listing_revision"]},
|
|
253
|
-
)
|
|
309
|
+
block.pop("candidate")
|
|
310
|
+
block["version"] = candidate["version"]
|
|
311
|
+
_write_block(folder, publish_file, block)
|
|
254
312
|
return str(published["url"])
|
|
255
313
|
|
|
256
314
|
|
|
315
|
+
def _read_listing(abyss: Abyss, widget: str) -> dict[str, Any]:
|
|
316
|
+
"""Reads a Widget's Listing Fields with its listing revision and its live version."""
|
|
317
|
+
return abyss._get(
|
|
318
|
+
abyss._start(None),
|
|
319
|
+
f"{abyss._widget_url(widget)}/listing",
|
|
320
|
+
lambda value: _is_listing(value) and "live_version" in value,
|
|
321
|
+
"The listing answered without its revision and its live version.",
|
|
322
|
+
)
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
@dataclass
|
|
326
|
+
class _Beside:
|
|
327
|
+
"""A published Widget's listing on Abyss, beside the folder that was its last publish or pull."""
|
|
328
|
+
|
|
329
|
+
listing: dict[str, Any]
|
|
330
|
+
changes: dict[str, Any]
|
|
331
|
+
"""The Publish File's Listing Fields whose value is not the website's."""
|
|
332
|
+
thumbnail_changed: bool
|
|
333
|
+
"""The folder's thumbnail is not the one last sent or pulled."""
|
|
334
|
+
listing_moved: bool
|
|
335
|
+
code_moved: bool
|
|
336
|
+
|
|
337
|
+
|
|
338
|
+
def _beside(abyss: Abyss, folder: Path, block: dict[str, Any], publish_file: dict[str, Any]) -> _Beside:
|
|
339
|
+
"""Reads the Widget's listing and prints it beside the folder. The Widget moved when
|
|
340
|
+
its listing revision or its live version is not the block's."""
|
|
341
|
+
widget = block["id"]
|
|
342
|
+
listing = _read_listing(abyss, widget)
|
|
343
|
+
changes = {name: value for name, value in _listing_fields(publish_file).items() if listing.get(name) != value}
|
|
344
|
+
thumbnail = publish_file.get("thumbnail")
|
|
345
|
+
path = folder / thumbnail if isinstance(thumbnail, str) and thumbnail else None
|
|
346
|
+
beside = _Beside(
|
|
347
|
+
listing,
|
|
348
|
+
changes,
|
|
349
|
+
path is not None and (not path.is_file() or _sha256(path) != block.get("thumbnail_sha256")),
|
|
350
|
+
listing["listing_revision"] != block.get("listing_revision"),
|
|
351
|
+
listing["live_version"] != block.get("version"),
|
|
352
|
+
)
|
|
353
|
+
|
|
354
|
+
print(f"{widget} on Abyss beside the folder:")
|
|
355
|
+
for name, value in changes.items():
|
|
356
|
+
print(f" {name}: {_shown(listing.get(name))} on Abyss, {_shown(value)} in the folder")
|
|
357
|
+
if beside.thumbnail_changed:
|
|
358
|
+
print(f" thumbnail: {thumbnail} changed in the folder")
|
|
359
|
+
if beside.listing_moved:
|
|
360
|
+
print(" listing: changed on Abyss since the folder's last publish or pull")
|
|
361
|
+
if beside.code_moved:
|
|
362
|
+
print(f" code: version {listing['live_version']} is live on Abyss, the folder's is version {block.get('version')}")
|
|
363
|
+
else:
|
|
364
|
+
print(f" code: version {listing['live_version']} is live on Abyss, as in the folder")
|
|
365
|
+
if not (changes or beside.thumbnail_changed or beside.listing_moved or beside.code_moved):
|
|
366
|
+
print(" nothing else differs")
|
|
367
|
+
return beside
|
|
368
|
+
|
|
369
|
+
|
|
370
|
+
def _moved(beside: _Beside) -> str:
|
|
371
|
+
"""What moved on Abyss, in words: its listing, its code, or both."""
|
|
372
|
+
return " and ".join(
|
|
373
|
+
what for what, did in (("its listing", beside.listing_moved), ("its code", beside.code_moved)) if did
|
|
374
|
+
)
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
def _compare(
|
|
378
|
+
abyss: Abyss, folder: Path, block: dict[str, Any], publish_file: dict[str, Any], force: bool
|
|
379
|
+
) -> tuple[dict[str, Any], dict[str, Any], bool]:
|
|
380
|
+
"""Prints the Widget beside the folder, before any upload. A Widget that moved stops
|
|
381
|
+
the publish, unless ``force`` makes the folder win. Answers the listing, the Listing
|
|
382
|
+
Fields to save, and whether to send the thumbnail."""
|
|
383
|
+
widget = block["id"]
|
|
384
|
+
beside = _beside(abyss, folder, block, publish_file)
|
|
385
|
+
moved = beside.listing_moved or beside.code_moved
|
|
386
|
+
if moved and not force:
|
|
387
|
+
pull = "take the website's listing with `abysshub pull`, or " if beside.listing_moved else ""
|
|
388
|
+
raise AbyssError(
|
|
389
|
+
"widget_moved",
|
|
390
|
+
f"{widget} moved on Abyss since this folder's last publish or pull: {_moved(beside)} changed there. "
|
|
391
|
+
f"Nothing was sent. To go on, {pull}publish with --force to make the folder win.",
|
|
392
|
+
)
|
|
393
|
+
if moved:
|
|
394
|
+
print("--force: the folder wins over what moved on Abyss.")
|
|
395
|
+
send_thumbnail = bool(publish_file.get("thumbnail")) and (
|
|
396
|
+
(beside.listing_moved and force) or beside.thumbnail_changed
|
|
397
|
+
)
|
|
398
|
+
return beside.listing, beside.changes, send_thumbnail
|
|
399
|
+
|
|
400
|
+
|
|
401
|
+
def status(abyss: Abyss, folder: Path, publish_file: dict[str, Any]) -> None:
|
|
402
|
+
"""Prints a published Widget beside its folder, and sends nothing that changes anything:
|
|
403
|
+
its link, its version, each Listing Field that differs, where each Environment name
|
|
404
|
+
comes from (never a value), and whether it moved since the folder's last publish or pull."""
|
|
405
|
+
block = publish_file["widget"]
|
|
406
|
+
widget = block["id"]
|
|
407
|
+
print(f"{widget}: {abyss.widgets.get(widget).url}")
|
|
408
|
+
beside = _beside(abyss, folder, block, publish_file)
|
|
409
|
+
environment = abyss._get(
|
|
410
|
+
abyss._start(None),
|
|
411
|
+
f"{abyss._widget_url(widget)}/environment",
|
|
412
|
+
_is_environment,
|
|
413
|
+
"The environment answered without its names.",
|
|
414
|
+
)
|
|
415
|
+
print("Environment:")
|
|
416
|
+
for name, entry in sorted(environment["names"].items()):
|
|
417
|
+
print(f" {name}: from {SOURCES.get(entry.get('source'), entry.get('source'))}")
|
|
418
|
+
if not environment["names"]:
|
|
419
|
+
print(" no names")
|
|
420
|
+
if beside.listing_moved or beside.code_moved:
|
|
421
|
+
print(f"{widget} moved on Abyss since this folder's last publish or pull: {_moved(beside)} changed there.")
|
|
422
|
+
if beside.listing_moved:
|
|
423
|
+
print(f"`abysshub pull {folder}` takes the website's listing into the folder.")
|
|
424
|
+
if beside.code_moved:
|
|
425
|
+
print("Only `abysshub publish --force` replaces the code on Abyss with the folder's.")
|
|
426
|
+
else:
|
|
427
|
+
print(f"{widget} did not move on Abyss since this folder's last publish or pull.")
|
|
428
|
+
|
|
429
|
+
|
|
430
|
+
def pull(abyss: Abyss, folder: Path, publish_file: dict[str, Any]) -> None:
|
|
431
|
+
"""Writes the website's value of each Listing Field the Publish File holds into it, the
|
|
432
|
+
Widget's thumbnail to the path the Publish File names, and the listing revision into the
|
|
433
|
+
``widget`` block, and prints each value it changed. It never touches code: a newer live
|
|
434
|
+
version is only named."""
|
|
435
|
+
block = dict(publish_file.get("widget") or {})
|
|
436
|
+
if "version" not in block:
|
|
437
|
+
raise AbyssError(
|
|
438
|
+
"not_published",
|
|
439
|
+
f"{folder} was never published, so there is nothing to pull. `abysshub publish {folder}` publishes it.",
|
|
440
|
+
)
|
|
441
|
+
thumbnail = publish_file.get("thumbnail")
|
|
442
|
+
if thumbnail and not (
|
|
443
|
+
isinstance(thumbnail, str) and Path(thumbnail).suffix.lower() in IMAGES and _inside(folder, thumbnail)
|
|
444
|
+
):
|
|
445
|
+
raise AbyssError(
|
|
446
|
+
"thumbnail_not_image",
|
|
447
|
+
f"The thumbnail {thumbnail!r} names no image file ({', '.join(IMAGES)}) inside the folder. Nothing was pulled.",
|
|
448
|
+
)
|
|
449
|
+
widget = block["id"]
|
|
450
|
+
listing = _read_listing(abyss, widget)
|
|
451
|
+
print(f"Pulled the listing of {widget} into the folder:")
|
|
452
|
+
changed = False
|
|
453
|
+
for name, value in _listing_fields(publish_file).items():
|
|
454
|
+
if listing[name] != value:
|
|
455
|
+
print(f" {name}: {_shown(listing[name])}, was {_shown(value)}")
|
|
456
|
+
publish_file[name] = listing[name]
|
|
457
|
+
changed = True
|
|
458
|
+
url = listing["thumbnail"]
|
|
459
|
+
# Only to the path the Publish File names: a path the command chose could be one of the folder's files.
|
|
460
|
+
if thumbnail and isinstance(url, str) and url:
|
|
461
|
+
answered, read = abyss._download(url, None)
|
|
462
|
+
data = _downloaded("the thumbnail", answered, read())
|
|
463
|
+
path = folder / thumbnail
|
|
464
|
+
if not path.is_file() or path.read_bytes() != data:
|
|
465
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
466
|
+
path.write_bytes(data)
|
|
467
|
+
print(f" thumbnail: written to {thumbnail}")
|
|
468
|
+
changed = True
|
|
469
|
+
block["thumbnail_sha256"] = hashlib.sha256(data).hexdigest()
|
|
470
|
+
if not changed:
|
|
471
|
+
print(" nothing changed")
|
|
472
|
+
block["listing_revision"] = listing["listing_revision"]
|
|
473
|
+
_write_block(folder, publish_file, block)
|
|
474
|
+
if listing["live_version"] != block["version"]:
|
|
475
|
+
print(
|
|
476
|
+
f"The code on Abyss is newer: version {listing['live_version']} is live, the folder's is version "
|
|
477
|
+
f"{block['version']}. A pull never touches code: only `abysshub publish --force` replaces it with the folder's."
|
|
478
|
+
)
|
|
479
|
+
|
|
480
|
+
|
|
257
481
|
def _folder_files(folder: Path, junk: dict[str, Any], thumbnail: str | None) -> tuple[list[str], list[str]]:
|
|
258
482
|
"""The folder's files to send, and what it leaves out, by their paths in the folder:
|
|
259
483
|
what the Junk list names, the Publish File, its thumbnail, and a root ``.env`` with its
|
|
@@ -320,13 +544,13 @@ def _test_run(
|
|
|
320
544
|
return reply.run
|
|
321
545
|
|
|
322
546
|
|
|
323
|
-
def _save_listing(
|
|
324
|
-
|
|
325
|
-
|
|
547
|
+
def _save_listing(
|
|
548
|
+
abyss: Abyss, folder: Path, widget: str, save: dict[str, Any], thumbnail: str | None
|
|
549
|
+
) -> dict[str, Any]:
|
|
550
|
+
"""Saves these Listing Fields, and sends the thumbnail at that path in the folder
|
|
551
|
+
first, if any. Answers the listing as saved."""
|
|
326
552
|
widget_url = abyss._widget_url(widget)
|
|
327
|
-
save =
|
|
328
|
-
save.setdefault("visibility", "private")
|
|
329
|
-
thumbnail = publish_file.get("thumbnail")
|
|
553
|
+
save = dict(save)
|
|
330
554
|
if thumbnail:
|
|
331
555
|
path = folder / thumbnail
|
|
332
556
|
data = path.read_bytes()
|
|
@@ -348,6 +572,23 @@ def _save_listing(abyss: Abyss, folder: Path, widget: str, publish_file: dict[st
|
|
|
348
572
|
)
|
|
349
573
|
|
|
350
574
|
|
|
575
|
+
def _listing_fields(publish_file: dict[str, Any]) -> dict[str, Any]:
|
|
576
|
+
"""The Listing Fields the Publish File holds, ``private`` unless it says."""
|
|
577
|
+
fields = {name: publish_file[name] for name in LISTING_FIELDS if name in publish_file}
|
|
578
|
+
fields.setdefault("visibility", "private")
|
|
579
|
+
return fields
|
|
580
|
+
|
|
581
|
+
|
|
582
|
+
def _sha256(path: Path) -> str:
|
|
583
|
+
"""The SHA-256 of a file, which tells whether the thumbnail changed since it was sent."""
|
|
584
|
+
return hashlib.sha256(path.read_bytes()).hexdigest()
|
|
585
|
+
|
|
586
|
+
|
|
587
|
+
def _shown(value: Any) -> str:
|
|
588
|
+
"""A Listing Field's value as JSON, as the Publish File writes it."""
|
|
589
|
+
return json.dumps(value, ensure_ascii=False)
|
|
590
|
+
|
|
591
|
+
|
|
351
592
|
def _read_json(path: Path, kind: str, shape: type, faults: list[tuple[str, str]]) -> Any:
|
|
352
593
|
"""Reads a JSON file of the folder, or adds its fault and answers None: ``<kind>_missing``
|
|
353
594
|
when it is absent, ``<kind>_invalid`` when it is not JSON of that shape."""
|
|
@@ -414,3 +655,8 @@ def _is_candidate(value: Any) -> bool:
|
|
|
414
655
|
|
|
415
656
|
def _is_listing(value: Any) -> bool:
|
|
416
657
|
return isinstance(value, dict) and isinstance(value.get("listing_revision"), str)
|
|
658
|
+
|
|
659
|
+
|
|
660
|
+
def _is_environment(value: Any) -> bool:
|
|
661
|
+
names = value.get("names") if isinstance(value, dict) else None
|
|
662
|
+
return isinstance(names, dict) and all(isinstance(entry, dict) for entry in names.values())
|
|
@@ -24,7 +24,12 @@ def test_command(recording: dict[str, Any], monkeypatch: pytest.MonkeyPatch, cap
|
|
|
24
24
|
with with_files(call["files"]) as dir:
|
|
25
25
|
monkeypatch.setenv("ABYSS_API_KEY", KEY)
|
|
26
26
|
monkeypatch.setenv("ABYSS_BASE_URL", server.base)
|
|
27
|
-
|
|
27
|
+
before = {path: (dir / path).read_bytes() for path in call["files"]}
|
|
28
|
+
commands = [call["command"], *(call.get("then") or [])]
|
|
29
|
+
# Each command before the last must go through.
|
|
30
|
+
for command in commands[:-1]:
|
|
31
|
+
assert main([*command, str(dir)]) == 0, capsys.readouterr().err
|
|
32
|
+
exit = main([*commands[-1], str(dir)])
|
|
28
33
|
printed = capsys.readouterr()
|
|
29
34
|
|
|
30
35
|
assert server.problems == []
|
|
@@ -33,7 +38,13 @@ def test_command(recording: dict[str, Any], monkeypatch: pytest.MonkeyPatch, cap
|
|
|
33
38
|
for text in outcome["printed"]:
|
|
34
39
|
assert text in printed.out + printed.err, f"printed {text}"
|
|
35
40
|
for path, fields in outcome["written"].items():
|
|
36
|
-
|
|
41
|
+
if isinstance(fields, str):
|
|
42
|
+
assert (dir / path).read_text("utf-8") == fields, path
|
|
43
|
+
else:
|
|
44
|
+
assert_fields(json.loads((dir / path).read_text("utf-8")), fields, path)
|
|
45
|
+
for path, content in before.items():
|
|
46
|
+
if path not in outcome["written"]:
|
|
47
|
+
assert (dir / path).read_bytes() == content, f"{path} was left as it was"
|
|
37
48
|
finally:
|
|
38
49
|
server.close()
|
|
39
50
|
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|