Improve gerrit_dash_creator.cmd.creator
* use configparser instead of self written parsing (add new method read_dashboard_file) * remove methods get_title and get_sections * add requirement six (ConfigParser in Python2, configparser in Python3) * remove import re and fileinput * check if dashboard file exist and is readable before trying to parse it (new imports os and os.path) * slighly change the output to be more informative * add new parameter in section dashboard to make the base URL configurable * check if necessary options in the used dashboard file are available * improve exception handling and error output * rename generate_url to generate_dashboard_url * use correct exit codes (correct return values in the main method) * rename internal argument name dash to dashboard_file * add missing doc strings Change-Id: Ifcaf20f7495b5619db30ac0046909b334672f756
This commit is contained in:
parent
b2b77d6cd1
commit
63cee294ab
|
@ -13,76 +13,97 @@
|
|||
# under the License.
|
||||
|
||||
import argparse
|
||||
import fileinput
|
||||
import re
|
||||
import os
|
||||
import os.path
|
||||
import sys
|
||||
import urllib
|
||||
|
||||
from six.moves import configparser
|
||||
|
||||
|
||||
def escape_comma(buff):
|
||||
"""Because otherwise Firefox is a sad panda."""
|
||||
return buff.replace(',', '%2c')
|
||||
|
||||
|
||||
def get_title(fname):
|
||||
title = ""
|
||||
foreach = ""
|
||||
for line in fileinput.input(fname):
|
||||
m = re.match("title = (.+)", line)
|
||||
if m:
|
||||
title = m.group(1)
|
||||
def generate_dashboard_url(dashboard):
|
||||
"""Generate a dashboard URL from a given definition."""
|
||||
try:
|
||||
title = dashboard.get('dashboard', 'title')
|
||||
except configparser.NoOptionError:
|
||||
raise ValueError("option 'title' in section 'dashboard' not set")
|
||||
|
||||
m = re.match("foreach = (.+)", line)
|
||||
if m:
|
||||
foreach = escape_comma(m.group(1))
|
||||
fileinput.close()
|
||||
return title, foreach
|
||||
try:
|
||||
foreach = escape_comma(dashboard.get('dashboard', 'foreach'))
|
||||
except configparser.NoOptionError:
|
||||
raise ValueError("option 'foreach' in section 'dashboard' not set")
|
||||
|
||||
try:
|
||||
baseurl = dashboard.get('dashboard', 'baseurl')
|
||||
except configparser.NoOptionError:
|
||||
baseurl = 'https://review.openstack.org/#/dashboard/?'
|
||||
|
||||
def get_sections(fname):
|
||||
sections = []
|
||||
sname = None
|
||||
for line in fileinput.input(fname):
|
||||
m = re.match('\[section "([^"]+)', line)
|
||||
if m:
|
||||
sname = m.group(1)
|
||||
elif sname:
|
||||
m = re.match("query = (.+)", line)
|
||||
if m:
|
||||
query = escape_comma(m.group(1))
|
||||
|
||||
sections.append({'title': sname, 'query': query})
|
||||
fileinput.close()
|
||||
return sections
|
||||
|
||||
|
||||
def gen_url(title, foreach, sections):
|
||||
base = 'https://review.openstack.org/#/dashboard/?'
|
||||
base = baseurl
|
||||
base += urllib.urlencode({'title': title, 'foreach': foreach})
|
||||
base += '&'
|
||||
encoded = [urllib.urlencode({x['title']: x['query']}) for x in sections]
|
||||
base += '&'.join(encoded)
|
||||
for section in dashboard.sections():
|
||||
if not section.startswith('section'):
|
||||
continue
|
||||
|
||||
try:
|
||||
query = dashboard.get(section, 'query')
|
||||
except configparser.NoOptionError:
|
||||
raise ValueError("option 'query' in '%s' not set" % section)
|
||||
|
||||
title = section[9:-1]
|
||||
encoded = urllib.urlencode({title: query})
|
||||
base += "&%s" % encoded
|
||||
return base
|
||||
|
||||
|
||||
def get_options():
|
||||
"""Parse command line arguments and options."""
|
||||
parser = argparse.ArgumentParser(
|
||||
description='Create a gerrit dashboard URL from a dashboard '
|
||||
'definition')
|
||||
parser.add_argument('dash',
|
||||
description='Create a Gerrit dashboard URL from a dashboard '
|
||||
'definition file')
|
||||
parser.add_argument('dashboard_file',
|
||||
metavar='dashboard_file',
|
||||
help="Dashboard file to create url from")
|
||||
help='Dashboard definition file to create URL from')
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
def read_dashboard_file(fname):
|
||||
"""Read and parse a dashboard definition from a specified file."""
|
||||
dashboard = configparser.ConfigParser()
|
||||
dashboard.readfp(open(fname))
|
||||
return dashboard
|
||||
|
||||
|
||||
def main():
|
||||
"""Entrypoint."""
|
||||
opts = get_options()
|
||||
title, foreach = get_title(opts.dash)
|
||||
sections = get_sections(opts.dash)
|
||||
url = gen_url(title, foreach, sections)
|
||||
print("URL for %s" % title)
|
||||
|
||||
if (not os.path.isfile(opts.dashboard_file) or
|
||||
not os.access(opts.dashboard_file, os.R_OK)):
|
||||
print("error: dashboard file '%s' is missing or is not readable" %
|
||||
opts.dashboard_file)
|
||||
return 1
|
||||
|
||||
dashboard = read_dashboard_file(opts.dashboard_file)
|
||||
|
||||
try:
|
||||
url = generate_dashboard_url(dashboard)
|
||||
except ValueError as e:
|
||||
print("error:\tgenerating dashboard '%s' failed\n\t%s" %
|
||||
(opts.dashboard_file, e))
|
||||
return 1
|
||||
|
||||
print("Generated URL for the Gerrit dashboard '%s':" % opts.dashboard_file)
|
||||
print("")
|
||||
print(url)
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
||||
|
|
|
@ -1,2 +1,3 @@
|
|||
argparse
|
||||
pbr>=0.6,!=0.7,<1.0
|
||||
six>=1.7.0
|
||||
|
|
Loading…
Reference in New Issue