From 6698ffe6a4efdfe139761758b3dc6c325058dd50 Mon Sep 17 00:00:00 2001 From: Tristan Cacqueray Date: Mon, 12 Sep 2016 08:37:13 +0000 Subject: [PATCH] Use gerrit fullname in list rendering This change improves candidate list rendering using gerrit to retrieve fullname. The list rendering now uses the openstack_election libraries and some part have been refactored accordingly. Change-Id: Iccba3e4c529740758323b51548a2144fc7c77879 --- doc/source/_exts/candidates.py | 34 +++++++----------------- doc/source/_exts/ptl.jinja | 2 +- doc/source/_exts/tc.jinja | 2 +- doc/source/candidates | 1 - doc/source/index.rst | 4 +-- openstack_election/check_candidacy.py | 2 +- openstack_election/utils.py | 38 +++++++++++++++++++++++++-- 7 files changed, 50 insertions(+), 33 deletions(-) delete mode 120000 doc/source/candidates diff --git a/doc/source/_exts/candidates.py b/doc/source/_exts/candidates.py index 9796a9e5..9511a0d9 100644 --- a/doc/source/_exts/candidates.py +++ b/doc/source/_exts/candidates.py @@ -14,20 +14,11 @@ """ import os -import subprocess from jinja2 import FileSystemLoader from jinja2.environment import Environment -BASE_URL = "http://git.openstack.org/cgit/openstack/election" -PATH_PREFIX = 'candidates' - - -def get_fullname(filepath, exceptions): - if filepath in exceptions: - return exceptions[filepath] - return subprocess.Popen(["git", "log", "--format=%aN", filepath], - stdout=subprocess.PIPE).stdout.readlines()[-1][:-1] +from openstack_election import utils def render_template(template, data, **kwargs): @@ -38,18 +29,12 @@ def render_template(template, data, **kwargs): return template.render(data) -def build_candidates_list(election): - project_list = os.listdir(os.path.join(PATH_PREFIX, election)) +def build_candidates_list(election=utils.SERIES_NAME): + project_list = os.listdir(os.path.join(utils.CANDIDATE_PATH, election)) project_list.sort() - exceptions = {} - for e in open("exceptions.txt").readlines(): - if e[0] == "#" or ":" not in e: - continue - exceptions[e.split(':')[0]] = " ".join(e.split(':')[1:])[:-1].strip() - candidates_lists = {} for project in project_list: - project_prefix = os.path.join(PATH_PREFIX, election, project) + project_prefix = os.path.join(utils.CANDIDATE_PATH, election, project) file_list = filter( lambda x: x.endswith(".txt"), os.listdir(unicode(project_prefix)), @@ -59,9 +44,10 @@ def build_candidates_list(election): filepath = os.path.join(project_prefix, candidate_file) candidates_list.append( { - 'path': filepath, + 'url': '%s/openstack/election/plain/%s' % (utils.CGIT_URL, + filepath), 'ircname': os.path.basename(filepath)[:-4], - 'fullname': get_fullname(filepath, exceptions) + 'fullname': utils.get_fullname(filepath) }) candidates_list.sort(key=lambda x: x['fullname']) @@ -73,7 +59,7 @@ def build_candidates_list(election): def render_list(list_type, candidates_list): - output_file = os.path.join(PATH_PREFIX, "%s.rst" % list_type) + output_file = os.path.join(utils.CANDIDATE_PATH, "%s.rst" % list_type) template_name = "%s.jinja" % list_type template_dir = os.path.join(".", "doc", "source", "_exts") with open(output_file, "w") as out: @@ -87,9 +73,7 @@ def render_list(list_type, candidates_list): def build_lists(app): - # TODO: make election a parameter - election = 'ocata' - candidates_list = build_candidates_list(election) + candidates_list = build_candidates_list() render_list("ptl", candidates_list) render_list("tc", candidates_list) diff --git a/doc/source/_exts/ptl.jinja b/doc/source/_exts/ptl.jinja index fb69b0db..7713479a 100644 --- a/doc/source/_exts/ptl.jinja +++ b/doc/source/_exts/ptl.jinja @@ -4,7 +4,7 @@ * {{ project.replace('_', ' ') }} {% for candidate in candidates[project] %} - * `{{ candidate['fullname'] }} ({{ candidate['ircname'] }}) `_ + * `{{ candidate['fullname'] }} ({{ candidate['ircname'] }}) <{{ candidate['url'] }}>`_ {% endfor %} {% endif %}{% endfor %} diff --git a/doc/source/_exts/tc.jinja b/doc/source/_exts/tc.jinja index e87ef01d..cf9fff98 100644 --- a/doc/source/_exts/tc.jinja +++ b/doc/source/_exts/tc.jinja @@ -2,5 +2,5 @@ ====================== {% for candidate in candidates['TC'] %} - * `{{ candidate['fullname'] }} ({{ candidate['ircname'] }}) `_ + * `{{ candidate['fullname'] }} ({{ candidate['ircname'] }}) <{{ candidate['url'] }}>`_ {% endfor %} diff --git a/doc/source/candidates b/doc/source/candidates deleted file mode 120000 index 19f22900..00000000 --- a/doc/source/candidates +++ /dev/null @@ -1 +0,0 @@ -../../candidates/ \ No newline at end of file diff --git a/doc/source/index.rst b/doc/source/index.rst index 4cc2b4ee..fca24409 100644 --- a/doc/source/index.rst +++ b/doc/source/index.rst @@ -8,14 +8,14 @@ OpenStack Election ================== -.. include:: ./candidates/events.rst +.. include:: ../../candidates/events.rst See `Election system`_, `PTL details`_ and `TC details`_. Below is the official list of candidates for the current round. .. TODO: Change from ptl.rst to tc.rst when TC rounds starts -.. include:: ./candidates/ptl.rst +.. include:: ../../candidates/ptl.rst .. TODO: Adds TC Results and a link to `TC Candidates list`_ when TC candidacies are over .. TODO: Adds PTL Results and a link to `PTL Candidates list`_ when PTL candidacies are over diff --git a/openstack_election/check_candidacy.py b/openstack_election/check_candidacy.py index 043abfd0..117bc48a 100755 --- a/openstack_election/check_candidacy.py +++ b/openstack_election/check_candidacy.py @@ -59,7 +59,7 @@ def check_candidate(project_name, email, projects, limit=1): (repo_name, email)) for review in utils.get_reviews(query): url = ('%s/%s/commit/?id=%s' % ( - utils.BASE_URL, review['project'], + utils.CGIT_URL, review['project'], review['current_revision'])) print('%2d: %s %s' % (found, pretty_datetime(review['submitted']), diff --git a/openstack_election/utils.py b/openstack_election/utils.py index 1f04a037..32dc96f4 100644 --- a/openstack_election/utils.py +++ b/openstack_election/utils.py @@ -20,6 +20,7 @@ import os import pickle import pytz import requests +import subprocess import time import urllib import yaml @@ -37,9 +38,21 @@ PROJECTS_TAG = 'sept-2016-elections' CANDIDATE_PATH = 'candidates' GERRIT_BASE = 'https://review.openstack.org' ELECTION_REPO = 'openstack/election' -BASE_URL = 'https://git.openstack.org/cgit' +CGIT_URL = 'https://git.openstack.org/cgit' PROJECTS_URL = ('%s/openstack/governance/plain/reference/projects.yaml' % - (BASE_URL)) + (CGIT_URL)) + +exceptions = None + + +# Generic functions +def load_exceptions(): + global exceptions + exceptions = {} + for e in open("exceptions.txt").readlines(): + if e[0] == "#" or ":" not in e: + continue + exceptions[e.split(':')[0]] = " ".join(e.split(':')[1:])[:-1].strip() # Gerrit functions @@ -56,6 +69,27 @@ def gerrit_query(url): return data +def get_email(filepath): + return subprocess.Popen(["git", "log", "--format=%aE", filepath], + stdout=subprocess.PIPE).stdout.readlines()[-1][:-1] + + +def get_fullname(filepath): + # Check if filepath is an exception + if exceptions is None: + load_exceptions() + if filepath in exceptions: + return exceptions[filepath] + + # Otherwise query gerrit using git log email + email = get_email(filepath) + url = '%s/accounts/%s' % (GERRIT_BASE, email) + fullname = gerrit_query(url)['name'] + + # Return capitalized name + return u" ".join(map(unicode.capitalize, fullname.split())) + + def get_reviews(query): opts = ['CURRENT_REVISION', 'CURRENT_FILES', 'DETAILED_ACCOUNTS'] opts_str = '&o=%s' % ('&o='.join(opts))