aboutsummaryrefslogtreecommitdiff
path: root/industriart/artifactory.py
blob: 241d7ba09d20424e2276312117a7ec9e9aeb2b2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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)