|
| 1 | +#!/usr/bin/python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# |
| 4 | +# Copyright (C) 2013 Gauvain Pocentek <gauvain@pocentek.net> |
| 5 | +# |
| 6 | +# This program is free software: you can redistribute it and/or modify |
| 7 | +# it under the terms of the GNU Lesser General Public License as published by |
| 8 | +# the Free Software Foundation, either version 3 of the License, or |
| 9 | +# (at your option) any later version. |
| 10 | +# |
| 11 | +# This program is distributed in the hope that it will be useful, |
| 12 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +# GNU Lesser General Public License for more details. |
| 15 | +# |
| 16 | +# You should have received a copy of the GNU Lesser General Public License |
| 17 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + |
| 19 | +import os |
| 20 | +import sys |
| 21 | + |
| 22 | +from ConfigParser import ConfigParser |
| 23 | +from optparse import OptionParser |
| 24 | + |
| 25 | +from inspect import getmro |
| 26 | + |
| 27 | +import gitlab |
| 28 | + |
| 29 | +def die(msg): |
| 30 | + sys.stderr.write(msg + "\n") |
| 31 | + sys.exit(1) |
| 32 | + |
| 33 | +# cmd line options |
| 34 | +parser = OptionParser() |
| 35 | +parser.add_option("-g", "--gitlab", dest="gitlab_id", |
| 36 | + help="select the gitlab connection from the configuration file", |
| 37 | + metavar="GITLAB") |
| 38 | +(options, args) = parser.parse_args() |
| 39 | + |
| 40 | +# read the config |
| 41 | +config = ConfigParser() |
| 42 | +config.read(['/etc/python-gitlab.cfg', |
| 43 | + os.path.expanduser('~/.python-gitlab.cfg')]) |
| 44 | + |
| 45 | +gitlab_id = options.gitlab_id |
| 46 | +if gitlab_id is None: |
| 47 | + try: |
| 48 | + gitlab_id = config.get('global', 'default') |
| 49 | + except: |
| 50 | + die("Impossible to get the gitlab id (not specified in config file)") |
| 51 | + |
| 52 | +try: |
| 53 | + gitlab_url = config.get(gitlab_id, 'url') |
| 54 | + gitlab_token = config.get(gitlab_id, 'private_token') |
| 55 | +except: |
| 56 | + die("Impossible to get gitlab informations from configuration (%s)" % gitlab_id) |
| 57 | + |
| 58 | +try: |
| 59 | + gl = gitlab.Gitlab(gitlab_url, private_token=gitlab_token) |
| 60 | + gl.auth() |
| 61 | +except: |
| 62 | + die("Could not connect to GitLab (%s)" % gitlab_url) |
| 63 | + |
| 64 | +try: |
| 65 | + what = args.pop(0) |
| 66 | + action = args.pop(0) |
| 67 | +except: |
| 68 | + die("Missing arguments") |
| 69 | + |
| 70 | +if action not in ['get', 'list', 'update', 'create', 'delete']: |
| 71 | + die("Unknown action: %s" % action) |
| 72 | + |
| 73 | +try: |
| 74 | + cls = gitlab.__dict__[what] |
| 75 | +except: |
| 76 | + die("Unknown object: %s" % what) |
| 77 | + |
| 78 | +if gitlab.GitlabObject not in getmro(cls): |
| 79 | + die("Unknown object: %s" % what) |
| 80 | + |
| 81 | +if action == "create": |
| 82 | + d = {} |
| 83 | + try: |
| 84 | + for arg in args: |
| 85 | + k, v = arg.split("=", 2) |
| 86 | + k = k.strip() |
| 87 | + v = v.strip() |
| 88 | + d[k] = v |
| 89 | + except: |
| 90 | + die("Impossible to parse data: %s" % arg) |
| 91 | + |
| 92 | + try: |
| 93 | + o = cls(gl, d) |
| 94 | + o.save() |
| 95 | + except Exception as e: |
| 96 | + die("Impossible to create object (%s)" % str(e)) |
| 97 | + |
| 98 | + sys.exit(0) |
| 99 | + |
| 100 | +elif action == "list": |
| 101 | + d = {} |
| 102 | + try: |
| 103 | + for arg in args: |
| 104 | + k, v = arg.split("=", 2) |
| 105 | + k = k.strip() |
| 106 | + v = v.strip() |
| 107 | + d[k] = v |
| 108 | + except: |
| 109 | + die("Impossible to parse data: %s" % arg) |
| 110 | + |
| 111 | + try: |
| 112 | + l = cls.list(gl, **d) |
| 113 | + except Exception as e: |
| 114 | + die("Impossible to list objects (%s)" % str(e)) |
| 115 | + |
| 116 | + for o in l: |
| 117 | + o.pretty_print() |
| 118 | + print |
| 119 | + |
| 120 | + sys.exit(0) |
| 121 | + |
| 122 | +elif action == "get": |
| 123 | + try: |
| 124 | + id = args.pop(0) |
| 125 | + except: |
| 126 | + die("First arg must be the object id") |
| 127 | + |
| 128 | + d = {} |
| 129 | + try: |
| 130 | + for arg in args: |
| 131 | + k, v = arg.split("=", 2) |
| 132 | + k = k.strip() |
| 133 | + v = v.strip() |
| 134 | + d[k] = v |
| 135 | + except: |
| 136 | + die("Impossible to parse data: %s" % arg) |
| 137 | + |
| 138 | + try: |
| 139 | + o = cls(gl, id, **d) |
| 140 | + except Exception as e: |
| 141 | + die("Impossible to get object (%s)" % str(e)) |
| 142 | + |
| 143 | + o.pretty_print() |
| 144 | + |
| 145 | + sys.exit(0) |
| 146 | + |
| 147 | +elif action == "delete": |
| 148 | + try: |
| 149 | + id = args.pop(0) |
| 150 | + except: |
| 151 | + die("First arg must be the object id") |
| 152 | + |
| 153 | + d = {} |
| 154 | + try: |
| 155 | + for arg in args: |
| 156 | + k, v = arg.split("=", 2) |
| 157 | + k = k.strip() |
| 158 | + v = v.strip() |
| 159 | + d[k] = v |
| 160 | + except: |
| 161 | + die("Impossible to parse data: %s" % arg) |
| 162 | + |
| 163 | + try: |
| 164 | + o = cls(gl, id, **d) |
| 165 | + except Exception as e: |
| 166 | + die("Impossible to get object (%s)" % id, str(e)) |
| 167 | + |
| 168 | + try: |
| 169 | + o.delete() |
| 170 | + except Exception as e: |
| 171 | + die("Impossible to destroy object (%s)" % str(e)) |
| 172 | + |
| 173 | + sys.exit(0) |
| 174 | + |
| 175 | +elif action == "update": |
| 176 | + try: |
| 177 | + id = args.pop(0) |
| 178 | + except: |
| 179 | + die("First arg must be the object id") |
| 180 | + |
| 181 | + d = {} |
| 182 | + try: |
| 183 | + for arg in args: |
| 184 | + k, v = arg.split("=", 2) |
| 185 | + k = k.strip() |
| 186 | + v = v.strip() |
| 187 | + d[k] = v |
| 188 | + except: |
| 189 | + die("Impossible to parse data: %s" % arg) |
| 190 | + |
| 191 | + try: |
| 192 | + o = cls(gl, id, **d) |
| 193 | + except Exception as e: |
| 194 | + die("Impossible to get object (%s)" % str(e)) |
| 195 | + |
| 196 | + try: |
| 197 | + for k, v in d.items(): |
| 198 | + o.__dict__[k] = v |
| 199 | + o.save() |
| 200 | + except Exception as e: |
| 201 | + die("Impossible to update object (%s)" % str(e)) |
| 202 | + |
| 203 | + sys.exit(0) |
0 commit comments