There a way to publish packages (`python_distribut...
# general
b
There a way to publish packages (
python_distribution
) where I get the version from the current git tag? Is there a way to do this without writing my own plugin?
c
There is a
vcs_version
target that lets you update a version string in some arbitrary source file. See: https://www.pantsbuild.org/docs/generating-version-tags
b
Right but I don’t need the version in the build to change, not a source file.
Am I missing something that connects one to the other?
c
perhaps make use of setuptools-scm directly, although I’m not sure how to do that, with nor without Pants in the picture 😉 there’s already some setuptools-scm integration in Pants, but I guess that is for the
vcs_version
target: https://www.pantsbuild.org/v2.15/docs/reference-setuptools-scm I’ll defer to @happy-kitchen-89482 in case he has a better answer for you 🙂
👍 1
h
To clarify,
vcs_version
doesn’t modify a checked-in source file, it’s a code generator. So it should do what you want.
b
@happy-kitchen-89482 I don't understand. How do I translate having the version in a python file to having a
package-<version>-py3-whatever.whl
built and uploaded to pypi? Like I'm getting a
Missing a 'version' kwarg in the 'provides' field
when I try to package without putting a hardcoded version in
python_artifact
. I'm trying to use
Copy code
python_distribution(
    name="library",
    provides=python_artifact(
        name="library",
        description="Long description here",
        author_email="Me <my@email.com>",
    ),
    generate_setup=True,
    interpreter_constraints=["CPython>=3.10.0"],
    sdist=False,
    skip_twine=True,
)
h
Ah, right, you’re generating a setup.py. Unfortunately the generated setup.py doesn’t use this yet. I have a TODO to fix that. Also @future-oxygen-10553’s doing some work that is related.
If you had a handwritten setup.py you could import from that file in it
👍 1
I will try and bang out a quick fix soon
🙏 1
this has been bugging me for a while
f
@happy-kitchen-89482 I’ve been pulled onto some other work, so I’ve not been able to put much time towards this work, unfortunately
👍 1
f
@happy-kitchen-89482 were you ever able to get that quick fix out?
h
Alas no. To be sure I remember the exact issue, this is about having the generated setup.py able to use a generated version?
f
Yeah in a setup.py generate by pants through the python_distribution functionality
h
Right, yeah, have not gotten around to that
Happy to guide someone on how to do it, wouldn't be hard
b
Hi there @happy-kitchen-89482 👋 is there already an update on this? I'd also like to use a generated version in a generated setup.py for the
python_distribution
target. Would also be happy to contribute this under some guidance! Thanks! 🙏
👋 1
s
We do the following: Create file `.pants.bootstrap`:
Copy code
#!/bin/sh

VERSION="${VERSION:-$(git describe --tags --dirty --match "[0-9\.]*" || echo 0.0.1)}"
export VERSION
then in `BUILD`:
Copy code
python_distribution(
    name="mydist",
    ...
    provides=python_artifact(
        name="mydist",
        version=env("VERSION"),
        ...
    ),
)
We use this with docker images, but it should work with python_distribution too
👍 2
h
That's actually a neat hack!
@brave-smartphone-45640 could something like this work for your needs?
@square-psychiatrist-19087 would you be open to writing a short blog post about this for https://www.pantsbuild.org/blog? We get questions like this from time to time, and it would be good to have it written down somewhere permanent
s
Yep, I can do that
h
That would be great, thanks! It's in https://github.com/pantsbuild/pantsbuild.org/tree/main/blog - so it's just a PR to that repo, when you have a spare moment
b
@square-psychiatrist-19087 Thanks for your input! I'll have a look at how we could use a similar setup in our project 👍
s
@fresh-continent-76371 take a look at the thread, maybe you don't need a custom plugin to read the VERSION, and the hack above is enough
f
:-) sadly we do need a new plugin. Because every package has a different version. Which means that you would have to execute VERSION=$(cat path/to/package/VERSION) pants publish path/to/package:: For every package. Not workable and not ideal. I do have a custom plugin that alters the reads the local VERSION.. README.md and CHANGELOG.md and returns a replacement python_artifact() which is what @brave-smartphone-45640 would be looking for, However we now have all the backed packaging needing it (:-) ) docker, python, scala, shell, and our rust efforts. I'm happy to share the setupkwargs plugin.
s
Cool!
h
@fresh-continent-76371 this is cool! could you make that public, for others to use? Or potentially even upstream it to pantsbuild/pants ?
f
Absolutley @happy-kitchen-89482 - that is my goal - i will need some help though (over here please)... https://pantsbuild.slack.com/archives/C01CQHVDMMW/p1709979771627989 • I have shared
setup_kwargs(..)
plugin. But this is limiting, only works for python_distrubutions(.. ) ◦ https://github.com/rbuckland/pants-playground ◦ I would not put that into
pantsbuild/pants
as it is limiting, the better way is my goal of
read_file_contents(filename=..., [regex=...])
will be MUCH nicer. ◦ e..g
Copy code
## read_file_contents proposal
python_distribution(
    name = "simple_lib_wheel",
    dependencies = [
        "./src:sources",
        ":reqs"
    ],
    generate_setup = True,
    provides = python_artifact(
        name = "my_simple_lib",
        version_file = read_file_contents(filename="VERSION"),                   
        long_description_file = read_file_contents(filenames=["README.md","CHANGELOG.md"]),
    ),
)
@brave-smartphone-45640 - if it was not obvious - this repo has the working example. https://github.com/rbuckland/pants-playground
s
would you be open to writing a short blog post about this
@happy-kitchen-89482 Here you go https://github.com/pantsbuild/pantsbuild.org/pull/197/files
🎉 1