diff options
-rw-r--r-- | .gitignore | 4 | ||||
-rw-r--r-- | README.md | 8 | ||||
-rw-r--r-- | examples/get-repos.py | 81 | ||||
-rwxr-xr-x | examples/refresh-yum-repo.py | 81 | ||||
-rw-r--r-- | industriart/__init__.py | 4 | ||||
-rw-r--r-- | industriart/artifactory.py | 142 | ||||
-rw-r--r-- | industriart/compat.py | 13 | ||||
-rw-r--r-- | setup.py | 29 |
8 files changed, 362 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a83ba69 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.pyc +*~ +build/ +industriart.egg-info/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..0d1e24c --- /dev/null +++ b/README.md @@ -0,0 +1,8 @@ +Industriart +----------- +Industriart is a simple client for the JFrog Artifactories REST API. It currently +only supports few basic methods but patches are certainly welcome. + +Run the example like: + + PYTHONPATH=. python examples/refresh-yum-repo.py -a http://localhost:8081/artifactory -u admin -p password repo1 repo2 diff --git a/examples/get-repos.py b/examples/get-repos.py new file mode 100644 index 0000000..ecca17e --- /dev/null +++ b/examples/get-repos.py @@ -0,0 +1,81 @@ +#!/usr/bin/python +# 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 optparse +import sys + +from industriart.artifactory import Artifactory, ArtifactoryError + +def setup_logging(debug=False): + logging.getLogger('industriart').addHandler(logging.StreamHandler()) + + logger = logging.getLogger() + level = logging.DEBUG if debug else logging.INFO + logger.setLevel(level) + + + +def main(): + parser = optparse.OptionParser() + + parser.add_option('-d', '--debug', + action="store_true", + default=False, + dest="debug", + help='Be more verbose.') + parser.add_option('-a', '--artifactory-base', + dest="base", + help='Artifactory base URL to use.') + parser.add_option('-u', '--user', + dest="user", + help='Artifactory user to use.') + parser.add_option('-p', '--password', + dest="password", + help='Artifactory user\'s password to use.') + + (options, args) = parser.parse_args() + setup_logging(options.debug) + + if options.base is None: + logging.error("Artifactory url must be given.") + return 1 + else: + artifactory = Artifactory(options.base, + options.user, + options.password) + + try: + result = artifactory.get_repositories() + for repo in result: + print("Key: %s" % repo['key']) + print("URL: %s" % repo['url']) + print("Type: %s" % repo['type']) + if repo.has_key('description'): + print("Description: %s" % repo['description']) + print + except ArtifactoryError as e: + logging.error("Fetching repos at %s failed with %d: %s", + e.url, e.status_code, str(e)) + return 1 + + return 0 + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/examples/refresh-yum-repo.py b/examples/refresh-yum-repo.py new file mode 100755 index 0000000..4131cca --- /dev/null +++ b/examples/refresh-yum-repo.py @@ -0,0 +1,81 @@ +#!/usr/bin/python +# 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 optparse +import sys + +from industriart.artifactory import Artifactory, ArtifactoryError + +def setup_logging(debug=False): + logging.getLogger('industriart').addHandler(logging.StreamHandler()) + + logger = logging.getLogger() + level = logging.DEBUG if debug else logging.INFO + logger.setLevel(level) + + + +def main(): + parser = optparse.OptionParser() + + parser.add_option('-d', '--debug', + action="store_true", + default=False, + dest="debug", + help='Be more verbose.') + parser.add_option('-a', '--artifactory-base', + dest="base", + help='Artifactory base URL to use.') + parser.add_option('-u', '--user', + dest="user", + help='Artifactory user to use.') + parser.add_option('-p', '--password', + dest="password", + help='Artifactory user\'s password to use.') + + (options, args) = parser.parse_args() + setup_logging(options.debug) + + if options.base is None: + logging.error("Artifactory url must be given.") + return 1 + else: + artifactory = Artifactory(options.base, + options.user, + options.password) + + if not len(args): + logging.error("Which repo should I refresh?") + return 1 + + ret = 0 + for repo in args: + try: + msg = artifactory.calc_yum_metadata(repo) + logging.info(msg) + except ArtifactoryError as e: + ret = 1 + logging.error("Refresh of %s failed with %d: %s", + e.url, e.status_code, str(e)) + + return ret + + +if __name__ == '__main__': + sys.exit(main()) 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 diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..a677ebe --- /dev/null +++ b/setup.py @@ -0,0 +1,29 @@ +#!/usr/bin/python +# vim: set fileencoding=utf-8 : +# +# Copyright (C) 2014 Guido Günther <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 +# END OF COPYRIGHT # + +from setuptools import setup + +setup(name = "industriart", + version = '0.0.1', + author = u'Guido Günther', + author_email = 'agx@sigxcpu.org', + packages = ['industriart'], + setup_requires=['nose>=0.11.1','requests>=1'], +) |