workpeg 0.164.0__tar.gz → 0.166.0__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.
- {workpeg-0.164.0/src/workpeg.egg-info → workpeg-0.166.0}/PKG-INFO +3 -1
- {workpeg-0.164.0 → workpeg-0.166.0}/README.md +2 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/pyproject.toml +1 -1
- workpeg-0.166.0/src/workpeg/builder/__init__.py +22 -0
- workpeg-0.166.0/src/workpeg/builder/__main__.py +3 -0
- workpeg-0.166.0/src/workpeg/builder/canonical.py +381 -0
- workpeg-0.166.0/src/workpeg/builder/envelope.py +94 -0
- workpeg-0.166.0/src/workpeg/builder/stages.py +188 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/cli.py +28 -4
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/function_check.py +16 -3
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/project_submit.py +64 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/templates/agents/AGENTS.md +5 -4
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/templates/project/function/wheels/README.txt +6 -2
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/compiler/js_generator.py +6 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/runtime/ai-question-frame.md +1 -1
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/runtime/ai.js +296 -23
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/runtime/scaffold.js +399 -37
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/runtime/workpeg-runtime.js +58 -13
- workpeg-0.166.0/src/workpeg/ui/scaffold_check.py +546 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/verify.py +390 -52
- {workpeg-0.164.0 → workpeg-0.166.0/src/workpeg.egg-info}/PKG-INFO +3 -1
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg.egg-info/SOURCES.txt +6 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/tests/test_project_submit.py +40 -3
- {workpeg-0.164.0 → workpeg-0.166.0}/LICENSE +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/MANIFEST.in +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/setup.cfg +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/__init__.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/auth.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/build.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/cdn.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/config.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/context.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/create_new.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/doctor.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/function_base.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/function_envelopes.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/function_local.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/listen.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/project.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/run.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/runtime.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/sandbox/__init__.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/sandbox/docker_driver.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/sandbox/driver.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/starter.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/submit.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/target_sdk.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/templates/__init__.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/templates/agents/CLAUDE.md +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/templates/functions/Dockerfile +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/templates/functions/LICENSE +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/templates/functions/README.md +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/templates/functions/app/__init__.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/templates/functions/app/main.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/templates/functions/requirements.txt +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/templates/functions/wheels/README.txt +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/templates/project/README.md +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/templates/project/function/Dockerfile +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/templates/project/function/app/__init__.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/templates/project/function/app/main.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/templates/project/function/requirements.txt +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/templates/project/workpeg.json +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/templates/starter/app_shell.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/templates/starter/components.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/templates/starter/main.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/templates/starter/pages/__init__.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/templates/starter/pages/customer.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/templates/starter/pages/customers.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/templates/starter/pages/home.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/templates/starter/pages/not_found.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/terminal.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/__init__.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/actions.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/actions_manifest.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/app.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/build.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/calendar.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/compiler/__init__.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/compiler/ast_parser.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/compiler/ir.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/compiler/rtc_checks.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/compiler/rtc_tables.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/create.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/dates.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/dev_refused_names.json +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/devmode.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/devserve.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/devsite.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/fake_cloud.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/minify.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/permissions_manifest.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/render_nodes.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/renderers_manifest.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/rtc.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/runtime/ai-concurrent-replies.md +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/runtime/ai-prompt-queue.md +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/runtime/ai-subagent-frame.md +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/runtime/editor.js +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/runtime/fonts/OFL.txt +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/runtime/fonts/geist-italic-cyrillic-ext.af944491.woff2 +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/runtime/fonts/geist-italic-cyrillic.88be6946.woff2 +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/runtime/fonts/geist-italic-latin-ext.370a8962.woff2 +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/runtime/fonts/geist-italic-latin.7611b95c.woff2 +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/runtime/fonts/geist-italic-vietnamese.8dfcf303.woff2 +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/runtime/fonts/geist-normal-cyrillic-ext.b7a545bb.woff2 +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/runtime/fonts/geist-normal-cyrillic.6129fc85.woff2 +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/runtime/fonts/geist-normal-latin-ext.58a6b173.woff2 +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/runtime/fonts/geist-normal-latin.9b6f5ff4.woff2 +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/runtime/fonts/geist-normal-vietnamese.f689f638.woff2 +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/runtime/icons.css.gz +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/runtime/overlay.js +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/runtime/pegment.css +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/runtime/rtc.js +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/scaffold.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/search_manifest.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/serve.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/skills_manifest.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/state.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/validators.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/watcher.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/widgets/__init__.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/widgets/base.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/widgets/canvas.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/widgets/display.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/widgets/fields.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/widgets/forms.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/widgets/futures.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/widgets/gestures.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/widgets/interactive.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/widgets/layout.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/ui/widgets/pickers.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg/utils.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg.egg-info/dependency_links.txt +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg.egg-info/entry_points.txt +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg.egg-info/requires.txt +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/src/workpeg.egg-info/top_level.txt +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/tests/test_agents_guide.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/tests/test_auth.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/tests/test_build.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/tests/test_cdn.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/tests/test_cdn_publish.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/tests/test_changelog.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/tests/test_changelog_guard.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/tests/test_cli.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/tests/test_command_cheatsheet.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/tests/test_config.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/tests/test_context.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/tests/test_create_new.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/tests/test_dev_functions_wire.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/tests/test_dev_install_example.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/tests/test_doctor.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/tests/test_function_check.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/tests/test_function_envelopes.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/tests/test_function_listen.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/tests/test_pegfn_base.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/tests/test_project_create.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/tests/test_release_script.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/tests/test_run.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/tests/test_run_cli.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/tests/test_runtime.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/tests/test_runtime_invoke_key.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/tests/test_submit.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/tests/test_target_sdk.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/tests/test_utils.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/tests/test_version.py +0 -0
- {workpeg-0.164.0 → workpeg-0.166.0}/tests/test_watch_pipeline.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: workpeg
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.166.0
|
|
4
4
|
Summary: Workpeg function runtime and SDK
|
|
5
5
|
Author-email: Workpeg <support@workpeg.com>
|
|
6
6
|
License: MIT
|
|
@@ -194,6 +194,8 @@ Full reference: [docs/cdn.md](https://gitlab.com/workpeg/workpeg-sdk/-/blob/main
|
|
|
194
194
|
|
|
195
195
|
Write Peg UI apps in Python. The SDK compiles them to vanilla JavaScript — no React, no Vue, no Python in the browser.
|
|
196
196
|
|
|
197
|
+
**A peg is Python only.** The SDK gives you no way to put your own JavaScript on a peg's page, or HTML that runs script: no API, flag, hook or escape hatch. The only JavaScript on a page is the SDK's own — the compiler's output, the runtime, `rtc.js` when the peg makes calls, and the chrome bundles (`ai.js`, `scaffold.js`, `editor.js`), which load from the CDN on every peg so the Workpeg team can update them everywhere at once. Every build carries a script policy naming exactly those CDN files, never all of `cdn.workpeg.com`. See [docs/index.md](https://gitlab.com/workpeg/workpeg-sdk/-/blob/main/docs/index.md#a-peg-is-python-only).
|
|
198
|
+
|
|
197
199
|
```
|
|
198
200
|
Python App (single-page or multi-route Router)
|
|
199
201
|
↓ AST Parser
|
|
@@ -169,6 +169,8 @@ Full reference: [docs/cdn.md](https://gitlab.com/workpeg/workpeg-sdk/-/blob/main
|
|
|
169
169
|
|
|
170
170
|
Write Peg UI apps in Python. The SDK compiles them to vanilla JavaScript — no React, no Vue, no Python in the browser.
|
|
171
171
|
|
|
172
|
+
**A peg is Python only.** The SDK gives you no way to put your own JavaScript on a peg's page, or HTML that runs script: no API, flag, hook or escape hatch. The only JavaScript on a page is the SDK's own — the compiler's output, the runtime, `rtc.js` when the peg makes calls, and the chrome bundles (`ai.js`, `scaffold.js`, `editor.js`), which load from the CDN on every peg so the Workpeg team can update them everywhere at once. Every build carries a script policy naming exactly those CDN files, never all of `cdn.workpeg.com`. See [docs/index.md](https://gitlab.com/workpeg/workpeg-sdk/-/blob/main/docs/index.md#a-peg-is-python-only).
|
|
173
|
+
|
|
172
174
|
```
|
|
173
175
|
Python App (single-page or multi-route Router)
|
|
174
176
|
↓ AST Parser
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"""Workpeg's own build of a peg version: the stages that run in the
|
|
2
|
+
sandbox on the builder host (design section 4; docs/builder.md).
|
|
3
|
+
|
|
4
|
+
Nothing a developer built goes live (owner, 2026-11-02). A peg version is
|
|
5
|
+
uploaded as Python source, and Workpeg builds it in three stages, each a
|
|
6
|
+
fresh container with no network that reads its input on stdin and writes
|
|
7
|
+
its output on stdout:
|
|
8
|
+
|
|
9
|
+
0. ``python -I -m workpeg.builder canonicalise`` (this SDK, the current
|
|
10
|
+
one): read ``upload.tar.gz`` as hostile, refuse what Workpeg refuses,
|
|
11
|
+
write the canonical ``source.tar`` (:mod:`workpeg.builder.canonical`).
|
|
12
|
+
1. ``python -I /opt/workpeg-builder/stage1.py`` in the builder image of
|
|
13
|
+
the peg's target SDK: build ``source.tar`` with that SDK's own compiler
|
|
14
|
+
(``builder/stage1.py`` in the SDK repository, copied into every image).
|
|
15
|
+
2. ``python -I -m workpeg.builder verify`` (this SDK): check the dist as
|
|
16
|
+
hostile against the target image's digest list, with
|
|
17
|
+
:func:`workpeg.ui.verify.verify`.
|
|
18
|
+
|
|
19
|
+
Stages 0 and 2 are trusted code reading untrusted bytes; stage 1 is the
|
|
20
|
+
only one that compiles a developer's source, and the one the sandbox is
|
|
21
|
+
built around.
|
|
22
|
+
"""
|
|
@@ -0,0 +1,381 @@
|
|
|
1
|
+
"""Stage 0 of Workpeg's build: read a peg version's upload as hostile, refuse
|
|
2
|
+
what Workpeg refuses, and write the canonical source.
|
|
3
|
+
|
|
4
|
+
Design section 1 and section 4, "Stage 0, canonicalise". It runs in the
|
|
5
|
+
current SDK's image, in a sandbox with no network, so a hostile archive
|
|
6
|
+
never meets the registry's own process. It reads ``upload.tar.gz`` from
|
|
7
|
+
memory and EXTRACTS NOTHING: no file is written, no link is followed, no
|
|
8
|
+
name reaches a disk. Every entry goes through the same tar reader
|
|
9
|
+
``workpeg ui verify`` reads a dist with (:mod:`workpeg.ui.verify`), and every
|
|
10
|
+
file through the same refusals ``workpeg project submit`` already applies on
|
|
11
|
+
the developer's computer (:mod:`workpeg.project_submit`), so the two can
|
|
12
|
+
never disagree about what is refused; here they are not a courtesy.
|
|
13
|
+
|
|
14
|
+
What is refused, each problem named, all of them at once:
|
|
15
|
+
|
|
16
|
+
- a gzip stream that is not exactly one member (a second member, or bytes
|
|
17
|
+
after the first, are what another reader may read), or one that inflates
|
|
18
|
+
past the caps;
|
|
19
|
+
- every entry :mod:`workpeg.ui.verify`'s reader refuses: links of both
|
|
20
|
+
kinds, devices and FIFOs, absolute paths, ``..``, names outside printable
|
|
21
|
+
ASCII or holding URL syntax, PAX headers that set anything but times, GNU
|
|
22
|
+
long names, duplicates, names that differ only in case, more than 5,000
|
|
23
|
+
entries, more than 60 MB unpacked;
|
|
24
|
+
- anything at the root but ``workpeg.json``, ``ui/``, ``function/`` and
|
|
25
|
+
``assets/``; a ``workpeg.json`` with no exact ``sdk`` at or above the
|
|
26
|
+
floor, or no ``namespace``; no ``ui/main.py``;
|
|
27
|
+
- under ``ui/``: any script or style file, anything but ``.py``, ``.json``
|
|
28
|
+
and the ``public/`` files a build serves (no ``.pdf`` yet),
|
|
29
|
+
``ui/workpeg.json``, and any file or folder named ``workpeg`` or
|
|
30
|
+
``workpeg_*``;
|
|
31
|
+
- under ``assets/``: anything but the CDN route's picture and data types;
|
|
32
|
+
- under ``function/``: built code (by name or by its first bytes: a
|
|
33
|
+
package, an archive, a native library, compiled Python) anywhere but as
|
|
34
|
+
``function/wheels/<name>.whl``, and anything in ``function/wheels/`` but
|
|
35
|
+
those wheels and a README.txt. Only wheels can be matched against PyPI.
|
|
36
|
+
|
|
37
|
+
What it cannot check here, because the sandbox has no network: whether a
|
|
38
|
+
wheel under ``function/wheels/`` is PyPI's own file. It lists each wheel
|
|
39
|
+
with its sha256 in the summary, and the builder host, which has a network,
|
|
40
|
+
asks PyPI before it records the build.
|
|
41
|
+
|
|
42
|
+
What it writes:
|
|
43
|
+
|
|
44
|
+
- ``source.tar``: the canonical source. A plain ustar of regular files
|
|
45
|
+
only, sorted by name, owner 0, time 0, mode 0644 or 0755, no PAX header.
|
|
46
|
+
The same files in any order, with any owners or times, make the same
|
|
47
|
+
bytes, and its sha256 is the version's ``source_sha256``;
|
|
48
|
+
- the function bundle, when there is a ``function/``: the same bytes
|
|
49
|
+
``workpeg project submit`` packs for it (``project_submit._tar_gz``), so
|
|
50
|
+
a developer's ``function_sha256`` and Workpeg's ``bundle_sha256`` agree;
|
|
51
|
+
- the summary the builder host records.
|
|
52
|
+
"""
|
|
53
|
+
from __future__ import annotations
|
|
54
|
+
|
|
55
|
+
import hashlib
|
|
56
|
+
import io
|
|
57
|
+
import json
|
|
58
|
+
import tarfile
|
|
59
|
+
import zlib
|
|
60
|
+
from dataclasses import dataclass, field
|
|
61
|
+
from typing import Optional
|
|
62
|
+
|
|
63
|
+
from workpeg import project_submit, target_sdk
|
|
64
|
+
from workpeg import function_check
|
|
65
|
+
from workpeg.ui import verify
|
|
66
|
+
|
|
67
|
+
__all__ = [
|
|
68
|
+
"Canonical", "Refused", "MAX_UPLOAD_BYTES", "canonicalise",
|
|
69
|
+
"canonical_tar",
|
|
70
|
+
]
|
|
71
|
+
|
|
72
|
+
#: The most compressed bytes Workpeg takes (decision 13).
|
|
73
|
+
MAX_UPLOAD_BYTES = project_submit.MAX_UPLOAD_BYTES
|
|
74
|
+
|
|
75
|
+
#: The most a stream may inflate to: the unpacked cap, plus a header and
|
|
76
|
+
#: padding per entry and the end-of-archive blocks.
|
|
77
|
+
MAX_INFLATED_BYTES = (project_submit.MAX_UNPACKED_BYTES
|
|
78
|
+
+ 1024 * project_submit.MAX_ENTRIES + 10240)
|
|
79
|
+
|
|
80
|
+
#: The most problems a refusal lists; the count of the rest follows.
|
|
81
|
+
_SHOWN = 50
|
|
82
|
+
|
|
83
|
+
_SUMMARY_FORMAT = 1
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
class Refused(Exception):
|
|
87
|
+
"""The upload is refused. ``problems`` names each reason."""
|
|
88
|
+
|
|
89
|
+
def __init__(self, problems):
|
|
90
|
+
self.problems = list(problems)
|
|
91
|
+
shown = self.problems[:_SHOWN]
|
|
92
|
+
more = len(self.problems) - len(shown)
|
|
93
|
+
text = "\n".join(shown)
|
|
94
|
+
if more > 0:
|
|
95
|
+
text += f"\n...and {more} more."
|
|
96
|
+
super().__init__(text)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
@dataclass
|
|
100
|
+
class Canonical:
|
|
101
|
+
"""What stage 0 made from one upload."""
|
|
102
|
+
|
|
103
|
+
source: bytes
|
|
104
|
+
source_sha256: str
|
|
105
|
+
summary: dict
|
|
106
|
+
#: The function bundle, gzip'd, when the version has a ``function/``.
|
|
107
|
+
bundle: Optional[bytes] = None
|
|
108
|
+
files: "dict[str, bytes]" = field(default_factory=dict)
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def _inflate(data: bytes) -> bytes:
|
|
112
|
+
"""The one gzip member *data* holds, or Refused: never more than
|
|
113
|
+
MAX_INFLATED_BYTES, and nothing after it."""
|
|
114
|
+
inflater = zlib.decompressobj(16 + zlib.MAX_WBITS)
|
|
115
|
+
out = []
|
|
116
|
+
size = 0
|
|
117
|
+
pending = data
|
|
118
|
+
try:
|
|
119
|
+
while not inflater.eof:
|
|
120
|
+
chunk = inflater.decompress(pending, 1 << 20)
|
|
121
|
+
pending = inflater.unconsumed_tail
|
|
122
|
+
if not chunk and not pending:
|
|
123
|
+
break
|
|
124
|
+
size += len(chunk)
|
|
125
|
+
if size > MAX_INFLATED_BYTES:
|
|
126
|
+
raise Refused([
|
|
127
|
+
f"The upload inflates to more than "
|
|
128
|
+
f"{MAX_INFLATED_BYTES:,} bytes, past the 60 MB a peg "
|
|
129
|
+
f"version may unpack to; it is refused unread."])
|
|
130
|
+
out.append(chunk)
|
|
131
|
+
except zlib.error as exc:
|
|
132
|
+
raise Refused([f"The upload is not a whole gzip stream ({exc})."]) \
|
|
133
|
+
from None
|
|
134
|
+
if not inflater.eof:
|
|
135
|
+
raise Refused(["The upload is not a whole gzip stream: it ends "
|
|
136
|
+
"before the stream does."])
|
|
137
|
+
if inflater.unused_data:
|
|
138
|
+
raise Refused(["The upload holds bytes after its gzip stream (a "
|
|
139
|
+
"second member, or anything else), which another "
|
|
140
|
+
"reader would read as more of it."])
|
|
141
|
+
return b"".join(out)
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def _read(raw: bytes) -> "verify._Reader":
|
|
145
|
+
"""Every entry of the tar *raw*, read by the verifier's own reader."""
|
|
146
|
+
reader = verify._Reader()
|
|
147
|
+
try:
|
|
148
|
+
with tarfile.open(fileobj=io.BytesIO(raw), mode="r:") as archive:
|
|
149
|
+
while not reader.full:
|
|
150
|
+
member = archive.next()
|
|
151
|
+
if member is None:
|
|
152
|
+
why = verify.trailing_problem(raw[archive.offset:])
|
|
153
|
+
if why:
|
|
154
|
+
reader.problems.append(f"The upload {why}")
|
|
155
|
+
break
|
|
156
|
+
verify._read_member(archive, member, reader)
|
|
157
|
+
except (tarfile.TarError, OSError, EOFError) as exc:
|
|
158
|
+
reader.problems.append(
|
|
159
|
+
f"The upload is not a plain, whole tar "
|
|
160
|
+
f"({verify._shown(str(exc))}).")
|
|
161
|
+
return reader
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
def _sdk_named_folder(rel: str) -> Optional[str]:
|
|
165
|
+
"""The part of a folder under ``ui/`` named like the SDK or a library."""
|
|
166
|
+
parts = rel.split("/")[1:]
|
|
167
|
+
for index, part in enumerate(parts):
|
|
168
|
+
name = part.lower()
|
|
169
|
+
if name == "workpeg" or name.startswith("workpeg_"):
|
|
170
|
+
return "/".join(parts[:index + 1]) + "/"
|
|
171
|
+
return None
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def _manifest_problems(files: "dict[str, bytes]") -> "tuple[dict, list]":
|
|
175
|
+
data = files.get("workpeg.json")
|
|
176
|
+
if data is None:
|
|
177
|
+
return {}, ["workpeg.json is missing at the root: it names the peg "
|
|
178
|
+
"and the SDK Workpeg builds it with."]
|
|
179
|
+
try:
|
|
180
|
+
manifest = json.loads(data.decode("utf-8"))
|
|
181
|
+
except (UnicodeDecodeError, ValueError, RecursionError) as exc:
|
|
182
|
+
return {}, [f"workpeg.json is not JSON: {verify._shown(str(exc))}."]
|
|
183
|
+
if not isinstance(manifest, dict):
|
|
184
|
+
return {}, ["workpeg.json is not a JSON object."]
|
|
185
|
+
problems = []
|
|
186
|
+
if target_sdk.KEY not in manifest:
|
|
187
|
+
problems.append(
|
|
188
|
+
'workpeg.json names no target SDK ("sdk"), and Workpeg builds a '
|
|
189
|
+
'peg only with the exact SDK it names. Set it with `workpeg '
|
|
190
|
+
'project target X.Y.Z`.')
|
|
191
|
+
else:
|
|
192
|
+
try:
|
|
193
|
+
target_sdk.check(manifest[target_sdk.KEY])
|
|
194
|
+
except target_sdk.TargetError as exc:
|
|
195
|
+
problems.append(str(exc))
|
|
196
|
+
namespace = manifest.get("namespace")
|
|
197
|
+
if not isinstance(namespace, str) or not namespace:
|
|
198
|
+
problems.append('workpeg.json names no "namespace".')
|
|
199
|
+
return manifest, problems
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def _file_problems(files: "dict[str, bytes]",
|
|
203
|
+
folders: "set[str]") -> "list[str]":
|
|
204
|
+
problems = []
|
|
205
|
+
for rel in sorted(files):
|
|
206
|
+
top = rel.split("/", 1)[0]
|
|
207
|
+
if rel == "workpeg.json":
|
|
208
|
+
continue
|
|
209
|
+
if top not in project_submit.ROOT_ENTRIES or "/" not in rel:
|
|
210
|
+
problems.append(
|
|
211
|
+
f"{rel}: a peg version holds workpeg.json and the ui/, "
|
|
212
|
+
f"function/ and assets/ folders, and nothing else at its "
|
|
213
|
+
f"root.")
|
|
214
|
+
continue
|
|
215
|
+
if top == "ui":
|
|
216
|
+
why = project_submit._ui_refusal(rel)
|
|
217
|
+
elif top == "assets":
|
|
218
|
+
why = project_submit._asset_refusal(rel)
|
|
219
|
+
else:
|
|
220
|
+
why = project_submit._function_refusal(rel, files[rel])
|
|
221
|
+
if why:
|
|
222
|
+
problems.append(why)
|
|
223
|
+
for rel in sorted(folders):
|
|
224
|
+
top = rel.split("/", 1)[0]
|
|
225
|
+
if top not in project_submit.ROOT_ENTRIES[1:]:
|
|
226
|
+
problems.append(
|
|
227
|
+
f"{rel}/: a peg version holds workpeg.json and the ui/, "
|
|
228
|
+
f"function/ and assets/ folders, and nothing else at its "
|
|
229
|
+
f"root.")
|
|
230
|
+
elif top == "ui":
|
|
231
|
+
named = _sdk_named_folder(rel)
|
|
232
|
+
if named is not None:
|
|
233
|
+
problems.append(
|
|
234
|
+
f"{rel}/: {named} has the name of the SDK or a Workpeg "
|
|
235
|
+
f"library, so it could stand in for code the reviewer "
|
|
236
|
+
f"reads as Workpeg's. Rename it.")
|
|
237
|
+
if "ui/main.py" not in files:
|
|
238
|
+
problems.append("ui/main.py is missing: Workpeg's build starts "
|
|
239
|
+
"there.")
|
|
240
|
+
return problems
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
def canonical_tar(files: "dict[str, bytes]",
|
|
244
|
+
modes: "dict[str, int]") -> bytes:
|
|
245
|
+
"""A plain ustar of *files* (name to bytes), sorted, owner and time 0,
|
|
246
|
+
mode 0644 or 0755 from *modes*. Regular files only."""
|
|
247
|
+
raw = io.BytesIO()
|
|
248
|
+
with tarfile.open(fileobj=raw, mode="w",
|
|
249
|
+
format=tarfile.USTAR_FORMAT) as tar:
|
|
250
|
+
for name in sorted(files):
|
|
251
|
+
data = files[name]
|
|
252
|
+
info = tarfile.TarInfo(name)
|
|
253
|
+
info.type = tarfile.REGTYPE
|
|
254
|
+
info.size = len(data)
|
|
255
|
+
info.mode = 0o755 if modes.get(name, 0) & 0o111 else 0o644
|
|
256
|
+
info.mtime = 0
|
|
257
|
+
info.uid = info.gid = 0
|
|
258
|
+
info.uname = info.gname = ""
|
|
259
|
+
tar.addfile(info, io.BytesIO(data))
|
|
260
|
+
return raw.getvalue()
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
def _modes(raw: bytes) -> "dict[str, int]":
|
|
264
|
+
"""Each regular file's mode, from a tar the reader already accepted."""
|
|
265
|
+
modes = {}
|
|
266
|
+
with tarfile.open(fileobj=io.BytesIO(raw), mode="r:") as archive:
|
|
267
|
+
for member in archive:
|
|
268
|
+
name = member.name[2:] if member.name.startswith("./") \
|
|
269
|
+
else member.name
|
|
270
|
+
if member.isreg():
|
|
271
|
+
modes[name] = member.mode
|
|
272
|
+
return modes
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
def _function(files: "dict[str, bytes]", modes: "dict[str, int]") -> dict:
|
|
276
|
+
"""The function half: its bundle, packed as ``project submit`` packs
|
|
277
|
+
it, its FROM lines, its wheels, and whether its Dockerfile passes."""
|
|
278
|
+
inside = {rel[len("function/"):]: data for rel, data in files.items()
|
|
279
|
+
if rel.startswith("function/")}
|
|
280
|
+
if not inside:
|
|
281
|
+
return {"bundle": None, "bundle_sha256": None,
|
|
282
|
+
"dockerfile_from": [], "function_check": None,
|
|
283
|
+
"wheels": []}
|
|
284
|
+
folders = set()
|
|
285
|
+
for name in inside:
|
|
286
|
+
parts = name.split("/")[:-1]
|
|
287
|
+
for depth in range(1, len(parts) + 1):
|
|
288
|
+
folders.add("/".join(parts[:depth]))
|
|
289
|
+
entries = [(name, data,
|
|
290
|
+
0o755 if modes.get("function/" + name, 0) & 0o111 else 0o644)
|
|
291
|
+
for name, data in inside.items()]
|
|
292
|
+
bundle = project_submit._tar_gz(entries, sorted(folders))
|
|
293
|
+
froms = []
|
|
294
|
+
try:
|
|
295
|
+
text = inside["Dockerfile"].decode("utf-8")
|
|
296
|
+
for _number, line in function_check._instructions(text):
|
|
297
|
+
if line.split(None, 1)[0].lower() == "from":
|
|
298
|
+
froms.append(line.strip())
|
|
299
|
+
except (KeyError, UnicodeDecodeError, function_check.BundleRefused):
|
|
300
|
+
pass
|
|
301
|
+
return {
|
|
302
|
+
"bundle": bundle,
|
|
303
|
+
"bundle_sha256": hashlib.sha256(bundle).hexdigest(),
|
|
304
|
+
"dockerfile_from": froms,
|
|
305
|
+
"function_check": _function_check(inside),
|
|
306
|
+
"wheels": [
|
|
307
|
+
{"name": name.rsplit("/", 1)[-1],
|
|
308
|
+
"sha256": hashlib.sha256(data).hexdigest()}
|
|
309
|
+
for name, data in sorted(inside.items())
|
|
310
|
+
if project_submit._pypi_wheel(name)
|
|
311
|
+
],
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
def _function_check(inside: "dict[str, bytes]") -> str:
|
|
316
|
+
"""``passed``, or ``refused: <why>``, as the install would decide on
|
|
317
|
+
these bytes: :mod:`workpeg.function_check`'s own rules, read from
|
|
318
|
+
memory."""
|
|
319
|
+
try:
|
|
320
|
+
for required in function_check._REQUIRED:
|
|
321
|
+
if required not in inside:
|
|
322
|
+
raise function_check.BundleRefused(
|
|
323
|
+
f"The function bundle has no regular file {required}.")
|
|
324
|
+
try:
|
|
325
|
+
dockerfile = inside["Dockerfile"].decode("utf-8")
|
|
326
|
+
except ValueError:
|
|
327
|
+
raise function_check.BundleRefused(
|
|
328
|
+
"The function bundle's Dockerfile cannot be read as UTF-8 "
|
|
329
|
+
"text.") from None
|
|
330
|
+
function_check.check_dockerfile(dockerfile)
|
|
331
|
+
function_check.check_config_data(inside.get("workpeg.json"))
|
|
332
|
+
except function_check.BundleRefused as exc:
|
|
333
|
+
return f"refused: {exc}"
|
|
334
|
+
return "passed"
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
def canonicalise(upload: bytes, *,
|
|
338
|
+
expect_sha256: Optional[str] = None) -> Canonical:
|
|
339
|
+
"""Stage 0 over the bytes of one ``upload.tar.gz``. Raises
|
|
340
|
+
:class:`Refused` naming every problem, or returns the canonical source,
|
|
341
|
+
the function bundle and the summary."""
|
|
342
|
+
upload_sha256 = hashlib.sha256(upload).hexdigest()
|
|
343
|
+
if expect_sha256 is not None and upload_sha256 != expect_sha256:
|
|
344
|
+
raise Refused([
|
|
345
|
+
f"The upload's sha256 is {upload_sha256}, not {expect_sha256}, "
|
|
346
|
+
f"the one the developer declared: these are not the bytes that "
|
|
347
|
+
f"were submitted."])
|
|
348
|
+
if len(upload) > MAX_UPLOAD_BYTES:
|
|
349
|
+
raise Refused([f"The upload is {len(upload):,} bytes, and Workpeg "
|
|
350
|
+
f"takes at most {MAX_UPLOAD_BYTES:,}."])
|
|
351
|
+
raw = _inflate(upload)
|
|
352
|
+
reader = _read(raw)
|
|
353
|
+
files = reader.files
|
|
354
|
+
problems = list(reader.problems)
|
|
355
|
+
if not problems:
|
|
356
|
+
problems += _file_problems(files, reader.folders)
|
|
357
|
+
manifest, why = _manifest_problems(files)
|
|
358
|
+
problems += why
|
|
359
|
+
if problems:
|
|
360
|
+
raise Refused(problems)
|
|
361
|
+
modes = _modes(raw)
|
|
362
|
+
source = canonical_tar(files, modes)
|
|
363
|
+
function = _function(files, modes)
|
|
364
|
+
summary = {
|
|
365
|
+
"format": _SUMMARY_FORMAT,
|
|
366
|
+
"upload_sha256": upload_sha256,
|
|
367
|
+
"source_sha256": hashlib.sha256(source).hexdigest(),
|
|
368
|
+
"source_size": len(source),
|
|
369
|
+
"sdk": manifest[target_sdk.KEY],
|
|
370
|
+
"namespace": manifest["namespace"],
|
|
371
|
+
"entries": len(files),
|
|
372
|
+
"unpacked": sum(len(data) for data in files.values()),
|
|
373
|
+
"function": function["bundle"] is not None,
|
|
374
|
+
"bundle_sha256": function["bundle_sha256"],
|
|
375
|
+
"dockerfile_from": function["dockerfile_from"],
|
|
376
|
+
"function_check": function["function_check"],
|
|
377
|
+
"wheels": function["wheels"],
|
|
378
|
+
}
|
|
379
|
+
return Canonical(source=source, source_sha256=summary["source_sha256"],
|
|
380
|
+
summary=summary, bundle=function["bundle"],
|
|
381
|
+
files=files)
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
"""The envelope: how a stage passes more than one file on one stream.
|
|
2
|
+
|
|
3
|
+
A stage reads one stream and writes one stream (design section 4: input on
|
|
4
|
+
stdin, output on stdout). Where it needs more than one file, the stream is
|
|
5
|
+
an envelope: a plain ustar of named regular files, nothing else, in a fixed
|
|
6
|
+
order. Reading one is reading a stage's output, so it is read as hostile:
|
|
7
|
+
only the names the reader expects, each once, each a regular file under its
|
|
8
|
+
own cap, with plain ustar headers (the verifier's own header check), and no
|
|
9
|
+
byte after the end of the archive but the zero padding a tar ends with.
|
|
10
|
+
|
|
11
|
+
It is the standard library only, written so that ``builder/stage1.py``
|
|
12
|
+
can carry a copy of :func:`write` for images of SDKs that predate this
|
|
13
|
+
module; a test holds the two to the same bytes.
|
|
14
|
+
"""
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
17
|
+
import io
|
|
18
|
+
import tarfile
|
|
19
|
+
|
|
20
|
+
__all__ = ["EnvelopeError", "read", "write"]
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class EnvelopeError(Exception):
|
|
24
|
+
"""The stream is not the envelope the reader expects."""
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def write(entries) -> bytes:
|
|
28
|
+
"""A plain ustar of *entries* (``(name, bytes)``), in the order given,
|
|
29
|
+
owner and time 0, mode 0644."""
|
|
30
|
+
raw = io.BytesIO()
|
|
31
|
+
with tarfile.open(fileobj=raw, mode="w",
|
|
32
|
+
format=tarfile.USTAR_FORMAT) as tar:
|
|
33
|
+
for name, data in entries:
|
|
34
|
+
info = tarfile.TarInfo(name)
|
|
35
|
+
info.type = tarfile.REGTYPE
|
|
36
|
+
info.size = len(data)
|
|
37
|
+
info.mode = 0o644
|
|
38
|
+
info.mtime = 0
|
|
39
|
+
info.uid = info.gid = 0
|
|
40
|
+
info.uname = info.gname = ""
|
|
41
|
+
tar.addfile(info, io.BytesIO(data))
|
|
42
|
+
return raw.getvalue()
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def read(data: bytes, caps: "dict[str, int]",
|
|
46
|
+
required=()) -> "dict[str, bytes]":
|
|
47
|
+
"""The files of the envelope *data*. *caps* names every file it may
|
|
48
|
+
hold and the most bytes each may be; *required* those it must hold.
|
|
49
|
+
Raises :class:`EnvelopeError` otherwise."""
|
|
50
|
+
from workpeg.ui import verify
|
|
51
|
+
|
|
52
|
+
limit = sum(caps.values()) + 512 * 2 * (len(caps) + 1) + 10240
|
|
53
|
+
if len(data) > limit:
|
|
54
|
+
raise EnvelopeError(f"the envelope is {len(data):,} bytes, more "
|
|
55
|
+
f"than its files may add up to.")
|
|
56
|
+
files: "dict[str, bytes]" = {}
|
|
57
|
+
try:
|
|
58
|
+
with tarfile.open(fileobj=io.BytesIO(data), mode="r:") as archive:
|
|
59
|
+
while True:
|
|
60
|
+
member = archive.next()
|
|
61
|
+
if member is None:
|
|
62
|
+
break
|
|
63
|
+
name = member.name
|
|
64
|
+
if name not in caps:
|
|
65
|
+
raise EnvelopeError(
|
|
66
|
+
f"it holds {verify._shown(name)}, which is not one "
|
|
67
|
+
f"of {', '.join(sorted(caps))}.")
|
|
68
|
+
if name in files:
|
|
69
|
+
raise EnvelopeError(f"it holds {name} twice.")
|
|
70
|
+
why = verify._header_problem(archive, member)
|
|
71
|
+
if why is not None:
|
|
72
|
+
raise EnvelopeError(f"{name}: {why}")
|
|
73
|
+
if not member.isreg() or member.issparse():
|
|
74
|
+
raise EnvelopeError(f"{name} is not a regular file.")
|
|
75
|
+
if member.size > caps[name]:
|
|
76
|
+
raise EnvelopeError(
|
|
77
|
+
f"{name} is {member.size:,} bytes, more than the "
|
|
78
|
+
f"{caps[name]:,} it may be.")
|
|
79
|
+
handle = archive.extractfile(member)
|
|
80
|
+
body = handle.read(member.size + 1) if handle else b""
|
|
81
|
+
if len(body) != member.size:
|
|
82
|
+
raise EnvelopeError(f"{name} is shorter than its header "
|
|
83
|
+
f"says.")
|
|
84
|
+
files[name] = body
|
|
85
|
+
end = archive.offset
|
|
86
|
+
except (tarfile.TarError, OSError, EOFError) as exc:
|
|
87
|
+
raise EnvelopeError(f"it is not a plain, whole tar ({exc}).") \
|
|
88
|
+
from None
|
|
89
|
+
if data[end:].strip(b"\0"):
|
|
90
|
+
raise EnvelopeError("it holds bytes after the end of the archive.")
|
|
91
|
+
missing = [name for name in required if name not in files]
|
|
92
|
+
if missing:
|
|
93
|
+
raise EnvelopeError(f"it holds no {', '.join(missing)}.")
|
|
94
|
+
return files
|