Skip to content

Commit

Permalink
Fix README for PyPI
Browse files Browse the repository at this point in the history
  • Loading branch information
akaihola committed Apr 24, 2024
1 parent 3650037 commit 4a555ae
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
50 changes: 49 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -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())

0 comments on commit 4a555ae

Please sign in to comment.