Minor improvements to micropip (from #1340) (#1347)

This commit is contained in:
Hood Chatham 2021-03-20 02:44:39 -07:00 committed by GitHub
parent 3f9e36a986
commit 95908d4ef6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 41 deletions

View File

@ -12,7 +12,12 @@ from distlib import markers, util, version
import sys
# Provide stubs for testing in native python
IN_BROWSER = "js" in sys.modules
try:
from js import pyodide as js_pyodide
IN_BROWSER = True
except ImportError:
IN_BROWSER = False
if IN_BROWSER:
# In practice, this is the `site-packages` directory.
@ -41,23 +46,6 @@ else:
return io.BytesIO(content)
if IN_BROWSER:
from js import pyodide as js_pyodide
else:
class js_pyodide: # type: ignore
"""A mock object to allow import of this package outside pyodide
Report that all dependencies are empty.
"""
class _module:
class packages:
class dependencies:
@staticmethod
def object_entries():
return []
if IN_BROWSER:
from asyncio import gather
else:
@ -132,10 +120,10 @@ class _PackageManager:
version_scheme = version.get_scheme("normalized")
def __init__(self):
self.builtin_packages = {}
self.builtin_packages.update(
js_pyodide._module.packages.dependencies.object_entries()
)
if IN_BROWSER:
self.builtin_packages = js_pyodide._module.packages.dependencies.to_py()
else:
self.builtin_packages = {}
self.installed_packages = {}
async def install(self, requirements: Union[str, List[str]], ctx=None):

View File

@ -7,22 +7,38 @@ import pytest
sys.path.append(str(Path(__file__).resolve().parent / "micropip"))
def test_install_simple(selenium_standalone):
assert (
selenium_standalone.run_js(
"""
let result = await pyodide.runPythonAsync(`
import os
import micropip
# Package 'pyodide-micropip-test' has dependency on 'snowballstemmer'
# It is used to test markers support
await micropip.install('pyodide-micropip-test')
import snowballstemmer
stemmer = snowballstemmer.stemmer('english')
stemmer.stemWords('go going goes gone'.split())
`);
return result.toJs();
@pytest.fixture
def selenium_standalone_micropip(selenium_standalone):
"""Import micropip before entering test so that global initialization of
micropip doesn't count towards hiwire refcount.
"""
selenium_standalone.run_js(
"""
await languagePluginLoader;
await pyodide.loadPackage("micropip");
await pyodide.runPythonAsync("import micropip");
"""
)
yield selenium_standalone
def test_install_simple(selenium_standalone_micropip):
selenium = selenium_standalone_micropip
assert (
selenium.run_js(
"""
let result = await pyodide.runPythonAsync(`
import os
import micropip
# Package 'pyodide-micropip-test' has dependency on 'snowballstemmer'
# It is used to test markers support
await micropip.install('pyodide-micropip-test')
import snowballstemmer
stemmer = snowballstemmer.stemmer('english')
stemmer.stemWords('go going goes gone'.split())
`);
return result.toJs();
"""
)
== ["go", "go", "goe", "gone"]
)
@ -57,11 +73,12 @@ def test_parse_wheel_url():
assert wheel["platform"] == "macosx_10_9_intel"
def test_install_custom_url(selenium_standalone, web_server_tst_data):
def test_install_custom_url(selenium_standalone_micropip, web_server_tst_data):
selenium = selenium_standalone_micropip
server_hostname, server_port, server_log = web_server_tst_data
base_url = f"http://{server_hostname}:{server_port}/"
url = base_url + "snowballstemmer-2.0.0-py2.py3-none-any.whl"
selenium_standalone.run_js(
selenium.run_js(
f"""
await pyodide.runPythonAsync(`
import micropip
@ -101,14 +118,15 @@ def test_add_requirement_relative_url():
assert req["url"] == "./snowballstemmer-2.0.0-py2.py3-none-any.whl"
def test_install_custom_relative_url(selenium_standalone):
def test_install_custom_relative_url(selenium_standalone_micropip):
selenium = selenium_standalone_micropip
root = Path(__file__).resolve().parents[2]
src = root / "src" / "tests" / "data"
target = root / "build" / "test_data"
target.symlink_to(src, True)
url = "./test_data/snowballstemmer-2.0.0-py2.py3-none-any.whl"
try:
selenium_standalone.run_js(
selenium.run_js(
f"""
await pyodide.runPythonAsync(`
import micropip