Skip to content

Split python-libzim between reader and writer parts - #292

Merged
benoit74 merged 1 commit into
openzim:mainfrom
pawank925:split-writer-reader-final
Oct 30, 2026
Merged

benoit74 merged 1 commit into
openzim:mainfrom
pawank925:split-writer-reader-final

Conversation

@pawank925

Copy link
Copy Markdown
Contributor

Split writer/reader parts to support libzim built without writer (e.g. Android/reader-only builds).

Fixes #287

@pawank925
pawank925 marked this pull request as draft October 28, 2026 02:53
@pawank925
pawank925 marked this pull request as ready for review October 28, 2026 03:01
@pawank925
pawank925 marked this pull request as draft October 28, 2026 03:03
@pawank925
pawank925 force-pushed the split-writer-reader-final branch 3 times, most recently from 7e3bf7b to 3cce80a Compare October 28, 2026 09:47
@codecov

codecov Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.56962% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 95.29%. Comparing base (fabc288) to head (574dbf0).

Files with missing lines Patch % Lines
libzim/libzim.pyx 95.56% 7 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #292      +/-   ##
==========================================
+ Coverage   93.70%   95.29%   +1.59%     
==========================================
  Files           1        1              
  Lines         635      638       +3     
==========================================
+ Hits          595      608      +13     
+ Misses         40       30      -10     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@pawank925
pawank925 force-pushed the split-writer-reader-final branch from 3a13b29 to 3803f8c Compare October 28, 2026 14:21
@pawank925
pawank925 marked this pull request as ready for review October 28, 2026 14:23
@pawank925
pawank925 force-pushed the split-writer-reader-final branch from 3803f8c to 1f37944 Compare October 28, 2026 14:27
@pawank925

Copy link
Copy Markdown
Contributor Author

@benoit74 once look into this please thanks!

@benoit74 benoit74 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review of this PR's reader/writer split. Findings below, each with a suggested fix where one is mechanical; the setup.py detection and CI-coverage ones are judgment calls for you to evaluate rather than blind applies.

Comment thread tests/test_libzim_creator.py Resolved
Comment thread setup.py Resolved
Comment thread setup.py Resolved
Comment thread pyproject.toml Resolved
Comment thread libzim/libzim.pyx Resolved
Comment thread libzim/__init__.pyi Resolved
Comment thread libzim/libzim.pyx Resolved
Comment thread tests/test_libzim_modules.py Resolved
@pawank925
pawank925 force-pushed the split-writer-reader-final branch from 1f37944 to 3dcff3b Compare October 29, 2026 14:01
@pawank925
pawank925 requested a review from benoit74 October 29, 2026 14:17

@benoit74 benoit74 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reading all modifications in setup.py twice, I realize that automated detection seems too fragile IMHO ; I would rather prefer we resort only on an env variable LIBZIM_WITHOUT_WRITER which makes the choice explicit in all cases. Pretty concerned that we will have to force detection in some cases, and not build with writer in cases where we would have expected to.

This also makes codes much simpler, which is always good from a maintenance perspective

WDYT?

@pawank925
pawank925 force-pushed the split-writer-reader-final branch from 3dcff3b to 0a5a53d Compare October 29, 2026 14:43
@pawank925

pawank925 commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor Author

@benoit74 agreed, and done — detection is gone entirely.

  • detect_without_writer() and its header/pkg-config scanning are removed; LIBZIM_WITHOUT_WRITER is now the only switch, read once in Config (setup.py) and applied to both the C macro and the Cython compile-time env.
  • The CI job now sets LIBZIM_WITHOUT_WRITER=1 instead of the temporary FORCE_ variable, and the README documents the variable instead of the heuristics.

The failure mode is now the one you want: if a reader-only build forgets the variable, the build tries to compile the writer bindings against a writer-less libzim and fails loudly at compile time, rather than silently producing a wheel without writer support. Nothing can silently downgrade a writer build either.

Verified after the change: default build 180 passed; LIBZIM_WITHOUT_WRITER=1 build 37 passed / 141 skipped, import libzim.writer

@pawank925
pawank925 requested a review from benoit74 October 29, 2026 14:46
@pawank925
pawank925 marked this pull request as draft October 29, 2026 14:50
@pawank925
pawank925 marked this pull request as ready for review October 29, 2026 15:03
@pawank925
pawank925 force-pushed the split-writer-reader-final branch from 9799b05 to 0a5a53d Compare October 29, 2026 16:23
Comment thread setup.py Resolved
Comment thread setup.py Resolved
Cython cannot conditionally parse `cdef extern` blocks, so declare the
writer API through a local shim header that only includes libzim's writer
headers when the library was built with writer support. Guard the writer
implementations (C++ wrappers, writer callbacks, Hint, Creator and the
libzim.writer registration) with LIBZIM_WITHOUT_WRITER, and let the build
select that path explicitly through the LIBZIM_WITHOUT_WRITER env var.

On a writer-less libzim the extension now builds and `import libzim.writer`
raises ImportError, which is what Android/reader-only wheels need (openzim#283).
The reader-only build is covered by a dedicated CI job. Adds tests for lazy
submodule loading and creator guards.

Fixes openzim#287
@pawank925
pawank925 force-pushed the split-writer-reader-final branch from 0a5a53d to 574dbf0 Compare October 30, 2026 08:39
@pawank925
pawank925 requested a review from benoit74 October 30, 2026 08:42

@benoit74 benoit74 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@rgaudin do you wanna have a second look now that PR seems ready to me?

@rgaudin

rgaudin commented Oct 30, 2026

Copy link
Copy Markdown
Member

Just skimmed through it. Looks good 👍

What bothers me is that we'll be publishing the very same libzim package on PyPi but the library is quite different on android, for reasons that are entirely Kiwix.

Now that the work for a reader-only libzim has been done, I think we should discuss (outside this PR) how we want to distribute this. I'd be happy to pull a read-only libzim in many scenarios. The extra scheme makes the most sense I believe but it could also be a different package.
If we keep it as it is, it mandates updates to the documentation.

On a personal note, I'd find LIBZIM_WITH_WRITER=0 better than LIBZIM_WITHOUT_WRITER=1

@benoit74

Copy link
Copy Markdown
Contributor

Thank you @rgaudin

What bothers me is that we'll be publishing the very same libzim package on PyPi but the library is quite different on android, for reasons that are entirely Kiwix.

This is unfortunately the decision which has already been taken at libzim level, python-libzim can only cope with that decision and report concern upstream. I will do so.

Now that the work for a reader-only libzim has been done, I think we should discuss (outside this PR) how we want to distribute this. I'd be happy to pull a read-only libzim in many scenarios. The extra scheme makes the most sense I believe but it could also be a different package.

Same problem ; we do not have split between read-only + write-only libzim binaries on most platforms, so we can't build a read-only python-libzim with a "writer" extra ; this requires to have libzim-core + libzim-writer binaries. Otherwise we just remove the writer part of python-libzim while still pulling the whole libzim binaries ... which does not really makes sense (no strong added value IMHO).

If we keep it as it is, it mandates updates to the documentation.

Indeed ; since there is no platform building without writer in this PR, it does not seems reasonable to do in this PR ; but we clearly need to document in #283 that Android platform is available but without writer module.

On a personal note, I'd find LIBZIM_WITH_WRITER=0 better than LIBZIM_WITHOUT_WRITER=1

LIBZIM_WITHOUT_WRITER is mimicking libzim --without-writer flag ; not saying we must follow all libzim conventions, but here it seems reasonable IMHO.

@benoit74

Copy link
Copy Markdown
Contributor

Opened openzim/libzim#1139 to discuss the "extra" approach.

@benoit74
benoit74 merged commit 1ffeb52 into openzim:main Oct 30, 2026
73 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Split python-libzim between reader and writer parts

3 participants