diff --git a/README.rst b/README.rst index 5507d58..a3afa51 100644 --- a/README.rst +++ b/README.rst @@ -38,6 +38,10 @@ This package contains two tools for backing up PostgreSQL database dumps. | look at one of them and shoot us a comment! | to ask for help and advice! | +------------------------------------------------+--------------------------------+ +.. |you-can-help| image:: https://img.shields.io/badge/-You%20can%20help-green?style=for-the-badge + :alt: You can help +.. |support| image:: https://img.shields.io/badge/-Support-green?style=for-the-badge + :alt: Support .. _community support channel: https://github.com/akaihola/pgtricks/discussions diff --git a/setup.py b/setup.py index b024da8..4d6186a 100644 --- a/setup.py +++ b/setup.py @@ -1,4 +1,52 @@ +"""Setuptools entry point for pgtricks""" + +import re +from typing import Pattern + from setuptools import setup -setup() +SIDEBAR_RE = re.compile(r"\+---.*:alt: Support", re.DOTALL) +CONTRIBUTORS_RE = re.compile(r"""\nThanks goes .*\nThis project follows """, re.DOTALL) + + +def replace(name: str, regex: Pattern[str], replacement: str, content: str) -> str: + """Replace/remove a section from the package description, based on a regex + + Raise an exception if the regular expression doesn't match anything. + + """ + modified_content = regex.sub(replacement, content) + if modified_content != content: + return modified_content + raise RuntimeError( + f"The {name} wasn't found in README.rst using the pattern {regex.pattern!r}" + ) + + +def make_pypi_compliant_readme() -> str: + """Remove raw HTML from ``README.rst`` before packaging + + The sidebar and the contributors table should only be shown on GitHub. They must be + removed before being displayed on PyPI. The simplest way to do this is to simply + match and strip it when creating a distribution archive. + + This function reads the contents of ``README.rst``, removes the sidebar, replaces + the contributors raw HTML section with a plain-text link to the README on GitHub and + returns the resulting string. + + :return: The contents of a PyPI compliant ``README.rst`` + + """ + with open("README.rst", encoding="utf-8") as readme_file: + original_readme = readme_file.read() + no_sidebar_readme = replace("sidebar", SIDEBAR_RE, "", original_readme) + return replace( + "contributors table", + CONTRIBUTORS_RE, + "\nSee README.rst_ for the list of contributors.\n\nThis project follows ", + no_sidebar_readme, + ) + + +setup(long_description=make_pypi_compliant_readme())