aboutsummaryrefslogtreecommitdiff
path: root/industriart
diff options
context:
space:
mode:
authorGuido Günther <agx@sigxcpu.org>2014-07-15 21:57:12 +0100
committerGuido Günther <agx@sigxcpu.org>2014-07-16 12:51:15 +0200
commit37123e9ca25778d53b675626ddc32ff2e3e04314 (patch)
treeb0e7ca18738c10925bb410c323ddb8fbb51c738a /industriart
Initial commit
Diffstat (limited to 'industriart')
-rw-r--r--industriart/__init__.py4
-rw-r--r--industriart/artifactory.py142
-rw-r--r--industriart/compat.py13
3 files changed, 159 insertions, 0 deletions
diff --git a/industriart/__init__.py b/industriart/__init__.py
new file mode 100644
index 0000000..e058189
--- /dev/null
+++ b/industriart/__init__.py
@@ -0,0 +1,4 @@
+import logging
+from .compat import NullHandler
+
+logging.getLogger(__name__).addHandler(NullHandler())
diff --git a/industriart/artifactory.py b/industriart/artifactory.py
new file mode 100644
index 0000000..241d7ba
--- /dev/null
+++ b/industriart/artifactory.py
@@ -0,0 +1,142 @@
+# vim: set fileencoding=utf-8 :
+#
+# (C) 2014 Guido Gunther <agx@sigxcpu.org>
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+import logging
+import os
+import posixpath
+import requests
+
+log = logging.getLogger(__name__)
+
+class ArtifactoryError(Exception):
+ def __init__(self, msg, url, status_code=0):
+ self.url = url
+ self.status_code = status_code
+ Exception.__init__(self, msg)
+
+class ArtifactoryObjectNotFound(ArtifactoryError):
+ """Object not found aka 404"""
+ pass
+
+class ArtifactoryNoPermission(ArtifactoryError):
+ """Object not found aka 401"""
+ pass
+
+class Artifactory(object):
+ """
+ Extremely simple JFrog Artifactory API Wrapper
+ """
+ def __init__(self, base, user=None, password=None):
+ self.user = user
+ self.password = password
+ self.base = base
+
+ def search_gavc(self, groupid=None, artifactid=None, version=None, classifier=None):
+ """
+ Query the artifactory via Groupid, ArtifacId, Version
+ """
+ path='api/search/gavc'
+ param_map = {
+ 'groupid': 'g',
+ 'artifactid': 'a',
+ 'version': 'v',
+ 'classifier': 'c',
+ }
+ params = {}
+ for k,v in locals().items():
+ if k in param_map and v is not None:
+ params[param_map[k]] = v
+
+ url = posixpath.join(self.base, path)
+ log.debug("Searching on %s with %s" % (url, params))
+ return self.get(url, params)['results']
+
+
+ def calc_yum_metadata(self, repository):
+ url = posixpath.join(self.base, 'api/yum', repository)
+ params = { 'async': 0 }
+ log.debug("Refreshing on %s" % url)
+ return self.post(url, params)
+
+ def get_repositories(self):
+ url = posixpath.join(self.base, 'api/repositories')
+ return self.get(url)
+
+ def _transform_url(self, url):
+ """
+ Helper to rewrite URLs. All artifactory URLs are passed through this method
+ in order to cope with broken installations where e.g. artifactory sits behind
+ a reverse proxy but doesn't know about it.
+
+ Rewrite this in derived classes as needed (but hopefully not).
+ """
+ return url
+
+
+ @staticmethod
+ def _parse_error(data):
+ try:
+ if data.has_key('errors'):
+ return data['errors'][0]['message']
+ return None
+ except:
+ return None
+
+
+ def _request(self, http_method, url, params=None):
+ """
+ Perform a HTTP request on the artifactory
+ """
+ url = self._transform_url(url)
+ auth = None
+
+ if self.user and self.password:
+ auth = requests.auth.HTTPBasicAuth(self.user, self.password)
+
+ method = getattr(requests, http_method.lower())
+ r = method(url, params=params, auth=auth)
+ if r.status_code / 100 != 2:
+ err = self._parse_error(r.json())
+ if r.status_code == 404:
+ raise ArtifactoryObjectNotFound(err or "Not found" % url,
+ url,
+ r.status_code)
+ elif r.status_code == 401:
+ raise ArtifactoryNoPermission(err or "Unauthorized" % url,
+ url,
+ r.status_code)
+ raise ArtifactoryError(err or "Did not get a 2xx",
+ url,
+ r.status_code)
+ if r.headers['content-type'].endswith('json'):
+ return r.json()
+ else:
+ return r.text
+
+
+ def get(self, url, params=None):
+ """
+ Perform a GET request on the artifactory
+ """
+ return self._request('GET', url, params)
+
+
+ def post(self, url, params):
+ """
+ Perform a POST request on the artifactory
+ """
+ return self._request('POST', url, params)
diff --git a/industriart/compat.py b/industriart/compat.py
new file mode 100644
index 0000000..06fe440
--- /dev/null
+++ b/industriart/compat.py
@@ -0,0 +1,13 @@
+"""Compatiblity with older python"""
+
+import sys
+
+# python 2.7 introduced a NullHandler which we want to use, but to support
+# older versions, we implement our own if needed.
+if sys.version_info[:2] > (2, 6):
+ from logging import NullHandler
+else:
+ from logging import Handler
+ class NullHandler(Handler):
+ def emit(self, record):
+ pass