pybubble 0.1.1__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.
- pybubble-0.1.1/.gitignore +16 -0
- pybubble-0.1.1/.python-version +1 -0
- pybubble-0.1.1/LICENSE +202 -0
- pybubble-0.1.1/PKG-INFO +76 -0
- pybubble-0.1.1/README.md +65 -0
- pybubble-0.1.1/build_backend.py +92 -0
- pybubble-0.1.1/default-rootfs.dockerfile +12 -0
- pybubble-0.1.1/docs/build-rootfs.md +11 -0
- pybubble-0.1.1/docs/sandbox.md +39 -0
- pybubble-0.1.1/pyproject.toml +28 -0
- pybubble-0.1.1/src/pybubble/__cli__.py +229 -0
- pybubble-0.1.1/src/pybubble/__init__.py +14 -0
- pybubble-0.1.1/src/pybubble/data.py +0 -0
- pybubble-0.1.1/src/pybubble/rootfs.py +79 -0
- pybubble-0.1.1/src/pybubble/sandbox.py +135 -0
- pybubble-0.1.1/tests/conftest.py +38 -0
- pybubble-0.1.1/tests/test_dockerfile.dockerfile +3 -0
- pybubble-0.1.1/tests/test_rootfs.py +216 -0
- pybubble-0.1.1/tests/test_sandbox.py +288 -0
- pybubble-0.1.1/tests/test_sandbox_performance.py +58 -0
- pybubble-0.1.1/uv.lock +104 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
3.12
|
pybubble-0.1.1/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
pybubble-0.1.1/PKG-INFO
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pybubble
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: A Python package for running sandboxed code.
|
|
5
|
+
License-File: LICENSE
|
|
6
|
+
Requires-Python: >=3.12
|
|
7
|
+
Provides-Extra: test
|
|
8
|
+
Requires-Dist: pytest-asyncio>=0.25.2; extra == 'test'
|
|
9
|
+
Requires-Dist: pytest>=8.0; extra == 'test'
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
|
|
12
|
+
# pybubble
|
|
13
|
+
|
|
14
|
+
A simple wrapper around `bwrap` to create sandbox environments for executing code. It works without Docker or other daemon-based container runtimes, using shared read-only root filesystems for quick (1-2ms) setup times.
|
|
15
|
+
|
|
16
|
+
While these environments are sandboxed and provide protection from accidental modification of your host system by overzealous LLMs, **pybubble is not an acceptable substitute for virtualization when running untrusted code**. If you are giving untrusted people access to this, either directly or via an LLM frontend, consider using more production-ready sandboxing or virtualization tools with pybubble just isolating environment state.
|
|
17
|
+
|
|
18
|
+
Feel free to submit bug reports and pull requests via GitHub, but note that Arcee is not committing to long-term maintenence of this software. This is just a small library I built in my spare time and thought everyone else would find useful.
|
|
19
|
+
|
|
20
|
+
Due to relying on Linux kernel features to operate, pybubble is not compatible with macOS or Windows.
|
|
21
|
+
|
|
22
|
+
## Setup
|
|
23
|
+
|
|
24
|
+
Install `bwrap`. On Ubuntu, do:
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
sudo apt-get install bubblewrap
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Then, add `pybubble` to your project.
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
uv add pybubble
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Root filesystem archives
|
|
37
|
+
|
|
38
|
+
If all you need is basic Python code execution, consider using the provided root filesystem archive under our GitHub release. It comes preinstalled with:
|
|
39
|
+
|
|
40
|
+
- Python
|
|
41
|
+
- uv
|
|
42
|
+
- bash
|
|
43
|
+
- ripgrep
|
|
44
|
+
- cURL & wget
|
|
45
|
+
- numpy
|
|
46
|
+
- pandas
|
|
47
|
+
- httpx & requests
|
|
48
|
+
- pillow
|
|
49
|
+
- ImageMagick
|
|
50
|
+
|
|
51
|
+
If you need more tools or want to run a leaner environment, follow [this guide](docs/build-rootfs.md) to build one yourself.
|
|
52
|
+
|
|
53
|
+
## Run code
|
|
54
|
+
|
|
55
|
+
Create a sandbox by doing:
|
|
56
|
+
|
|
57
|
+
```python
|
|
58
|
+
from pybubble import Sandbox
|
|
59
|
+
import asyncio
|
|
60
|
+
|
|
61
|
+
async def main():
|
|
62
|
+
s = Sandbox("path/to/rootfs.tgz")
|
|
63
|
+
|
|
64
|
+
stdout, stderr = await s.run("ping -c 1 google.com", allow_network=True)
|
|
65
|
+
|
|
66
|
+
print(stdout.decode("utf-8")) # ping output
|
|
67
|
+
|
|
68
|
+
stdout, stderr = await s.run_python("print('hello, world')", timeout=5.0)
|
|
69
|
+
|
|
70
|
+
print(stdout.decode("utf-8")) # "hello, world"
|
|
71
|
+
|
|
72
|
+
if __name__ == "__main__":
|
|
73
|
+
asyncio.run(main())
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
To learn more about the features available in `Sandbox`, see [this page](docs/sandbox.md).
|
pybubble-0.1.1/README.md
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# pybubble
|
|
2
|
+
|
|
3
|
+
A simple wrapper around `bwrap` to create sandbox environments for executing code. It works without Docker or other daemon-based container runtimes, using shared read-only root filesystems for quick (1-2ms) setup times.
|
|
4
|
+
|
|
5
|
+
While these environments are sandboxed and provide protection from accidental modification of your host system by overzealous LLMs, **pybubble is not an acceptable substitute for virtualization when running untrusted code**. If you are giving untrusted people access to this, either directly or via an LLM frontend, consider using more production-ready sandboxing or virtualization tools with pybubble just isolating environment state.
|
|
6
|
+
|
|
7
|
+
Feel free to submit bug reports and pull requests via GitHub, but note that Arcee is not committing to long-term maintenence of this software. This is just a small library I built in my spare time and thought everyone else would find useful.
|
|
8
|
+
|
|
9
|
+
Due to relying on Linux kernel features to operate, pybubble is not compatible with macOS or Windows.
|
|
10
|
+
|
|
11
|
+
## Setup
|
|
12
|
+
|
|
13
|
+
Install `bwrap`. On Ubuntu, do:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
sudo apt-get install bubblewrap
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Then, add `pybubble` to your project.
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
uv add pybubble
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Root filesystem archives
|
|
26
|
+
|
|
27
|
+
If all you need is basic Python code execution, consider using the provided root filesystem archive under our GitHub release. It comes preinstalled with:
|
|
28
|
+
|
|
29
|
+
- Python
|
|
30
|
+
- uv
|
|
31
|
+
- bash
|
|
32
|
+
- ripgrep
|
|
33
|
+
- cURL & wget
|
|
34
|
+
- numpy
|
|
35
|
+
- pandas
|
|
36
|
+
- httpx & requests
|
|
37
|
+
- pillow
|
|
38
|
+
- ImageMagick
|
|
39
|
+
|
|
40
|
+
If you need more tools or want to run a leaner environment, follow [this guide](docs/build-rootfs.md) to build one yourself.
|
|
41
|
+
|
|
42
|
+
## Run code
|
|
43
|
+
|
|
44
|
+
Create a sandbox by doing:
|
|
45
|
+
|
|
46
|
+
```python
|
|
47
|
+
from pybubble import Sandbox
|
|
48
|
+
import asyncio
|
|
49
|
+
|
|
50
|
+
async def main():
|
|
51
|
+
s = Sandbox("path/to/rootfs.tgz")
|
|
52
|
+
|
|
53
|
+
stdout, stderr = await s.run("ping -c 1 google.com", allow_network=True)
|
|
54
|
+
|
|
55
|
+
print(stdout.decode("utf-8")) # ping output
|
|
56
|
+
|
|
57
|
+
stdout, stderr = await s.run_python("print('hello, world')", timeout=5.0)
|
|
58
|
+
|
|
59
|
+
print(stdout.decode("utf-8")) # "hello, world"
|
|
60
|
+
|
|
61
|
+
if __name__ == "__main__":
|
|
62
|
+
asyncio.run(main())
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
To learn more about the features available in `Sandbox`, see [this page](docs/sandbox.md).
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
"""Custom build backend that wraps hatchling and builds the default rootfs."""
|
|
2
|
+
import os
|
|
3
|
+
import shutil
|
|
4
|
+
import subprocess
|
|
5
|
+
import sys
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from hatchling.build import build_sdist as _build_sdist
|
|
8
|
+
from hatchling.build import build_wheel as _build_wheel
|
|
9
|
+
try:
|
|
10
|
+
from hatchling.build import build_editable as _build_editable
|
|
11
|
+
except ImportError:
|
|
12
|
+
_build_editable = None
|
|
13
|
+
|
|
14
|
+
# Import the rootfs generation function from the source
|
|
15
|
+
sys.path.insert(0, str(Path(__file__).parent / "src"))
|
|
16
|
+
from pybubble.rootfs import generate_rootfs
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def build_wheel(wheel_directory, config_settings=None, metadata_directory=None):
|
|
20
|
+
"""Build a wheel and then generate the default rootfs."""
|
|
21
|
+
# First, build the wheel using hatchling
|
|
22
|
+
wheel_filename = _build_wheel(wheel_directory, config_settings, metadata_directory)
|
|
23
|
+
|
|
24
|
+
# After successful wheel build, generate the default rootfs
|
|
25
|
+
try:
|
|
26
|
+
# Get the project root directory (where build_backend.py is located)
|
|
27
|
+
project_root = Path(__file__).parent.absolute()
|
|
28
|
+
|
|
29
|
+
# Use absolute paths from project root
|
|
30
|
+
temp_tgz = project_root / "dist" / "default.tgz"
|
|
31
|
+
dockerfile = project_root / "default-rootfs.dockerfile"
|
|
32
|
+
dist_dir = project_root / "dist"
|
|
33
|
+
|
|
34
|
+
# Remove existing default.tgz to ensure fresh build
|
|
35
|
+
if temp_tgz.exists():
|
|
36
|
+
print(f"Removing existing {temp_tgz}", flush=True)
|
|
37
|
+
temp_tgz.unlink()
|
|
38
|
+
|
|
39
|
+
# Ensure dist directory exists
|
|
40
|
+
dist_dir.mkdir(exist_ok=True)
|
|
41
|
+
|
|
42
|
+
print(f"Building default rootfs from {dockerfile}...", flush=True)
|
|
43
|
+
|
|
44
|
+
# Change to project root for docker context
|
|
45
|
+
original_cwd = os.getcwd()
|
|
46
|
+
os.chdir(project_root)
|
|
47
|
+
|
|
48
|
+
try:
|
|
49
|
+
generate_rootfs(dockerfile, temp_tgz, compress_level=9)
|
|
50
|
+
print(f"Default rootfs built successfully at {temp_tgz}!", flush=True)
|
|
51
|
+
|
|
52
|
+
# Now copy to the actual dist directory (wheel_directory parent)
|
|
53
|
+
# wheel_directory is where the wheel is being built
|
|
54
|
+
actual_dist = Path(wheel_directory).absolute()
|
|
55
|
+
final_tgz = actual_dist / "default.tgz"
|
|
56
|
+
|
|
57
|
+
print(f"Copying {temp_tgz} to {final_tgz}...", flush=True)
|
|
58
|
+
shutil.copy2(temp_tgz, final_tgz)
|
|
59
|
+
print(f"Default rootfs copied to {final_tgz}!", flush=True)
|
|
60
|
+
|
|
61
|
+
finally:
|
|
62
|
+
os.chdir(original_cwd)
|
|
63
|
+
|
|
64
|
+
except Exception as e:
|
|
65
|
+
print(f"Warning: Failed to build default rootfs: {e}", file=sys.stderr, flush=True)
|
|
66
|
+
import traceback
|
|
67
|
+
traceback.print_exc()
|
|
68
|
+
# Don't fail the build if rootfs generation fails
|
|
69
|
+
|
|
70
|
+
return wheel_filename
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def build_sdist(sdist_directory, config_settings=None):
|
|
74
|
+
"""Build an sdist. The rootfs will be built when build_wheel is called."""
|
|
75
|
+
# Just build the sdist using hatchling
|
|
76
|
+
# The rootfs generation happens in build_wheel since that's called after sdist
|
|
77
|
+
return _build_sdist(sdist_directory, config_settings)
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def build_editable(wheel_directory, config_settings=None, metadata_directory=None):
|
|
81
|
+
"""Build an editable wheel. Don't build rootfs for editable installs."""
|
|
82
|
+
if _build_editable is None:
|
|
83
|
+
raise NotImplementedError("Editable installs are not supported by this version of hatchling")
|
|
84
|
+
|
|
85
|
+
# For editable installs, just use hatchling's editable build
|
|
86
|
+
# We don't build the rootfs here since it's a development install
|
|
87
|
+
return _build_editable(wheel_directory, config_settings, metadata_directory)
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
# Expose the required hooks for the build backend
|
|
91
|
+
__all__ = ['build_wheel', 'build_sdist', 'build_editable']
|
|
92
|
+
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
FROM alpine:latest
|
|
2
|
+
|
|
3
|
+
RUN apk add --no-cache python3 bash curl wget py3-numpy py3-pandas py3-httpx py3-requests py3-pillow py3-pip imagemagick ripgrep
|
|
4
|
+
|
|
5
|
+
# Set up uv
|
|
6
|
+
RUN wget https://github.com/astral-sh/uv/releases/download/0.9.9/uv-x86_64-unknown-linux-musl.tar.gz
|
|
7
|
+
RUN tar -xzvf uv-x86_64-unknown-linux-musl.tar.gz
|
|
8
|
+
RUN mv uv-x86_64-unknown-linux-musl/* /usr/bin/
|
|
9
|
+
RUN rm -rfv uv-x86_64-unknown-linux-musl*
|
|
10
|
+
|
|
11
|
+
RUN adduser -D sandbox -u 1000
|
|
12
|
+
USER sandbox
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# Building a custom root filesystem archive
|
|
2
|
+
|
|
3
|
+
A custom root filesystem archive can be generated from any Dockerfile. Ensure your image contains a user named "sandbox" with an empty home directory at `/home/sandbox`. Your ephemeral writable session storage will be mounted at this location.
|
|
4
|
+
|
|
5
|
+
To generate a root filesystem, ensure you have Docker installed and running, then run:
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pybubble rootfs your.dockerfile rootfs.tgz
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Your root filesystem archive can now be used with sandboxes. Docker does not need to be installed to use this file, only to generate it.
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# Sandboxes
|
|
2
|
+
|
|
3
|
+
Sandbox objects are a reference to an unpacked root filesystem (stored, usually, in `~/.cache/pybubble/rootfs/{hash_of_rootfs_archive}/` and reused between environments) and a writable session directory, usually stored in a uniquely-named directory in `/tmp`.
|
|
4
|
+
|
|
5
|
+
Unless you pass `work_dir` to the constructor, the session directory will be automatically deleted as soon as the Sandbox goes out of scope or is deleted with the `del` keyword. The directory bound to `/tmp` will always be deleted when a sandbox goes out of scope.
|
|
6
|
+
|
|
7
|
+
Programs running in the sandbox see a read-only root filesystem and a writable partition at `/home/sandbox`, which is also the default working directory. The sandbox inherits the hostname of the host system, as well as a read-only copy of `/etc/resolv.conf` for DNS resolution when `allow_network=True` is passed to `run()` or `run_python()`. A separate writable directory is used under the host's `/tmp` for the sandbox's `/tmp`.
|
|
8
|
+
|
|
9
|
+
## API
|
|
10
|
+
|
|
11
|
+
`def __init__(self, rootfs: str | Path, work_dir: str | Path | None = None, rootfs_path: str | Path | None = None)`
|
|
12
|
+
|
|
13
|
+
Creates a sandbox from the specified `rootfs` tarball, expected to be in the form of a tarball or compressed tarball.
|
|
14
|
+
|
|
15
|
+
If `work_dir` is specified, the writable working directory for the sandbox will be stored at that location and will not be deleted when the sandbox goes out of scope. Otherwise, a temporary directory in `/tmp` will be used.
|
|
16
|
+
|
|
17
|
+
If `rootfs_path` is provided, the root filesystem tarball will be extracted to that directory. Otherwise, it will be extracted to `~/.cache/pybubble/rootfs/{hash}`. If a directory already exists at the path, extraction will be skipped and the cached filesystem will be reused.
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
`async def run(self, command: str, allow_network: bool = False, timeout: float = 10.0) -> tuple[bytes, bytes]`
|
|
22
|
+
|
|
23
|
+
Runs a given shell command in a sandbox. The command is run asynchronously.
|
|
24
|
+
|
|
25
|
+
If `allow_network` is True, network access is granted to the process and the host's `/etc/resolv.conf` is mounted read-only to the sandbox for DNS resolution.
|
|
26
|
+
|
|
27
|
+
The argument `timeout` sets a timer for that value, in seconds, and raises a `TimeoutError` if the process takes longer than that.
|
|
28
|
+
|
|
29
|
+
Returns two `bytes` objects, the first being the `stdout` and the second being the `stderr`.
|
|
30
|
+
|
|
31
|
+
---
|
|
32
|
+
|
|
33
|
+
`async def run_python(self, code: str, allow_network: bool = False, timeout: float = 10.0) -> tuple[bytes, bytes]`
|
|
34
|
+
|
|
35
|
+
Convenience wrapper for running Python scripts. Creates a file called `script.py` and writes the value of `code` to it, and then runs `python script.py`. Otherwise behaves identically to `run()`.
|
|
36
|
+
|
|
37
|
+
## Accessing the session data
|
|
38
|
+
|
|
39
|
+
The writable portion of the sandbox's filesystem can be accessed from the host with the Path object stored at `Sandbox.work_dir`. Changes made to this directory made by the host will be visible instantly in the sandbox, and vice versa. You can use this to access files created by the sandboxed code or place files in the session directory for the sandboxed code to access.
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling", "editables"]
|
|
3
|
+
build-backend = "build_backend"
|
|
4
|
+
backend-path = ["."]
|
|
5
|
+
|
|
6
|
+
[project]
|
|
7
|
+
name = "pybubble"
|
|
8
|
+
version = "0.1.1"
|
|
9
|
+
description = "A Python package for running sandboxed code."
|
|
10
|
+
readme = "README.md"
|
|
11
|
+
requires-python = ">=3.12"
|
|
12
|
+
dependencies = []
|
|
13
|
+
|
|
14
|
+
[project.scripts]
|
|
15
|
+
pybubble = "pybubble.__cli__:main"
|
|
16
|
+
|
|
17
|
+
[project.optional-dependencies]
|
|
18
|
+
test = [
|
|
19
|
+
"pytest>=8.0",
|
|
20
|
+
"pytest-asyncio>=0.25.2",
|
|
21
|
+
]
|
|
22
|
+
|
|
23
|
+
[tool.hatch.build.targets.wheel]
|
|
24
|
+
packages = ["src/pybubble"]
|
|
25
|
+
|
|
26
|
+
[tool.pytest.ini_options]
|
|
27
|
+
addopts = "-ra"
|
|
28
|
+
testpaths = ["tests"]
|