#!/usr/bin/python3
"""
pypi2pclos: write a PCLinuxOS python-<module> spec file for a PyPI project.

Downloads the project's sdist into %_sourcedir (if it is not there yet),
reads its metadata (PKG-INFO, pyproject.toml, setup.cfg, *.egg-info) without
running any of its code, and writes %_specdir/pclos-python-<name>.spec
following the standard PCLinuxOS python module spec layout. Review the result
before building: Summary, License, %description and the module names given to
%pyproject_save_files are best guesses.

usage: pypi2pclos [--force] [-o SPEC] <project> [version]
"""

import argparse
import configparser
import datetime
import email.parser
import hashlib
import io
import json
import os
import re
import subprocess
import sys
import tarfile
import tempfile
import textwrap
import tomllib
import urllib.error
import urllib.request
import zipfile

import rpm

from packaging.markers import default_environment
from packaging.requirements import InvalidRequirement, Requirement
from packaging.specifiers import SpecifierSet
from packaging.utils import canonicalize_name
from packaging.version import InvalidVersion, Version

API_URL = "https://pypi.org/pypi"
PYPI_SOURCE_URL = "https://files.pythonhosted.org/packages/source"
USER_AGENT = "pypi2pclos (PCLinuxOS python packaging)"
MAX_WHEEL_SIZE = 20 * 1024 * 1024

SDIST_EXTENSIONS = ("tar.gz", "tar.bz2", "tar.xz", "tgz", "zip")
LICENSE_PREFIXES = ("LICENSE", "LICENCE", "COPYING", "NOTICE", "UNLICENSE")
DOC_PREFIXES = ("README", "CHANGELOG", "CHANGES", "HISTORY", "NEWS")
NOT_MODULES = {"test", "tests", "testing", "doc", "docs", "example", "examples", "bench",
               "benchmark", "benchmarks", "script", "scripts", "tools", "build", "dist",
               "ci", "requirements", "conftest", "setup", "noxfile", "tasks", "fabfile"}
COMPILED_EXTENSIONS = (".c", ".cc", ".cpp", ".cxx", ".pyx", ".f", ".f90", ".rs")
COMPILED_BACKENDS = ("mesonpy", "scikit_build_core", "maturin", "setuptools_rust", "pybind11")
RUST_HINTS = ("maturin", "setuptools-rust", "setuptools_rust")

CLASSIFIER_LICENSES = {
    "MIT License": "MIT",
    "MIT No Attribution License (MIT-0)": "MIT-0",
    "Apache Software License": "Apache-2.0",
    "BSD License": "BSD-3-Clause",
    "ISC License (ISCL)": "ISC",
    "Python Software Foundation License": "PSF-2.0",
    "Mozilla Public License 2.0 (MPL 2.0)": "MPL-2.0",
    "The Unlicense (Unlicense)": "Unlicense",
    "Zope Public License": "ZPL-2.1",
    "GNU General Public License v2 (GPLv2)": "GPL-2.0-only",
    "GNU General Public License v2 or later (GPLv2+)": "GPL-2.0-or-later",
    "GNU General Public License v3 (GPLv3)": "GPL-3.0-only",
    "GNU General Public License v3 or later (GPLv3+)": "GPL-3.0-or-later",
    "GNU Lesser General Public License v2 (LGPLv2)": "LGPL-2.1-only",
    "GNU Lesser General Public License v2 or later (LGPLv2+)": "LGPL-2.1-or-later",
    "GNU Lesser General Public License v3 (LGPLv3)": "LGPL-3.0-only",
    "GNU Lesser General Public License v3 or later (LGPLv3+)": "LGPL-3.0-or-later",
    "GNU Affero General Public License v3": "AGPL-3.0-only",
    "GNU Affero General Public License v3 or later (AGPLv3+)": "AGPL-3.0-or-later",
}

KNOWN_SPDX = set(CLASSIFIER_LICENSES.values()) | {
    "0BSD", "BSD-2-Clause", "BSD-3-Clause", "Apache-2.0", "MIT", "Python-2.0", "PSF-2.0", "MPL-2.0",
    "LGPL-2.0-or-later", "GPL-2.0-or-later", "HPND", "CC0-1.0", "BSL-1.0", "Zlib", "EPL-2.0", "ISC"}

TEMPLATE = """\
%global pypi_name {pypi_name}
{globals}{extras_comment}
Name:           python-%{{pypi_name}}
Version:        {version}
Release:        %mkrel 1
Summary:        {summary}
Group:          Development/Python
License:        {license}
URL:            https://pypi.org/project/%{{pypi_name}}/
Source:         {source}

{buildarch}BuildRequires:  python3-devel

%description
{description}

%package -n     python3-%{{pypi_name}}
Summary:        %{{summary}}
%{{?python_provide:%python_provide python3-%{{pypi_name}}}}

%description -n python3-%{{pypi_name}}
{description}

%prep
%autosetup -n {setup_dir} -p1

%generate_buildrequires
%pyproject_buildrequires

%build
%pyproject_wheel

%install
%pyproject_install
%pyproject_save_files {save_files}

%check
%pyproject_check_import

%files -n python3-%{{pypi_name}} -f %{{pyproject_files}}
{files}
%changelog
* {date} {packager} - {version}-{release}
- New package
"""


warnings = []


def warn(message: str):
    warnings.append(message)


def rpm_eval(expr: str, *defines: str) -> str:
    cmd = ["rpm"]
    for define in defines:
        cmd += ["--define", define]
    return subprocess.run(cmd + ["--eval", expr], capture_output=True, text=True, check=True).stdout.strip()


def fetch(url: str, method: str = "GET") -> bytes:
    request = urllib.request.Request(url, headers={"User-Agent": USER_AGENT}, method=method)
    with urllib.request.urlopen(request) as response:
        return response.read()


def spec_escape(text: str) -> str:
    return text.replace("%", "%%")


# --- versions and names ----------------------------------------------------

def rpm_version(version: Version) -> str:
    """PEP 440 -> RPM: 1.0rc1 -> 1.0~rc1, 1.0.dev2 -> 1.0~~dev2, 1.0.post1 stays."""

    if version.local:
        sys.exit(f"local version {version} cannot be packaged")
    if version.epoch:
        warn(f"version has a PEP 440 epoch ({version.epoch}!): consider an Epoch tag")
    result = ".".join(map(str, version.release))
    if version.pre:
        result += f"~{version.pre[0]}{version.pre[1]}"
    if version.post is not None:
        result += f".post{version.post}"
    if version.dev is not None:
        result += f"~~dev{version.dev}"
    return result


def split_sdist_filename(filename: str, version: str) -> tuple[str, str, str]:
    """foo_bar-1.0.tar.gz -> ("foo_bar", "1.0", "tar.gz")"""

    for ext in SDIST_EXTENSIONS:
        if filename.endswith("." + ext):
            stem = filename[:-len(ext) - 1]
            break
    else:
        sys.exit(f"unknown sdist format: {filename}")
    if stem.endswith("-" + version):
        return stem[:-len(version) - 1], version, ext
    name, _, file_version = stem.rpartition("-")
    return name, file_version, ext


def provided_version(text: str) -> Version | None:
    """Version of an RPM python3dist() provide: 2~rc1 -> 2rc1, 2~~dev1 -> 2.dev1."""

    try:
        return Version(text.replace("~~", ".").replace("~", ""))
    except InvalidVersion:
        return None


# --- sdist and wheel inspection ---------------------------------------------

def extract_sdist(path: str, dest: str) -> str:
    """Unpack an (untrusted) sdist into dest and return its top-level directory."""

    if path.endswith(".zip"):
        with zipfile.ZipFile(path) as archive:
            for name in archive.namelist():
                if name.startswith("/") or ".." in name.split("/"):
                    sys.exit(f"refusing to unpack {path}: unsafe member {name!r}")
            archive.extractall(dest)
    else:
        with tarfile.open(path) as archive:
            archive.extractall(dest, filter="data")
    entries = os.listdir(dest)
    if len(entries) != 1 or not os.path.isdir(os.path.join(dest, entries[0])):
        sys.exit(f"{os.path.basename(path)} does not unpack into a single directory: write the spec by hand")
    return entries[0]


def read_text(path: str) -> str | None:
    try:
        with open(path, encoding="utf-8", errors="replace") as f:
            return f.read()
    except (FileNotFoundError, IsADirectoryError):
        return None


def find_egg_info(srcdir: str) -> str | None:
    for base in (srcdir, os.path.join(srcdir, "src")):
        if os.path.isdir(base):
            for entry in sorted(os.listdir(base)):
                if entry.endswith(".egg-info") and os.path.isdir(os.path.join(base, entry)):
                    return os.path.join(base, entry)
    return None


def parse_requires_txt(text: str, env: dict) -> list[str]:
    """setuptools *.egg-info/requires.txt: keep the base and true [:marker] sections."""

    reqs = []
    keep = True
    for line in text.splitlines():
        line = line.strip()
        if not line or line.startswith("#"):
            continue
        if line.startswith("[") and line.endswith("]"):
            extra, _, marker = line[1:-1].partition(":")
            keep = not extra and (not marker or Requirement(f"x; {marker}").marker.evaluate(env))
            continue
        if keep:
            reqs.append(line)
    return reqs


def entry_point_scripts(text: str | None) -> list[str]:
    if not text:
        return []
    parser = configparser.ConfigParser(delimiters=("=",))
    parser.optionxform = str
    try:
        parser.read_string(text)
    except configparser.Error:
        return []
    scripts = []
    for section in ("console_scripts", "gui_scripts"):
        if parser.has_section(section):
            scripts += parser.options(section)
    return scripts


def wheel_contents(data: bytes) -> tuple[list[str], list[str], bool]:
    """Top-level importable names, scripts and whether data files land outside site-packages."""

    modules, scripts, extra_data = set(), [], False
    with zipfile.ZipFile(io.BytesIO(data)) as wheel:
        for name in wheel.namelist():
            top, _, rest = name.partition("/")
            if top.endswith(".dist-info"):
                if rest == "entry_points.txt":
                    scripts += entry_point_scripts(wheel.read(name).decode("utf-8", "replace"))
                continue
            if top.endswith(".data"):
                kind, _, script = rest.partition("/")
                if kind == "scripts" and script:
                    scripts.append(script)
                elif kind in ("data", "headers"):
                    extra_data = True
                continue
            if rest:
                modules.add(top)
            elif top.endswith(".py"):
                modules.add(top[:-3])
    return sorted(modules), sorted(set(scripts)), extra_data


def guess_modules(srcdir: str, pyproject: dict, egg_info: str | None, pypi_name: str) -> list[str]:
    """Import names for %pyproject_save_files when no pure wheel is available."""

    tool = pyproject.get("tool", {})
    names = set()

    setuptools = tool.get("setuptools", {})
    if isinstance(setuptools.get("packages"), list):
        names.update(p.split(".")[0] for p in setuptools["packages"] if isinstance(p, str))
    names.update(m for m in setuptools.get("py-modules", []) if isinstance(m, str))
    if isinstance(tool.get("flit", {}).get("module", {}).get("name"), str):
        names.add(tool["flit"]["module"]["name"])
    hatch_packages = tool.get("hatch", {}).get("build", {}).get("targets", {}).get("wheel", {}).get("packages", [])
    names.update(os.path.basename(p.rstrip("/")) for p in hatch_packages if isinstance(p, str))
    maturin_module = tool.get("maturin", {}).get("module-name")
    if isinstance(maturin_module, str):
        names.add(maturin_module.split(".")[0])
    if names:
        return sorted(names)

    if egg_info:
        top_level = read_text(os.path.join(egg_info, "top_level.txt"))
        if top_level:
            names.update(line.strip() for line in top_level.splitlines() if line.strip())
            if names:
                return sorted(n.split("/")[0] for n in names)

    base = os.path.join(srcdir, "src") if os.path.isdir(os.path.join(srcdir, "src")) else srcdir
    for entry in sorted(os.listdir(base)):
        path = os.path.join(base, entry)
        if entry.startswith((".", "_")) and entry != "__init__.py" or entry.lower() in NOT_MODULES:
            continue
        if os.path.isdir(path) and os.path.exists(os.path.join(path, "__init__.py")):
            names.add(entry)
        elif entry.endswith(".py") and entry[:-3].lower() not in NOT_MODULES:
            names.add(entry[:-3])
    if names:
        return sorted(names)

    fallback = pypi_name.replace("-", "_")
    warn(f"could not find the module names: guessed {fallback!r} for %pyproject_save_files")
    return [fallback]


def sdist_scripts(srcdir: str, pyproject: dict, egg_info: str | None) -> list[str]:
    project = pyproject.get("project", {})
    scripts = list(project.get("scripts", {})) + list(project.get("gui-scripts", {}))
    scripts += list(pyproject.get("tool", {}).get("poetry", {}).get("scripts", {}))
    if egg_info:
        scripts += entry_point_scripts(read_text(os.path.join(egg_info, "entry_points.txt")))
    setup_cfg = read_text(os.path.join(srcdir, "setup.cfg"))
    if setup_cfg:
        parser = configparser.ConfigParser()
        try:
            parser.read_string(setup_cfg)
            for key in ("console_scripts", "gui_scripts"):
                value = parser.get("options.entry_points", key, fallback="")
                scripts += [line.split("=")[0].strip() for line in value.splitlines() if "=" in line]
        except configparser.Error:
            pass
    if not scripts and "entry_points" in (read_text(os.path.join(srcdir, "setup.py")) or ""):
        warn("setup.py declares entry_points: check for scripts installed in %{_bindir}")
    return sorted(set(scripts))


def compiled_sources(srcdir: str) -> bool:
    for root, dirs, files in os.walk(srcdir):
        dirs[:] = [d for d in dirs if d.lower() not in NOT_MODULES and not d.startswith(".")]
        if any(f.endswith(COMPILED_EXTENSIONS) for f in files):
            return True
    return False


# --- metadata ---------------------------------------------------------------

def guess_license(pkg_info, info: dict) -> str:
    expression = pkg_info.get("License-Expression") or info.get("license_expression")
    if expression:
        return expression.strip()
    field = (pkg_info.get("License") or info.get("license") or "").strip()
    if "\n" in field or len(field) > 60 or field.upper() == "UNKNOWN":
        field = ""
    if field and all(i.strip("()") in KNOWN_SPDX for i in re.split(r"\s+(?:AND|OR|WITH)\s+", field)):
        return field
    classifiers = pkg_info.get_all("Classifier") or info.get("classifiers") or []
    licenses = [CLASSIFIER_LICENSES[c.split(" :: ")[-1]] for c in classifiers
                if c.startswith("License :: OSI Approved ::") and c.split(" :: ")[-1] in CLASSIFIER_LICENSES]
    if licenses:
        licenses = list(dict.fromkeys(licenses))
        warn(f"License guessed from trove classifiers ({' OR '.join(licenses)}"
             f"{', License field says ' + repr(field) if field else ''}): check the licence files")
        return " OR ".join(licenses)
    if field:
        warn(f"License taken from the free-text License field ({field!r}): check it is SPDX")
        return field
    warn("no license metadata found: fill in License by hand")
    return "FIXME"


MD_LINK = re.compile(r"!?\[([^\]]*)\]\([^)]*\)")
RST_LINK = re.compile(r"`([^`<]+?)\s*<[^>]+>`_+")
INLINE = re.compile(r"(\*\*|__|``|`|\*)(.+?)\1")
MD_REF_LINK = re.compile(r"\[([^\]]+)\](?:\[[^\]]*\])?(?![(:])")
RST_FOOTNOTE = re.compile(r"\s*\[#?\w*\]_")
BULLET = re.compile(r"^\s*[-*+]\s+")
UNDERLINE = re.compile(r"[=\-~^*#+`'\".:_]{3,}")
STOP_HEADINGS = re.compile(r"(install|usage|example|quick ?start|getting started|licen[cs]e|contribut|"
                           r"change ?log|changes|history|documentation|demo|requirements|support|credits)", re.I)
RST_NAMED = re.compile(r"`([^`<]+)`_+")
RST_REF = re.compile(r"\b(\w+)_(?=\W|$)")
FENCED = re.compile(r"^[ \t]*(```|~~~).*?^[ \t]*\1[^\n]*$", re.M | re.S)


def clean_inline(text: str) -> str:
    text = RST_FOOTNOTE.sub("", text)
    text = MD_LINK.sub(r"\1", text)
    text = MD_REF_LINK.sub(r"\1", text)
    text = RST_LINK.sub(r"\1", text)
    text = RST_NAMED.sub(r"\1", text)
    text = RST_REF.sub(r"\1", text)
    text = INLINE.sub(r"\2", text)
    return re.sub(r"\s{2,}", " ", text)


def first_paragraph(text: str) -> str | None:
    """The first prose paragraph of a README / long description (plus the list it introduces)."""

    text = FENCED.sub("", text.replace("\r\n", "\n"))
    paragraphs = [[line for line in p.splitlines() if line.strip()] for p in re.split(r"\n\s*\n", text)]
    return next((result for _, result in prose_paragraphs(paragraphs)), None)


def prose_paragraphs(paragraphs: list[list[str]]):
    for index, lines in enumerate(paragraphs):
        if not lines:
            continue
        first = lines[0].lstrip()
        heading = (first.lstrip("#").strip() if first.startswith("#") else
                   lines[-2].strip() if len(lines) >= 2 and UNDERLINE.fullmatch(lines[-1].strip()) else None)
        if heading is not None and STOP_HEADINGS.match(heading):
            return
        if (heading is not None
                or first.startswith(("..", "<", "|", ":", "!", "[", ">", "{"))
                or BULLET.match(first) or re.match(r"\d+[.)]\s", first)
                or lines[0].startswith(("    ", "\t"))
                or any(UNDERLINE.fullmatch(line.strip()) for line in lines)):
            continue
        prose = clean_inline(" ".join(line.strip() for line in lines))
        introduces_list = (prose.endswith(":") and index + 1 < len(paragraphs)
                           and any(BULLET.match(l) for l in paragraphs[index + 1]))
        if (len(prose) < 40 and not introduces_list) or " " not in prose:
            continue
        result = textwrap.fill(prose, width=79, break_on_hyphens=False)
        if prose.endswith(":"):
            items = []
            for following in paragraphs[index + 1:]:
                bullets = [l for l in following if BULLET.match(l)]
                if not following or len(bullets) == 0:
                    break
                for line in following:
                    if BULLET.match(line):
                        items.append(clean_inline(BULLET.sub("", line)).strip())
                    elif items:
                        items[-1] += " " + clean_inline(line.strip())
            if items:
                result += "\n" + "\n".join(textwrap.fill(item, width=79, initial_indent="- ",
                                                          subsequent_indent="  ", break_on_hyphens=False)
                                            for item in items)
            else:
                result = result.rstrip(":") + "."
        yield index, result


def guess_description(srcdir: str, pkg_info, summary: str) -> str:
    sources = [os.path.join(srcdir, f) for f in sorted(os.listdir(srcdir)) if f.upper().startswith("README")]
    texts = [read_text(path) or "" for path in sources]
    texts.append(pkg_info.get_payload() or pkg_info.get("Description") or "")
    for text in texts:
        paragraph = first_paragraph(text)
        if paragraph:
            return spec_escape(paragraph)
    warn("no usable README paragraph: %description is just the Summary")
    return spec_escape(summary + "." if summary != "FIXME" else "FIXME")


# --- dependencies -----------------------------------------------------------

def installed_provides(name: str) -> list[tuple[Version | None, str]]:
    """(version, source rpm) of the installed packages providing name."""

    ts = rpm.TransactionSet()
    found = []
    for hdr in ts.dbMatch("providename", name):
        for prov_name, prov_version in zip(hdr[rpm.RPMTAG_PROVIDENAME], hdr[rpm.RPMTAG_PROVIDEVERSION]):
            if prov_name == name:
                found.append((provided_version(prov_version) if prov_version else None, hdr[rpm.RPMTAG_SOURCERPM] or ""))
    return found


def satisfied(providers: list[tuple[Version | None, str]], specifier: SpecifierSet) -> bool:
    if not providers:
        return False
    if not str(specifier):
        return True
    return any(v is not None and specifier.contains(v, prereleases=True) for v, _ in providers)


def repo_provides(names: list[str]) -> dict[str, list[tuple[Version | None, str]]]:
    """(version, source rpm) of the packages in the configured repositories providing each name."""

    if not names:
        return {}
    try:
        out = subprocess.run(["dnf", "repoquery", "-q", "--qf", "@@%{sourcerpm}\n%{provides}\n",
                              "--whatprovides", ",".join(names)],
                             capture_output=True, text=True, timeout=300).stdout
    except (OSError, subprocess.TimeoutExpired):
        return {}
    found = {}
    srpm = ""
    for line in out.splitlines():
        if line.startswith("@@"):
            srpm = line[2:].strip()
            continue
        name, _, version = line.partition(" = ")
        if name.strip() in names:
            found.setdefault(name.strip(), []).append(
                (provided_version(version.strip()) if version else None, srpm))
    return found


def newest(providers: list[tuple[Version | None, str]]) -> tuple[Version | None, str]:
    versioned = [p for p in providers if p[0] is not None]
    return max(versioned, key=lambda p: p[0]) if versioned else providers[0]


def spec_for(srpm: str) -> str:
    """The SPEC that builds a source rpm: python-rich-13.9.4-1pclos2025.src.rpm -> SPECS/pclos-python-rich.spec"""

    name = srpm.removesuffix(".src.rpm").rsplit("-", 2)[0] if srpm else ""
    if not name:
        return "update its SPEC"
    spec = os.path.join(rpm_eval("%_specdir"), f"pclos-{name}.spec")
    return spec if os.path.exists(spec) else f"update the {name} SPEC (pclos-{name}.spec not in %_specdir)"


def check_dependencies(build_requires: list[str], runtime_requires: list[str], self_name: str) -> list[str]:
    """Report lines for requirements that no installed package satisfies."""

    env = dict(default_environment(), extra="")
    wanted = {}
    runtime = set(runtime_requires)
    for text in build_requires + runtime_requires:
        try:
            req = Requirement(text)
        except InvalidRequirement:
            warn(f"cannot parse requirement {text!r}")
            continue
        if req.marker and not req.marker.evaluate(env):
            continue
        name = canonicalize_name(req.name)
        if name == self_name:
            continue
        if req.extras and text in runtime:
            warn(f"needs {req.name}[{','.join(sorted(req.extras))}]: the extras' own dependencies are not "
                 "pulled in automatically (no extras subpackages), add Requires: for them if needed")
        key = f"python3dist({name})"
        if key in wanted:
            wanted[key] = (wanted[key][0], wanted[key][1] & req.specifier)
        else:
            wanted[key] = (req.name, req.specifier)

    installed = {key: installed_provides(key) for key in wanted}
    missing = {key: value for key, value in wanted.items() if not satisfied(installed[key], value[1])}
    report = []
    if not missing:
        return report
    in_repo = repo_provides(sorted(missing))
    available, too_old, unpackaged = [], [], []
    for key in sorted(missing):
        if satisfied(in_repo.get(key, []), missing[key][1]):
            available.append(key)
        elif in_repo.get(key) or installed[key]:
            too_old.append(key)
        else:
            unpackaged.append(key)

    def line(key: str, note: str) -> str:
        return f"  {key} {missing[key][1]}".rstrip().ljust(50) + note

    if available:
        report.append("Build dependencies not installed but available (dnf builddep will install them):")
        report += [line(key, "") .rstrip() for key in available]
    if too_old:
        report.append("Build dependencies too old in the repository (update the SPEC):")
        for key in too_old:
            version, srpm = newest(in_repo.get(key) or installed[key])
            where = "repo" if in_repo.get(key) else "installed"
            report.append(line(key, f"  {where} has {version or '?'}  ->  {spec_for(srpm)}"))
    if unpackaged:
        report.append("Build dependencies not packaged yet:")
        report += [line(key, f"  ->  pypi2pclos {missing[key][0]}") for key in unpackaged]
    return report


# --- main -------------------------------------------------------------------

def main():
    parser = argparse.ArgumentParser(description="Write a PCLinuxOS spec file for a PyPI project.")
    parser.add_argument("project", help="PyPI project name")
    parser.add_argument("version", nargs="?", help="project version (default: latest release)")
    parser.add_argument("--force", action="store_true", help="overwrite an existing spec file")
    parser.add_argument("-o", "--output", help="write the spec here instead of %%_specdir/pclos-python-<name>.spec")
    args = parser.parse_args()

    url = f"{API_URL}/{args.project}/{args.version}/json" if args.version else f"{API_URL}/{args.project}/json"
    try:
        data = json.loads(fetch(url))
    except urllib.error.HTTPError as exc:
        sys.exit(f"PyPI lookup for {args.project!r} {args.version or ''} failed: {exc}")
    info = data["info"]
    pypi_version = info["version"]
    pypi_name = canonicalize_name(info["name"])

    sdists = [u for u in data["urls"] if u["packagetype"] == "sdist"]
    if not sdists:
        sys.exit(f"{info['name']} {pypi_version} has no sdist on PyPI: use the upstream archive and write the spec by hand")
    sdist = sdists[0]
    if sdist.get("yanked"):
        warn(f"{pypi_version} has been yanked on PyPI: {sdist.get('yanked_reason') or 'no reason given'}")
    wheels = [u for u in data["urls"] if u["packagetype"] == "bdist_wheel"]
    pure_wheels = [u for u in wheels if u["filename"].endswith("-none-any.whl")]
    platform_wheels = [u for u in wheels if not u["filename"].endswith("-none-any.whl")]

    sourcedir = rpm_eval("%_sourcedir")
    os.makedirs(sourcedir, exist_ok=True)
    spec_path = args.output or os.path.join(rpm_eval("%_specdir"), f"pclos-python-{pypi_name}.spec")
    os.makedirs(os.path.dirname(os.path.abspath(spec_path)), exist_ok=True)
    if os.path.exists(spec_path) and not args.force:
        sys.exit(f"{spec_path} already exists (use --force to overwrite)")

    sdist_path = os.path.join(sourcedir, sdist["filename"])
    sha256 = sdist["digests"]["sha256"]
    if os.path.exists(sdist_path):
        with open(sdist_path, "rb") as f:
            if hashlib.sha256(f.read()).hexdigest() != sha256:
                sys.exit(f"{sdist_path} exists but does not match the PyPI checksum: remove it and retry")
    else:
        print(f"Downloading {sdist['filename']}", file=sys.stderr)
        payload = fetch(sdist["url"])
        if hashlib.sha256(payload).hexdigest() != sha256:
            sys.exit(f"checksum mismatch downloading {sdist['filename']}")
        with open(sdist_path, "wb") as f:
            f.write(payload)

    # --- names and the Source/%autosetup lines
    srcname, file_version, ext = split_sdist_filename(sdist["filename"], pypi_version)
    try:
        version = rpm_version(Version(pypi_version))
    except InvalidVersion:
        sys.exit(f"{pypi_version!r} is not a PEP 440 version: write the spec by hand")
    globals_ = ""
    name_macro = "%{pypi_name}"
    if srcname != pypi_name:
        globals_ += f"%global srcname {srcname}\n"
        name_macro = "%{srcname}"
    version_macro = "%{version}"
    if version != file_version:
        globals_ += f"%global pypi_version {file_version}\n"
        version_macro = "%{pypi_version}"
    if version_macro == "%{version}" and ext == "tar.gz":
        source = "%{pypi_source}"
    else:
        source = f"%{{pypi_source {name_macro} {version_macro}{'' if ext == 'tar.gz' else ' ' + ext}}}"

    defines = [f"pypi_name {pypi_name}", f"version {version}", f"srcname {srcname}", f"pypi_version {file_version}"]
    expanded = rpm_eval(source, *defines)
    expected = f"{PYPI_SOURCE_URL}/{srcname[0]}/{srcname}/{sdist['filename']}"
    if expanded != expected:
        warn(f"Source expands to {expanded}, expected {expected}")
    else:
        try:
            fetch(expanded, method="HEAD")
        except urllib.error.URLError as exc:
            warn(f"{expanded} is not downloadable ({exc}): rpmbuild cannot fetch the Source")

    with tempfile.TemporaryDirectory(prefix="pypi2pclos-") as tmp:
        topdir = extract_sdist(sdist_path, tmp)
        srcdir = os.path.join(tmp, topdir)
        if topdir == f"{srcname}-{file_version}":
            setup_dir = f"{name_macro}-{version_macro}"
        else:
            setup_dir = topdir.replace(file_version, version_macro)
            warn(f"the sdist unpacks into {topdir}/: check %autosetup -n")

        pkg_info = email.parser.Parser().parsestr(read_text(os.path.join(srcdir, "PKG-INFO")) or "")
        try:
            pyproject = tomllib.loads(read_text(os.path.join(srcdir, "pyproject.toml")) or "")
        except tomllib.TOMLDecodeError as exc:
            warn(f"cannot parse pyproject.toml: {exc}")
            pyproject = {}
        egg_info = find_egg_info(srcdir)
        top_files = sorted(f for f in os.listdir(srcdir) if os.path.isfile(os.path.join(srcdir, f)))

        build_system = pyproject.get("build-system", {})
        build_requires = build_system.get("requires", ["setuptools >= 40.8", "wheel"])
        backend = build_system.get("build-backend", "setuptools.build_meta:__legacy__")

        # --- noarch or not
        rust = (os.path.exists(os.path.join(srcdir, "Cargo.toml"))
                or any(hint in r for r in build_requires for hint in RUST_HINTS))
        if platform_wheels:
            noarch = False
        elif pure_wheels:
            noarch = True
        else:
            noarch = not (compiled_sources(srcdir) or rust or backend.startswith(COMPILED_BACKENDS))
        if rust:
            warn("builds a Rust extension: it needs cargo-rpm-macros and its crates packaged, review by hand")

        # --- module names and scripts
        modules, scripts, extra_data = [], [], False
        wheel = pure_wheels[0] if pure_wheels else None
        if wheel and wheel.get("size", 0) <= MAX_WHEEL_SIZE:
            try:
                modules, scripts, extra_data = wheel_contents(fetch(wheel["url"]))
            except (urllib.error.URLError, zipfile.BadZipFile) as exc:
                warn(f"could not inspect {wheel['filename']}: {exc}")
        if not modules:
            modules = guess_modules(srcdir, pyproject, egg_info, pypi_name)
            scripts = sdist_scripts(srcdir, pyproject, egg_info)
        if extra_data:
            warn("the wheel installs data files outside site-packages: add them to %files")

        # --- metadata
        summary = " ".join((pkg_info.get("Summary") or info.get("summary") or "").split()).rstrip(".") or "FIXME"
        summary = summary[0].upper() + summary[1:]
        if len(summary) > 79:
            warn(f"Summary is {len(summary)} characters long: shorten it")
        if summary == "FIXME":
            warn("no Summary in the metadata: fill it in by hand")
        license_ = guess_license(pkg_info, info)
        description = guess_description(srcdir, pkg_info, summary)
        extras = pkg_info.get_all("Provides-Extra") or info.get("provides_extra") or []
        requires_python = pkg_info.get("Requires-Python") or info.get("requires_python")
        if requires_python:
            python3_version = rpm_eval("%{python3_version}")
            try:
                if not SpecifierSet(requires_python).contains(python3_version, prereleases=True):
                    warn(f"{info['name']} {pypi_version} needs Python {requires_python}, we have {python3_version}")
            except Exception:
                pass

        runtime_requires = pkg_info.get_all("Requires-Dist")
        if runtime_requires is None and egg_info:
            text = read_text(os.path.join(egg_info, "requires.txt"))
            runtime_requires = parse_requires_txt(text, dict(default_environment(), extra="")) if text else None
        if runtime_requires is None:
            runtime_requires = info.get("requires_dist") or []

        # --- %files
        licenses = [f for f in top_files if f.upper().startswith(LICENSE_PREFIXES)]
        docs = [f for f in top_files if f.upper().startswith(DOC_PREFIXES)]
        if os.path.isdir(os.path.join(srcdir, "LICENSES")):
            licenses.append("LICENSES/")
        files = ""
        save_files = ""
        if pkg_info.get_all("License-File"):
            save_files = "-l "
        else:
            files += "".join(f"%license {f}\n" for f in licenses)
            if not licenses:
                warn("no licence file found in the sdist")
        files += "".join(f"%doc {f}\n" for f in docs)
        files += "".join(f"%{{_bindir}}/{s}\n" for s in scripts)
        save_files += " ".join(modules)

    extras_comment = ""
    if extras:
        extras_comment = ("# Extras (their dependencies are not pulled in, there are no extras subpackages):\n"
                          f"#   {' '.join(extras)}\n")

    spec = TEMPLATE.format(
        pypi_name=pypi_name,
        globals=globals_,
        extras_comment=extras_comment,
        version=version,
        summary=spec_escape(summary),
        license=license_,
        source=source,
        buildarch="BuildArch:      noarch\n" if noarch else "",
        description=description,
        setup_dir=setup_dir,
        save_files=save_files,
        files=files,
        date=datetime.date.today().strftime("%a %b %d %Y"),
        packager=rpm_eval("%packager"),
        release=rpm_eval("%mkrel 1"),
    )

    with open(spec_path, "w") as f:
        f.write(spec)
    print(f"Wrote {spec_path}", flush=True)

    report = check_dependencies(build_requires, runtime_requires, pypi_name)
    for message in warnings:
        print(f"warning: {message}", file=sys.stderr)
    for line in report:
        print(line, file=sys.stderr)


if __name__ == "__main__":
    main()
