-
-
Notifications
You must be signed in to change notification settings - Fork 43
Add add_mock_package #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 11 commits
525d0d7
111046f
799560d
f66370c
43fa3f3
16739f5
0a6471a
5e5b0d9
0eb9803
20b74f0
c76a8e6
3418848
ea8c424
6628396
9cb4ac2
d6aab95
005efec
a12cd86
10c7f84
5d96d84
786e5e8
35539ef
107004d
ac8bc37
98b9192
e83cb59
4d719c2
fd037ae
3b77d88
782b6af
d34ff58
5e8f5e6
2c34df0
9c16b28
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,9 @@ | ||
| from ._micropip import _list as list | ||
| from ._micropip import freeze, install | ||
| from ._micropip import add_mock_package, freeze, install | ||
|
|
||
| try: | ||
| from ._version import __version__ | ||
| except ImportError: | ||
| pass | ||
|
|
||
| __all__ = ["install", "list", "freeze", "__version__"] | ||
| __all__ = ["install", "list", "freeze", "add_mock_package", "__version__"] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,8 @@ | |
| import hashlib | ||
| import importlib | ||
| import json | ||
| import os | ||
| import site | ||
| import warnings | ||
| from asyncio import gather | ||
| from dataclasses import dataclass, field | ||
|
|
@@ -10,6 +12,7 @@ | |
| from importlib.metadata import version as importlib_version | ||
| from pathlib import Path | ||
| from sysconfig import get_platform | ||
| from textwrap import dedent | ||
| from typing import IO, Any | ||
| from urllib.parse import ParseResult, urlparse | ||
| from zipfile import ZipFile | ||
|
|
@@ -692,3 +695,72 @@ def _list(): | |
| source_ = pkg_source | ||
| packages[name] = PackageMetadata(name=name, version=version, source=source_) | ||
| return packages | ||
|
|
||
|
|
||
| def add_mock_package(name, version, *, modules=None): | ||
| """ | ||
| Add a mock version of a package to the package dictionary. | ||
|
|
||
| This means that if it is a dependency, it is skipped on install. | ||
|
|
||
| By default a single empty module is installed with the same | ||
| name as the package. You can alternatively give one or more modules to make a | ||
| set of named modules. | ||
|
|
||
| The modules parameter is a dictionary mapping module name to module text. | ||
|
|
||
| e.g. | ||
| `` | ||
| { | ||
| mylovely_module:''' | ||
| def module_method(an_argument): | ||
| print("This becomes a module level argument") | ||
| module_value = "this value becomes a module level variable" | ||
|
|
||
| print("This is run on import of module") | ||
| ''' | ||
| } | ||
|
|
||
|
|
||
| `` | ||
|
|
||
| Parameters | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think there is an indentation issue in this docstring, but we can fix it once we start actually building docs for it. |
||
| ---------- | ||
| name : ``str `` | ||
|
|
||
| Package name to add | ||
|
|
||
| modules : ``Optional(dict) `` | ||
|
joemarshall marked this conversation as resolved.
|
||
|
|
||
| Dictionary of module_name:string pairs. | ||
| The string contains the source of the mock module or is blank for | ||
| an empty module. | ||
| """ | ||
| if modules is None: | ||
| # make a single mock module with this name | ||
| modules = {name: ""} | ||
| # make empty mock modules with the requested names in user site packages | ||
| site_packages = Path(site.getusersitepackages()) | ||
| metadata_file = site_packages / (name + "-" + version + ".egg-info") | ||
|
joemarshall marked this conversation as resolved.
Outdated
|
||
| with open(metadata_file, "w") as mf: | ||
|
|
||
| mf.write( | ||
| f"""Metadata-Version: 1.1 | ||
| Name: {name} | ||
| Version: {version} | ||
| Summary: {name} mock module generated by micropip | ||
| Author-email: {name}@micro.pip.non-working-fake-host | ||
| """ | ||
| ) | ||
| for n, s in modules.items(): | ||
| mf.write(f"Provides:{n}\n") | ||
|
|
||
| if s is None: | ||
| s = "" | ||
| s = dedent(s) | ||
| path_parts = n.split(".") | ||
| dir_path = Path(site_packages, *path_parts) | ||
| os.makedirs(dir_path, exist_ok=True) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we should raise error if a directory exists, because it would mean that the package is already installed somehow. |
||
| init_file = dir_path / "__init__.py" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we actually need to create these physical files? I was thinking we could maybe directly initialize Though yes, the API to create modules dynamically via
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe making another custom Finder/Loader would be a good way to go. The API for that isn't too bad.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I actually wrote it initially using mock objects and putting them into sys.modules, but I reverted to writing them out to files. I did it like that because it is really simple and reliable, things like file paths etc. just work. It also works nicely in terms of loading modules, in that init code is only run on actual import. Oh and in the case that you're running somewhere with persistent file system, your mock modules continue to exist on reload of python. It's a pretty minor change - you just create a module and exec the code into it in the loader, I can put that back in if you reckon the possibility of using mock objects makes sense.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually I think rather than take an object it needs to take a callable which takes a module object and does whatever needs to be done on it to make it. I.e. the equivalent of loader.exec_module That way you can mock module initialisation correctly.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One further thought on this - at some point I want to implement some kind of mega wheel option, to bundle a set of wheels and all deps into a single wheel with no deps. For that use case keeping persistent mock modules would be nice. If it's okay I'd like to keep that as an option in micropip? It would also be nice for any point that pyodide is run in a persistent file system. I'm thinking that a flag "persistent" could be added (which only works for string modules) and then the behaviour with finder and loader could be used in the case that the flag isn't set. |
||
| with open(init_file, "w") as f: | ||
| f.write(s) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should keep the list of mocked modules, so it can be accessed via e.g.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added list_mock_modules instead - which uses module installer metadata to list modules it has installed |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -934,3 +934,45 @@ def test_check_compatible(mock_platform, interp, abi, arch, ctx): | |
| wheel_name = f"{pkg}-{interp}-{abi}-{arch}.whl" | ||
| with ctx: | ||
| WheelInfo.from_url(wheel_name).check_compatible() | ||
|
|
||
|
|
||
| def test_add_mock_package(monkeypatch, capsys): | ||
| import site | ||
| from importlib.metadata import version as importlib_version | ||
|
|
||
| from micropip._micropip import add_mock_package | ||
|
|
||
| with TemporaryDirectory() as tmpdirname: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a |
||
|
|
||
| def _getusersitepackages(): | ||
| return tmpdirname | ||
|
|
||
| monkeypatch.setattr(site, "getusersitepackages", _getusersitepackages) | ||
| monkeypatch.setattr(sys, "path", [tmpdirname]) | ||
| add_mock_package("test_1", "1.0.0") | ||
| add_mock_package( | ||
| "test_2", | ||
| "1.2.0", | ||
| modules={ | ||
| "t1": "print('hi from t1')", | ||
| "t2": """ | ||
| def fn(): | ||
| print("Hello from fn") | ||
| """, | ||
| }, | ||
| ) | ||
| import t1 | ||
|
|
||
| dir(t1) | ||
| import t2 | ||
|
|
||
| dir(t2) | ||
| import test_1 | ||
|
|
||
| dir(test_1) | ||
|
|
||
| t2.fn() | ||
| assert importlib_version("test_2") == "1.2.0" | ||
| captured = capsys.readouterr() | ||
| assert captured.out.find("hi from t1") != -1 | ||
| assert captured.out.find("Hello from fn") != -1 | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please add type hints here?