Skip to content
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

prebuild action before 'poetry build' command #5539

Closed
2 tasks done
sblondon opened this issue May 4, 2022 · 4 comments
Closed
2 tasks done

prebuild action before 'poetry build' command #5539

sblondon opened this issue May 4, 2022 · 4 comments

Comments

@sblondon
Copy link

sblondon commented May 4, 2022

  • I have searched the issues of this repo and believe that this is not a duplicate.
  • I have searched the documentation and believe that my question is not covered.

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:

  • add a Makefile command to generate them, then execute poetry build. It works but we have to remember to not use poetry build alone.
  • use tool.poetry.scripts section in pyproject.toml, then execute poetry run mo-files; poetry build
[tool.poetry.scripts]
mo-files = "prebuild:main"

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:
[tool.poetry.build]
prebuild = "prebuild:main"

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.

  • What do you think about the idea? Do you see flaws or does it need improvements?
  • I'm interested to write a PR if maintainers think it's a good solution.
@sblondon sblondon added kind/feature Feature requests/implementations status/triage This issue needs to be triaged labels May 4, 2022
@abn
Copy link
Member

abn commented May 5, 2022

@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.

@abn abn added status/waiting-on-response Waiting on response from author and removed status/triage This issue needs to be triaged labels May 9, 2022
@sblondon
Copy link
Author

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
Copy link
Member

abn commented May 14, 2022

You might want to also add pylabel to build-system.requires so that PEP 517 builds work too.

The changes in #5401 will also ensure these are picked up before the build script is run.

Copy link

github-actions bot commented Mar 2, 2024

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants