@@ -67,6 +67,69 @@ def _py_untar(tarball_path, target_dir):
67
67
}
68
68
69
69
70
+ Module [ "_unzip_from_python" ] = function ( tarball_path , target_dir ) {
71
+ Module . exec ( `
72
+ def _py_unzip(tarball_path, target_dir):
73
+ import json
74
+ from pathlib import Path
75
+ import zipfile
76
+
77
+ target = Path(target_dir)
78
+ target.mkdir(parents=True, exist_ok=True)
79
+ pkg_file = {"name": "", "path": ""}
80
+ with zipfile.ZipFile(tarball_path, mode="r") as archive:
81
+
82
+ for filename in archive.namelist():
83
+ if filename.startswith("pkg-"):
84
+ pkg_file["name"] = filename
85
+ pkg_file["path"] = str(target / filename)
86
+ archive.extract(filename, target_dir)
87
+ break
88
+ return json.dumps(pkg_file)
89
+
90
+ ` )
91
+ let extracted_file = Module . eval ( `_py_unzip("${ tarball_path } ", "${ target_dir } ")` )
92
+
93
+ return JSON . parse ( extracted_file )
94
+ }
95
+
96
+ Module [ "_install_conda_file_from_python" ] = function ( tarball_path , target_dir ) {
97
+ Module . exec ( `
98
+ def _py_unbz2(tarball_path, target_dir):
99
+ import json
100
+ from pathlib import Path
101
+ import tarfile
102
+ import shutil
103
+ import os
104
+ import sys
105
+
106
+ target = Path(target_dir)
107
+ prefix = Path(sys.prefix)
108
+ try:
109
+ with tarfile.open(tarball_path) as tar:
110
+ tar.extractall(target_dir)
111
+
112
+ src = target / "site-packages"
113
+ dest = prefix / "lib/python3.11/site-packages"
114
+ shutil.copytree(src, dest, dirs_exist_ok=True)
115
+ for folder in ["etc", "share"]:
116
+ src = target / folder
117
+ dest = prefix / folder
118
+ if src.exists():
119
+ shutil.copytree(src, dest, dirs_exist_ok=True)
120
+ shutil.rmtree(target)
121
+ except Exception as e:
122
+ print("ERROR",e)
123
+ raise e
124
+
125
+ return json.dumps([])
126
+
127
+ ` )
128
+ let extracted_file = Module . eval ( `_py_unbz2("${ tarball_path } ", "${ target_dir } ")` )
129
+
130
+ return JSON . parse ( extracted_file )
131
+ }
132
+
70
133
71
134
72
135
@@ -108,20 +171,55 @@ Module["bootstrap_from_empack_packed_environment"] = async function
108
171
pkg ,
109
172
verbose
110
173
) {
111
- const package_url = pkg ?. url ?? `${ package_tarballs_root_url } /${ pkg . filename } ` ;
112
- if ( verbose ) {
113
- console . log ( `!!fetching pkg ${ pkg . name } from ${ package_url } ` )
114
- }
115
- let byte_array = await fetchByteArray ( package_url )
116
- const tarball_path = `/package_tarballs/${ pkg . filename } ` ;
117
- Module . FS . writeFile ( tarball_path , byte_array ) ;
118
- if ( verbose ) {
119
- console . log ( `!!extract ${ tarball_path } (${ byte_array . length } bytes)` )
174
+ const package_url =
175
+ pkg ?. url ?? `${ package_tarballs_root_url } /${ pkg . filename } ` ;
176
+ if ( verbose ) {
177
+ console . log ( `!!fetching pkg ${ pkg . name } from ${ package_url } ` ) ;
178
+ }
179
+ let byte_array = await fetchByteArray ( package_url ) ;
180
+ const tarball_path = `/package_tarballs/${ pkg . filename } ` ;
181
+ Module . FS . writeFile ( tarball_path , byte_array ) ;
182
+ if ( verbose ) {
183
+ console . log (
184
+ `!!extract ${ tarball_path } (${ byte_array . length } bytes)`
185
+ ) ;
186
+ }
187
+
188
+ if ( verbose ) {
189
+ console . log ( "await python_is_ready_promise" ) ;
190
+ }
191
+ await python_is_ready_promise ;
192
+
193
+ if ( package_url . toLowerCase ( ) . endsWith ( ".conda" ) ) {
194
+ // Conda v2 packages
195
+ if ( verbose ) {
196
+ console . log (
197
+ `!!extract conda package ${ package_url } (${ byte_array . length } bytes)`
198
+ ) ;
199
+ }
200
+ const dest = `/conda_packages/${ pkg . name } ` ;
201
+ const pkg_file = Module [ "_unzip_from_python" ] (
202
+ tarball_path ,
203
+ dest
204
+ ) ;
205
+ return Module . _install_conda_file ( pkg_file . path , dest , prefix ) ;
206
+ } else if ( package_url . toLowerCase ( ) . endsWith ( ".tar.bz2" ) ) {
207
+ // Conda v1 packages
208
+ if ( verbose ) {
209
+ console . log (
210
+ `!!extract conda package ${ package_url } (${ byte_array . length } bytes)`
211
+ ) ;
212
+ }
213
+ const dest = `/conda_packages/${ pkg . name } ` ;
214
+ return Module [ "_install_conda_file_from_python" ] (
215
+ tarball_path ,
216
+ dest
217
+ ) ;
218
+ } else {
219
+ // Pre-relocated packages
220
+ return Module [ "_untar_from_python" ] ( tarball_path ) ;
221
+ }
120
222
}
121
- if ( verbose ) { console . log ( "await python_is_ready_promise" ) ; }
122
- await python_is_ready_promise ;
123
- return Module [ "_untar_from_python" ] ( tarball_path ) ;
124
- }
125
223
126
224
127
225
async function bootstrap_python ( prefix , package_tarballs_root_url , python_package , verbose ) {
0 commit comments