From f2b4ec165960c3fc8bfbfa7eaebf3eed2e055524 Mon Sep 17 00:00:00 2001 From: Steve Kowalik Date: Fri, 31 Jul 2015 12:26:00 +1000 Subject: [PATCH] Add a register-zanata-projects script Currently, projects in Transifex are set up by hand, which requires infrastructure members log into its admin interface and add projects. With Zanata, we can do much better, so add a script that will parse projects.yaml, and create any projects that are required. This grew out of Ic28d5dcfdd471dccbe406955a85dbc6f5d7335d1, but moves the configuration for the project from translate: true to underneath options to match things like direct-release. Change-Id: Id78a590ea07b886afc0c98e309ff6df45f2240bf --- jeepyb/cmd/register_zanata_projects.py | 53 +++++++++++++++ jeepyb/projects.py | 7 ++ jeepyb/translations.py | 91 ++++++++++++++++++++++++++ requirements.txt | 1 + setup.cfg | 1 + 5 files changed, 153 insertions(+) create mode 100644 jeepyb/cmd/register_zanata_projects.py create mode 100755 jeepyb/translations.py diff --git a/jeepyb/cmd/register_zanata_projects.py b/jeepyb/cmd/register_zanata_projects.py new file mode 100644 index 0000000..355662a --- /dev/null +++ b/jeepyb/cmd/register_zanata_projects.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python +# Copyright (c) 2015 Hewlett-Packard Development Company, L.P. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import logging +import os + +import jeepyb.projects as p +import jeepyb.translations as t +import jeepyb.utils as u + +PROJECTS_YAML = os.environ.get('PROJECTS_YAML', '/home/gerrit2/projects.yaml') +ZANATA_URL = os.environ.get('ZANATA_URL') +ZANATA_USER = os.environ.get('ZANATA_USER') +ZANATA_KEY = os.environ.get('ZANATA_KEY') + +log = logging.getLogger('register_zanata_projects') + + +def main(): + logging.basicConfig(level=logging.ERROR, + format='%(asctime)-6s: %(name)s - %(levelname)s' + ' - %(message)s') + registry = u.ProjectsRegistry(PROJECTS_YAML) + rest_service = t.ZanataRestService(ZANATA_URL, ZANATA_USER, ZANATA_KEY) + log.info("Registering projects in Zanata") + for entry in registry.configs_list: + project = entry['project'] + if not p.has_translations(project): + continue + log.info("Processing project %s" % project) + (org, name) = project.split('/') + try: + translation_proect = t.TranslationProject(rest_service, name) + translation_proect.register() + except ValueError as e: + log.error(e) + + +if __name__ == "__main__": + main() diff --git a/jeepyb/projects.py b/jeepyb/projects.py index 81db5ba..5caaa66 100644 --- a/jeepyb/projects.py +++ b/jeepyb/projects.py @@ -75,6 +75,13 @@ def has_github(project_full_name): return True +def has_translations(project_full_name): + try: + return 'translate' in registry[project_full_name]['options'] + except KeyError: + return False + + def is_direct_release(project_full_name): try: return 'direct-release' in registry[project_full_name]['options'] diff --git a/jeepyb/translations.py b/jeepyb/translations.py new file mode 100755 index 0000000..b814c74 --- /dev/null +++ b/jeepyb/translations.py @@ -0,0 +1,91 @@ +# Copyright (c) 2015 Hewlett-Packard Development Company, L.P. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import json +try: + from urllib.parse import urljoin +except ImportError: + from urlparse import urljoin + +import requests + + +class ZanataRestService: + def __init__(self, url, username, api_key, verify=False): + self.url = url + self.verify = verify + content_type = 'application/json;charset=utf8' + self.headers = {'Accept': content_type, + 'Content-Type': content_type, + 'X-Auth-User': username, + 'X-Auth-Token': api_key} + + def _construct_url(self, url_fragment): + return urljoin(self.url, url_fragment) + + def query(self, url_fragment): + request_url = self._construct_url(url_fragment) + try: + return requests.get(request_url, verify=self.verify, + headers=self.headers) + except requests.exceptions.ConnectionError: + raise ValueError('Connection error') + + def push(self, url_fragment, data): + request_url = self._construct_url(url_fragment) + try: + return requests.put(request_url, verify=self.verify, + headers=self.headers, data=json.dumps(data)) + except requests.exceptions.ConnectionError: + raise ValueError('Connection error') + + +class TranslationProject: + def __init__(self, rest_service, project): + self.rest_service = rest_service + self.project = project + + def is_registered(self): + r = self.rest_service.query('/rest/projects/p/%s' % self.project) + return r.status_code == 200 + + def has_master(self): + r = self.rest_service.query( + '/rest/projects/p/%s/iterations/i/master' % self.project) + return r.status_code == 200 + + def register_project(self): + project_data = {u'defaultType': u'Gettext', u'status': u'ACTIVE', + u'id': self.project, u'name': self.project, + u'description': self.project.title()} + r = self.rest_service.push('/rest/projects/p/%s' % self.project, + project_data) + return r.status_code in (200, 201) + + def register_master_iteration(self): + iteration = {u'status': u'ACTIVE', u'projectType': u'Gettext', + u'id': u'master'} + r = self.rest_service.push( + '/rest/projects/p/%s/iterations/i/master' % self.project, + iteration) + return r.status_code in (200, 201) + + def register(self): + if not self.is_registered(): + if not self.register_project(): + raise ValueError('Failed to register project.') + if not self.has_master(): + if not self.register_master_iteration(): + raise ValueError('Failed to register master iteration.') diff --git a/requirements.txt b/requirements.txt index 32faa12..65d5218 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,4 +9,5 @@ pyyaml pkginfo PyRSS2Gen python-swiftclient +requests>=2.5.2 six>=1.7.0 diff --git a/setup.cfg b/setup.cfg index 12e056e..937ebee 100644 --- a/setup.cfg +++ b/setup.cfg @@ -25,6 +25,7 @@ console_scripts = notify-impact = jeepyb.cmd.notify_impact:main openstackwatch = jeepyb.cmd.openstackwatch:main process-cache = jeepyb.cmd.process_cache:main + register-zanata-projects = jeepyb.cmd.register_zanata_projects:main trivial-rebase = jeepyb.cmd.trivial_rebase:main update-blueprint = jeepyb.cmd.update_blueprint:main update-bug = jeepyb.cmd.update_bug:main