|
| 1 | +"""This script creates a list of plugins for pytask. |
| 2 | +
|
| 3 | +It is shamelessly stolen from pytest and therefore includes its license. |
| 4 | +
|
| 5 | +https://github.com/pytest-dev/pytest/blob/main/scripts/update-plugin-list.py |
| 6 | +
|
| 7 | +
|
| 8 | +MIT License |
| 9 | +
|
| 10 | +Copyright (c) 2004 Holger Krekel and others |
| 11 | +
|
| 12 | +Permission is hereby granted, free of charge, to any person obtaining a copy of this |
| 13 | +software and associated documentation files (the "Software"), to deal in the Software |
| 14 | +without restriction, including without limitation the rights to use, copy, modify, |
| 15 | +merge, publish, distribute, sublicense, and/or sell copies of the Software, and to |
| 16 | +permit persons to whom the Software is furnished to do so, subject to the following |
| 17 | +conditions: |
| 18 | +
|
| 19 | +The above copyright notice and this permission notice shall be included in all copies or |
| 20 | +substantial portions of the Software. |
| 21 | +
|
| 22 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 23 | +INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
| 24 | +PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 25 | +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT |
| 26 | +OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 27 | +OTHER DEALINGS IN THE SOFTWARE. |
| 28 | +
|
| 29 | +""" |
| 30 | +from __future__ import annotations |
| 31 | + |
| 32 | +import datetime |
| 33 | +import pathlib |
| 34 | +import re |
| 35 | +from textwrap import dedent |
| 36 | +from textwrap import indent |
| 37 | + |
| 38 | +import packaging.version |
| 39 | +import requests |
| 40 | +import tabulate |
| 41 | +import wcwidth |
| 42 | +from tqdm import tqdm |
| 43 | + |
| 44 | + |
| 45 | +_FILE_HEAD = r""" |
| 46 | +.. _plugin-list: |
| 47 | +
|
| 48 | +Plugin List |
| 49 | +=========== |
| 50 | +
|
| 51 | +PyPI projects that match "pytask-\*" are considered plugins and are listed |
| 52 | +automatically. Packages classified as inactive are excluded. |
| 53 | +
|
| 54 | +.. The following conditional uses a different format for this list when |
| 55 | + creating a PDF, because otherwise the table gets far too wide for the |
| 56 | + page. |
| 57 | +
|
| 58 | +""" |
| 59 | + |
| 60 | + |
| 61 | +_DEVELOPMENT_STATUS_CLASSIFIERS = ( |
| 62 | + "Development Status :: 1 - Planning", |
| 63 | + "Development Status :: 2 - Pre-Alpha", |
| 64 | + "Development Status :: 3 - Alpha", |
| 65 | + "Development Status :: 4 - Beta", |
| 66 | + "Development Status :: 5 - Production/Stable", |
| 67 | + "Development Status :: 6 - Mature", |
| 68 | + "Development Status :: 7 - Inactive", |
| 69 | +) |
| 70 | + |
| 71 | + |
| 72 | +_EXCLUDED_PACKAGES = ["pytask-io"] |
| 73 | + |
| 74 | + |
| 75 | +def _escape_rst(text: str) -> str: |
| 76 | + """Rudimentary attempt to escape special RST characters to appear as plain text.""" |
| 77 | + text = ( |
| 78 | + text.replace("*", "\\*") |
| 79 | + .replace("<", "\\<") |
| 80 | + .replace(">", "\\>") |
| 81 | + .replace("`", "\\`") |
| 82 | + ) |
| 83 | + text = re.sub(r"_\b", "", text) |
| 84 | + return text |
| 85 | + |
| 86 | + |
| 87 | +def _iter_plugins(): |
| 88 | + """Iterate over all plugins and format entries.""" |
| 89 | + regex = r">([\d\w-]*)</a>" |
| 90 | + response = requests.get("https://pypi.org/simple") |
| 91 | + |
| 92 | + matches = [ |
| 93 | + match |
| 94 | + for match in re.finditer(regex, response.text) |
| 95 | + if match.groups()[0].startswith("pytask-") |
| 96 | + and match.groups()[0] not in _EXCLUDED_PACKAGES |
| 97 | + ] |
| 98 | + |
| 99 | + for match in tqdm(matches, smoothing=0): |
| 100 | + name = match.groups()[0] |
| 101 | + response = requests.get(f"https://pypi.org/pypi/{name}/json") |
| 102 | + response.raise_for_status() |
| 103 | + info = response.json()["info"] |
| 104 | + |
| 105 | + if "Development Status :: 7 - Inactive" in info["classifiers"]: |
| 106 | + continue |
| 107 | + for classifier in _DEVELOPMENT_STATUS_CLASSIFIERS: |
| 108 | + if classifier in info["classifiers"]: |
| 109 | + status = classifier[22:] |
| 110 | + break |
| 111 | + else: |
| 112 | + status = "N/A" |
| 113 | + requires = "N/A" |
| 114 | + |
| 115 | + if info["requires_dist"]: |
| 116 | + for requirement in info["requires_dist"]: |
| 117 | + if requirement == "pytask" or "pytask " in requirement: |
| 118 | + requires = requirement |
| 119 | + break |
| 120 | + releases = response.json()["releases"] |
| 121 | + |
| 122 | + for release in sorted(releases, key=packaging.version.parse, reverse=True): |
| 123 | + if releases[release]: |
| 124 | + release_date = datetime.date.fromisoformat( |
| 125 | + releases[release][-1]["upload_time_iso_8601"].split("T")[0] |
| 126 | + ) |
| 127 | + last_release = release_date.strftime("%b %d, %Y") |
| 128 | + break |
| 129 | + |
| 130 | + name = f':pypi:`{info["name"]}`' |
| 131 | + summary = _escape_rst(info["summary"].replace("\n", "")) |
| 132 | + |
| 133 | + yield { |
| 134 | + "name": name, |
| 135 | + "summary": summary.strip(), |
| 136 | + "last release": last_release, |
| 137 | + "status": status, |
| 138 | + "requires": requires, |
| 139 | + } |
| 140 | + |
| 141 | + |
| 142 | +def _plugin_definitions(plugins): |
| 143 | + """Return RST for the plugin list that fits better on a vertical page.""" |
| 144 | + |
| 145 | + for plugin in plugins: |
| 146 | + yield dedent( |
| 147 | + f""" |
| 148 | + {plugin['name']} |
| 149 | + *last release*: {plugin["last release"]}, |
| 150 | + *status*: {plugin["status"]}, |
| 151 | + *requires*: {plugin["requires"]} |
| 152 | +
|
| 153 | + {plugin["summary"]} |
| 154 | + """ |
| 155 | + ) |
| 156 | + |
| 157 | + |
| 158 | +def main(): |
| 159 | + plugins = list(_iter_plugins()) |
| 160 | + |
| 161 | + reference_dir = pathlib.Path("docs", "source") |
| 162 | + |
| 163 | + plugin_list = reference_dir / "plugin_list.rst" |
| 164 | + with plugin_list.open("w") as f: |
| 165 | + f.write(_FILE_HEAD) |
| 166 | + f.write(f"This list contains {len(plugins)} plugins.\n\n") |
| 167 | + f.write(".. only:: not latex\n\n") |
| 168 | + |
| 169 | + wcwidth # reference library that must exist for tabulate to work |
| 170 | + plugin_table = tabulate.tabulate(plugins, headers="keys", tablefmt="rst") |
| 171 | + f.write(indent(plugin_table, " ")) |
| 172 | + f.write("\n\n") |
| 173 | + |
| 174 | + f.write(".. only:: latex\n\n") |
| 175 | + f.write(indent("".join(_plugin_definitions(plugins)), " ")) |
| 176 | + |
| 177 | + |
| 178 | +if __name__ == "__main__": |
| 179 | + main() |
0 commit comments