diff options
| author | Scott Bahling <sbahling@suse.de> | 2016-04-19 16:06:57 +0200 |
|---|---|---|
| committer | Scott Bahling <sbahling@suse.de> | 2016-04-19 16:06:57 +0200 |
| commit | 0827824cab43b7a5fd732f6ed5e94b44a745cc00 (patch) | |
| tree | f81a2b34093fc0cdb6b7cad6573386b354e9aa1f /obsapi | |
| parent | 3ea5bd07c23838b1a96357089e8173a094dfaed1 (diff) | |
| download | obsapi-0827824cab43b7a5fd732f6ed5e94b44a745cc00.tar.gz obsapi-0827824cab43b7a5fd732f6ed5e94b44a745cc00.tar.xz obsapi-0827824cab43b7a5fd732f6ed5e94b44a745cc00.zip | |
Update api core to use buildapi and sourceapiobsapi-0.0.5
Diffstat (limited to 'obsapi')
| -rw-r--r-- | obsapi/core.py | 144 | ||||
| -rw-r--r-- | obsapi/sourceapi.py | 4 |
2 files changed, 26 insertions, 122 deletions
diff --git a/obsapi/core.py b/obsapi/core.py index 0148796..f3c1281 100644 --- a/obsapi/core.py +++ b/obsapi/core.py @@ -1,11 +1,11 @@ # -*- coding: utf-8 -*- # -import requests -from requests.auth import HTTPBasicAuth from io import BytesIO from lxml import etree from collections import namedtuple from datetime import datetime +from obsapi.sourceapi import ObsSourceApi +from obsapi.buildapi import ObsBuildApi from obsapi.formatter import Formatter try: @@ -130,113 +130,24 @@ class ObsApi(object): def __init__(self, apiurl=None): self.apiurl = apiurl or DEFAULTAPIURL - self.__get_auth() - self._response = None - self.retries = 3 - self.verify_ssl = True - - def __get_auth(self): - conf = {} - if osc_conf: - try: - conf = osc_conf.get_apiurl_api_host_options(self.apiurl) - except: - pass - - user = conf.get('user', None) - password = conf.get('pass', None) - if user and password: - self.auth = HTTPBasicAuth(user, password) - else: - self.auth = None - - def __api_get(self, api, payload=None): - - def try_get(): - url = '{0}/{1}'.format(self.apiurl, api) - r = requests.get(url, - auth=self.auth, - params=payload, - verify=self.verify_ssl) - self._response = r - return r - - for attempt in range(self.retries): - r = try_get() - if self.success: - return r - print("Failed: %s" % self.response) - print("retry: %s" % (attempt + 1)) - - if self.retries == 0: - r = try_get() - - return r - - def __api_put(self, api, data): - - url = '{0}/{1}'.format(self.apiurl, api) - r = requests.put(url, - auth=self.auth, - data=data, - verify=self.verify_ssl) - self._response = r - - return r + self.source = ObsSourceApi(self.apiurl) + self.build = ObsBuildApi(self.apiurl) + self.source.retries = 3 + self.source.verify_ssl = True + self.build.retries = 3 + self.build.verify_ssl = True + self.lastapi = None def __xml2etree(self, xml): xml = xml.encode('utf-8') parser = etree.XMLParser(remove_blank_text=True) return etree.parse(BytesIO(xml), parser).xpath('.')[0] - @property - def response(self): - '''Return requests response from last api query''' - return self._response - - @property - def success(self): - '''Return True if last api query was successful, else - return False''' - return self._response.status_code == requests.codes.ok - - def get_xml(self, api, payload=None): - r = self.__api_get(api, payload) - if not self.success: - print self.response - return self.default_xml - - return r.text - - def put_xml(self, api, data): - r = self.__api_put(api, data) - return r - def get_meta(self, prj, pkg=None): - if pkg is not None: - api = '/source/{}/{}/_meta'.format(prj, pkg) - else: - api = '/source/{}/_meta'.format(prj) - return self.get_xml(api) + return self.source.get_meta(prj, pkg) def put_meta(self, prj, pkg=None, xml=None): - if pkg is not None: - api = '/source/{}/{}/_meta'.format(prj, pkg) - else: - api = '/source/{}/_meta'.format(prj) - return self.put_xml(api, data=xml) - - def get_project_meta(self, prj): - return self.get_meta(prj) - - def put_project_meta(self, prj, xml): - return self.put_meta(prj, xml) - - def get_package_meta(self, prj, pkg): - return self.get_meta(prj, pkg) - - def put_package_meta(self, prj, pkg, xml): - return self.put_meta(prj, pkg, xml) + return self.source.put_meta(prj, pkg, data=xml) def ls(self, prj=None, pkg=None, repo=None, arch=None): if repo and arch: @@ -249,16 +160,14 @@ class ObsApi(object): return None def project_ls(self, prj): - api = '/source/{}'.format(prj) - xml = self.get_xml(api) + xml = self.source.get(prj) d = etree.fromstring(xml) lsitems = [e.get('name') for e in d.findall('entry')] return lsitems def package_ls(self, prj, pkg): - api = '/source/{}/{}'.format(prj, pkg) - xml = self.get_xml(api) + xml = self.source.get(prj, pkg) d = etree.fromstring(xml) directory = Directory(d.get('name'), d.get('rev'), @@ -276,9 +185,7 @@ class ObsApi(object): return (directory, lsitems) def get_source_info(self, prj, pkg): - api = '/source/{}/{}'.format(prj, pkg) - query = {'view': 'info'} - xml = self.get_xml(api, payload=query) + xml = self.source.get(prj, pkg, view='info') tree = etree.fromstring(xml) sinfo = SourceInfo(tree.get('package'), tree.get('rev'), @@ -290,8 +197,7 @@ class ObsApi(object): return sinfo def binaries_ls(self, prj, pkg, repo, arch): - api = '/build/{}/{}/{}/{}'.format(prj, repo, arch, pkg) - xml = self.get_xml(api) + xml = self.build.get(prj, repo, arch, pkg) blist = etree.fromstring(xml) binaries = [Binary(filename=i.get('filename'), size=i.get('size'), @@ -301,14 +207,11 @@ class ObsApi(object): return binaries def get_binary_fileinfo(self, prj, pkg, repo, arch, binary): - payload = dict(view='fileinfo') - api = '/build/{}/{}/{}/{}/{}'.format(prj, repo, arch, pkg, binary) - xml = self.get_xml(api, payload) + xml = self.build.get(prj, repo, arch, pkg, binary, view='fileinfo') return FileInfo(xml) def get_project_repos(self, prj): - api = '/build/{}'.format(prj) - xml = self.get_xml(api) + xml = self.build.get(prj) directory = etree.fromstring(xml) entries = [] @@ -342,12 +245,10 @@ class ObsApi(object): return version def get_build_config(self, prj, repo): - api = '/build/{}/{}/_buildconfig'.format(prj, repo) - return self.get_xml(api) + return self.build.get_buildconfig(prj, repo) def get_build_info(self, prj, pkg, repo, arch): - api = '/build/{}/{}/{}/{}/_buildinfo'.format(prj, repo, arch, pkg) - return self.get_xml(api) + return self.build.get_buildinfo(prj, pkg, repo, arch) def get_nothing_provides(self, prj, pkg, repo, arch): tree = etree.fromstring(self.get_build_info(prj, pkg, repo, arch)) @@ -369,8 +270,11 @@ class ObsApi(object): ''' Attempt to get the value of the %vendor macro if exists Search build configs from all repos. Take first occurance. ''' - project_repos = self.get_project_repos(prj) - repos = [repo] or project_repos + if repo is not None: + repos = [repo] + else: + repos = self.get_project_repos(prj) + vendor = None for repo in repos: for line in self.get_build_config(prj, repo).splitlines(): diff --git a/obsapi/sourceapi.py b/obsapi/sourceapi.py index 4841ad9..23fee99 100644 --- a/obsapi/sourceapi.py +++ b/obsapi/sourceapi.py @@ -13,8 +13,8 @@ class ObsSourceApi(ObsHttpApi): def __put(self, *args, **kwargs): return super(ObsSourceApi, self).put(*args, **kwargs) - def get(self, prj, pkg='', filename=''): - return self.__get(prj, pkg, filename) + def get(self, prj, pkg='', filename='', view=None): + return self.__get(prj, pkg, filename, view=view) def put(self, prj, pkg, filename, data): return self.__put(prj, pkg, filename, data=data) |
