abysshub 1.0.1.dev10__tar.gz → 1.0.1.dev11__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.
Files changed (26) hide show
  1. {abysshub-1.0.1.dev10 → abysshub-1.0.1.dev11}/PKG-INFO +1 -1
  2. {abysshub-1.0.1.dev10 → abysshub-1.0.1.dev11}/pyproject.toml +1 -1
  3. {abysshub-1.0.1.dev10 → abysshub-1.0.1.dev11}/src/abysshub/_command.py +106 -26
  4. {abysshub-1.0.1.dev10 → abysshub-1.0.1.dev11}/.gitignore +0 -0
  5. {abysshub-1.0.1.dev10 → abysshub-1.0.1.dev11}/LICENSE +0 -0
  6. {abysshub-1.0.1.dev10 → abysshub-1.0.1.dev11}/README.md +0 -0
  7. {abysshub-1.0.1.dev10 → abysshub-1.0.1.dev11}/src/abysshub/__init__.py +0 -0
  8. {abysshub-1.0.1.dev10 → abysshub-1.0.1.dev11}/src/abysshub/__main__.py +0 -0
  9. {abysshub-1.0.1.dev10 → abysshub-1.0.1.dev11}/src/abysshub/_async_client.py +0 -0
  10. {abysshub-1.0.1.dev10 → abysshub-1.0.1.dev11}/src/abysshub/_client.py +0 -0
  11. {abysshub-1.0.1.dev10 → abysshub-1.0.1.dev11}/src/abysshub/_error.py +0 -0
  12. {abysshub-1.0.1.dev10 → abysshub-1.0.1.dev11}/src/abysshub/_files.py +0 -0
  13. {abysshub-1.0.1.dev10 → abysshub-1.0.1.dev11}/src/abysshub/_http.py +0 -0
  14. {abysshub-1.0.1.dev10 → abysshub-1.0.1.dev11}/src/abysshub/_run.py +0 -0
  15. {abysshub-1.0.1.dev10 → abysshub-1.0.1.dev11}/src/abysshub/_types.py +0 -0
  16. {abysshub-1.0.1.dev10 → abysshub-1.0.1.dev11}/src/abysshub/py.typed +0 -0
  17. {abysshub-1.0.1.dev10 → abysshub-1.0.1.dev11}/tests/caller.py +0 -0
  18. {abysshub-1.0.1.dev10 → abysshub-1.0.1.dev11}/tests/conftest.py +0 -0
  19. {abysshub-1.0.1.dev10 → abysshub-1.0.1.dev11}/tests/fake_server.py +0 -0
  20. {abysshub-1.0.1.dev10 → abysshub-1.0.1.dev11}/tests/test_client.py +0 -0
  21. {abysshub-1.0.1.dev10 → abysshub-1.0.1.dev11}/tests/test_command.py +0 -0
  22. {abysshub-1.0.1.dev10 → abysshub-1.0.1.dev11}/tests/test_files.py +0 -0
  23. {abysshub-1.0.1.dev10 → abysshub-1.0.1.dev11}/tests/test_http.py +0 -0
  24. {abysshub-1.0.1.dev10 → abysshub-1.0.1.dev11}/tests/test_package.py +0 -0
  25. {abysshub-1.0.1.dev10 → abysshub-1.0.1.dev11}/tests/test_replay.py +0 -0
  26. {abysshub-1.0.1.dev10 → abysshub-1.0.1.dev11}/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.dev10
3
+ Version: 1.0.1.dev11
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.dev10"
7
+ version = "1.0.1.dev11"
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",
@@ -4,6 +4,7 @@ prompt: it stops or it goes, and every stop exits non-zero with its ``code``."""
4
4
  from __future__ import annotations
5
5
 
6
6
  import argparse
7
+ import hashlib
7
8
  import json
8
9
  import os
9
10
  import sys
@@ -40,6 +41,9 @@ def main(argv: list[str] | None = None) -> int:
40
41
  description=f"Publish a Widget folder, with its {PUBLISH_FILE} and {TEST_INPUTS}, and print its link.",
41
42
  )
42
43
  publish_verb.add_argument("folder", type=Path)
44
+ publish_verb.add_argument(
45
+ "--force", action="store_true", help="publish over a Widget that moved on Abyss: the folder wins"
46
+ )
43
47
  args = parser.parse_args(argv)
44
48
  widget_folder, faults = _read_folder(args.folder)
45
49
  try:
@@ -51,7 +55,7 @@ def main(argv: list[str] | None = None) -> int:
51
55
  if faults:
52
56
  return _stop(faults)
53
57
  try:
54
- print(publish(abyss, args.folder, widget_folder))
58
+ print(publish(abyss, args.folder, widget_folder, args.force))
55
59
  except AbyssError as error:
56
60
  print(f"abysshub: stopped: {error.code}: {error.message}", file=sys.stderr)
57
61
  if error.request_id:
@@ -175,36 +179,43 @@ def _names_file(folder: Path, path: Any) -> bool:
175
179
  return target.is_file() and folder.resolve() in target.parents
176
180
 
177
181
 
178
- def publish(abyss: Abyss, folder: Path, widget_folder: _Folder) -> str:
179
- """Publishes a folder that was never published, and answers the Widget's link.
182
+ def publish(abyss: Abyss, folder: Path, widget_folder: _Folder, force: bool = False) -> str:
183
+ """Publishes a folder, and answers the Widget's link: a new Widget for a folder that was
184
+ never published, a new version of the same Widget for one that was.
180
185
 
181
186
  A Draft's id goes into the Publish File as soon as it is made, and its Candidate's
182
187
  number once committed, so a publish that stops is taken up again on the same Draft.
188
+ A published Widget is first compared with the folder, and a Widget that moved on
189
+ Abyss stops the publish before any upload, unless ``force``.
183
190
  """
184
191
  publish_file = widget_folder.publish_file
185
192
  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
-
193
+ block = dict(publish_file.get("widget") or {})
193
194
  widget = block.get("id")
194
- if widget is None:
195
+ # A Widget published already saves only what differs; a new one saves every Listing Field.
196
+ listing: dict[str, Any] | None = None
197
+ changes = _listing_fields(publish_file)
198
+ send_thumbnail = bool(publish_file.get("thumbnail"))
199
+ if "version" in block:
200
+ listing, changes, send_thumbnail = _compare(abyss, folder, block, publish_file, force)
201
+ base_version = listing["live_version"]
202
+ elif widget is None:
195
203
  draft = _expect(
196
204
  abyss._send(abyss._start(None), "POST", f"{abyss.base_url}/v1/widgets", retries=0),
197
205
  _is_widget,
198
206
  "The Draft answered without an id.",
199
207
  )
200
208
  widget = draft["id"]
201
- _write_block(folder, publish_file, {"id": widget})
209
+ block["id"] = widget
210
+ _write_block(folder, publish_file, block)
202
211
  print(f"Made the Draft {widget}.")
212
+ base_version = None
203
213
  else:
204
214
  print(f"Publishing the Draft {widget}.")
215
+ base_version = block.get("candidate")
205
216
  widget_url = abyss._widget_url(widget)
206
217
 
207
- base = json.dumps({"base_version": block.get("candidate")}).encode()
218
+ base = json.dumps({"base_version": base_version}).encode()
208
219
  opened = _expect(
209
220
  abyss._send(abyss._start(None), "POST", f"{widget_url}/versions", JSON, base),
210
221
  _is_project_version,
@@ -227,7 +238,8 @@ def publish(abyss: Abyss, folder: Path, widget_folder: _Folder) -> str:
227
238
  _is_candidate,
228
239
  "The commit answered without a Candidate.",
229
240
  )
230
- _write_block(folder, publish_file, {"id": widget, "candidate": candidate["version"]})
241
+ block["candidate"] = candidate["version"]
242
+ _write_block(folder, publish_file, block)
231
243
 
232
244
  print(f"Test Run of version {candidate['version']}...")
233
245
  run = _test_run(abyss, folder, widget, candidate["version"], widget_folder.test_input, interface)
@@ -240,20 +252,71 @@ def publish(abyss: Abyss, folder: Path, widget_folder: _Folder) -> str:
240
252
  run_id=run["id"],
241
253
  )
242
254
 
243
- listing = _save_listing(abyss, folder, widget, publish_file)
255
+ thumbnail = publish_file.get("thumbnail") if send_thumbnail else None
256
+ if listing is None or changes or thumbnail:
257
+ listing = _save_listing(abyss, folder, widget, changes, thumbnail)
258
+ if thumbnail:
259
+ block["thumbnail_sha256"] = _sha256(folder / thumbnail)
260
+ block["listing_revision"] = listing["listing_revision"]
261
+ _write_block(folder, publish_file, block)
244
262
  published = _expect(
245
263
  abyss._send(abyss._start(None), "POST", f"{widget_url}/publish"),
246
264
  _is_widget,
247
265
  "The publish answered without the Widget.",
248
266
  )
249
- _write_block(
250
- folder,
251
- publish_file,
252
- {"id": widget, "version": candidate["version"], "listing_revision": listing["listing_revision"]},
253
- )
267
+ block.pop("candidate")
268
+ block["version"] = candidate["version"]
269
+ _write_block(folder, publish_file, block)
254
270
  return str(published["url"])
255
271
 
256
272
 
273
+ def _compare(
274
+ abyss: Abyss, folder: Path, block: dict[str, Any], publish_file: dict[str, Any], force: bool
275
+ ) -> tuple[dict[str, Any], dict[str, Any], bool]:
276
+ """Reads the Widget's listing and prints it beside the folder, before any upload. The
277
+ Widget moved when its listing revision or its live version is not the block's: then
278
+ the publish stops, unless ``force`` makes the folder win. Answers the listing, the
279
+ Listing Fields to save, and whether to send the thumbnail."""
280
+ widget = block["id"]
281
+ listing = abyss._get(
282
+ abyss._start(None),
283
+ f"{abyss._widget_url(widget)}/listing",
284
+ lambda value: _is_listing(value) and "live_version" in value,
285
+ "The listing answered without its revision and its live version.",
286
+ )
287
+ changes = {name: value for name, value in _listing_fields(publish_file).items() if listing.get(name) != value}
288
+ listing_moved = listing["listing_revision"] != block.get("listing_revision")
289
+ code_moved = listing["live_version"] != block.get("version")
290
+ thumbnail = publish_file.get("thumbnail")
291
+ send_thumbnail = bool(thumbnail) and (
292
+ (listing_moved and force) or _sha256(folder / thumbnail) != block.get("thumbnail_sha256")
293
+ )
294
+
295
+ print(f"{widget} on Abyss beside the folder:")
296
+ for name, value in changes.items():
297
+ print(f" {name}: {_shown(listing.get(name))} on Abyss, {_shown(value)} in the folder")
298
+ if send_thumbnail:
299
+ print(f" thumbnail: {thumbnail} in the folder")
300
+ if listing_moved:
301
+ print(" listing: changed on Abyss since the folder's last publish or pull")
302
+ if code_moved:
303
+ print(f" code: version {listing['live_version']} is live on Abyss, the folder's is version {block.get('version')}")
304
+ if not (changes or send_thumbnail or listing_moved or code_moved):
305
+ print(" nothing differs")
306
+
307
+ if (listing_moved or code_moved) and not force:
308
+ moved = " and ".join(what for what, did in (("its listing", listing_moved), ("its code", code_moved)) if did)
309
+ pull = "take the website's listing with `abysshub pull`, or " if listing_moved else ""
310
+ raise AbyssError(
311
+ "widget_moved",
312
+ f"{widget} moved on Abyss since this folder's last publish or pull: {moved} changed there. "
313
+ f"Nothing was sent. To go on, {pull}publish with --force to make the folder win.",
314
+ )
315
+ if listing_moved or code_moved:
316
+ print("--force: the folder wins over what moved on Abyss.")
317
+ return listing, changes, send_thumbnail
318
+
319
+
257
320
  def _folder_files(folder: Path, junk: dict[str, Any], thumbnail: str | None) -> tuple[list[str], list[str]]:
258
321
  """The folder's files to send, and what it leaves out, by their paths in the folder:
259
322
  what the Junk list names, the Publish File, its thumbnail, and a root ``.env`` with its
@@ -320,13 +383,13 @@ def _test_run(
320
383
  return reply.run
321
384
 
322
385
 
323
- def _save_listing(abyss: Abyss, folder: Path, widget: str, publish_file: dict[str, Any]) -> dict[str, Any]:
324
- """Saves the Publish File's Listing Fields, ``private`` unless it says, and sends the
325
- thumbnail it names first. Answers the listing as saved."""
386
+ def _save_listing(
387
+ abyss: Abyss, folder: Path, widget: str, save: dict[str, Any], thumbnail: str | None
388
+ ) -> dict[str, Any]:
389
+ """Saves these Listing Fields, and sends the thumbnail at that path in the folder
390
+ first, if any. Answers the listing as saved."""
326
391
  widget_url = abyss._widget_url(widget)
327
- save = {name: publish_file[name] for name in LISTING_FIELDS if name in publish_file}
328
- save.setdefault("visibility", "private")
329
- thumbnail = publish_file.get("thumbnail")
392
+ save = dict(save)
330
393
  if thumbnail:
331
394
  path = folder / thumbnail
332
395
  data = path.read_bytes()
@@ -348,6 +411,23 @@ def _save_listing(abyss: Abyss, folder: Path, widget: str, publish_file: dict[st
348
411
  )
349
412
 
350
413
 
414
+ def _listing_fields(publish_file: dict[str, Any]) -> dict[str, Any]:
415
+ """The Listing Fields the Publish File holds, ``private`` unless it says."""
416
+ fields = {name: publish_file[name] for name in LISTING_FIELDS if name in publish_file}
417
+ fields.setdefault("visibility", "private")
418
+ return fields
419
+
420
+
421
+ def _sha256(path: Path) -> str:
422
+ """The SHA-256 of a file, which tells whether the thumbnail changed since it was sent."""
423
+ return hashlib.sha256(path.read_bytes()).hexdigest()
424
+
425
+
426
+ def _shown(value: Any) -> str:
427
+ """A Listing Field's value as JSON, as the Publish File writes it."""
428
+ return json.dumps(value, ensure_ascii=False)
429
+
430
+
351
431
  def _read_json(path: Path, kind: str, shape: type, faults: list[tuple[str, str]]) -> Any:
352
432
  """Reads a JSON file of the folder, or adds its fault and answers None: ``<kind>_missing``
353
433
  when it is absent, ``<kind>_invalid`` when it is not JSON of that shape."""
File without changes
File without changes