Merge "diff_branches: use jinja templates to create the files"

This commit is contained in:
Jenkins 2015-11-13 12:48:57 +00:00 committed by Gerrit Code Review
commit 2c446f04e4
3 changed files with 104 additions and 99 deletions

View File

@ -23,7 +23,7 @@ import subprocess
import sys
import git
from lxml import etree
import jinja2
PROJECTS = ['ceilometer', 'cinder', 'glance', 'heat', 'ironic', 'keystone',
@ -128,54 +128,10 @@ def _cmpopts(x, y):
return cmp(x, y)
def dbk_append_table(parent, title, cols):
"""Create a docbook table and append it to `parent`.
:param parent: the element to which the table is added
:param title: the table title
:param cols: the number of columns in this table
"""
table = etree.Element("table")
parent.append(table)
caption = etree.Element("caption")
caption.text = title
table.append(caption)
for i in range(cols):
# We cast to int for python 3
width = "%d%%" % int(100 / cols)
table.append(etree.Element("col", width=width))
return table
def dbk_append_row(parent, cells):
"""Append a row to a table.
:param parent: the table
:param cells: a list of strings, one string per column
"""
tr = etree.Element("tr")
for text in cells:
td = etree.Element("td")
td.text = str(text)
tr.append(td)
parent.append(tr)
def dbk_append_header(parent, cells):
"""Append a header to a table.
:param parent: the table
:param cells: a list of strings, one string per column
"""
thead = etree.Element("thead")
dbk_append_row(thead, cells)
parent.append(thead)
def diff(old_list, new_list):
"""Compare the old and new lists of options."""
new_opts = []
changed_default = []
new_defaults = []
deprecated_opts = []
for name, (group, option) in new_list.items():
# Find the new options
@ -184,7 +140,7 @@ def diff(old_list, new_list):
# Find the options for which the default value has changed
elif option['default'] != old_list[name][1]['default']:
changed_default.append(name)
new_defaults.append(name)
# Find options that have been deprecated in the new release.
# If an option name is a key in the old_list dict, it means that it
@ -209,7 +165,7 @@ def diff(old_list, new_list):
if full_name in old_list.viewkeys():
deprecated_opts.append((full_name, name))
return new_opts, changed_default, deprecated_opts
return new_opts, new_defaults, deprecated_opts
def format_option_name(name):
@ -240,50 +196,34 @@ def release_from_branch(branch):
return branch.replace('stable/', '').title()
def generate_docbook(project, new_branch, old_list, new_list):
"""Generate the diff between the 2 options lists for `project`."""
new_opts, changed_default, deprecated_opts = diff(old_list, new_list)
def get_env(project, new_branch, old_list, new_list):
"""Generate the jinja2 environment for the defined branch and project."""
new_opts, new_defaults, deprecated_opts = diff(old_list, new_list)
release = release_from_branch(new_branch)
XMLNS = '{http://www.w3.org/XML/1998/namespace}'
DOCBOOKMAP = {None: "http://docbook.org/ns/docbook"}
section = etree.Element("section", nsmap=DOCBOOKMAP, version="5.0")
id = "%(project)s-conf-changes-%(release)s" % {'project': project,
'release': release.lower()}
section.set(XMLNS + 'id', id)
section.append(etree.Comment(" Warning: Do not edit this file. It is "
"automatically generated and your changes "
"will be overwritten. The tool to do so "
"lives in the openstack-doc-tools "
"repository. "))
title = etree.Element("title")
title.text = ("New, updated, and deprecated options "
"in %(release)s for %(project)s" %
{'release': release,
'project': CODENAME_TITLE[project]})
section.append(title)
env = {
'release': release,
'project': project,
'codename': CODENAME_TITLE[project],
'new_opts': [],
'new_defaults': [],
'deprecated_opts': []
}
# New options
if new_opts:
table = dbk_append_table(section, "New options", 2)
dbk_append_header(table, ["Option = default value",
"(Type) Help string"])
for name in sorted(new_opts, _cmpopts):
opt = new_list[name][1]
name = format_option_name(name)
cells = ["%(name)s = %(default)s" % {'name': name,
cells = ("%(name)s = %(default)s" % {'name': name,
'default': opt['default']},
"(%(type)s) %(help)s" % {'type': opt['type'],
'help': opt['help']}]
dbk_append_row(table, cells)
'help': opt['help']})
env['new_opts'].append(cells)
# Updated default
if changed_default:
table = dbk_append_table(section, "New default values", 3)
dbk_append_header(table, ["Option", "Previous default value",
"New default value"])
for name in sorted(changed_default, _cmpopts):
# New defaults
if new_defaults:
for name in sorted(new_defaults, _cmpopts):
old_default = old_list[name][1]['default']
new_default = new_list[name][1]['default']
if isinstance(old_default, list):
@ -291,29 +231,17 @@ def generate_docbook(project, new_branch, old_list, new_list):
if isinstance(new_default, list):
new_default = ", ".join(new_default)
name = format_option_name(name)
cells = [name, old_default, new_default]
dbk_append_row(table, cells)
cells = (name, old_default, new_default)
env['new_defaults'].append(cells)
# Deprecated options
if deprecated_opts:
table = dbk_append_table(section, "Deprecated options", 2)
dbk_append_header(table, ["Deprecated option", "New Option"])
for deprecated, new in deprecated_opts:
deprecated = format_option_name(deprecated)
new = format_option_name(new)
dbk_append_row(table, [deprecated, new])
env['deprecated_opts'].append((deprecated, new))
# No new, updated and deprecated options
if not new_opts and not changed_default and not deprecated_opts:
para = etree.Element("para")
para.text = ("There are no new, updated, and deprecated options "
"in %(release)s for %(project)s." %
{'release': release,
'project': CODENAME_TITLE[project]})
section.append(para)
return etree.tostring(section, pretty_print=True, xml_declaration=True,
encoding="UTF-8")
return env
def main():
@ -359,14 +287,20 @@ def main():
new_list = get_options(project, args.new_branch, args)
release = args.new_branch.replace('stable/', '')
xml = generate_docbook(project, release, old_list, new_list)
env = get_env(project, release, old_list, new_list)
filename = ("%(project)s-conf-changes.xml" %
{'project': project, 'release': release})
tmpl_file = 'templates/changes.docbook.j2'
if not os.path.exists(args.target):
os.makedirs(args.target)
dest = os.path.join(args.target, filename)
with open(tmpl_file) as fd:
template = jinja2.Template(fd.read(), trim_blocks=True)
output = template.render(**env)
with open(dest, 'w') as fd:
fd.write(xml)
fd.write(output)
return 0

View File

@ -1,3 +1,4 @@
jinja2
GitPython>=0.3.2.RC1
lxml
oslo.config

View File

@ -0,0 +1,70 @@
<?xml version='1.0' encoding='UTF-8'?>
<section xmlns="http://docbook.org/ns/docbook" version="5.0" xml:id="{{ project }}-conf-changes-{{ release|lower }}">
<!-- Warning: Do not edit this file. It is automatically generated and your changes will be overwritten. The tool to do so lives in the openstack-doc-tools repository. -->
<title>New, updated, and deprecated options in {{ release }} for {{ codename }}</title>
{% if new_opts %}
<table>
<caption>New options</caption>
<col width="50%"/>
<col width="50%"/>
<thead>
<tr>
<td>Option = default value</td>
<td>(Type) Help string</td>
</tr>
</thead>
{% for cells in new_opts %}
<tr>
{% for cell in cells %}
<td>{{ cell }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
{% endif %}
{% if new_defaults %}
<table>
<caption>New default values</caption>
<col width="33%"/>
<col width="33%"/>
<col width="33%"/>
<thead>
<tr>
<td>Option</td>
<td>Previous default value</td>
<td>New default value</td>
</tr>
</thead>
{% for cells in new_defaults %}
<tr>
{% for cell in cells %}
<td>{{ cell }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
{% endif %}
{% if deprecated_opts %}
<table>
<caption>Deprecated options</caption>
<col width="50%"/>
<col width="50%"/>
<thead>
<tr>
<td>Deprecated option</td>
<td>New Option</td>
</tr>
</thead>
{% for cells in deprecated_opts %}
<tr>
{% for cell in cells %}
<td>{{ cell }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
{% endif %}
{% if not new_opts and not new_defaults and not deprecated_opts %}
<para>There are no new, updated, and deprecated options in {{ release }} for {{ codename }}.</para>
{% endif %}
</section>