snowflake-sandbox-python 0.2.2a6__tar.gz → 0.2.2a8__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 (110) hide show
  1. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/PKG-INFO +3 -3
  2. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/pyproject.toml +5 -4
  3. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/cli_sandbox/_git_repo_flags.py +2 -1
  4. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/cli_sandbox/_shell_command.py +3 -1
  5. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/__init__.py +7 -0
  6. snowflake_sandbox_python-0.2.2a8/src/snowflake/sandbox/_build_context.py +342 -0
  7. snowflake_sandbox_python-0.2.2a8/src/snowflake/sandbox/_build_context_fs.py +318 -0
  8. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/_preview.py +33 -27
  9. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/_retry.py +10 -0
  10. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/_sandbox_state.py +80 -8
  11. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/_session.py +29 -4
  12. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/_sse.py +9 -4
  13. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/_ssh.py +38 -3
  14. snowflake_sandbox_python-0.2.2a8/src/snowflake/sandbox/_ssh_trace.py +102 -0
  15. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/_sync_transport.py +180 -25
  16. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/_transport.py +69 -7
  17. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/_transport_errors.py +47 -14
  18. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/_version.py +1 -1
  19. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/_wire.py +11 -10
  20. snowflake_sandbox_python-0.2.2a8/src/snowflake/sandbox/bundles.py +205 -0
  21. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/client.py +203 -25
  22. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/egress.py +42 -7
  23. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/exceptions.py +23 -1
  24. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/git_repo.py +16 -3
  25. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/image.py +439 -10
  26. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/secret.py +7 -6
  27. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/shell.py +6 -2
  28. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/sync_client.py +199 -25
  29. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake_sandbox_python.egg-info/PKG-INFO +3 -3
  30. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake_sandbox_python.egg-info/SOURCES.txt +4 -0
  31. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake_sandbox_python.egg-info/requires.txt +2 -2
  32. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/tests/test_docs_references.py +1 -0
  33. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/LICENSE +0 -0
  34. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/README.md +0 -0
  35. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/setup.cfg +0 -0
  36. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/setup.py +0 -0
  37. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/cli_sandbox/__init__.py +0 -0
  38. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/cli_sandbox/_adapter.py +0 -0
  39. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/cli_sandbox/_common.py +0 -0
  40. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/cli_sandbox/_egress_flags.py +0 -0
  41. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/cli_sandbox/_get_command.py +0 -0
  42. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/cli_sandbox/_lifecycle_command.py +0 -0
  43. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/cli_sandbox/_remote_command.py +0 -0
  44. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/cli_sandbox/_run_command.py +0 -0
  45. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/cli_sandbox/_upload_plan.py +0 -0
  46. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/cli_sandbox/commands.py +0 -0
  47. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/cli_sandbox/plugin_spec.py +0 -0
  48. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/cli_sandbox/py.typed +0 -0
  49. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/__main__.py +0 -0
  50. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/_ansi.py +0 -0
  51. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/_args.py +0 -0
  52. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/_assemble.py +0 -0
  53. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/_bundle.py +0 -0
  54. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/_code_delivery.py +0 -0
  55. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/_connection_resolve.py +0 -0
  56. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/_deploy_spec.py +0 -0
  57. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/_diagnostics.py +0 -0
  58. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/_env.py +0 -0
  59. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/_files_mixin.py +0 -0
  60. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/_fs_ops.py +0 -0
  61. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/_globs.py +0 -0
  62. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/_hosts.py +0 -0
  63. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/_mcp_discovery.py +0 -0
  64. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/_mcp_status.py +0 -0
  65. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/_proto/__init__.py +0 -0
  66. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/_proto/sandbox_pb2.py +0 -0
  67. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/_proto/sandbox_pb2.pyi +0 -0
  68. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/_runtime/__init__.py +0 -0
  69. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/_runtime/_fs_helper.py +0 -0
  70. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/_runtime/_job_runner.py +0 -0
  71. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/_runtime/_protocol.py +0 -0
  72. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/_runtime/_shims.py +0 -0
  73. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/_session_registry.py +0 -0
  74. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/_stage.py +0 -0
  75. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/_sync_files_mixin.py +0 -0
  76. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/_sync_fs_ops.py +0 -0
  77. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/_sync_watch.py +0 -0
  78. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/_tunnel.py +0 -0
  79. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/_upload_plan.py +0 -0
  80. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/_watch.py +0 -0
  81. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/app.py +0 -0
  82. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/config.py +0 -0
  83. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/connect.py +0 -0
  84. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/deploy.py +0 -0
  85. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/endpoint.py +0 -0
  86. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/exec_stream.py +0 -0
  87. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/failure.py +0 -0
  88. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/files.py +0 -0
  89. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/function.py +0 -0
  90. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/jobs.py +0 -0
  91. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/lifecycle.py +0 -0
  92. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/log_stream.py +0 -0
  93. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/mcp.py +0 -0
  94. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/mount.py +0 -0
  95. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/py.typed +0 -0
  96. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/session_app.py +0 -0
  97. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/snowflake_connection.py +0 -0
  98. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/sync_exec_stream.py +0 -0
  99. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/sync_files.py +0 -0
  100. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/sync_log_stream.py +0 -0
  101. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/sync_shell.py +0 -0
  102. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/types.py +0 -0
  103. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake/sandbox/warm_session.py +0 -0
  104. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake_sandbox_python.egg-info/dependency_links.txt +0 -0
  105. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake_sandbox_python.egg-info/entry_points.txt +0 -0
  106. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/src/snowflake_sandbox_python.egg-info/top_level.txt +0 -0
  107. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/tests/test_gate_tristate.py +0 -0
  108. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/tests/test_imports.py +0 -0
  109. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/tests/test_smoke_offline.py +0 -0
  110. {snowflake_sandbox_python-0.2.2a6 → snowflake_sandbox_python-0.2.2a8}/tests/test_tutorial_sources.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: snowflake-sandbox-python
3
- Version: 0.2.2a6
3
+ Version: 0.2.2a8
4
4
  Summary: Snowflake sandboxes — Python SDK and CLI.
5
5
  Author: Snowflake, Inc.
6
6
  License:
@@ -221,9 +221,9 @@ Description-Content-Type: text/markdown
221
221
  License-File: LICENSE
222
222
  Requires-Dist: httpx[http2]<1.0,>=0.27
223
223
  Requires-Dist: typing-extensions>=4.6
224
- Requires-Dist: snowflake-connector-python>=3.6
224
+ Requires-Dist: snowflake-connector-python>=4.7.3
225
225
  Requires-Dist: websockets<16,>=15
226
- Requires-Dist: protobuf<6,>=5.29
226
+ Requires-Dist: protobuf<6,>=5.29.6
227
227
  Provides-Extra: cli
228
228
  Requires-Dist: snowflake-cli>=3.0; extra == "cli"
229
229
  Provides-Extra: connector
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "snowflake-sandbox-python"
7
- version = "0.2.2a6"
7
+ version = "0.2.2a8"
8
8
  description = "Snowflake sandboxes — Python SDK and CLI."
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.11"
@@ -29,10 +29,11 @@ dependencies = [
29
29
  # (config_manager.CONFIG_MANAGER) and is the only working stage-PUT upload
30
30
  # path. The SDK delegates all connection/env resolution to it rather than
31
31
  # reimplementing the file format, and imports it lazily so `import
32
- # snowflake.sandbox` stays fast. 3.6+ ships the tomlkit config_manager.
33
- "snowflake-connector-python>=3.6",
32
+ # snowflake.sandbox` stays fast. 3.6+ ships the tomlkit config_manager;
33
+ # require the patched connector for wheel consumers, who do not use uv.lock.
34
+ "snowflake-connector-python>=4.7.3",
34
35
  "websockets>=15,<16",
35
- "protobuf>=5.29,<6",
36
+ "protobuf>=5.29.6,<6",
36
37
  ]
37
38
 
38
39
  [project.optional-dependencies]
@@ -19,7 +19,8 @@ __all__ = ["GIT_INTEGRATION_HELP", "GIT_REPO_HELP", "build_git_repos"]
19
19
  GIT_REPO_HELP = (
20
20
  "Git repository to mount, as an https URL with an optional =MOUNT_PATH "
21
21
  "(default /mnt/repos/<repo>); repeatable. Needs --git-integration. The default "
22
- "branch is mounted writable, and git push from the sandbox has no credential."
22
+ "branch is mounted writable. The sandbox gets no GitHub credential, so git "
23
+ "fetch, pull, and push need GitHub egress and a credential you provide."
23
24
  )
24
25
 
25
26
  GIT_INTEGRATION_HELP = (
@@ -693,7 +693,9 @@ async def _exec_streaming(sb: Any, cmd: str, cwd: str | None, env: dict[str, str
693
693
 
694
694
  async def _attach_pty(sb: Any, *, cwd: str | None, sanitize: bool, quiet: bool = False) -> int:
695
695
  """Open an interactive PTY in `sb` and return the shell's exit code."""
696
- _emit_status(f"Connected to {sb.name or sb.id}; exit the shell to disconnect", quiet=quiet)
696
+ _emit_status(
697
+ f"Opening SSH shell in {sb.name or sb.id}; exit the shell to disconnect", quiet=quiet
698
+ )
697
699
  return await _shell_over_ssh(sb, command=None, cwd=cwd)
698
700
 
699
701
 
@@ -87,6 +87,9 @@ __all__ = [
87
87
  "SnowflakeConnection",
88
88
  "Egress",
89
89
  "Bundle",
90
+ "BundleReference",
91
+ "BundlePublication",
92
+ "BundleProvenance",
90
93
  "StageMount",
91
94
  "GitRepo",
92
95
  "McpServer",
@@ -147,6 +150,9 @@ __all__ = [
147
150
  # Public attribute name -> (submodule, attribute). Resolved lazily on first
148
151
  # access so ``import snowflake.sandbox`` doesn't pull httpx / pydantic.
149
152
  _LAZY: dict[str, tuple[str, str]] = {
153
+ "BundleReference": ("snowflake.sandbox.bundles", "BundleReference"),
154
+ "BundlePublication": ("snowflake.sandbox.bundles", "BundlePublication"),
155
+ "BundleProvenance": ("snowflake.sandbox.bundles", "BundleProvenance"),
150
156
  "ApplicationEndpoint": ("snowflake.sandbox.endpoint", "ApplicationEndpoint"),
151
157
  "AsyncApplicationEndpoint": ("snowflake.sandbox.endpoint", "AsyncApplicationEndpoint"),
152
158
  "Sandbox": ("snowflake.sandbox.sync_client", "Sandbox"),
@@ -274,6 +280,7 @@ if TYPE_CHECKING: # pragma: no cover -- for type checkers / IDEs only
274
280
  from snowflake.sandbox._mcp_discovery import list_mcp_servers
275
281
  from snowflake.sandbox._mcp_status import McpConnectionStatus, mcp_server_status
276
282
  from snowflake.sandbox.app import App
283
+ from snowflake.sandbox.bundles import BundleProvenance, BundlePublication, BundleReference
277
284
  from snowflake.sandbox.client import AsyncSandbox
278
285
  from snowflake.sandbox.client import (
279
286
  destroy_sandbox as destroy_sandbox_async,
@@ -0,0 +1,342 @@
1
+ """Bounded zip+manifest-v1 encoding of already-selected inputs.
2
+
3
+ This private codec neither selects files nor reads the filesystem. The caller
4
+ supplies finite lists/tuples of strict records and actual bytes. No input size,
5
+ digest, link target, or ZIP attributes are trusted or accepted from the caller.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ import bisect
11
+ import hashlib
12
+ import json
13
+ import struct
14
+ import zlib
15
+ from collections.abc import Mapping, Sequence
16
+ from dataclasses import dataclass, field
17
+
18
+ _CEILINGS = {
19
+ "archive_bytes": 256 << 20,
20
+ "expanded_bytes": 1 << 30,
21
+ "manifest_bytes": 1 << 20,
22
+ "control_bytes": 1 << 20,
23
+ "entries": 100000,
24
+ }
25
+
26
+
27
+ @dataclass(frozen=True)
28
+ class PackedBuildContext:
29
+ archive: bytes
30
+ manifest: bytes
31
+ archive_sha256: str
32
+ manifest_sha256: str
33
+ context_sha256: str
34
+ control_sha256: str
35
+ compressed_bytes: int
36
+ expanded_bytes: int
37
+ format: str = "zip+manifest-v1"
38
+
39
+
40
+ def _limits(values: Mapping[str, int] | None) -> dict[str, int]:
41
+ out = _CEILINGS.copy()
42
+ if values is None:
43
+ return out
44
+ if type(values) is not dict or values.keys() - out.keys():
45
+ raise ValueError("invalid context quota fields")
46
+ for key, value in values.items():
47
+ if type(value) is not int or value < 0:
48
+ raise ValueError("context quotas must be nonnegative integers")
49
+ if value:
50
+ out[key] = min(value, out[key])
51
+ return out
52
+
53
+
54
+ def _path(value: object) -> str:
55
+ if (
56
+ type(value) is not str
57
+ or not value
58
+ or len(value) > _CEILINGS["manifest_bytes"]
59
+ or "\x00" in value
60
+ or "\\" in value
61
+ ):
62
+ raise ValueError("context path must be canonical relative UTF-8")
63
+ if any(part in ("", ".", "..") for part in value.split("/")):
64
+ raise ValueError("context path must be canonical relative UTF-8")
65
+ try:
66
+ encoded = value.encode("utf-8")
67
+ except UnicodeEncodeError:
68
+ raise ValueError("context path must be canonical relative UTF-8") from None
69
+ if len(encoded) > _CEILINGS["manifest_bytes"]:
70
+ raise ValueError("context path exceeds manifest quota")
71
+ return value
72
+
73
+
74
+ def _record(value: object, fields: set[str]) -> dict[str, object]:
75
+ if type(value) is not dict or value.keys() != fields:
76
+ raise ValueError("invalid context record fields")
77
+ return value.copy()
78
+
79
+
80
+ def _content(value: object) -> bytes:
81
+ if not isinstance(value, bytes) or type(value) is not bytes:
82
+ raise ValueError("context content must be bytes")
83
+ assert isinstance(value, bytes)
84
+ return value
85
+
86
+
87
+ def _json(value: object, limit: int) -> bytes:
88
+ # Schema keys are ASCII. Valid Unicode scalar path order agrees with UTF-8;
89
+ # ensure_ascii=False preserves U+2028/U+2029, HTML characters and DEL.
90
+ chunks: list[bytes] = []
91
+ size = 0
92
+ encoder = json.JSONEncoder(ensure_ascii=False, sort_keys=True, separators=(",", ":"))
93
+ for chunk in encoder.iterencode(value):
94
+ encoded = chunk.encode("utf-8")
95
+ size += len(encoded)
96
+ if size > limit:
97
+ raise ValueError("context manifest exceeds quota")
98
+ chunks.append(encoded)
99
+ return b"".join(chunks)
100
+
101
+
102
+ def _sha(content: bytes) -> str:
103
+ return hashlib.sha256(content).hexdigest()
104
+
105
+
106
+ def _check_collisions(paths: list[str], files: set[str]) -> None:
107
+ for path in files:
108
+ prefix = path + "/"
109
+ at = bisect.bisect_left(paths, prefix)
110
+ if at < len(paths) and paths[at].startswith(prefix):
111
+ raise ValueError("context file collides with descendant")
112
+
113
+
114
+ def _zip(members: list[tuple[bytes, bytes, int]], size: int) -> bytes:
115
+ # One exact bounded allocation; all validation precedes archive creation.
116
+ archive = bytearray(size)
117
+ central: list[tuple[bytes, bytes, int, int, int]] = []
118
+ offset = 0
119
+ for name, content, mode in members:
120
+ crc = zlib.crc32(content)
121
+ header = struct.pack(
122
+ "<IHHHHHIIIHH",
123
+ 0x04034B50,
124
+ 20,
125
+ 0x800,
126
+ 0,
127
+ 0,
128
+ 33,
129
+ crc,
130
+ len(content),
131
+ len(content),
132
+ len(name),
133
+ 0,
134
+ )
135
+ central.append((name, content, mode, crc, offset))
136
+ for part in (header, name, content):
137
+ archive[offset : offset + len(part)] = part
138
+ offset += len(part)
139
+ central_offset = offset
140
+ for name, content, mode, crc, local_offset in central:
141
+ attrs = (mode << 16) | (0x10 if mode & 0o170000 == 0o040000 else 0)
142
+ header = struct.pack(
143
+ "<IHHHHHHIIIHHHHHII",
144
+ 0x02014B50,
145
+ 0x314,
146
+ 20,
147
+ 0x800,
148
+ 0,
149
+ 0,
150
+ 33,
151
+ crc,
152
+ len(content),
153
+ len(content),
154
+ len(name),
155
+ 0,
156
+ 0,
157
+ 0,
158
+ 0,
159
+ attrs,
160
+ local_offset,
161
+ )
162
+ for part in (header, name):
163
+ archive[offset : offset + len(part)] = part
164
+ offset += len(part)
165
+ central_size = offset - central_offset
166
+ count = len(members)
167
+ # Even minimal directory records exceed the 1 MiB manifest ceiling long
168
+ # before 65535 members; the archive ceiling also keeps sizes/offsets ZIP32.
169
+ assert count < 65535
170
+ end = struct.pack(
171
+ "<IHHHHIIH",
172
+ 0x06054B50,
173
+ 0,
174
+ 0,
175
+ count,
176
+ count,
177
+ central_size,
178
+ central_offset,
179
+ 0,
180
+ )
181
+ archive[offset : offset + 22] = end
182
+ assert offset + 22 == size
183
+ return bytes(archive)
184
+
185
+
186
+ @dataclass
187
+ class _Budget:
188
+ quotas: dict[str, int]
189
+ archive_size: int
190
+ expanded: int = 0
191
+ manifest_size: int = len(b'{"controls":[],"entries":[],"version":1}')
192
+ members: list[tuple[bytes, bytes, int]] = field(default_factory=list)
193
+
194
+ def add_member(self, name: bytes, content: bytes, mode: int) -> bytes:
195
+ if len(name) > 65535:
196
+ raise ValueError("context path exceeds ZIP name limit")
197
+ self.archive_size += 76 + 2 * len(name) + len(content)
198
+ self.expanded += len(content)
199
+ if (
200
+ self.archive_size > self.quotas["archive_bytes"]
201
+ or self.expanded > self.quotas["expanded_bytes"]
202
+ ):
203
+ raise ValueError("context payload exceeds quota")
204
+ owned = bytes(content)
205
+ self.members.append((name, owned, mode))
206
+ return owned
207
+
208
+ def add_metadata(self, items: list[dict[str, object]], item: dict[str, object]) -> None:
209
+ self.manifest_size += len(_json(item, self.quotas["manifest_bytes"])) + bool(items)
210
+ if self.manifest_size > self.quotas["manifest_bytes"]:
211
+ raise ValueError("context manifest exceeds quota")
212
+ items.append(item)
213
+
214
+
215
+ def _add_entry(
216
+ value: Mapping[str, object],
217
+ budget: _Budget,
218
+ paths: dict[str, tuple[str, bytes]],
219
+ manifest_entries: list[dict[str, object]],
220
+ ) -> None:
221
+ kind = value.get("kind") if isinstance(value, dict) else None
222
+ if kind not in ("file", "dir"):
223
+ raise ValueError("context entry must be a file or directory")
224
+ fields = {"path", "kind", "mode"} | ({"content"} if kind == "file" else set())
225
+ item = _record(value, fields)
226
+ path = _path(item["path"])
227
+ mode = item["mode"]
228
+ if type(mode) is not int or not 0 <= mode <= 511:
229
+ raise ValueError("context mode must contain ordinary permission bits")
230
+ if path in paths:
231
+ raise ValueError("duplicate context path")
232
+ content = _content(item["content"]) if kind == "file" else b""
233
+ name = ("context/" + path + ("/" if kind == "dir" else "")).encode("utf-8")
234
+ owned = budget.add_member(name, content, (0o100000 if kind == "file" else 0o040000) | mode)
235
+ paths[path] = (kind, owned)
236
+ entry: dict[str, object] = {"path": path, "kind": kind, "mode": mode, "size": len(owned)}
237
+ if kind == "file":
238
+ entry["sha256"] = _sha(owned)
239
+ budget.add_metadata(manifest_entries, entry)
240
+
241
+
242
+ def _controls(
243
+ controls: Sequence[Mapping[str, object]],
244
+ budget: _Budget,
245
+ paths: dict[str, tuple[str, bytes]],
246
+ ordered_paths: list[str],
247
+ ) -> list[dict[str, object]]:
248
+ manifest_controls: list[dict[str, object]] = []
249
+ control_records = [_record(c, {"role", "path", "content", "copyable"}) for c in controls]
250
+ if any(c["role"] not in ("dockerfile", "ignore") for c in control_records):
251
+ raise ValueError("invalid control role")
252
+ control_records.sort(key=lambda c: str(c["role"]))
253
+ roles = [c["role"] for c in control_records]
254
+ if roles not in (["dockerfile"], ["dockerfile", "ignore"]):
255
+ raise ValueError("context requires unique control roles")
256
+ control_paths: list[str] = []
257
+ control_bytes = 0
258
+ for item in control_records:
259
+ path = _path(item["path"])
260
+ copyable = item["copyable"]
261
+ content = _content(item["content"])
262
+ if type(copyable) is not bool:
263
+ raise ValueError("control copyable must be a boolean")
264
+ if item["role"] == "ignore" and path not in (
265
+ ".dockerignore",
266
+ control_paths[0] + ".dockerignore",
267
+ ):
268
+ raise ValueError("ignore control must apply to the Dockerfile")
269
+ ordinary = paths.get(path)
270
+ if copyable != (ordinary is not None) or (
271
+ ordinary is not None and ordinary != ("file", content)
272
+ ):
273
+ raise ValueError("control disagrees with copyable entry")
274
+ for other in control_paths:
275
+ if path == other or path.startswith(other + "/") or other.startswith(path + "/"):
276
+ raise ValueError("colliding control paths")
277
+ _check_collisions(
278
+ sorted([*ordered_paths, path]), {path} | {p for p in paths if paths[p][0] == "file"}
279
+ )
280
+ control_paths.append(path)
281
+ control_bytes += len(content)
282
+ if control_bytes > budget.quotas["control_bytes"]:
283
+ raise ValueError("context controls exceed quota")
284
+ owned = budget.add_member(("controls/" + str(item["role"])).encode(), content, 0o100644)
285
+ budget.add_metadata(
286
+ manifest_controls,
287
+ {
288
+ "role": item["role"],
289
+ "path": path,
290
+ "copyable": copyable,
291
+ "size": len(owned),
292
+ "sha256": _sha(owned),
293
+ },
294
+ )
295
+ return manifest_controls
296
+
297
+
298
+ def pack_build_context(
299
+ entries: Sequence[Mapping[str, object]],
300
+ controls: Sequence[Mapping[str, object]],
301
+ *,
302
+ limits: Mapping[str, int] | None = None,
303
+ ) -> PackedBuildContext:
304
+ """Pack selected strict records, deriving all wire metadata from copied bytes.
305
+
306
+ File: {path, kind='file', mode, content}; directory: {path, kind='dir',
307
+ mode}. Control: {role='dockerfile'|'ignore', path, content, copyable: bool}.
308
+ Ignore selection and copyability are supplied by the upstream selector.
309
+ Zero quota uses the validator default; positive quotas only tighten it.
310
+ """
311
+ quotas = _limits(limits)
312
+ if type(entries) not in (list, tuple) or len(entries) > quotas["entries"]:
313
+ raise ValueError("context entries must be a bounded sequence")
314
+ if type(controls) not in (list, tuple) or not 1 <= len(controls) <= 2:
315
+ raise ValueError("context requires one Dockerfile and at most one ignore")
316
+ manifest_entries: list[dict[str, object]] = []
317
+ paths: dict[str, tuple[str, bytes]] = {}
318
+ budget = _Budget(quotas, 22)
319
+
320
+ for value in entries:
321
+ _add_entry(value, budget, paths, manifest_entries)
322
+ ordered_paths = sorted(paths)
323
+ _check_collisions(ordered_paths, {p for p in paths if paths[p][0] == "file"})
324
+ manifest_controls = _controls(controls, budget, paths, ordered_paths)
325
+ manifest_entries.sort(key=lambda e: str(e["path"]).encode("utf-8"))
326
+ manifest = _json(
327
+ {"version": 1, "entries": manifest_entries, "controls": manifest_controls},
328
+ quotas["manifest_bytes"],
329
+ )
330
+ budget.members.sort(key=lambda member: member[0])
331
+ archive = _zip(budget.members, budget.archive_size)
332
+ manifest_sha = _sha(manifest)
333
+ return PackedBuildContext(
334
+ archive,
335
+ manifest,
336
+ _sha(archive),
337
+ manifest_sha,
338
+ manifest_sha,
339
+ _sha(_json(manifest_controls, quotas["manifest_bytes"])),
340
+ len(archive),
341
+ budget.expanded,
342
+ )