Make syntax checks of dashboard files possible

Change-Id: I587a06271c85a4f57f42041142d2aa20aea1a319
This commit is contained in:
Christian Berendt 2014-08-08 17:08:58 +02:00
parent b7cc43aaf4
commit b7f0471b3e
2 changed files with 75 additions and 38 deletions

View File

@ -69,9 +69,14 @@ def get_options():
parser = argparse.ArgumentParser(
description='Create a Gerrit dashboard URL from specified dashboard '
'definition files')
parser.add_argument('dashboard_files', nargs='+',
metavar='dashboard_file',
help='Dashboard definition file to create URL from')
parser.add_argument('dashboard_paths', nargs='+',
metavar='dashboard_path',
help='Path to a dashboard definition file or a '
'directory containing a set of dashboard '
'definition files with the file suffix .dash.')
parser.add_argument('--check-only', default=False, action="store_true",
help='Only check the syntax of the specified '
'dasbhoard files')
parser.add_argument('--template', default='single.txt',
help='Name of template')
parser.add_argument('--template-directory',
@ -83,10 +88,14 @@ def get_options():
return parser.parse_args()
def read_dashboard_file(fname):
def read_dashboard_file(dashboard_file):
"""Read and parse a dashboard definition from a specified file."""
if (not os.path.isfile(dashboard_file) or
not os.access(dashboard_file, os.R_OK)):
raise ValueError("dashboard file '%s' is missing or "
"is not readable" % dashboard_file)
dashboard = configparser.ConfigParser()
dashboard.readfp(open(fname))
dashboard.readfp(open(dashboard_file))
return dashboard
@ -118,49 +127,24 @@ def get_configuration(dashboard):
return result
def main():
"""Entrypoint."""
def generate_dashboard_urls(dashboards, template):
"""Prints the dashboard URLs of a set of dashboards."""
result = 0
opts = get_options()
template = load_template(
template_file=opts.template_file,
template_directory=opts.template_directory,
template_name=opts.template
)
if not template:
return 1
for dashboard_file in opts.dashboard_files:
if (not os.path.isfile(dashboard_file) or
not os.access(dashboard_file, os.R_OK)):
print("\nerror: dashboard file '%s' is missing or is not "
"readable" % dashboard_file)
result = 1
continue
try:
dashboard = read_dashboard_file(dashboard_file)
except configparser.Error as e:
print("\nerror: dashboard file '%s' cannot be parsed\n\n%s" %
(dashboard_file, e))
return 1
continue
for dashboard_file in dashboards:
dashboard = dashboards[dashboard_file]
try:
url = generate_dashboard_url(dashboard)
except ValueError as e:
print("\nerror:\tgenerating dashboard '%s' failed\n\t%s" %
(dashboard_file, e))
raise ValueError("generating dashboard '%s' failed: %s" %
(dashboard_file, e))
result = 1
continue
variables = {
'url': url,
'title': dashboard.get('dashboard', 'title') or None,
'description': dashboard.get('dashboard', 'description') or None,
'url': url,
'filename': dashboard_file,
'configuration': get_configuration(dashboard)
}
print(template.render(variables))
@ -168,5 +152,55 @@ def main():
return result
def load_dashboards(paths):
"""Load specified dashboards from files or directories."""
dashboards = {}
for dashboard_path in paths:
dashboard_files = []
if os.path.isdir(dashboard_path):
for root, dirs, files in os.walk(dashboard_path):
for file in files:
if file.endswith('.dash'):
dashboard_files.append(os.path.join(root, file))
else:
dashboard_files.append(dashboard_path)
for dashboard_file in dashboard_files:
try:
dashboards[dashboard_file] = read_dashboard_file(
dashboard_file
)
except configparser.Error as e:
raise ValueError("dashboard file '%s' cannot be "
"parsed: %s" % (dashboard_file, e))
return dashboards
def main():
"""Entrypoint."""
opts = get_options()
template = None
if not opts.check_only:
template = load_template(
template_file=opts.template_file,
template_directory=opts.template_directory,
template_name=opts.template
)
try:
dashboards = load_dashboards(opts.dashboard_paths)
if not opts.check_only and template:
generate_dashboard_urls(dashboards, template)
elif not opts.check_only and not template:
return 1
except ValueError as e:
print("error: %s" % e)
return 1
return 0
if __name__ == '__main__':
sys.exit(main())

View File

@ -1,6 +1,6 @@
[tox]
minversion = 1.6
envlist = py33,py26,py27,pypy,pep8
envlist = py33,py26,py27,pypy,pep8,checksyntax
skipsdist = True
[testenv]
@ -18,6 +18,9 @@ commands = flake8
[testenv:venv]
commands = {posargs}
[testenv:checksyntax]
commands = python gerrit_dash_creator/cmd/creator.py --check-only dashboards
[testenv:cover]
commands = python setup.py testr --coverage --testr-args='{posargs}'