pear.py: new generator
continuous-integration/drone/push Build is failing Details

This commit is contained in:
Anna “CyberTailor” 2023-07-02 06:14:24 +05:00
parent f572e2fbef
commit 1b88f68aff
Signed by: CyberTaIlor
GPG Key ID: E7B76EDC50864BB1
12 changed files with 1491 additions and 2 deletions

View File

@ -20,6 +20,7 @@ import gentle.generators.composer
import gentle.generators.doap
import gentle.generators.hpack
import gentle.generators.npm
import gentle.generators.pear
import gentle.generators.python.pkg_info
import gentle.generators.python.pyproject
import gentle.generators.shards

60
gentle/generators/pear.py Normal file
View File

@ -0,0 +1,60 @@
# SPDX-License-Identifier: WTFPL
# SPDX-FileCopyrightText: 2022 Anna <cyber@sysrq.in>
# No warranty
"""
Metadata XML generator for PHP PEAR and PECL.
The following attributes are supported:
* Upstream maintainer(s)
"""
import logging
import xml.etree.ElementTree as ET
from pathlib import Path
from gentle.generators import AbstractGenerator
from gentle.metadata import MetadataXML
from gentle.metadata.types import MaintainerStatus, Person
logger = logging.getLogger("pear")
class PearGenerator(AbstractGenerator):
def __init__(self, srcdir: Path):
self.package_xmls = list(srcdir.glob("package*.xml"))
def update_metadata_xml(self, mxml: MetadataXML) -> None:
try:
xml: ET.ElementTree = ET.parse(self.package_xmls[0])
except ET.ParseError:
return
pear_ns = "http://pear.php.net/dtd/package-2.0"
ns = {"pear": pear_ns}
if xml.getroot().tag != f"{{{pear_ns}}}package":
return
for lead in xml.findall("pear:lead", ns):
if (lead_name := lead.find("pear:name", ns)) is None:
continue
person = Person("".join(lead_name.itertext()))
if (lead_email := lead.find("pear:email", ns)) is not None:
person.email = "".join(lead_email.itertext())
if (lead_active := lead.find("pear:active", ns)) is not None:
match "".join(lead_active.itertext()):
case "yes":
person.status = MaintainerStatus.ACTIVE
case "no":
person.status = MaintainerStatus.INACTIVE
logger.info("Found upstream maintainer: %s", person)
mxml.add_upstream_maintainer(person)
@property
def active(self) -> bool:
return len(self.package_xmls) == 1

View File

@ -6,6 +6,15 @@
import xml.etree.ElementTree as ET
from dataclasses import dataclass, field
from enum import Enum, auto
class MaintainerStatus(Enum):
""" Maintainer status enum """
NONE = auto()
ACTIVE = auto()
INACTIVE = auto()
@dataclass
@ -14,13 +23,21 @@ class Person:
name: str = field(default="", compare=False)
email: str = ""
status: MaintainerStatus = MaintainerStatus.NONE
def to_xml(self, attrib: dict | None = None) -> ET.Element:
"""
:param attrib: attributes for the ``<maintainer>`` tag
:return: :file:`metadata.xml` respresentation of a person
"""
result = ET.Element("maintainer", attrib=attrib or {})
attrib = attrib or {}
match self.status:
case MaintainerStatus.ACTIVE:
attrib["status"] = "active"
case MaintainerStatus.INACTIVE:
attrib["status"] = "inactive"
result = ET.Element("maintainer", attrib=attrib)
if self.name:
name_elem = ET.SubElement(result, "name")
name_elem.text = self.name

View File

@ -71,7 +71,7 @@ def extract_name_email(author: str) -> Person | None:
:param author: string in the ``name <email>`` format
>>> extract_name_email("Foo Bar <foobar@example.com>")
Person(name='Foo Bar', email='foobar@example.com')
Person(name='Foo Bar', email='foobar@example.com', status=<MaintainerStatus.NONE: 1>)
>>> extract_name_email("Foo Bar") is None
True
"""

0
tests/pear/__init__.py Normal file
View File

View File

@ -0,0 +1,32 @@
<pkgmetadata>
<upstream>
<maintainer status="inactive">
<name>Greg Beaver</name>
<email>cellog@php.net</email>
</maintainer>
<maintainer status="inactive">
<name>Pierre-Alain Joye</name>
<email>pierre@php.net</email>
</maintainer>
<maintainer status="inactive">
<name>Stig Bakken</name>
<email>stig@php.net</email>
</maintainer>
<maintainer status="inactive">
<name>Tomas V.V.Cox</name>
<email>cox@idecnet.com</email>
</maintainer>
<maintainer status="inactive">
<name>Helgi Thormar</name>
<email>dufuz@php.net</email>
</maintainer>
<maintainer status="inactive">
<name>Christian Weiske</name>
<email>cweiske@php.net</email>
</maintainer>
<maintainer status="active">
<name>Chuck Burgess</name>
<email>ashnazg@php.net</email>
</maintainer>
</upstream>
</pkgmetadata>

View File

@ -0,0 +1,3 @@
SPDX-FileCopyrightText: 2023 Anna <cyber@sysrq.in>
SPDX-License-Identifier: CC0-1.0

1326
tests/pear/pear/package2.xml Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,9 @@
SPDX-FileCopyrightText: 2023 Christian Weiske <cweiske@php.net>
SPDX-FileCopyrightText: 2023 Chuck Burgess <ashnazg@php.net>
SPDX-FileCopyrightText: 2023 Greg Beaver <cellog@php.net>
SPDX-FileCopyrightText: 2023 Helgi Thormar <dufuz@php.net>
SPDX-FileCopyrightText: 2023 Pierre-Alain Joye <pierre@php.net>
SPDX-FileCopyrightText: 2023 Stig Bakken <stig@php.net>
SPDX-FileCopyrightText: 2023 Tomas V.V.Cox <cox@idecnet.com>
SPDX-License-Identifier: BSD

View File

View File

View File

@ -0,0 +1,41 @@
# SPDX-License-Identifier: WTFPL
# SPDX-FileCopyrightText: 2023 Anna <cyber@sysrq.in>
# No warranty
from copy import deepcopy
from pathlib import Path
import pytest
from gentle.generators.pear import PearGenerator
from gentle.metadata import MetadataXML
from tests.utils import compare_mxml
def test_pkg_none(mxml: MetadataXML):
gen = PearGenerator(Path(__file__).parent / "pkg_none")
assert not gen.active
def test_pkg_empty(mxml: MetadataXML):
gen = PearGenerator(Path(__file__).parent / "pkg_empty")
assert gen.active
mxml_old = deepcopy(mxml)
gen.update_metadata_xml(mxml)
assert compare_mxml(mxml_old, mxml) == ""
@pytest.mark.parametrize("dirname", ["pear"])
def test_pkg(mxml: MetadataXML, dirname: str):
gen = PearGenerator(Path(__file__).parent / dirname)
assert gen.active
gen.update_metadata_xml(mxml)
with open(Path(__file__).parent / dirname / "metadata.xml") as file:
assert mxml.dumps() == file.read().rstrip()
mxml_prev = deepcopy(mxml)
gen.update_metadata_xml(mxml)
assert compare_mxml(mxml_prev, mxml) == ""