Move version metadata key to setup.cfg (#65091)

* Move version to setup.cfg
* Move python_requires to setup.cfg
* Add script to validate project metadata
* Add dedicated pre-commit hook
This commit is contained in:
Marc Mueller 2022-01-28 13:36:20 +01:00 committed by GitHub
parent a9cc35d6b6
commit 75f39f9ca2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 55 additions and 5 deletions

View File

@ -115,3 +115,10 @@ repos:
language: script
types: [text]
files: ^(homeassistant/.+/(manifest|strings)\.json|\.coveragerc|\.strict-typing|homeassistant/.+/services\.yaml|script/hassfest/.+\.py)$
- id: hassfest-metadata
name: hassfest-metadata
entry: script/run-in-env.sh python3 -m script.hassfest -p metadata
pass_filenames: false
language: script
types: [text]
files: ^(script/hassfest/.+\.py|homeassistant/const\.py$|setup\.cfg)$

View File

@ -12,6 +12,7 @@ from . import (
dhcp,
json,
manifest,
metadata,
mqtt,
mypy_config,
requirements,
@ -41,6 +42,7 @@ INTEGRATION_PLUGINS = [
HASS_PLUGINS = [
coverage,
mypy_config,
metadata,
]
ALL_PLUGIN_NAMES = [

View File

@ -0,0 +1,31 @@
"""Package metadata validation."""
import configparser
from homeassistant.const import REQUIRED_PYTHON_VER, __version__
from .model import Config, Integration
def validate(integrations: dict[str, Integration], config: Config) -> None:
"""Validate project metadata keys."""
metadata_path = config.root / "setup.cfg"
parser = configparser.ConfigParser()
parser.read(metadata_path)
try:
if parser["metadata"]["version"] != __version__:
config.add_error(
"metadata", f"'metadata.version' value does not match '{__version__}'"
)
except KeyError:
config.add_error("metadata", "No 'metadata.version' key found!")
required_py_version = f">={'.'.join(map(str, REQUIRED_PYTHON_VER))}"
try:
if parser["options"]["python_requires"] != required_py_version:
config.add_error(
"metadata",
f"'options.python_requires' value doesn't match '{required_py_version}",
)
except KeyError:
config.add_error("metadata", "No 'options.python_requires' key found!")

View File

@ -117,7 +117,18 @@ def write_version(version):
)
with open("homeassistant/const.py", "wt") as fil:
content = fil.write(content)
fil.write(content)
def write_version_metadata(version: Version) -> None:
"""Update setup.cfg file with new version."""
with open("setup.cfg") as fp:
content = fp.read()
content = re.sub(r"(version\W+=\W).+\n", f"\\g<1>{version}\n", content, count=1)
with open("setup.cfg", "w") as fp:
fp.write(content)
def main():
@ -142,6 +153,7 @@ def main():
assert bumped > current, "BUG! New version is not newer than old version"
write_version(bumped)
write_version_metadata(bumped)
if not arguments.commit:
return

View File

@ -1,4 +1,5 @@
[metadata]
version = 2022.3.0.dev0
license = Apache-2.0
license_file = LICENSE.md
platforms = any
@ -15,6 +16,7 @@ classifier =
Topic :: Home Automation
[options]
python_requires = >=3.9.0
install_requires =
aiohttp==3.8.1
astral==2.2

View File

@ -31,11 +31,8 @@ PROJECT_URLS = {
PACKAGES = find_packages(exclude=["tests", "tests.*"])
MIN_PY_VERSION = ".".join(map(str, hass_const.REQUIRED_PYTHON_VER))
setup(
name=PROJECT_PACKAGE_NAME,
version=hass_const.__version__,
url=PROJECT_URL,
download_url=DOWNLOAD_URL,
project_urls=PROJECT_URLS,
@ -44,7 +41,6 @@ setup(
packages=PACKAGES,
include_package_data=True,
zip_safe=False,
python_requires=f">={MIN_PY_VERSION}",
test_suite="tests",
entry_points={"console_scripts": ["hass = homeassistant.__main__:main"]},
)