-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3dbc4e4
commit 62e8daf
Showing
3 changed files
with
63 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,71 @@ | ||
import shutil | ||
from pathlib import Path | ||
from contextlib import ExitStack, suppress | ||
|
||
from hatchling.metadata.plugin.interface import MetadataHookInterface | ||
|
||
|
||
# @dataclasses.dataclass | ||
# class Configuration: | ||
|
||
|
||
def readlines(filepath): | ||
with open(filepath) as file: | ||
yield from file | ||
|
||
|
||
class MetadataHook(MetadataHookInterface): | ||
def update(self, metadata): | ||
license_path = Path(self.root, self.config["license-file"]) | ||
license = license_path.read_text(encoding="utf-8") | ||
readme = Path(self.root, self.config["readme-file"]).read_text(encoding="utf-8") | ||
version = self.config["version-default"] | ||
""" | ||
Notes | ||
----- | ||
Example of configuration: | ||
{ | ||
'path': 'hatch/metadata.py', | ||
'license': {'file': '../LICENSE'}, | ||
'readme': {'file': '../README.md'}, | ||
'version-default': '0.0.0+unknown', | ||
'dependencies': {'file': 'requirements.in'}, | ||
'optional-dependencies': { | ||
'test': {'file': 'test-requirements.in'}, | ||
'sphinx': {'file': 'sphinx-requirements.in'}, | ||
} | ||
} | ||
""" | ||
|
||
# Retrieve LICENCE from root files | ||
license_filepath = self.config["license"]["file"] | ||
license = Path(self.root, license_filepath).read_text(encoding="utf-8") | ||
|
||
# Copy LICENCE file in `sdist` | ||
with open(Path(self.root, "LICENSE"), "w") as f: | ||
f.write(license) | ||
|
||
with suppress(FileNotFoundError): | ||
# Retrieve README from root files | ||
readme_filepath = self.config["readme"]["file"] | ||
readme = Path(self.root, readme_filepath).read_text(encoding="utf-8") | ||
|
||
# Retrieve VERSION from file created by the CI | ||
try: | ||
version = Path(self.root, "VERSION.txt").read_text(encoding="utf-8") | ||
except FileNotFoundError: | ||
version = self.config["version-default"] | ||
|
||
# Retrieve dependencies from requirements.in | ||
dependencies_filepath = self.config["dependencies"]["file"] | ||
dependencies = list(map(str.strip, readlines(dependencies_filepath))) | ||
|
||
# Retrieve optional dependencies from *-requirements.in | ||
optional_dependencies_label_to_filepath = self.config["optional-dependencies"] | ||
optional_dependencies = {} | ||
|
||
for label, filepath in optional_dependencies_label_to_filepath: | ||
optional_dependencies_filepath = filepath["file"] | ||
optional_dependencies[label] = list( | ||
map( | ||
str.strip, | ||
readlines(optional_dependencies_filepath), | ||
) | ||
) | ||
|
||
# Update metadata | ||
metadata["license"] = {"text": license, "content-type": "text/plain"} | ||
metadata["readme"] = {"text": readme, "content-type": "text/markdown"} | ||
metadata["version"] = version | ||
|
||
license_dest = Path(self.root, license_path.name) | ||
shutil.copy2(license_path, license_dest) | ||
|
||
# { | ||
# 'path': 'hatch/metadata.py', | ||
# 'license-file': '../LICENSE', | ||
# 'readme-file': '../README.md', | ||
# 'version-default': '0.0.0+unknown', | ||
# 'dependencies': {'file': 'requirements.in'}, | ||
# 'optional-dependencies': { | ||
# 'test': {'file': 'test-requirements.in'}, | ||
# 'sphinx': {'file': 'sphinx-requirements.in'} | ||
# } | ||
# } | ||
|
||
metadata["dependencies"] = list( | ||
map(str.strip, readlines(self.config["dependencies"])) | ||
) | ||
metadata["optional-dependencies"] = { | ||
label: list(map(str.strip, readlines(filepath))) | ||
for label, filepath in self.config["optional-dependencies"].items() | ||
} | ||
Metadata["dependencies"] = dependencies | ||
metadata["optional-dependencies"] = optional_dependencies |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters