11import csv
22import os
3- from copy import copy
4- from pathlib import Path
5- import requests
63import shutil
7- from subprocess import check_call , run , DEVNULL
4+ import sys
5+ from pathlib import Path
6+ from subprocess import run
87from tempfile import TemporaryDirectory
9- from typing import List
8+ from typing import List , Optional
109from urllib .parse import urlparse
11- import sys
1210
11+ import requests
12+ import typer
1313import yaml
14-
15- from empack .pack import pack_env , DEFAULT_CONFIG_PATH
1614from empack .file_patterns import PkgFileFilter , pkg_file_filter_from_yaml
17-
18- import typer
15+ from empack .pack import DEFAULT_CONFIG_PATH , pack_env
1916
2017try :
2118 from mamba .api import create as mamba_create
4037]
4138
4239PLATFORM = "emscripten-32"
40+ DEFAULT_REQUEST_TIMEOUT = 1 # in minutes
4341
4442
4543def create_env (
@@ -131,10 +129,12 @@ def _install_pip_dependencies(prefix_path, dependencies, log=None):
131129 if log is not None :
132130 log .warning (
133131 """
134- Installing pip dependencies. This is very much experimental so use this feature at your own risks.
132+ Installing pip dependencies. This is very much experimental so use
133+ this feature at your own risks.
135134 Note that you can only install pure-python packages.
136- pip is being run with the --no-deps option to not pull undesired system-specific dependencies, so please
137- install your package dependencies from emscripten-forge or conda-forge.
135+ pip is being run with the --no-deps option to not pull undesired
136+ system-specific dependencies, so please install your package dependencies
137+ from emscripten-forge or conda-forge.
138138 """
139139 )
140140
@@ -144,6 +144,8 @@ def _install_pip_dependencies(prefix_path, dependencies, log=None):
144144
145145 run (
146146 [
147+ sys .executable ,
148+ "-m" ,
147149 "pip" ,
148150 "install" ,
149151 * dependencies ,
@@ -166,7 +168,7 @@ def _install_pip_dependencies(prefix_path, dependencies, log=None):
166168 packages_dist_info = Path (pkg_dir .name ).glob ("*.dist-info" )
167169
168170 for package_dist_info in packages_dist_info :
169- with open (package_dist_info / "RECORD" , "r" ) as record :
171+ with open (package_dist_info / "RECORD" ) as record :
170172 record_content = record .read ()
171173 record_csv = csv .reader (record_content .splitlines ())
172174 all_files = [_file [0 ] for _file in record_csv ]
@@ -181,7 +183,7 @@ def _install_pip_dependencies(prefix_path, dependencies, log=None):
181183 with open (package_dist_info / "RECORD" , "w" ) as record :
182184 record .write (fixed_record_data )
183185
184- non_supported_files = [".so" , ".a" , ".dylib" , ".lib" , ".exe" " .dll" ]
186+ non_supported_files = [".so" , ".a" , ".dylib" , ".lib" , ".exe.dll" ]
185187
186188 # COPY files under `prefix_path`
187189 for _file , inside_site_packages in files :
@@ -208,10 +210,10 @@ def _install_pip_dependencies(prefix_path, dependencies, log=None):
208210 shutil .copy (src_path , dest_path )
209211
210212
211- def build_and_pack_emscripten_env (
213+ def build_and_pack_emscripten_env ( # noqa: C901, PLR0912, PLR0915
212214 python_version : str = PYTHON_VERSION ,
213215 xeus_python_version : str = XEUS_PYTHON_VERSION ,
214- packages : List [str ] = [] ,
216+ packages : Optional [ List [str ]] = None ,
215217 environment_file : str = "" ,
216218 root_prefix : str = "/tmp/xeus-python-kernel" ,
217219 env_name : str = "xeus-python-kernel" ,
@@ -222,13 +224,13 @@ def build_and_pack_emscripten_env(
222224 log = None ,
223225):
224226 """Build a conda environment for the emscripten platform and pack it with empack."""
227+ if packages is None :
228+ packages = []
225229 channels = CHANNELS
226230 specs = [
227231 f"python={ python_version } " ,
228232 "xeus-lite" ,
229- "xeus-python"
230- if not xeus_python_version
231- else f"xeus-python={ xeus_python_version } " ,
233+ "xeus-python" if not xeus_python_version else f"xeus-python={ xeus_python_version } " ,
232234 * packages ,
233235 ]
234236 bail_early = True
@@ -261,13 +263,17 @@ def build_and_pack_emscripten_env(
261263 elif isinstance (dependency , dict ) and dependency .get ("pip" ) is not None :
262264 # If it's a local Python package, make its path relative to the environment file
263265 pip_dependencies = [
264- ((env_file .parent / pip_dep ).resolve () if os .path .isdir (env_file .parent / pip_dep ) else pip_dep )
266+ (
267+ (env_file .parent / pip_dep ).resolve ()
268+ if os .path .isdir (env_file .parent / pip_dep )
269+ else pip_dep
270+ )
265271 for pip_dep in dependency ["pip" ]
266272 ]
267273
268274 # Bail early if there is nothing to do
269275 if bail_early and not force :
270- return []
276+ return ""
271277
272278 orig_config = os .environ .get ("CONDARC" )
273279
@@ -294,7 +300,9 @@ def build_and_pack_emscripten_env(
294300 if empack_config :
295301 empack_config_is_url = urlparse (empack_config ).scheme in ("http" , "https" )
296302 if empack_config_is_url :
297- empack_config_content = requests .get (empack_config ).content
303+ empack_config_content = requests .get (
304+ empack_config , timeout = DEFAULT_REQUEST_TIMEOUT
305+ ).content
298306 pack_kwargs ["file_filters" ] = PkgFileFilter .parse_obj (
299307 yaml .safe_load (empack_config_content )
300308 )
@@ -324,7 +332,7 @@ def build_and_pack_emscripten_env(
324332 dirs_exist_ok = True ,
325333 )
326334
327- with open (Path (output_path ) / "worker.ts" , "r" ) as fobj :
335+ with open (Path (output_path ) / "worker.ts" ) as fobj :
328336 worker = fobj .read ()
329337
330338 worker = worker .replace ("XEUS_KERNEL_FILE" , "'xpython_wasm.js'" )
@@ -356,9 +364,7 @@ def build_and_pack_emscripten_env(
356364def main (
357365 python_version : str = PYTHON_VERSION ,
358366 xeus_python_version : str = XEUS_PYTHON_VERSION ,
359- packages : List [str ] = typer .Option (
360- [], help = "The list of packages you want to install"
361- ),
367+ packages : List [str ] = typer .Option ([], help = "The list of packages you want to install" ),
362368 environment_file : str = typer .Option (
363369 "" , help = "The path to the environment.yml file you want to use"
364370 ),
0 commit comments