python-poetry / poetry

Python packaging and dependency management made easy
https://python-poetry.org
MIT License
31.68k stars 2.27k forks source link

prebuild action before 'poetry build' command #5539

Closed sblondon closed 2 years ago

sblondon commented 2 years ago

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 :

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:

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 the tool.poetry.scripts. If someone needs to run a script, it's possible to use subprocess.run() to do it. In my case, I would probably do that to run pybabel compile ...

Such solution is generic enough to solve other needs before building package. It's not limited to generate .mo files.

abn commented 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.

sblondon commented 2 years ago

Thank you @abn for the hint. It works for translation too.

In case it helps someone, there are two modifications:

  1. Three lines are inserted in ./pyproject.toml file:
[tool.poetry.build]
generate-setup-file = false
script = "build.py"
  1. A ./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.

abn commented 2 years ago

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.

github-actions[bot] commented 8 months ago

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.