summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--gitlab/__init__.py10
-rw-r--r--gitlab/cli.py61
-rw-r--r--gitlab/config.py83
-rwxr-xr-xtools/functional_tests.sh8
4 files changed, 102 insertions, 60 deletions
diff --git a/gitlab/__init__.py b/gitlab/__init__.py
index bc11071..93770f9 100644
--- a/gitlab/__init__.py
+++ b/gitlab/__init__.py
@@ -26,6 +26,9 @@ import warnings
import requests
import six
+import gitlab.config
+
+
__title__ = 'python-gitlab'
__version__ = '0.9.1'
__author__ = 'Gauvain Pocentek'
@@ -155,6 +158,13 @@ class Gitlab(object):
#: (Passed to requests-library)
self.ssl_verify = ssl_verify
+ @staticmethod
+ def from_config(gitlab_id=None, config_files=None):
+ config = gitlab.config.GitlabConfigParser(gitlab_id=gitlab_id,
+ config_files=config_files)
+ return Gitlab(config.url, private_token=config.token,
+ ssl_verify=config.ssl_verify, timeout=config.timeout)
+
def auth(self):
"""Performs an authentication.
diff --git a/gitlab/cli.py b/gitlab/cli.py
index d678439..29e33c2 100644
--- a/gitlab/cli.py
+++ b/gitlab/cli.py
@@ -21,13 +21,8 @@ from __future__ import division
from __future__ import absolute_import
import argparse
import inspect
-import os
import re
import sys
-try:
- import ConfigParser as configparser
-except ImportError:
- import configparser
import gitlab
@@ -129,14 +124,13 @@ def populate_sub_parser_by_class(cls, sub_parser):
for arg in d['requiredAttrs']]
-def do_auth(gitlab_url, gitlab_token, ssl_verify, timeout):
+def do_auth(gitlab_id, config_files):
try:
- gl = gitlab.Gitlab(gitlab_url, private_token=gitlab_token,
- ssl_verify=ssl_verify, timeout=timeout)
+ gl = gitlab.Gitlab.from_config(gitlab_id, config_files)
gl.auth()
return gl
except Exception as e:
- die("Could not connect to GitLab %s (%s)" % (gitlab_url, str(e)))
+ die(str(e))
def get_id(cls, args):
@@ -237,9 +231,6 @@ def do_project_owned(gl, what, args):
def main():
- ssl_verify = True
- timeout = 60
-
parser = argparse.ArgumentParser(
description="GitLab API Command Line Interface")
parser.add_argument("-v", "--verbose", "--fancy",
@@ -276,17 +267,7 @@ def main():
arg = parser.parse_args()
args = arg.__dict__
- files = arg.config_file or ['/etc/python-gitlab.cfg',
- os.path.expanduser('~/.python-gitlab.cfg')]
- # read the config
- config = configparser.ConfigParser()
- try:
- config.read(files)
- except Exception as e:
- print("Impossible to parse the configuration file(s): %s" %
- str(e))
- sys.exit(1)
-
+ config_files = arg.config_file
gitlab_id = arg.gitlab
verbose = arg.verbose
action = arg.action
@@ -298,45 +279,13 @@ def main():
args.pop("verbose")
args.pop("what")
- if gitlab_id is None:
- try:
- gitlab_id = config.get('global', 'default')
- except Exception:
- die("Impossible to get the gitlab id "
- "(not specified in config file)")
-
- try:
- gitlab_url = config.get(gitlab_id, 'url')
- gitlab_token = config.get(gitlab_id, 'private_token')
- except Exception:
- die("Impossible to get gitlab informations from configuration "
- "(%s)" % gitlab_id)
-
- try:
- ssl_verify = config.getboolean('global', 'ssl_verify')
- except Exception:
- pass
- try:
- ssl_verify = config.getboolean(gitlab_id, 'ssl_verify')
- except Exception:
- pass
-
- try:
- timeout = config.getint('global', 'timeout')
- except Exception:
- pass
- try:
- timeout = config.getint(gitlab_id, 'timeout')
- except Exception:
- pass
-
cls = None
try:
cls = gitlab.__dict__[whatToCls(what)]
except Exception:
die("Unknown object: %s" % what)
- gl = do_auth(gitlab_url, gitlab_token, ssl_verify, timeout)
+ gl = do_auth(gitlab_id, config_files)
if action == CREATE or action == GET:
o = globals()['do_%s' % action.lower()](cls, gl, what, args)
diff --git a/gitlab/config.py b/gitlab/config.py
new file mode 100644
index 0000000..c9dc5aa
--- /dev/null
+++ b/gitlab/config.py
@@ -0,0 +1,83 @@
+# -*- coding: utf-8 -*-
+#
+# Copyright (C) 2013-2015 Gauvain Pocentek <gauvain@pocentek.net>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as published by
+# the Free Software Foundation, either version 3 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 Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+try:
+ import ConfigParser as configparser
+except ImportError:
+ import configparser
+import os
+
+
+_DEFAULT_FILES = [
+ '/etc/python-gitlab.cfg',
+ os.path.expanduser('~/.python-gitlab.cfg')
+]
+
+
+class ConfigError(Exception):
+ pass
+
+
+class GitlabIDError(ConfigError):
+ pass
+
+
+class GitlabDataError(ConfigError):
+ pass
+
+
+class GitlabConfigParser(object):
+ def __init__(self, gitlab_id=None, config_files=None):
+ self.gitlab_id = gitlab_id
+ _files = config_files or _DEFAULT_FILES
+ self._config = configparser.ConfigParser()
+ self._config.read(_files)
+
+ if self.gitlab_id is None:
+ try:
+ self.gitlab_id = self._config.get('global', 'default')
+ except Exception:
+ raise GitlabIDError("Impossible to get the gitlab id "
+ "(not specified in config file)")
+
+ try:
+ self.url = self._config.get(self.gitlab_id, 'url')
+ self.token = self._config.get(self.gitlab_id, 'private_token')
+ except Exception:
+ raise GitlabDataError("Impossible to get gitlab informations from "
+ "configuration (%s)" % self.gitlab_id)
+
+ self.ssl_verify = True
+ try:
+ self.ssl_verify = self._config.getboolean('global', 'ssl_verify')
+ except Exception:
+ pass
+ try:
+ self.ssl_verify = self._config.getboolean(self.gitlab_id,
+ 'ssl_verify')
+ except Exception:
+ pass
+
+ self.timeout = 60
+ try:
+ self.timeout = self._config.getint('global', 'timeout')
+ except Exception:
+ pass
+ try:
+ self.timeout = self._config.getint(self.gitlab_id, 'timeout')
+ except Exception:
+ pass
diff --git a/tools/functional_tests.sh b/tools/functional_tests.sh
index dd31c90..acce6a9 100755
--- a/tools/functional_tests.sh
+++ b/tools/functional_tests.sh
@@ -84,10 +84,6 @@ echo -n "Testing project update... "
$GITLAB project update --id $PROJECT_ID --description "My New Description"
$OK
-echo -n "Testing project deletion... "
-$GITLAB project delete --id $PROJECT_ID
-$OK
-
echo -n "Testing user creation... "
USER_ID=$($GITLAB user create --email fake@email.com --username user1 --name "User One" --password fakepassword | grep ^id: | cut -d' ' -f2)
$OK
@@ -103,3 +99,7 @@ $OK
echo -n "Testing adding member to a project... "
$GITLAB project-member create --project-id $PROJECT_ID --user-id $USER_ID --access-level 40 >/dev/null 2>&1
$OK
+
+echo -n "Testing project deletion... "
+$GITLAB project delete --id $PROJECT_ID
+$OK