2013-12-18 05:34:07 +08:00
|
|
|
"""
|
2014-01-29 20:47:11 +08:00
|
|
|
Script to generate the file `index.txt` with information about
|
2014-02-19 08:33:13 +08:00
|
|
|
pytest plugins taken directly from PyPI.
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
python plugins_index.py
|
|
|
|
|
|
|
|
This command will update `index.txt` in the same directory found as this script.
|
|
|
|
This should be issued before every major documentation release to obtain latest
|
|
|
|
versions from PyPI.
|
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,
|
2015-03-01 04:22:42 +08:00
|
|
|
obtained from http://plugincompat.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.
|
2014-01-29 20:47:11 +08:00
|
|
|
|
2013-12-18 07:42:51 +08:00
|
|
|
:param client: ServerProxy
|
2014-01-29 20:47:11 +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
|
|
|
|
|
|
|
|
2015-03-01 04:22:42 +08:00
|
|
|
def obtain_plugins_table(plugins, client, verbose, pytest_ver):
|
2013-12-18 05:34:07 +08:00
|
|
|
"""
|
|
|
|
Returns information to populate a table of plugins, their versions,
|
|
|
|
authors, etc.
|
2014-01-29 20:47:11 +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.
|
2014-01-29 20:47:11 +08:00
|
|
|
|
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
|
2014-09-27 23:28:29 +08:00
|
|
|
:param verbose: print plugin name and version as they are fetch
|
2015-03-01 04:22:42 +08:00
|
|
|
:param pytest_ver: pytest version to use.
|
2013-12-18 05:34:07 +08:00
|
|
|
"""
|
2015-03-01 04:22:42 +08:00
|
|
|
if pytest_ver is None:
|
|
|
|
pytest_ver = pytest.__version__
|
|
|
|
|
2014-02-19 09:40:36 +08:00
|
|
|
def get_repo_markup(repo):
|
|
|
|
"""
|
|
|
|
obtains appropriate markup for the given repository, as two lines
|
|
|
|
that should be output in the same table row. We use this to display an icon
|
|
|
|
for known repository hosts (github, etc), just a "?" char when
|
|
|
|
repository is not registered in pypi or a simple link otherwise.
|
|
|
|
"""
|
|
|
|
target = repo
|
|
|
|
if 'github.com' in repo:
|
|
|
|
image = 'github.png'
|
|
|
|
elif 'bitbucket.org' in repo:
|
|
|
|
image = 'bitbucket.png'
|
|
|
|
elif repo.lower() == 'unknown':
|
|
|
|
return '?', ''
|
|
|
|
else:
|
|
|
|
image = None
|
|
|
|
|
|
|
|
if image is not None:
|
|
|
|
image_markup = '.. image:: %s' % image
|
|
|
|
target_markup = ' :target: %s' % repo
|
|
|
|
pad_right = ('%-' + str(len(target_markup)) + 's')
|
|
|
|
return pad_right % image_markup, target_markup
|
|
|
|
else:
|
2014-09-27 23:28:29 +08:00
|
|
|
return ('`link <%s>`_' % target), ''
|
2014-07-19 08:10:50 +08:00
|
|
|
|
|
|
|
def sanitize_summary(summary):
|
|
|
|
"""Make sure summaries don't break our table formatting.
|
|
|
|
"""
|
|
|
|
return summary.replace('\n', ' ')
|
2014-02-19 09:40:36 +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-09-27 23:28:29 +08:00
|
|
|
headers = ['Name', 'Py27', 'Py34', 'Home', 'Summary']
|
2014-01-16 06:26:05 +08:00
|
|
|
repositories = obtain_override_repositories()
|
2015-03-01 04:22:42 +08:00
|
|
|
print('Generating plugins_index page (pytest-{0})'.format(pytest_ver))
|
2013-10-15 06:14:05 +08:00
|
|
|
plugins = list(plugins)
|
|
|
|
for index, (package_name, version) in enumerate(plugins):
|
2014-09-27 23:28:29 +08:00
|
|
|
if verbose:
|
|
|
|
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)
|
2014-01-16 06:48:11 +08:00
|
|
|
|
|
|
|
common_params = dict(
|
2015-03-01 04:22:42 +08:00
|
|
|
site='http://plugincompat.herokuapp.com',
|
2013-12-18 05:34:07 +08:00
|
|
|
name=package_name,
|
|
|
|
version=version)
|
2014-01-16 06:48:11 +08:00
|
|
|
|
2014-02-19 09:40:36 +08:00
|
|
|
repository = repositories.get(package_name, release_data['home_page'])
|
|
|
|
repo_markup_1, repo_markup_2 = get_repo_markup(repository)
|
|
|
|
|
2014-01-16 06:48:11 +08:00
|
|
|
# first row: name, images and simple links
|
2014-02-12 09:38:30 +08:00
|
|
|
url = '.. image:: {site}/status/{name}-latest'
|
2014-01-16 06:48:11 +08:00
|
|
|
image_url = url.format(**common_params)
|
2013-11-13 09:54:13 +08:00
|
|
|
image_url += '?py={py}&pytest={pytest}'
|
2013-10-12 06:14:22 +08:00
|
|
|
row = (
|
2014-09-27 23:28:29 +08:00
|
|
|
ColumnData(package_name, release_data['package_url']),
|
2015-03-01 04:22:42 +08:00
|
|
|
ColumnData(image_url.format(py='py27', pytest=pytest_ver),
|
2013-12-18 05:34:07 +08:00
|
|
|
None),
|
2015-03-01 04:22:42 +08:00
|
|
|
ColumnData(image_url.format(py='py34', pytest=pytest_ver),
|
2013-12-18 05:34:07 +08:00
|
|
|
None),
|
2014-01-16 06:26:05 +08:00
|
|
|
ColumnData(
|
2014-02-19 09:40:36 +08:00
|
|
|
repo_markup_1,
|
2014-01-16 06:26:05 +08:00
|
|
|
None),
|
2014-07-19 08:10:50 +08:00
|
|
|
ColumnData(sanitize_summary(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
|
|
|
|
2014-01-16 06:48:11 +08:00
|
|
|
# second row: links for images (they should be in their own line)
|
2014-02-12 09:38:30 +08:00
|
|
|
url = ' :target: {site}/output/{name}-latest'
|
2014-01-16 06:48:11 +08:00
|
|
|
output_url = url.format(**common_params)
|
|
|
|
output_url += '?py={py}&pytest={pytest}'
|
|
|
|
|
|
|
|
row = (
|
|
|
|
ColumnData('', None),
|
2015-03-01 04:22:42 +08:00
|
|
|
ColumnData(output_url.format(py='py27', pytest=pytest_ver),
|
2014-01-16 06:48:11 +08:00
|
|
|
None),
|
2015-03-01 04:22:42 +08:00
|
|
|
ColumnData(output_url.format(py='py34', pytest=pytest_ver),
|
2014-01-16 06:48:11 +08:00
|
|
|
None),
|
2014-02-19 09:40:36 +08:00
|
|
|
ColumnData(repo_markup_2, None),
|
2014-01-16 06:48:11 +08:00
|
|
|
ColumnData('', None),
|
2014-02-19 09:40:36 +08:00
|
|
|
|
2014-01-16 06:48:11 +08:00
|
|
|
)
|
|
|
|
assert len(row) == len(headers)
|
|
|
|
rows.append(row)
|
|
|
|
|
2014-09-27 23:28:29 +08:00
|
|
|
if verbose:
|
|
|
|
print('OK (%d%%)' % ((index + 1) * 100 / len(plugins)))
|
|
|
|
|
|
|
|
print('Done: %d plugins' % 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',
|
2014-04-02 18:03:08 +08:00
|
|
|
'pytest-sugar': 'https://github.com/Frozenball/pytest-sugar',
|
2014-01-16 06:26:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-01 04:22:42 +08:00
|
|
|
def generate_plugins_index_from_table(filename, headers, rows, pytest_ver):
|
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.
|
2014-01-29 20:47:11 +08:00
|
|
|
|
2013-10-12 06:47:40 +08:00
|
|
|
:param filename: output filename
|
|
|
|
:param headers: see `obtain_plugins_table`
|
|
|
|
:param rows: see `obtain_plugins_table`
|
2015-03-01 04:22:42 +08:00
|
|
|
:param pytest_ver: 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:
|
2014-02-19 08:33:13 +08:00
|
|
|
# header
|
2015-03-01 04:22:42 +08:00
|
|
|
header_text = HEADER.format(pytest_version=pytest_ver)
|
2014-02-19 08:33:13 +08:00
|
|
|
print(header_text, file=f)
|
2013-12-18 07:42:51 +08:00
|
|
|
print(file=f)
|
2013-12-18 05:34:07 +08:00
|
|
|
|
2014-01-29 20:47:11 +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
|
|
|
|
|
|
|
|
2015-03-01 04:22:42 +08:00
|
|
|
def generate_plugins_index(client, filename, verbose, pytest_ver):
|
2013-12-18 05:34:07 +08:00
|
|
|
"""
|
|
|
|
Generates an RST file with a table of the latest pytest plugins found in
|
|
|
|
PyPI.
|
2014-01-29 20:47:11 +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
|
2014-09-27 23:28:29 +08:00
|
|
|
:param verbose: print name and version of each plugin as they are fetch
|
2015-03-01 04:22:42 +08:00
|
|
|
:param pytest_ver: pytest version to use; if not given, use current pytest
|
|
|
|
version.
|
2013-12-18 05:34:07 +08:00
|
|
|
"""
|
2013-10-12 06:47:40 +08:00
|
|
|
plugins = get_latest_versions(iter_plugins(client))
|
2015-03-01 04:22:42 +08:00
|
|
|
headers, rows = obtain_plugins_table(plugins, client, verbose, pytest_ver)
|
|
|
|
generate_plugins_index_from_table(filename, headers, rows, pytest_ver)
|
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.
|
|
|
|
"""
|
2014-01-29 20:47:11 +08:00
|
|
|
filename = os.path.join(os.path.dirname(__file__), '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]')
|
2014-09-27 23:28:29 +08:00
|
|
|
parser.add_option('-v', '--verbose', default=False, action='store_true',
|
|
|
|
help='verbose output')
|
2015-03-01 04:22:42 +08:00
|
|
|
parser.add_option('--pytest-ver', default=None, action='store',
|
|
|
|
help='generate index for this pytest version (default current version)')
|
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)
|
2015-03-01 04:22:42 +08:00
|
|
|
generate_plugins_index(client, options.filename, options.verbose, options.pytest_ver)
|
2013-12-18 05:34:07 +08:00
|
|
|
|
2013-12-18 07:42:51 +08:00
|
|
|
print()
|
2014-09-27 23:28:29 +08:00
|
|
|
print('%s updated.' % options.filename)
|
2013-10-12 06:14:22 +08:00
|
|
|
return 0
|
|
|
|
|
2013-12-18 07:42:51 +08:00
|
|
|
|
2014-02-19 08:33:13 +08:00
|
|
|
# header for the plugins_index page
|
|
|
|
HEADER = '''.. _plugins_index:
|
|
|
|
|
|
|
|
List of Third-Party Plugins
|
|
|
|
===========================
|
|
|
|
|
|
|
|
The table below contains a listing of plugins found in PyPI and
|
|
|
|
their status when tested using py.test **{pytest_version}** and python 2.7 and
|
|
|
|
3.3.
|
|
|
|
|
|
|
|
A complete listing can also be found at
|
2015-03-01 04:22:42 +08:00
|
|
|
`plugincompat <http://plugincompat.herokuapp.com/>`_, which contains tests
|
2014-02-19 08:33:13 +08:00
|
|
|
status against other py.test releases.
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
2013-10-12 06:14:22 +08:00
|
|
|
if __name__ == '__main__':
|
|
|
|
sys.exit(main(sys.argv))
|