From 8bc57cc5555d933a78b74c5bcc68940b30073414 Mon Sep 17 00:00:00 2001 From: Grant Murphy Date: Wed, 10 Dec 2014 14:27:33 -0800 Subject: [PATCH] Add conversion utility for yaml to rst format tox should now be able to build the html documentation to completion. Change-Id: I384971732166fbeb123d572d3ccbcde6bad39dfc --- doc/source/index.rst | 2 +- test-requirements.txt | 2 + tox.ini | 5 +- yaml2rst.py | 104 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 yaml2rst.py diff --git a/doc/source/index.rst b/doc/source/index.rst index 0bb2b7a..0346ffd 100644 --- a/doc/source/index.rst +++ b/doc/source/index.rst @@ -8,7 +8,7 @@ These pages contain published OpenStack Security Advisories. :maxdepth: 2 :glob: - ossa/index + ./ossa/* .. seealso:: diff --git a/test-requirements.txt b/test-requirements.txt index 8e7314b..47baad1 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -1,3 +1,5 @@ # needed for doc build +rstcloth==0.2.2 +PyYAML==3.11 sphinx>=1.1.2,!=1.2.0,<1.3 oslosphinx>=2.2.0 # Apache-2.0 diff --git a/tox.ini b/tox.ini index d16e0f4..b32096c 100644 --- a/tox.ini +++ b/tox.ini @@ -13,4 +13,7 @@ deps = -r{toxinidir}/test-requirements.txt commands = {posargs} [testenv:docs] -commands = python setup.py build_sphinx +deps = -r{toxinidir}/test-requirements.txt +commands = + python yaml2rst.py + python setup.py build_sphinx diff --git a/yaml2rst.py b/yaml2rst.py new file mode 100644 index 0000000..42c12ce --- /dev/null +++ b/yaml2rst.py @@ -0,0 +1,104 @@ +#!/usr/bin/env python +import glob +import os +from rstcloth import rstcloth +import sys +import yaml +reload(sys) +sys.setdefaultencoding("utf-8") + +TW=3 + +def format_affected(affected): + content = "" + seen = False + for product in affected: + if seen: + content += " "*TW + content +="- {product}: {version}\n".format(**product) + seen = True + + content += "\n" + return content + +def format_reporters(reporters): + content ="" + seen = False + for reporter in reporters: + if seen: + content += " "*TW + + cves = ", ".join(reporter['reported']) + data = dict(reporter) + data["cves"] = cves + if not 'affiliation' in data or data['affiliation'] == 'UNKNOWN': + content += "- {name} ({cves})\n".format(**data) + else: + content += "- {name} from {affiliation} ({cves})\n".format(**data) + seen = True + + content += "\n" + return content + +def format_reviews(reviews): + content = "\n" + for review in [ x for x in reviews if x != 'type' ]: + seen = False + for link in reviews[review]: + content += " "*TW + content += "- `{url} ({review}) <{url}>`_\n".format(review=review, url=link) + seen = True + + content += "\n" + return content + +def format_urls(links): + content ="" + seen = False + for link in links: + if seen: + content += " "*TW + content += "- `{url} <{url}>`_\n".format(url=link) + seen = True + content += "\n" + return content + +def convert(old, new): + data = yaml.safe_load(open(old).read()) + doc = rstcloth.RstCloth() + doc.title("{id}".format(**data)) + doc.newline() + doc.h2(data['title']) + + doc.field('Date', "{:%B %d, %Y}".format(data['date'])) + doc.newline() + + doc.field('Description', data['description']) + doc.newline() + + doc.field('Announcement', doc.inline_link(data['reference'],data['reference']), wrap=False) + doc.newline() + + doc.field("Affects", format_affected(data['affected-products']), wrap=False) + doc.newline() + + doc.field("Credits", format_reporters(data['reporters']), wrap=False) + doc.newline() + + doc.field("Bug reports", format_urls(data['issues']['links']), wrap=False) + doc.newline() + + doc.field("Reviews", format_reviews(data['reviews']), wrap=False) + doc.newline() + + doc.write(new) + + +if __name__ == '__main__': + + input_files = glob.glob(os.path.join(".", "ossa", "*.yaml")) + output_files = [ x.replace(".yaml", ".rst") for x in input_files ] + for old, new in zip(input_files, output_files): + convert(old, new) + +