Closed sblondon closed 2 years ago
@sblondon we do have an undocumented feature to use a build script. This is undocumented because we have not yet formalised the api for this, so use with caution.
You can see an example configuration for this here. The script it refers to is here. This example is for generation of extensions, but you can use the same principle to generate files however you want.
Thank you @abn for the hint. It works for translation too.
In case it helps someone, there are two modifications:
./pyproject.toml
file:[tool.poetry.build]
generate-setup-file = false
script = "build.py"
./build.py
file is added:import subprocess
def create_mo_files(setup_kwargs):
print("Compile translations:")
subprocess.run(["pybabel", "compile", "--directory", "package_name/translations"])
return setup_kwargs
if __name__ == "__main__":
create_mo_files({})
pybabel
must be installed on the system (or in an activated virtualenv) but it's not a problem for us (at least in a first version).
I don't close the issue because I don't know if you want to keep it open until the feature is released. In my point of view, it could be closed.
You might want to also add pylabel
to build-system.requires
so that PEP 517 builds work too.
The changes in https://github.com/python-poetry/poetry/pull/5401 will also ensure these are picked up before the build script is run.
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Feature Request
My usecase is to compile .po files to .mo files before building the package. This issue talks also about a generic way to implement it.
There is already topics about including data :
nox
(https://github.com/cjolowicz/cookiecutter-hypermodern-python/issues/790). Closed, and I don't think it's a good way to fix this usecase.The problem is not about including the files: they are inside the python tree. If it was not the case, set up
include
configuration (include documentation, about sdist and wheel) would solve it.The need is to generate automatically the .mo files. I think about several way to do it:
add a Makefile command to generate them, then execute
poetry build
. It works but we have to remember to not usepoetry build
alone.use
tool.poetry.scripts
section in pyproject.toml, then executepoetry run mo-files; poetry build
Everything is in poetry, but we still have to remember to not use
poetry build
alone.add a optional prebuild command, so
poetry build
does the prebuild command before. The pyproject.toml could be:The value could also be a path to a shell script too instead of
package_name:function
if you think it's better. It's not my prefered choice because it change the behaviour with thetool.poetry.scripts
. If someone needs to run a script, it's possible to usesubprocess.run()
to do it. In my case, I would probably do that to runpybabel compile ...
Such solution is generic enough to solve other needs before building package. It's not limited to generate .mo files.