| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 6cc8126 commit fef8c7f
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -26,6 +26,9 @@ | |||
| 26 | 26 | import requests | |
| 27 | 27 | import six | |
| 28 | 28 | ||
| 29 | + import gitlab.config | ||
| 30 | + | ||
| 31 | + | ||
| 29 | 32 | __title__ = 'python-gitlab' | |
| 30 | 33 | __version__ = '0.9.1' | |
| 31 | 34 | __author__ = 'Gauvain Pocentek' | |
@@ -155,6 +158,13 @@ def __init__(self, url, private_token=None, | |||
| 155 | 158 | #: (Passed to requests-library) | |
| 156 | 159 | self.ssl_verify = ssl_verify | |
| 157 | 160 | ||
| 161 | + @staticmethod | ||
| 162 | + def from_config(gitlab_id=None, config_files=None): | ||
| 163 | + config = gitlab.config.GitlabConfigParser(gitlab_id=gitlab_id, | ||
| 164 | + config_files=config_files) | ||
| 165 | + return Gitlab(config.url, private_token=config.token, | ||
| 166 | + ssl_verify=config.ssl_verify, timeout=config.timeout) | ||
| 167 | + | ||
| 158 | 168 | def auth(self): | |
| 159 | 169 | """Performs an authentication. | |
| 160 | 170 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -21,13 +21,8 @@ | |||
| 21 | 21 | from __future__ import absolute_import | |
| 22 | 22 | import argparse | |
| 23 | 23 | import inspect | |
| 24 | - import os | ||
| 25 | 24 | import re | |
| 26 | 25 | import sys | |
| 27 | - try: | ||
| 28 | - import ConfigParser as configparser | ||
| 29 | - except ImportError: | ||
| 30 | - import configparser | ||
| 31 | 26 | ||
| 32 | 27 | import gitlab | |
| 33 | 28 | ||
@@ -129,14 +124,13 @@ def populate_sub_parser_by_class(cls, sub_parser): | |||
| 129 | 124 | for arg in d['requiredAttrs']] | |
| 130 | 125 | ||
| 131 | 126 | ||
| 132 | - def do_auth(gitlab_url, gitlab_token, ssl_verify, timeout): | ||
| 127 | + def do_auth(gitlab_id, config_files): | ||
| 133 | 128 | try: | |
| 134 | - gl = gitlab.Gitlab(gitlab_url, private_token=gitlab_token, | ||
| 135 | - ssl_verify=ssl_verify, timeout=timeout) | ||
| 129 | + gl = gitlab.Gitlab.from_config(gitlab_id, config_files) | ||
| 136 | 130 | gl.auth() | |
| 137 | 131 | return gl | |
| 138 | 132 | except Exception as e: | |
| 139 | - die("Could not connect to GitLab %s (%s)" % (gitlab_url, str(e))) | ||
| 133 | + die(str(e)) | ||
| 140 | 134 | ||
| 141 | 135 | ||
| 142 | 136 | def get_id(cls, args): | |
@@ -237,9 +231,6 @@ def do_project_owned(gl, what, args): | |||
| 237 | 231 | ||
| 238 | 232 | ||
| 239 | 233 | def main(): | |
| 240 | - ssl_verify = True | ||
| 241 | - timeout = 60 | ||
| 242 | - | ||
| 243 | 234 | parser = argparse.ArgumentParser( | |
| 244 | 235 | description="GitLab API Command Line Interface") | |
| 245 | 236 | parser.add_argument("-v", "--verbose", "--fancy", | |
@@ -276,17 +267,7 @@ def main(): | |||
| 276 | 267 | arg = parser.parse_args() | |
| 277 | 268 | args = arg.__dict__ | |
| 278 | 269 | ||
| 279 | - files = arg.config_file or ['/etc/python-gitlab.cfg', | ||
| 280 | - os.path.expanduser('~/.python-gitlab.cfg')] | ||
| 281 | - # read the config | ||
| 282 | - config = configparser.ConfigParser() | ||
| 283 | - try: | ||
| 284 | - config.read(files) | ||
| 285 | - except Exception as e: | ||
| 286 | - print("Impossible to parse the configuration file(s): %s" % | ||
| 287 | - str(e)) | ||
| 288 | - sys.exit(1) | ||
| 289 | - | ||
| 270 | + config_files = arg.config_file | ||
| 290 | 271 | gitlab_id = arg.gitlab | |
| 291 | 272 | verbose = arg.verbose | |
| 292 | 273 | action = arg.action | |
@@ -298,45 +279,13 @@ def main(): | |||
| 298 | 279 | args.pop("verbose") | |
| 299 | 280 | args.pop("what") | |
| 300 | 281 | ||
| 301 | - if gitlab_id is None: | ||
| 302 | - try: | ||
| 303 | - gitlab_id = config.get('global', 'default') | ||
| 304 | - except Exception: | ||
| 305 | - die("Impossible to get the gitlab id " | ||
| 306 | - "(not specified in config file)") | ||
| 307 | - | ||
| 308 | - try: | ||
| 309 | - gitlab_url = config.get(gitlab_id, 'url') | ||
| 310 | - gitlab_token = config.get(gitlab_id, 'private_token') | ||
| 311 | - except Exception: | ||
| 312 | - die("Impossible to get gitlab informations from configuration " | ||
| 313 | - "(%s)" % gitlab_id) | ||
| 314 | - | ||
| 315 | - try: | ||
| 316 | - ssl_verify = config.getboolean('global', 'ssl_verify') | ||
| 317 | - except Exception: | ||
| 318 | - pass | ||
| 319 | - try: | ||
| 320 | - ssl_verify = config.getboolean(gitlab_id, 'ssl_verify') | ||
| 321 | - except Exception: | ||
| 322 | - pass | ||
| 323 | - | ||
| 324 | - try: | ||
| 325 | - timeout = config.getint('global', 'timeout') | ||
| 326 | - except Exception: | ||
| 327 | - pass | ||
| 328 | - try: | ||
| 329 | - timeout = config.getint(gitlab_id, 'timeout') | ||
| 330 | - except Exception: | ||
| 331 | - pass | ||
| 332 | - | ||
| 333 | 282 | cls = None | |
| 334 | 283 | try: | |
| 335 | 284 | cls = gitlab.__dict__[whatToCls(what)] | |
| 336 | 285 | except Exception: | |
| 337 | 286 | die("Unknown object: %s" % what) | |
| 338 | 287 | ||
| 339 | - gl = do_auth(gitlab_url, gitlab_token, ssl_verify, timeout) | ||
| 288 | + gl = do_auth(gitlab_id, config_files) | ||
| 340 | 289 | ||
| 341 | 290 | if action == CREATE or action == GET: | |
| 342 | 291 | o = globals()['do_%s' % action.lower()](cls, gl, what, args) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,83 @@ | |||
| 1 | + # -*- coding: utf-8 -*- | ||
| 2 | + # | ||
| 3 | + # Copyright (C) 2013-2015 Gauvain Pocentek <gauvain@pocentek.net> | ||
| 4 | + # | ||
| 5 | + # This program is free software: you can redistribute it and/or modify | ||
| 6 | + # it under the terms of the GNU Lesser General Public License as published by | ||
| 7 | + # the Free Software Foundation, either version 3 of the License, or | ||
| 8 | + # (at your option) any later version. | ||
| 9 | + # | ||
| 10 | + # This program is distributed in the hope that it will be useful, | ||
| 11 | + # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | + # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 13 | + # GNU Lesser General Public License for more details. | ||
| 14 | + # | ||
| 15 | + # You should have received a copy of the GNU Lesser General Public License | ||
| 16 | + # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 17 | + | ||
| 18 | + try: | ||
| 19 | + import ConfigParser as configparser | ||
| 20 | + except ImportError: | ||
| 21 | + import configparser | ||
| 22 | + import os | ||
| 23 | + | ||
| 24 | + | ||
| 25 | + _DEFAULT_FILES = [ | ||
| 26 | + '/etc/python-gitlab.cfg', | ||
| 27 | + os.path.expanduser('~/.python-gitlab.cfg') | ||
| 28 | + ] | ||
| 29 | + | ||
| 30 | + | ||
| 31 | + class ConfigError(Exception): | ||
| 32 | + pass | ||
| 33 | + | ||
| 34 | + | ||
| 35 | + class GitlabIDError(ConfigError): | ||
| 36 | + pass | ||
| 37 | + | ||
| 38 | + | ||
| 39 | + class GitlabDataError(ConfigError): | ||
| 40 | + pass | ||
| 41 | + | ||
| 42 | + | ||
| 43 | + class GitlabConfigParser(object): | ||
| 44 | + def __init__(self, gitlab_id=None, config_files=None): | ||
| 45 | + self.gitlab_id = gitlab_id | ||
| 46 | + _files = config_files or _DEFAULT_FILES | ||
| 47 | + self._config = configparser.ConfigParser() | ||
| 48 | + self._config.read(_files) | ||
| 49 | + | ||
| 50 | + if self.gitlab_id is None: | ||
| 51 | + try: | ||
| 52 | + self.gitlab_id = self._config.get('global', 'default') | ||
| 53 | + except Exception: | ||
| 54 | + raise GitlabIDError("Impossible to get the gitlab id " | ||
| 55 | + "(not specified in config file)") | ||
| 56 | + | ||
| 57 | + try: | ||
| 58 | + self.url = self._config.get(self.gitlab_id, 'url') | ||
| 59 | + self.token = self._config.get(self.gitlab_id, 'private_token') | ||
| 60 | + except Exception: | ||
| 61 | + raise GitlabDataError("Impossible to get gitlab informations from " | ||
| 62 | + "configuration (%s)" % self.gitlab_id) | ||
| 63 | + | ||
| 64 | + self.ssl_verify = True | ||
| 65 | + try: | ||
| 66 | + self.ssl_verify = self._config.getboolean('global', 'ssl_verify') | ||
| 67 | + except Exception: | ||
| 68 | + pass | ||
| 69 | + try: | ||
| 70 | + self.ssl_verify = self._config.getboolean(self.gitlab_id, | ||
| 71 | + 'ssl_verify') | ||
| 72 | + except Exception: | ||
| 73 | + pass | ||
| 74 | + | ||
| 75 | + self.timeout = 60 | ||
| 76 | + try: | ||
| 77 | + self.timeout = self._config.getint('global', 'timeout') | ||
| 78 | + except Exception: | ||
| 79 | + pass | ||
| 80 | + try: | ||
| 81 | + self.timeout = self._config.getint(self.gitlab_id, 'timeout') | ||
| 82 | + except Exception: | ||
| 83 | + pass | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -84,10 +84,6 @@ echo -n "Testing project update... " | |||
| 84 | 84 | $GITLAB project update --id $PROJECT_ID --description "My New Description" | |
| 85 | 85 | $OK | |
| 86 | 86 | ||
| 87 | - echo -n "Testing project deletion... " | ||
| 88 | - $GITLAB project delete --id $PROJECT_ID | ||
| 89 | - $OK | ||
| 90 | - | ||
| 91 | 87 | echo -n "Testing user creation... " | |
| 92 | 88 | USER_ID=$($GITLAB user create --email fake@email.com --username user1 --name "User One" --password fakepassword | grep ^id: | cut -d' ' -f2) | |
| 93 | 89 | $OK | |
@@ -103,3 +99,7 @@ $OK | |||
| 103 | 99 | echo -n "Testing adding member to a project... " | |
| 104 | 100 | $GITLAB project-member create --project-id $PROJECT_ID --user-id $USER_ID --access-level 40 >/dev/null 2>&1 | |
| 105 | 101 | $OK | |
| 102 | + | ||
| 103 | + echo -n "Testing project deletion... " | ||
| 104 | + $GITLAB project delete --id $PROJECT_ID | ||
| 105 | + $OK | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments