workers-py 1.1.7__py3-none-any.whl → 1.2.0__py3-none-any.whl

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.
pywrangler/__init__.py CHANGED
@@ -1 +0,0 @@
1
-
pywrangler/cli.py CHANGED
@@ -106,7 +106,6 @@ def sync_command(force=False, directly_requested=True):
106
106
  create_pyodide_venv,
107
107
  create_workers_venv,
108
108
  parse_requirements,
109
- install_pyodide_build,
110
109
  install_requirements,
111
110
  )
112
111
 
@@ -129,7 +128,6 @@ def sync_command(force=False, directly_requested=True):
129
128
  create_workers_venv()
130
129
 
131
130
  # Set up Pyodide virtual env
132
- install_pyodide_build()
133
131
  create_pyodide_venv()
134
132
 
135
133
  # Generate requirements.txt from pyproject.toml by directly parsing the TOML file then install into vendor folder.
pywrangler/sync.py CHANGED
@@ -1,8 +1,10 @@
1
1
  import logging
2
2
  import os
3
3
  import shutil
4
- from pathlib import Path
5
4
  import tempfile
5
+ from contextlib import contextmanager
6
+ from pathlib import Path
7
+ from typing import Literal
6
8
 
7
9
  import click
8
10
 
@@ -22,7 +24,9 @@ logger = logging.getLogger(__name__)
22
24
  PYPROJECT_TOML_PATH = find_pyproject_toml()
23
25
  PROJECT_ROOT = PYPROJECT_TOML_PATH.parent
24
26
  VENV_WORKERS_PATH = PROJECT_ROOT / ".venv-workers"
27
+ VENV_WORKERS_TOKEN = PROJECT_ROOT / ".venv-workers/.synced"
25
28
  PYODIDE_VENV_PATH = VENV_WORKERS_PATH / "pyodide-venv"
29
+ VENDOR_TOKEN = PROJECT_ROOT / "python_modules/.synced"
26
30
  VENV_REQUIREMENTS_PATH = VENV_WORKERS_PATH / "temp-venv-requirements.txt"
27
31
 
28
32
 
@@ -56,8 +60,33 @@ def check_wrangler_config():
56
60
  raise click.exceptions.Exit(code=1)
57
61
 
58
62
 
59
- def _get_python_version():
60
- return os.environ.get("_PYWRANGLER_PYTHON_VERSION", "3.12")
63
+ def _get_python_version() -> Literal["3.12", "3.13"]:
64
+ res = os.environ.get("_PYWRANGLER_PYTHON_VERSION", "3.12")
65
+ match res:
66
+ case "3.12" | "3.13":
67
+ return res
68
+ case _:
69
+ raise ValueError(
70
+ f"Unexpected value for Python version '{res}', expected '3.12' or '3.13'"
71
+ )
72
+
73
+
74
+ def _get_uv_pyodide_interp_name():
75
+ match _get_python_version():
76
+ case "3.12":
77
+ v = "3.12.7"
78
+ case "3.13":
79
+ v = "3.13.2"
80
+ return f"cpython-{v}-emscripten-wasm32-musl"
81
+
82
+
83
+ def _get_pyodide_index():
84
+ match _get_python_version():
85
+ case "3.12":
86
+ v = "0.27.7"
87
+ case "3.13":
88
+ v = "0.28.3"
89
+ return "https://index.pyodide.org/" + v
61
90
 
62
91
 
63
92
  def _get_venv_python_version() -> str | None:
@@ -123,65 +152,18 @@ def create_workers_venv():
123
152
  )
124
153
 
125
154
 
126
- def _get_pyodide_cli_path():
127
- venv_bin_path = VENV_WORKERS_PATH / ("Scripts" if os.name == "nt" else "bin")
128
- pyodide_cli_path = venv_bin_path / ("pyodide.exe" if os.name == "nt" else "pyodide")
129
- return pyodide_cli_path
130
-
131
-
132
- def install_pyodide_build():
133
- pyodide_cli_path = _get_pyodide_cli_path()
134
-
135
- if pyodide_cli_path.is_file():
136
- logger.debug(
137
- f"pyodide-build CLI already found at {pyodide_cli_path} (skipping install.)"
138
- )
139
- return
140
-
141
- logger.debug(
142
- f"Installing pyodide-build in {VENV_WORKERS_PATH} using 'uv pip install'..."
143
- )
144
- venv_bin_path = pyodide_cli_path.parent
145
-
146
- # Ensure the python executable path is correct for the venv
147
- venv_python_executable = venv_bin_path / (
148
- "python.exe" if os.name == "nt" else "python"
149
- )
150
- if not venv_python_executable.is_file():
151
- logger.error(f"Python executable not found at {venv_python_executable}")
152
- raise click.exceptions.Exit(code=1)
153
-
154
- run_command(["uv", "pip", "install", "-p", str(venv_python_executable), "pip"])
155
-
156
- run_command(
157
- [
158
- "uv",
159
- "pip",
160
- "install",
161
- "-p",
162
- str(venv_python_executable),
163
- "pyodide-build==0.30.7",
164
- ]
165
- )
166
-
167
-
168
155
  def create_pyodide_venv():
169
- pyodide_cli_path = _get_pyodide_cli_path()
170
156
  if PYODIDE_VENV_PATH.is_dir():
171
157
  logger.debug(
172
158
  f"Pyodide virtual environment at {PYODIDE_VENV_PATH} already exists."
173
159
  )
174
160
  return
175
161
 
176
- # Workaround to fix caching issue on some machines.
177
- #
178
- # Fix is here: pyodide/pyodide-build#239
179
- logger.debug("Installing xbuildenv...")
180
- run_command([str(pyodide_cli_path), "xbuildenv", "install"])
181
-
182
162
  logger.debug(f"Creating Pyodide virtual environment at {PYODIDE_VENV_PATH}...")
183
163
  PYODIDE_VENV_PATH.parent.mkdir(parents=True, exist_ok=True)
184
- run_command([str(pyodide_cli_path), "venv", str(PYODIDE_VENV_PATH)])
164
+ interp_name = _get_uv_pyodide_interp_name()
165
+ run_command(["uv", "python", "install", interp_name])
166
+ run_command(["uv", "venv", PYODIDE_VENV_PATH, "--python", interp_name])
185
167
 
186
168
 
187
169
  def parse_requirements() -> list[str]:
@@ -200,6 +182,15 @@ def parse_requirements() -> list[str]:
200
182
  raise click.exceptions.Exit(code=1)
201
183
 
202
184
 
185
+ @contextmanager
186
+ def temp_requirements_file(requirements: list[str]):
187
+ # Write dependencies to a requirements.txt-style temp file.
188
+ with tempfile.NamedTemporaryFile(mode="w", suffix=".txt") as temp_file:
189
+ temp_file.write("\n".join(requirements))
190
+ temp_file.flush()
191
+ yield temp_file.name
192
+
193
+
203
194
  def _install_requirements_to_vendor(requirements: list[str]):
204
195
  vendor_path = PROJECT_ROOT / "python_modules"
205
196
  logger.debug(f"Using vendor path: {vendor_path}")
@@ -210,95 +201,72 @@ def _install_requirements_to_vendor(requirements: list[str]):
210
201
  )
211
202
  return
212
203
 
213
- # Write dependencies to a requirements.txt-style temp file.
214
- with tempfile.NamedTemporaryFile(
215
- mode="w", suffix=".txt", dir=PYODIDE_VENV_PATH
216
- ) as temp_file:
217
- temp_file.write("\n".join(requirements))
218
- temp_file.flush()
219
- temp_file_path = Path(temp_file.name)
220
-
221
- # Install packages into vendor directory
222
- vendor_path.mkdir(parents=True, exist_ok=True)
223
- pyodide_venv_pip_path = (
224
- PYODIDE_VENV_PATH
225
- / ("Scripts" if os.name == "nt" else "bin")
226
- / ("pip.exe" if os.name == "nt" else "pip")
227
- )
228
- relative_vendor_path = vendor_path.relative_to(PROJECT_ROOT)
229
- logger.info(
230
- f"Installing packages into [bold]{relative_vendor_path}[/bold] using Pyodide pip...",
231
- extra={"markup": True},
232
- )
204
+ # Install packages into vendor directory
205
+ vendor_path.mkdir(parents=True, exist_ok=True)
206
+ relative_vendor_path = vendor_path.relative_to(PROJECT_ROOT)
207
+ logger.info(
208
+ f"Installing packages into [bold]{relative_vendor_path}[/bold]...",
209
+ extra={"markup": True},
210
+ )
211
+ with temp_requirements_file(requirements) as requirements_file:
233
212
  run_command(
234
213
  [
235
- str(pyodide_venv_pip_path),
214
+ "uv",
215
+ "pip",
236
216
  "install",
237
- "-t",
238
- str(vendor_path),
217
+ "--no-build",
239
218
  "-r",
240
- str(temp_file_path),
241
- ]
219
+ requirements_file,
220
+ "--extra-index-url",
221
+ _get_pyodide_index(),
222
+ "--index-strategy",
223
+ "unsafe-best-match",
224
+ ],
225
+ env=os.environ | {"VIRTUAL_ENV": PYODIDE_VENV_PATH},
226
+ )
227
+ pyv = _get_python_version()
228
+ shutil.rmtree(vendor_path)
229
+ shutil.copytree(
230
+ PYODIDE_VENV_PATH / f"lib/python{pyv}/site-packages", vendor_path
242
231
  )
243
232
 
244
- # Create a pyvenv.cfg file in python_modules to mark it as a virtual environment
245
- (vendor_path / "pyvenv.cfg").touch()
233
+ # Create a pyvenv.cfg file in python_modules to mark it as a virtual environment
234
+ (vendor_path / "pyvenv.cfg").touch()
235
+ VENDOR_TOKEN.touch()
246
236
 
247
- logger.info(
248
- f"Packages installed in [bold]{relative_vendor_path}[/bold].",
249
- extra={"markup": True},
250
- )
237
+ logger.info(
238
+ f"Packages installed in [bold]{relative_vendor_path}[/bold].",
239
+ extra={"markup": True},
240
+ )
251
241
 
252
242
 
253
243
  def _install_requirements_to_venv(requirements: list[str]):
254
244
  # Create a requirements file for .venv-workers that includes webtypy and pyodide-py
255
- VENV_REQUIREMENTS_PATH.parent.mkdir(parents=True, exist_ok=True)
256
-
245
+ relative_venv_workers_path = VENV_WORKERS_PATH.relative_to(PROJECT_ROOT)
257
246
  requirements = requirements.copy()
258
247
  requirements.append("webtypy")
259
248
  requirements.append("pyodide-py")
260
249
 
261
- # Write dependencies to a requirements.txt-style temp file.
262
- with tempfile.NamedTemporaryFile(
263
- mode="w", suffix=".txt", dir=VENV_REQUIREMENTS_PATH.parent
264
- ) as temp_file:
265
- temp_file.write("\n".join(requirements))
266
- temp_file.flush()
267
- temp_file_path = Path(temp_file.name)
268
-
269
- # Install packages into .venv-workers so that user's IDE can see the packages.
270
- venv_bin_path = VENV_WORKERS_PATH / ("Scripts" if os.name == "nt" else "bin")
271
- venv_python_executable = venv_bin_path / (
272
- "python.exe" if os.name == "nt" else "python"
250
+ logger.info(
251
+ f"Installing packages into [bold]{relative_venv_workers_path}[/bold]...",
252
+ extra={"markup": True},
253
+ )
254
+ with temp_requirements_file(requirements) as requirements_file:
255
+ run_command(
256
+ [
257
+ "uv",
258
+ "pip",
259
+ "install",
260
+ "-r",
261
+ requirements_file,
262
+ ],
263
+ env=os.environ | {"VIRTUAL_ENV": VENV_WORKERS_PATH},
273
264
  )
274
-
275
- # For nicer logs, output the relative path.
276
- relative_venv_workers_path = VENV_WORKERS_PATH.relative_to(PROJECT_ROOT)
277
- if venv_python_executable.is_file():
278
- logger.info(
279
- f"Installing packages into [bold]{relative_venv_workers_path}[/bold] using uv pip...",
280
- extra={"markup": True},
281
- )
282
- run_command(
283
- [
284
- "uv",
285
- "pip",
286
- "install",
287
- "-p",
288
- venv_python_executable,
289
- "-r",
290
- str(temp_file_path),
291
- ]
292
- )
293
- logger.info(
294
- f"Packages installed in [bold]{relative_venv_workers_path}[/bold].",
295
- extra={"markup": True},
296
- )
297
- else:
298
- logger.warning(
299
- f"Python executable not found at {venv_python_executable}. Skipping installation in [bold]{relative_venv_workers_path}[/bold].",
300
- extra={"markup": True},
301
- )
265
+ VENV_WORKERS_TOKEN.touch()
266
+ logger.info(
267
+ f"Packages installed in [bold]{relative_venv_workers_path}[/bold].",
268
+ extra={"markup": True},
269
+ )
302
270
 
303
271
 
304
272
  def install_requirements(requirements: list[str]):
@@ -306,6 +274,12 @@ def install_requirements(requirements: list[str]):
306
274
  _install_requirements_to_venv(requirements)
307
275
 
308
276
 
277
+ def _is_out_of_date(token: Path, time: float) -> bool:
278
+ if not token.exists():
279
+ return True
280
+ return time > token.stat().st_mtime
281
+
282
+
309
283
  def is_sync_needed():
310
284
  """
311
285
  Checks if pyproject.toml has been modified since the last sync.
@@ -319,21 +293,6 @@ def is_sync_needed():
319
293
  return True
320
294
 
321
295
  pyproject_mtime = PYPROJECT_TOML_PATH.stat().st_mtime
322
-
323
- # Check if .venv-workers exists and get its timestamp
324
- if not VENV_WORKERS_PATH.is_dir():
325
- return True
326
-
327
- venv_mtime = VENV_WORKERS_PATH.stat().st_mtime
328
- venv_needs_update = pyproject_mtime > venv_mtime
329
- if venv_needs_update:
330
- return True
331
-
332
- # Check if vendor directory exists and get its timestamp
333
- vendor_path = PROJECT_ROOT / "python_modules"
334
- if not vendor_path.is_dir():
335
- return True
336
-
337
- vendor_mtime = vendor_path.stat().st_mtime
338
- vendor_needs_update = pyproject_mtime > vendor_mtime
339
- return vendor_needs_update
296
+ return _is_out_of_date(VENDOR_TOKEN, pyproject_mtime) or _is_out_of_date(
297
+ VENV_WORKERS_TOKEN, pyproject_mtime
298
+ )
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: workers-py
3
- Version: 1.1.7
3
+ Version: 1.2.0
4
4
  Summary: A set of libraries and tools for Python Workers
5
5
  Project-URL: Homepage, https://github.com/cloudflare/workers-py
6
6
  Project-URL: Bug Tracker, https://github.com/cloudflare/workers-py/issues
@@ -20,7 +20,7 @@ Description-Content-Type: text/markdown
20
20
  A set of libraries and tools for Python Workers.
21
21
 
22
22
 
23
- ## Pywrangler
23
+ ## Pywrangler
24
24
 
25
25
  A CLI tool for managing vendored packages in a Python Workers project.
26
26
 
@@ -0,0 +1,9 @@
1
+ pywrangler/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ pywrangler/__main__.py,sha256=BnrUM7YiBmlM4HAn2MI9hP1kVNtzeK_kEgQhRy5HTq0,38
3
+ pywrangler/cli.py,sha256=3hMjtOOib3HaTqCkuMGQe7MbBZNnim2ByHcDY4JxFlw,5091
4
+ pywrangler/sync.py,sha256=4LbwXlzi7YXXhsgYT4unJOwX9DHSLI6sREcf7abJAks,9441
5
+ pywrangler/utils.py,sha256=wfkT7GbKtgtjHXtV3AjNeb25ohdAfrprdZIlqqidiQU,3269
6
+ workers_py-1.2.0.dist-info/METADATA,sha256=s9qthgJPRW1inmDvDDl_UR-V0KA-M_Hewwp6GJxjq04,1749
7
+ workers_py-1.2.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
8
+ workers_py-1.2.0.dist-info/entry_points.txt,sha256=pt6X-Nv5-gSiKUwrnvLwzlSXs9yP37m7zdTAi8f6nAM,50
9
+ workers_py-1.2.0.dist-info/RECORD,,
@@ -1,9 +0,0 @@
1
- pywrangler/__init__.py,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
2
- pywrangler/__main__.py,sha256=BnrUM7YiBmlM4HAn2MI9hP1kVNtzeK_kEgQhRy5HTq0,38
3
- pywrangler/cli.py,sha256=BnGGrdksWP-qBj-b0ipji-61Q0kZJohyr2KZZz-XG5s,5150
4
- pywrangler/sync.py,sha256=cjqKuId3QSz7K1qUC0-zOF1Fb9ahV2HVybR9-Np5SqE,11135
5
- pywrangler/utils.py,sha256=wfkT7GbKtgtjHXtV3AjNeb25ohdAfrprdZIlqqidiQU,3269
6
- workers_py-1.1.7.dist-info/METADATA,sha256=tBk1r9Nk_ch-I1tUJ5biEczlLuPyvPPSp2TPpftuYNY,1750
7
- workers_py-1.1.7.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
8
- workers_py-1.1.7.dist-info/entry_points.txt,sha256=pt6X-Nv5-gSiKUwrnvLwzlSXs9yP37m7zdTAi8f6nAM,50
9
- workers_py-1.1.7.dist-info/RECORD,,