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

Custom files under dist-info as part of a build hook #620

Closed
cadojo opened this issue Nov 25, 2022 · 23 comments · Fixed by #621
Closed

Custom files under dist-info as part of a build hook #620

cadojo opened this issue Nov 25, 2022 · 23 comments · Fixed by #621

Comments

@cadojo
Copy link

cadojo commented Nov 25, 2022

I'm working on a package manager project that will add a unique identifier to each package. The [project] metadata is standardized, as are the contents under the METADATA file in dist-info/. I would like to include the UUID of each package under a new file called dist-info/UUID, or similar.

Can this be done using a Hatchlings build hook? Reading through the documentation, I'm not sure if "metadata" refers to the pyproject.toml metadata, or the metadata as compiled by the build tool, and written to dist-info.

@ofek
Copy link
Sponsor Collaborator

ofek commented Nov 25, 2022

@cadojo
Copy link
Author

cadojo commented Nov 26, 2022

Is the "mapping" referenced in the documentation "pyproject.toml key" to "filename under extra_metadata? For example, is this the intended usage?

[project]
...

[tool.my_tool.metadata]
uuid = "1234"

[tool.hatch.build.targets.wheel.extra_metadata]
"tool.my_tool.metadata.uuid" = "UUID"

@ofek
Copy link
Sponsor Collaborator

ofek commented Nov 26, 2022

[tool.hatch.build.targets.wheel.extra-metadata]
"./UUID.txt" = "UUID"

Your hook would need to write to the source location e.g. ./UUID.txt. If that is only known at build time then I'll need to add support to https://hatch.pypa.io/latest/plugins/builder/wheel/#build-data

@ofek
Copy link
Sponsor Collaborator

ofek commented Nov 26, 2022

or just commit a file...

@ofek
Copy link
Sponsor Collaborator

ofek commented Nov 26, 2022

You're the first to use this feature btw, hooray for premature optimization 😄

@cadojo
Copy link
Author

cadojo commented Nov 26, 2022

Well first off thanks for the quick replies!

I do want the data to live in the pyproject.toml file, not in a committed or external file. I posted on discuss.python.org about adding custom metadata to the ***.dist-info/METADATA file. The specific project I'm working on — a toy package manager that is registry-focused, as opposed to index-focused — would ideally read metadata for installed packages from the standard .dist-info directory. See the discussion below for motivation for the "feature" or "functionality" I'm talking about in this issue.

https://discuss.python.org/t/add-custom-metadata-to-wheel-file/21410/2

No need to add support to build-data, if this is possible with a custom hook I'm happy to write my own and release it as a plugin!

@ofek
Copy link
Sponsor Collaborator

ofek commented Nov 26, 2022

Thanks for the context!

I do want the data to live in the pyproject.toml file, not in a committed or external file [...] No need to add support to build-data

Since the option reads the sources from disk this implies your hook would write to a temporary file which is only known at build time which means I do need to add support to build-data

@cadojo
Copy link
Author

cadojo commented Nov 26, 2022

Hm okay, I believe you but I do not understand why more changes are necessary — of course, my understanding of Python packaging has a ton of room to grow, so that's not surprising.

I can't do this because the [project] key is standardized, but what I want to do is add a field under the [project] key called uuid, right alongside the package name. Instead of putting uuid in the standard [project] key, I'll put it under [tool.my_tool.project] or something.

Which step in this process requires the changes to build-data? Some data is pulled from pyproject.toml and stuck into dist-info already, like the package name and description. Why can't this new field, uuid, be written to a file under dist-info in the same way?

@ofek
Copy link
Sponsor Collaborator

ofek commented Nov 26, 2022

To illustrate, please:

  1. Create a ./UUID.txt
  2. Add:
    [tool.hatch.build.targets.wheel.extra-metadata]
    "./UUID.txt" = "UUID"
  3. Run hatch build
  4. Look inside the dist/*.whl ZIP file
  5. Finally, imagine how that would work if there was no existing file

@cadojo
Copy link
Author

cadojo commented Nov 26, 2022

I've done so, and I think I understand. Right now, extra-metadata provides the ability to copy <FILE> to dist-info/extra_metadata/<FILE>. And I'm asking for copying information from pyproject.toml to dist-info/extra_metadata/<NEW_FILE>. Is that correct?

If so, I didn't post this issue to create work for you. I understand if this is a feature hatch will not support, though I'm happy to see your Discourse post.

➜ tree python_pkg-0.1.0.dev0.dist-info 
python_pkg-0.1.0.dev0.dist-info
├── METADATA
├── RECORD
├── WHEEL
├── extra_metadata
│   └── UUID
└── licenses
    └── LICENSE

➜ cat python_pkg-0.1.0.dev0.dist-info/extra_metadata/UUID 
855f5fd5-12cd-4c2c-9cb7-ceb657731c58

@ofek
Copy link
Sponsor Collaborator

ofek commented Nov 26, 2022

Right now, extra-metadata provides the ability to copy <FILE> to dist-info/extra_metadata/<FILE>. And I'm asking for copying information from pyproject.toml to dist-info/extra_metadata/<NEW_FILE>. Is that correct?

Correct

I understand if this is a feature hatch will not support

I will, gimme a few hours

@ofek
Copy link
Sponsor Collaborator

ofek commented Nov 26, 2022

#621

No promises on a release timeline

@ofek ofek closed this as completed in #621 Nov 26, 2022
@cadojo
Copy link
Author

cadojo commented Nov 26, 2022

Thank you!!!

@ofek
Copy link
Sponsor Collaborator

ofek commented Nov 26, 2022

Anytime!

@pradyunsg
Copy link
Member

FWIW, the dist-info directory is supposed to be a flat set of files that is supposed to be modelled as a dictionary of "name": "text content" (at least, based off of my understanding from chats during the installer's API design discussions).

@ofek
Copy link
Sponsor Collaborator

ofek commented Nov 26, 2022

Perhaps we can standardize a user-defined extra_metadata directory

@pradyunsg
Copy link
Member

Perhaps -- but right now, that violates the designers' expectations for how files in dist-info are handled. I don't think any other backend allows injecting files in there directly, but it's probably worth disallowing folders to be cautious IMO. :)

@ofek
Copy link
Sponsor Collaborator

ofek commented Nov 26, 2022

Directories already exist inside there as of PEP 639 with licenses/

@cadojo
Copy link
Author

cadojo commented Nov 26, 2022

I'd also note that the use case I want (that motivates this issue) is just one more "name": "text" file. It could go anywhere, including top-level .dist-info, but I do think it makes sense to have non-standard files under an "extras" directory.

@cadojo
Copy link
Author

cadojo commented Nov 26, 2022

If directories are truly disallowed, perhaps user files could be placed at the top-level. Though as @ofek notes, the licenses directory already exists.

@pradyunsg
Copy link
Member

I will remind folks that PEP 639 is still in draft state.

@ofek
Copy link
Sponsor Collaborator

ofek commented Nov 26, 2022

Sure, but nobody has expressed any disagreement with a dedicated directory for licenses. If you personally do then you might want to bring it up on Discourse

@pradyunsg
Copy link
Member

pradyunsg commented Nov 26, 2022

Oh, I don't have a strong opinion but it's a thing we're changing and the design needs to consider/document the potential implications of that. I've already mentioned this, when I wrote the above comment, over on the discussion about designing the draft PEP: https://discuss.python.org/t/pep-639-round-2-improving-license-clarity-with-better-package-metadata/12622/86?u=pradyunsg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants