Make the OBS class a bit more PEP8-compilant

This commit is contained in:
Alberto Planas 2014-03-05 16:22:14 +01:00
parent 561ea4bf6d
commit 4608d12b69

View File

@ -6,15 +6,9 @@
import os
import sys
import contextlib
import httpretty
import xml.etree.ElementTree as ET
import time
# mock is part of python3.3
try:
import unittest.mock as mock
except ImportError:
import mock
from string import Template
import oscs
@ -31,7 +25,8 @@ if PY3:
else:
string_types = basestring,
class OBS:
class OBS(object):
"""
Class trying to simulate a simple OBS
"""
@ -64,37 +59,32 @@ class OBS:
"""
Resets states
"""
# XXX TODO Write fixtures in an external file, or recreate
# this data from other fixtures
# Initial request data
self.requests_data = { '123': { 'request': 'new', 'review': 'accepted',
'who': 'Admin', 'by': 'group', 'id': '123',
'by_who': 'opensuse-review-team',
'package': 'gcc' },
'321': { 'request': 'review', 'review': 'new',
'who': 'Admin', 'by': 'group', 'id': '321',
'by_who': 'factory-staging',
'package': 'puppet' },
'333': { 'request': 'review', 'review': 'new',
'who': 'Admin', 'by': 'project', 'id': '333',
'by_who': 'openSUSE:Factory:Staging:B',
'package': 'wine' }
self.requests_data = {
'123': {'request': 'new', 'review': 'accepted', 'who': 'Admin', 'by': 'group',
'id': '123', 'by_who': 'opensuse-review-team', 'package': 'gcc'},
'321': {'request': 'review', 'review': 'new', 'who': 'Admin', 'by': 'group',
'id': '321', 'by_who': 'factory-staging', 'package': 'puppet'},
'333': {'request': 'review', 'review': 'new', 'who': 'Admin', 'by': 'project',
'id': '333', 'by_who': 'openSUSE:Factory:Staging:B', 'package': 'wine'}
}
self.st_project_data = { 'A': { 'project': 'openSUSE:Factory:Staging:A',
'title': '', 'description': '' },
'U': { 'project': 'openSUSE:Factory:Staging:U',
'title': 'Unfrozen', 'description': '' },
'B': { 'project': 'openSUSE:Factory:Staging:B',
'title': 'wine',
self.st_project_data = {
'A': {'project': 'openSUSE:Factory:Staging:A', 'title': '', 'description': ''},
'U': {'project': 'openSUSE:Factory:Staging:U', 'title': 'Unfrozen', 'description': ''},
'B': {'project': 'openSUSE:Factory:Staging:B', 'title': 'wine',
'description': 'requests:\n- {id: 333, package: wine}'}
}
self.links_data = { 'openSUSE:Factory:Staging:B/wine':
{ 'prj': 'openSUSE:Factory:Staging:B',
'pkg': 'wine', 'devprj': 'home:Admin' }
self.links_data = {
'openSUSE:Factory:Staging:B/wine': {
'prj': 'openSUSE:Factory:Staging:B', 'pkg': 'wine', 'devprj': 'home:Admin'
}
self.pkg_data = { 'home:Admin/gcc':
{ 'rev': '1', 'vrev': '1', 'name': 'gcc',
}
self.pkg_data = {
'home:Admin/gcc': {'rev': '1', 'vrev': '1', 'name': 'gcc',
'srcmd5': 'de7a9f5e3bedb01980465f3be3d236cb'},
'home:Admin/wine':
{ 'rev': '1', 'vrev': '1', 'name': 'wine',
'home:Admin/wine': {'rev': '1', 'vrev': '1', 'name': 'wine',
'srcmd5': 'de7a9f5e3bedb01980465f3be3d236cb'}
}
@ -135,10 +125,10 @@ class OBS:
path = re.match(r'.*localhost([^?]*)(\?.*)?', uri).group(1)
reply = None
# Try to find a fallback
if self.responses['ALL'].has_key(path):
if path in self.responses['ALL']:
reply = self.responses['ALL'][path]
# Try to find a specific method
if self.responses[request.method].has_key(path):
if path in self.responses[request.method]:
reply = self.responses[request.method][path]
# We have something to reply with
if reply:
@ -167,9 +157,10 @@ class OBS:
if len(path) == 0:
path = uri
if len(path) > 1:
# XXX Warning. This ret is not returned
ret = self._pretty_callback(request, 'https://localhost' + posixpath.dirname(path), headers, False)
if exception:
raise BaseException("No tests/obs.pyresponse for {0} on {1} provided".format(request.method, uri))
raise BaseException("No tests/obs.pyresponse for {} on {} provided".format(request.method, uri))
else:
return None
@ -190,8 +181,8 @@ class OBS:
# Testing of frozen packages
tmpl = Template(self._get_fixture_content('project-f-metalist.xml'))
self.responses['GET']['/source/openSUSE:Factory:Staging:B/_project'] = tmpl.substitute({'mtime': str(int(time.time()) - 100)})
self.responses['GET']['/source/openSUSE:Factory:Staging:A/_project'] = tmpl.substitute({'mtime': str(int(time.time()) - 3600*24*356)})
self.responses['GET']['/source/openSUSE:Factory:Staging:B/_project'] = tmpl.substitute({'mtime': int(time.time()) - 100})
self.responses['GET']['/source/openSUSE:Factory:Staging:A/_project'] = tmpl.substitute({'mtime': int(time.time()) - 3600*24*356})
self.responses['GET']['/source/openSUSE:Factory:Staging:U/_project'] = 'project-u-metalist.xml'
def _build_results(self):
@ -202,18 +193,18 @@ class OBS:
def build_results(responses, request, uri):
ret_str = '<resultlist state="c7856c90c70c53fae88aacec964b80c0">\n'
prj = re.match(r'.*/([^/]*)/_result', uri).group(1)
if prj in [ "openSUSE:Factory:Staging:B" ]:
if prj == 'openSUSE:Factory:Staging:B':
states = ['failed', 'broken', 'building']
else:
states = ['excluded', 'succeeded']
for st in states:
if st in [ 'building' ]:
if st == 'building':
ret_str += ' <result project="{0}" repository="{1}" arch="x86_64" code="{2}" state="{2}">\n'.format(prj, st, st)
else:
ret_str += ' <result project="{0}" repository="{1}" arch="x86_64" code="{2}" state="{2}">\n'.format(prj, st, "published")
for dt in self.links_data:
if self.links_data[dt]['prj'] == prj:
ret_str += ' <status package="{0}" code="{1}" />\n'.format(self.links_data[dt]['pkg'], st)
ret_str += ' <status package="{}" code="{}" />\n'.format(self.links_data[dt]['pkg'], st)
ret_str += ' </result>\n'
ret_str += '</resultlist>\n'
return ret_str
@ -238,9 +229,7 @@ class OBS:
self.responses['ALL']['/source/openSUSE:Factory:Staging:' + pr + '/_meta'] = project_meta_change
def _request_review(self):
"""
Register requests methods
"""
"""Register requests methods."""
# Load template
tmpl = Template(self._get_fixture_content('request_review.xml'))
@ -250,19 +239,19 @@ class OBS:
rq_id = re.match(r'.*/([0-9]+)', uri).group(1)
args = self.requests_data[rq_id]
# Adding review
if request.querystring.has_key(u'cmd') and request.querystring[u'cmd'] == [u'addreview']:
if 'cmd' in request.querystring and 'addreview' in request.querystring['cmd']:
self.requests_data[rq_id]['request'] = 'review'
self.requests_data[rq_id]['review'] = 'new'
# Changing review
if request.querystring.has_key(u'cmd') and request.querystring[u'cmd'] == [u'changereviewstate']:
if 'cmd' in request.querystring and 'changereviewstate' in request.querystring[u'cmd']:
self.requests_data[rq_id]['request'] = 'new'
self.requests_data[rq_id]['review'] = str(request.querystring[u'newstate'][0])
# Project review
if request.querystring.has_key(u'by_project'):
if 'by_project' in request.querystring:
self.requests_data[rq_id]['by'] = 'project'
self.requests_data[rq_id]['by_who'] = str(request.querystring[u'by_project'][0])
# Group review
if request.querystring.has_key(u'by_group'):
if 'by_group' in request.querystring:
self.requests_data[rq_id]['by'] = 'group'
self.requests_data[rq_id]['by_who'] = str(request.querystring[u'by_group'][0])
responses['GET']['/request/' + rq_id] = tmpl.substitute(self.requests_data[rq_id])
@ -277,8 +266,8 @@ class OBS:
def _pkg_sources(self):
def pkg_source(responses, request, uri):
key = str(re.match( r'.*/source/([^?]+)(\?.*)?',uri).group(1))
return '<directory name="{0}" rev="{1}" vrev="{2}" srcmd5="{3}"/>'.format(
key = re.match(r'.*/source/([^?]+)(\?.*)?', uri).group(1)
return '<directory name="{}" rev="{}" vrev="{}" srcmd5="{}"/>'.format(
self.pkg_data[key]['name'],
self.pkg_data[key]['rev'],
self.pkg_data[key]['vrev'],
@ -307,7 +296,9 @@ class OBS:
key = re.match(r'.*/source/(.+)/_link', uri).group(1)
match = re.match(r'(.+)/(.+)', key)
xml = ET.fromstring(str(request.body))
self.links_data[str(key)] = { 'prj': match.group(1), 'pkg': match.group(2),
self.links_data[str(key)] = {
'prj': match.group(1),
'pkg': match.group(2),
'devprj': xml.get('project')
}
self.responses['GET']['/source/' + key] = tmpl.substitute(self.links_data[key])
@ -331,7 +322,7 @@ class OBS:
"""
def request_search(responses, request, uri):
# Searching for requests that has open review for staging group
if request.querystring.has_key(u'match') and request.querystring[u'match'][0] == u"state/@name='review' and review[@by_group='factory-staging' and @state='new']":
if 'match' in request.querystring and request.querystring['match'][0] == u"state/@name='review' and review[@by_group='factory-staging' and @state='new']":
rqs = []
# Itereate through all requests
for rq in self.requests_data:
@ -345,7 +336,7 @@ class OBS:
ret_str += '</collection>'
return ret_str
# Searching for requests that has open review for staging project
if request.querystring.has_key(u'match') and re.match( r"state/@name='review' and review\[@by_project='([^']+)' and @state='new'\]", request.querystring[u'match'][0]):
if 'match' in request.querystring and re.match(r"state/@name='review' and review\[@by_project='([^']+)' and @state='new'\]", request.querystring[u'match'][0]):
prj_match = re.match(r"state/@name='review' and review\[@by_project='([^']+)' and @state='new'\]", request.querystring[u'match'][0])
prj = str(prj_match.group(1))
rqs = []
@ -365,7 +356,7 @@ class OBS:
def id_project_search(responses, request, uri):
# Searching for project
if request.querystring.has_key(u'match') and request.querystring[u'match'][0] == u"starts-with(@name,\'openSUSE:Factory:Staging:\')":
if 'match' in request.querystring and request.querystring['match'][0] == u"starts-with(@name,\'openSUSE:Factory:Staging:\')":
ret_str = '<collection matches="' + str(len(self.st_project_data)) + '">\n'
# Itereate through all requests
for prj in self.st_project_data:
@ -410,4 +401,3 @@ class OBS:
content = response.read()
response.close()
return content