Skip to content

Commit

Permalink
Add configuration in pyproject.toml (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
rth authored Oct 28, 2023
1 parent a12b4af commit 2c82ab2
Show file tree
Hide file tree
Showing 22 changed files with 305 additions and 93 deletions.
5 changes: 0 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ repos:
exclude: examples


- repo: https://github.com/shellcheck-py/shellcheck-py
rev: "v0.9.0.6"
hooks:
- id: shellcheck

- repo: https://github.com/codespell-project/codespell
rev: "v2.2.6"
hooks:
Expand Down
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Add configuration for `pyodide pack` loaded from the `[tool.pyodide_pack]` section of a
`pyproject.toml` files
[#35](https://github.com/pyodide/pyodide-pack/pull/35)


- Add `pyidide minify` command to minify the Python packages with AST rewrites by,
removing comments and docstrings
[#23](https://github.com/pyodide/pyodide-pack/pull/23)
Expand All @@ -29,6 +34,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added support for Pyodide 0.24.0. This is now the minimal supported version of Pyodide.
[#26](https://github.com/pyodide/pyodide-pack/pull/26)

## Fixed

- Fix a syntax error when stripping docstrings from a function with an empty body
[#35](https://github.com/pyodide/pyodide-pack/pull/35)


## [0.2.0] - 2022-09-04

### Changed
Expand Down
48 changes: 48 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
(config)=
# Configuration

`pyodide pack` can be configured via a `pyproject.toml` file in the root of your project, or in a any of the parent directories.

Below is an example of configuration with default values. In most cases, the defaults should be fine, and you can only include fields you want to change.


```toml
[tool.pyodide_pack]
requires = []
include_paths = []

[tool.pyodide_pack.py]
strip_module_docstrings = false
strip_docstrings = false
py_compile = false

[tool.pyodide_pack.so]
drop_unused_so = true
```


## Configuration options

### `requires`

List of dependencies to load. This list is passed to micropip, so it can be any valid micropip specifier.

### `include_paths`

List of paths to include in the bundle. This is useful for including files that were otherwise excluded by `pyodide pack`

### `py.strip_module_docstrings`

Whether to strip module docstrings. Default: `false`

### `py.strip_docstrings`

Whether to strip docstrings. Default: `false`

### `py.py_compile`

Whether to compile python files. Default: `false`

### `so.drop_unused_so`

Whether to drop unused `.so` files. Default: `true`
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ Each of these approaches have different tradeoffs, and can be used separately or
ast-rewrite.md
module-elimination-at-runtime.md
cli.md
configuration.md
2 changes: 2 additions & 0 deletions examples/micropip_deps/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tool.pyodide_pack]
requires = ["snowballstemmer"]
1 change: 0 additions & 1 deletion examples/micropip_deps/requirements.txt

This file was deleted.

2 changes: 2 additions & 0 deletions examples/pandas/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tool.pyodide_pack]
requires = ["pandas"]
2 changes: 0 additions & 2 deletions examples/pandas/requirements.txt

This file was deleted.

5 changes: 5 additions & 0 deletions examples/scikit-learn/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[tool.pyodide_pack]
requires = [
"numpy",
"scikit-learn"
]
2 changes: 0 additions & 2 deletions examples/scikit-learn/requirements.txt

This file was deleted.

1 change: 1 addition & 0 deletions examples/stdlib_only/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[tool.pyodide_pack]
Empty file.
2 changes: 2 additions & 0 deletions examples/sympy/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tool.pyodide_pack]
requires = ["sympy"]
1 change: 0 additions & 1 deletion examples/sympy/requirements.txt

This file was deleted.

4 changes: 2 additions & 2 deletions examples/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

def gen_all_examples():
for path in Path("examples").glob("*"):
if path.is_dir() and (path / "requirements.txt").exists():
if path.is_dir() and (path / "pyproject.toml").exists():
path = path.resolve()
yield pytest.param(path, id=path.name)

Expand All @@ -29,7 +29,7 @@ def test_all(example_dir, tmp_path):
with redirect_stdout(stdout):
cli.main(
example_path=example_dir / "app.py",
requirement_path=example_dir / "requirements.txt",
config_path=None,
verbose=False,
include_paths=None,
write_debug_map=True,
Expand Down
21 changes: 12 additions & 9 deletions pyodide_pack/ast_rewrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import typer

from pyodide_pack.config import PyPackConfig

STRIP_DOCSTRING_EXCLUDES: list[str] = []
STRIP_DOCSTRING_MODULE_EXCLUDES: list[str] = [
"numpy/*" # known issue for v1.25 to double check for v1.26
Expand Down Expand Up @@ -62,19 +64,18 @@ def _strip_module_docstring(tree: ast.Module) -> ast.Module:
def _rewrite_py_code(
code: str,
file_name: str,
strip_docstrings: bool = False,
strip_module_docstrings: bool = False,
py_config: PyPackConfig,
) -> str:
try:
tree = ast.parse(code)
except SyntaxError:
return code
try:
if strip_docstrings and not _path_matches_patterns(
if py_config.strip_docstrings and not _path_matches_patterns(
file_name, STRIP_DOCSTRING_EXCLUDES
):
tree = _strip_module_docstring(tree)
if strip_module_docstrings and not _path_matches_patterns(
if py_config.strip_module_docstrings and not _path_matches_patterns(
file_name, STRIP_DOCSTRING_MODULE_EXCLUDES
):
tree = _StripDocstringsTransformer().visit(tree)
Expand All @@ -101,7 +102,12 @@ def main(
Note: this API will change before the next release
"""
output_dirname = input_dir.name + "_stripped"
if strip_docstrings:
py_config = PyPackConfig(
strip_docstrings=strip_docstrings,
strip_module_docstrings=strip_module_docstrings,
py_compile=False,
)
if py_config.strip_docstrings:
output_dirname += "_no_docstrings"
output_dir = input_dir.parent / output_dirname
shutil.rmtree(output_dir, ignore_errors=True)
Expand All @@ -117,10 +123,7 @@ def main(
except UnicodeDecodeError:
continue
uncommented_code = _rewrite_py_code(
code,
file_name=str(file),
strip_docstrings=strip_docstrings,
strip_module_docstrings=strip_module_docstrings,
code, file_name=str(file), py_config=py_config
)

if uncommented_code is None:
Expand Down
Loading

0 comments on commit 2c82ab2

Please sign in to comment.