2013-12-18 05:34:07 +08:00
|
|
|
"""
|
|
|
|
Script to generate the file `plugins_index.txt` with information about
|
|
|
|
pytest plugins taken directly from a live PyPI server.
|
2013-10-12 06:47:40 +08:00
|
|
|
|
2014-01-16 06:26:05 +08:00
|
|
|
Also includes plugin compatibility between different python and pytest versions,
|
|
|
|
obtained from http://pytest-plugs.herokuapp.com.
|
2013-12-18 05:34:07 +08:00
|
|
|
"""
|
2013-12-18 07:42:51 +08:00
|
|
|
from __future__ import print_function
|
2013-10-12 06:14:22 +08:00
|
|
|
from collections import namedtuple
|
2013-10-12 06:47:40 +08:00
|
|
|
import datetime
|
2013-10-12 06:14:22 +08:00
|
|
|
from distutils.version import LooseVersion
|
|
|
|
import itertools
|
2013-10-12 07:53:03 +08:00
|
|
|
from optparse import OptionParser
|
2013-10-12 06:14:22 +08:00
|
|
|
import os
|
|
|
|
import sys
|
2013-11-13 09:54:13 +08:00
|
|
|
import pytest
|
2013-10-12 06:14:22 +08:00
|
|
|
|
2013-12-18 05:34:07 +08:00
|
|
|
|
2013-12-18 07:42:51 +08:00
|
|
|
def get_proxy(url):
|
|
|
|
"""
|
|
|
|
wrapper function to obtain a xmlrpc proxy, taking in account import
|
|
|
|
differences between python 2.X and 3.X
|
|
|
|
|
|
|
|
:param url: url to bind the proxy to
|
|
|
|
:return: a ServerProxy instance
|
|
|
|
"""
|
|
|
|
if sys.version_info < (3, 0):
|
|
|
|
from xmlrpclib import ServerProxy
|
|
|
|
else:
|
|
|
|
from xmlrpc.client import ServerProxy
|
|
|
|
return ServerProxy(url)
|
|
|
|
|
|
|
|
|
2013-10-12 06:47:40 +08:00
|
|
|
def iter_plugins(client, search='pytest-'):
|
2013-12-18 05:34:07 +08:00
|
|
|
"""
|
2013-10-12 07:57:35 +08:00
|
|
|
Returns an iterator of (name, version) from PyPI.
|
2013-10-12 06:47:40 +08:00
|
|
|
|
2013-12-18 07:42:51 +08:00
|
|
|
:param client: ServerProxy
|
2013-10-12 06:47:40 +08:00
|
|
|
:param search: package names to search for
|
2013-12-18 05:34:07 +08:00
|
|
|
"""
|
|
|
|
for plug_data in client.search({'name': search}):
|
2013-10-12 06:14:22 +08:00
|
|
|
yield plug_data['name'], plug_data['version']
|
|
|
|
|
|
|
|
|
|
|
|
def get_latest_versions(plugins):
|
2013-12-18 05:34:07 +08:00
|
|
|
"""
|
|
|
|
Returns an iterator of (name, version) from the given list of (name,
|
|
|
|
version), but returning only the latest version of the package. Uses
|
|
|
|
distutils.LooseVersion to ensure compatibility with PEP386.
|
|
|
|
"""
|
2013-10-12 06:14:22 +08:00
|
|
|
plugins = [(name, LooseVersion(version)) for (name, version) in plugins]
|
|
|
|
for name, grouped_plugins in itertools.groupby(plugins, key=lambda x: x[0]):
|
|
|
|
name, loose_version = list(grouped_plugins)[-1]
|
|
|
|
yield name, str(loose_version)
|
2013-12-18 05:34:07 +08:00
|
|
|
|
|
|
|
|
|
|
|
def obtain_plugins_table(plugins, client):
|
|
|
|
"""
|
|
|
|
Returns information to populate a table of plugins, their versions,
|
|
|
|
authors, etc.
|
2013-10-12 06:47:40 +08:00
|
|
|
|
2013-12-18 05:34:07 +08:00
|
|
|
The returned information is a list of columns of `ColumnData`
|
|
|
|
namedtuples(text, link). Link can be None if the text for that column
|
|
|
|
should not be linked to anything.
|
2013-10-12 06:47:40 +08:00
|
|
|
|
|
|
|
:param plugins: list of (name, version)
|
2013-12-18 07:42:51 +08:00
|
|
|
:param client: ServerProxy
|
2013-12-18 05:34:07 +08:00
|
|
|
"""
|
2013-10-12 06:14:22 +08:00
|
|
|
rows = []
|
2013-10-12 06:47:40 +08:00
|
|
|
ColumnData = namedtuple('ColumnData', 'text link')
|
2014-01-16 06:26:05 +08:00
|
|
|
headers = ['Name', 'Py27', 'Py33', 'Repository', 'Summary']
|
2013-11-13 09:54:13 +08:00
|
|
|
pytest_version = pytest.__version__
|
2014-01-16 06:26:05 +08:00
|
|
|
repositories = obtain_override_repositories()
|
2013-12-18 07:42:51 +08:00
|
|
|
print('*** pytest-{0} ***'.format(pytest_version))
|
2013-10-15 06:14:05 +08:00
|
|
|
plugins = list(plugins)
|
|
|
|
for index, (package_name, version) in enumerate(plugins):
|
2013-12-18 07:42:51 +08:00
|
|
|
print(package_name, version, '...', end='')
|
2013-12-18 05:34:07 +08:00
|
|
|
|
2013-10-12 06:14:22 +08:00
|
|
|
release_data = client.release_data(package_name, version)
|
2013-12-18 05:34:07 +08:00
|
|
|
url = '.. image:: {site}/status/{name}-{version}'
|
|
|
|
image_url = url.format(
|
|
|
|
site='http://pytest-plugs.herokuapp.com',
|
|
|
|
name=package_name,
|
|
|
|
version=version)
|
2013-11-13 09:54:13 +08:00
|
|
|
image_url += '?py={py}&pytest={pytest}'
|
2013-10-12 06:14:22 +08:00
|
|
|
row = (
|
2013-12-18 05:34:07 +08:00
|
|
|
ColumnData(package_name + '-' + version,
|
|
|
|
release_data['release_url']),
|
|
|
|
ColumnData(image_url.format(py='py27', pytest=pytest_version),
|
|
|
|
None),
|
|
|
|
ColumnData(image_url.format(py='py33', pytest=pytest_version),
|
|
|
|
None),
|
2014-01-16 06:26:05 +08:00
|
|
|
ColumnData(
|
|
|
|
repositories.get(package_name, release_data['home_page']),
|
|
|
|
None),
|
2013-10-12 06:47:40 +08:00
|
|
|
ColumnData(release_data['summary'], None),
|
2013-10-12 06:14:22 +08:00
|
|
|
)
|
|
|
|
assert len(row) == len(headers)
|
|
|
|
rows.append(row)
|
2013-12-18 05:34:07 +08:00
|
|
|
|
2013-12-18 07:42:51 +08:00
|
|
|
print('OK (%d%%)' % ((index + 1) * 100 / len(plugins)))
|
2013-12-18 05:34:07 +08:00
|
|
|
|
|
|
|
return headers, rows
|
2013-10-12 06:14:22 +08:00
|
|
|
|
|
|
|
|
2014-01-16 06:26:05 +08:00
|
|
|
def obtain_override_repositories():
|
|
|
|
"""
|
|
|
|
Used to override the "home_page" obtained from pypi to known
|
|
|
|
package repositories. Used when the author didn't fill the "home_page"
|
|
|
|
field in setup.py.
|
|
|
|
|
|
|
|
:return: dict of {package_name: repository_url}
|
|
|
|
"""
|
|
|
|
return {
|
|
|
|
'pytest-blockage': 'https://github.com/rob-b/pytest-blockage',
|
|
|
|
'pytest-konira': 'http://github.com/alfredodeza/pytest-konira',
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-12 06:47:40 +08:00
|
|
|
def generate_plugins_index_from_table(filename, headers, rows):
|
2013-12-18 05:34:07 +08:00
|
|
|
"""
|
2013-10-12 06:47:40 +08:00
|
|
|
Generates a RST file with the table data given.
|
|
|
|
|
|
|
|
:param filename: output filename
|
|
|
|
:param headers: see `obtain_plugins_table`
|
|
|
|
:param rows: see `obtain_plugins_table`
|
2013-12-18 05:34:07 +08:00
|
|
|
"""
|
|
|
|
# creates a list of rows, each being a str containing appropriate column
|
|
|
|
# text and link
|
2013-10-12 06:14:22 +08:00
|
|
|
table_texts = []
|
|
|
|
for row in rows:
|
2013-10-12 06:47:40 +08:00
|
|
|
column_texts = []
|
2013-12-18 05:34:07 +08:00
|
|
|
for i, col_data in enumerate(row):
|
|
|
|
text = '`%s <%s>`_' % (
|
|
|
|
col_data.text,
|
|
|
|
col_data.link) if col_data.link else col_data.text
|
2013-10-12 06:47:40 +08:00
|
|
|
column_texts.append(text)
|
|
|
|
table_texts.append(column_texts)
|
2013-12-18 05:34:07 +08:00
|
|
|
|
2013-10-12 06:47:40 +08:00
|
|
|
# compute max length of each column so we can build the rst table
|
2013-10-12 06:14:22 +08:00
|
|
|
column_lengths = [len(x) for x in headers]
|
2013-10-12 06:47:40 +08:00
|
|
|
for column_texts in table_texts:
|
|
|
|
for i, row_text in enumerate(column_texts):
|
2013-10-12 06:14:22 +08:00
|
|
|
column_lengths[i] = max(column_lengths[i], len(row_text) + 2)
|
2013-12-18 05:34:07 +08:00
|
|
|
|
2013-10-12 06:47:40 +08:00
|
|
|
def get_row_limiter(char):
|
|
|
|
return ' '.join(char * length for length in column_lengths)
|
2013-12-18 05:34:07 +08:00
|
|
|
|
2013-12-18 07:42:51 +08:00
|
|
|
with open(filename, 'w') as f:
|
2013-10-12 06:47:40 +08:00
|
|
|
# write welcome
|
2013-12-18 07:42:51 +08:00
|
|
|
print('.. _plugins_index:', file=f)
|
|
|
|
print(file=f)
|
|
|
|
print('List of Third-Party Plugins', file=f)
|
|
|
|
print('===========================', file=f)
|
|
|
|
print(file=f)
|
2013-12-18 05:34:07 +08:00
|
|
|
|
2013-10-12 06:47:40 +08:00
|
|
|
# table
|
2013-12-18 07:42:51 +08:00
|
|
|
print(get_row_limiter('='), file=f)
|
|
|
|
formatted_headers = [
|
|
|
|
'{0:^{fill}}'.format(header, fill=column_lengths[i])
|
|
|
|
for i, header in enumerate(headers)]
|
|
|
|
print(*formatted_headers, file=f)
|
|
|
|
print(get_row_limiter('='), file=f)
|
2013-12-18 05:34:07 +08:00
|
|
|
|
2013-10-12 06:47:40 +08:00
|
|
|
for column_texts in table_texts:
|
2013-12-18 07:42:51 +08:00
|
|
|
formatted_rows = [
|
|
|
|
'{0:^{fill}}'.format(row_text, fill=column_lengths[i])
|
|
|
|
for i, row_text in enumerate(column_texts)
|
|
|
|
]
|
|
|
|
print(*formatted_rows, file=f)
|
|
|
|
print(file=f)
|
|
|
|
print(get_row_limiter('='), file=f)
|
|
|
|
print(file=f)
|
2014-01-16 06:26:05 +08:00
|
|
|
today = datetime.date.today().strftime('%Y-%m-%d')
|
|
|
|
print('*(Updated on %s)*' % today, file=f)
|
2013-10-12 06:14:22 +08:00
|
|
|
|
|
|
|
|
2013-10-12 06:47:40 +08:00
|
|
|
def generate_plugins_index(client, filename):
|
2013-12-18 05:34:07 +08:00
|
|
|
"""
|
|
|
|
Generates an RST file with a table of the latest pytest plugins found in
|
|
|
|
PyPI.
|
2013-10-12 06:47:40 +08:00
|
|
|
|
2013-12-18 07:42:51 +08:00
|
|
|
:param client: ServerProxy
|
2013-10-12 06:47:40 +08:00
|
|
|
:param filename: output filename
|
2013-12-18 05:34:07 +08:00
|
|
|
"""
|
2013-10-12 06:47:40 +08:00
|
|
|
plugins = get_latest_versions(iter_plugins(client))
|
2013-10-12 06:14:22 +08:00
|
|
|
headers, rows = obtain_plugins_table(plugins, client)
|
2013-10-12 06:47:40 +08:00
|
|
|
generate_plugins_index_from_table(filename, headers, rows)
|
2013-10-12 06:14:22 +08:00
|
|
|
|
2013-12-18 05:34:07 +08:00
|
|
|
|
2013-10-12 06:14:22 +08:00
|
|
|
def main(argv):
|
2013-12-18 05:34:07 +08:00
|
|
|
"""
|
|
|
|
Script entry point. Configures an option parser and calls the appropriate
|
|
|
|
internal function.
|
|
|
|
"""
|
2013-10-12 06:47:40 +08:00
|
|
|
filename = os.path.join(os.path.dirname(__file__), 'plugins_index.txt')
|
2013-10-12 07:53:03 +08:00
|
|
|
url = 'http://pypi.python.org/pypi'
|
2013-12-18 05:34:07 +08:00
|
|
|
|
|
|
|
parser = OptionParser(
|
|
|
|
description='Generates a restructured document of pytest plugins from PyPI')
|
|
|
|
parser.add_option('-f', '--filename', default=filename,
|
|
|
|
help='output filename [default: %default]')
|
|
|
|
parser.add_option('-u', '--url', default=url,
|
|
|
|
help='url of PyPI server to obtain data from [default: %default]')
|
2013-10-12 07:53:03 +08:00
|
|
|
(options, _) = parser.parse_args(argv[1:])
|
|
|
|
|
2013-12-18 07:42:51 +08:00
|
|
|
client = get_proxy(options.url)
|
2013-10-12 07:53:03 +08:00
|
|
|
generate_plugins_index(client, options.filename)
|
2013-12-18 05:34:07 +08:00
|
|
|
|
2013-12-18 07:42:51 +08:00
|
|
|
print()
|
|
|
|
print('%s Updated.' % options.filename)
|
2013-10-12 06:14:22 +08:00
|
|
|
return 0
|
|
|
|
|
2013-12-18 07:42:51 +08:00
|
|
|
|
2013-10-12 06:14:22 +08:00
|
|
|
if __name__ == '__main__':
|
|
|
|
sys.exit(main(sys.argv))
|