https://pantsbuild.org/ logo
#general
Title
# general
b

bright-pilot-85305

11/13/2022, 9:02 PM
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

curved-television-6568

11/13/2022, 10:43 PM
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

bright-pilot-85305

11/14/2022, 4:10 PM
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

curved-television-6568

11/14/2022, 4:24 PM
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

happy-kitchen-89482

11/14/2022, 7:20 PM
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

bright-pilot-85305

11/15/2022, 12:38 AM
@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

happy-kitchen-89482

11/15/2022, 1:14 AM
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

future-oxygen-10553

11/15/2022, 3:37 PM
@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

faint-dress-64989

01/25/2023, 9:55 PM
@happy-kitchen-89482 were you ever able to get that quick fix out?
h

happy-kitchen-89482

01/26/2023, 2:04 AM
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

faint-dress-64989

01/26/2023, 2:55 PM
Yeah in a setup.py generate by pants through the python_distribution functionality
h

happy-kitchen-89482

01/26/2023, 9:09 PM
Right, yeah, have not gotten around to that
Happy to guide someone on how to do it, wouldn't be hard
b

brave-smartphone-45640

02/20/2024, 4:16 PM
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

square-psychiatrist-19087

02/20/2024, 6:14 PM
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

happy-kitchen-89482

02/21/2024, 1:42 AM
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

square-psychiatrist-19087

02/21/2024, 11:16 AM
Yep, I can do that
h

happy-kitchen-89482

02/21/2024, 2:28 PM
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

brave-smartphone-45640

02/21/2024, 2:57 PM
@square-psychiatrist-19087 Thanks for your input! I'll have a look at how we could use a similar setup in our project 👍
s

square-psychiatrist-19087

03/09/2024, 11:18 AM
@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

fresh-continent-76371

03/09/2024, 6:45 PM
:-) 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

square-psychiatrist-19087

03/09/2024, 6:47 PM
Cool!
h

happy-kitchen-89482

03/09/2024, 9:58 PM
@fresh-continent-76371 this is cool! could you make that public, for others to use? Or potentially even upstream it to pantsbuild/pants ?
f

fresh-continent-76371

03/09/2024, 10:20 PM
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
2 Views