Path: blob/main/python/cpython/src/__pycache__/cowasm_importer.cpython-311.pyc
1924 views
�
��c9 � �4 � d Z ddlZddlZddlZddlZddlZddlZddlZddlmZ i Z dej
� dd� � v ZdZ
erd� Znd � Zdad
� Zd� Z G d� d
ej j � � Z G d� dej j � � Zdedefd�Zdefd�Zd� ZdS )a]
This tiny simple custom importer makes it so we can if you have a
tarball foo.tar.xz somewhere in your sys.path that contains a Python
module, then this works:
import foo
This even works with .so extension module code. It's reasonably
efficient too, in some ways. How is this possible? This works in a
very different way than Python's own zipfile importer and to me it
is both much simpler and much better. At
https://docs.python.org/3/library/zipfile.html#pyzipfile-objects
there are docs about turning a Python module (without extension code)
into a zip file which can then be exported. It works for that
application, but has drawbacks because zip files are much larger than
.tar.xz files; also, it seems like importing is a bit slower. What
we do here instead is much simpler -- we just automaticlaly extract
the .tar.xz file to a temporary folder, which we add to sys.path.
That's it! It's ridiculously simple, but works well for our application
to WebAssembly where small size is very important.
NOTES:
- See https://dev.to/dangerontheranger/dependency-injection-with-import-hooks-in-python-3-5hap
- When working on this, here's how to update things after a change:
~/cowasm/packages/cpython$ rm dist/wasm/.install-data && cp src/cowasm_importer.py dist/wasm/lib/python3.11/site-packages/ && make && cd ../python-wasm/ && make && cd ../cpython/
� N)�timezcowasm:importer�DEBUG� z.tar.xzc � � t | � d S �N)�print��argss ��/Users/wstein/build/cocalc/src/data/projects/2c9318d1-4f8b-4910-8da7-68a965514c95/cowasm-python/packages/cpython/src/cowasm_importer.py�logr 1 s � �
�t����� c � � d S r � r s r r r 5 � � ��r
c �� � t j D ]} | � d� � r| c S �t j D ]8} | � d� � r!t j � | � � c S �9d S )Nz/site-packagesz/lib-dynload)�sys�path�endswith�os�dirname)r s r �site_packages_directoryr <