Skip to content

Commit d7bea07

Browse files
author
Gauvain Pocentek
committed
Move the CLI in the gitlab package
Setup an console_script entry point to create the executable script.
1 parent 4c6c778 commit d7bea07

File tree

3 files changed

+41
-41
lines changed

3 files changed

+41
-41
lines changed

bin/gitlab renamed to gitlab/cli.py

+35-39
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020
from __future__ import division
2121
from __future__ import absolute_import
2222
import argparse
23-
from inspect import getmro
23+
import inspect
2424
import os
2525
import re
2626
import sys
2727
try:
28-
from ConfigParser import ConfigParser
28+
import ConfigParser as configparser
2929
except ImportError:
30-
from configparser import ConfigParser
30+
import configparser
3131

3232
import gitlab
3333

@@ -118,7 +118,7 @@ def populate_sub_parser_by_class(cls, sub_parser):
118118
for arg in d['requiredAttrs']]
119119

120120

121-
def do_auth():
121+
def do_auth(gitlab_url, gitlab_token, ssl_verify, timeout):
122122
try:
123123
gl = gitlab.Gitlab(gitlab_url, private_token=gitlab_token,
124124
ssl_verify=ssl_verify, timeout=timeout)
@@ -128,74 +128,74 @@ def do_auth():
128128
die("Could not connect to GitLab %s (%s)" % (gitlab_url, str(e)))
129129

130130

131-
def get_id(cls):
131+
def get_id(cls, args):
132132
try:
133-
id = d.pop(cls.idAttr)
133+
id = args.pop(cls.idAttr)
134134
except Exception:
135135
die("Missing --%s argument" % cls.idAttr.replace('_', '-'))
136136

137137
return id
138138

139139

140-
def do_create(cls, d):
140+
def do_create(cls, gl, what, args):
141141
if not cls.canCreate:
142142
die("%s objects can't be created" % what)
143143

144144
try:
145-
o = cls(gl, d)
145+
o = cls(gl, args)
146146
o.save()
147147
except Exception as e:
148148
die("Impossible to create object (%s)" % str(e))
149149

150150
return o
151151

152152

153-
def do_list(cls, d):
153+
def do_list(cls, gl, what, args):
154154
if not cls.canList:
155155
die("%s objects can't be listed" % what)
156156

157157
try:
158-
l = cls.list(gl, **d)
158+
l = cls.list(gl, **args)
159159
except Exception as e:
160160
die("Impossible to list objects (%s)" % str(e))
161161

162162
return l
163163

164164

165-
def do_get(cls, d):
165+
def do_get(cls, gl, what, args):
166166
if not cls.canGet:
167167
die("%s objects can't be retrieved" % what)
168168

169169
id = None
170170
if cls not in [gitlab.CurrentUser]:
171-
id = get_id(cls)
171+
id = get_id(cls, args)
172172

173173
try:
174-
o = cls(gl, id, **d)
174+
o = cls(gl, id, **args)
175175
except Exception as e:
176176
die("Impossible to get object (%s)" % str(e))
177177

178178
return o
179179

180180

181-
def do_delete(cls, d):
181+
def do_delete(cls, gl, what, args):
182182
if not cls.canDelete:
183183
die("%s objects can't be deleted" % what)
184184

185-
o = do_get(cls, d)
185+
o = do_get(cls, args)
186186
try:
187187
o.delete()
188188
except Exception as e:
189189
die("Impossible to destroy object (%s)" % str(e))
190190

191191

192-
def do_update(cls, d):
192+
def do_update(cls, gl, what, args):
193193
if not cls.canUpdate:
194194
die("%s objects can't be updated" % what)
195195

196-
o = do_get(cls, d)
196+
o = do_get(cls, args)
197197
try:
198-
for k, v in d.items():
198+
for k, v in args.items():
199199
o.__dict__[k] = v
200200
o.save()
201201
except Exception as e:
@@ -204,28 +204,28 @@ def do_update(cls, d):
204204
return o
205205

206206

207-
def do_project_search(d):
207+
def do_project_search(gl, what, args):
208208
try:
209-
return gl.search_projects(d['query'])
209+
return gl.search_projects(args['query'])
210210
except Exception as e:
211211
die("Impossible to search projects (%s)" % str(e))
212212

213213

214-
def do_project_all():
214+
def do_project_all(gl, what, args):
215215
try:
216216
return gl.all_projects()
217217
except Exception as e:
218218
die("Impossible to list all projects (%s)" % str(e))
219219

220220

221-
def do_project_owned():
221+
def do_project_owned(gl, what, args):
222222
try:
223223
return gl.owned_projects()
224224
except Exception as e:
225225
die("Impossible to list owned projects (%s)" % str(e))
226226

227227

228-
if __name__ == "__main__":
228+
def main():
229229
ssl_verify = True
230230
timeout = 60
231231

@@ -247,7 +247,7 @@ def do_project_owned():
247247
classes = []
248248
for cls in gitlab.__dict__.values():
249249
try:
250-
if gitlab.GitlabObject in getmro(cls):
250+
if gitlab.GitlabObject in inspect.getmro(cls):
251251
classes.append(cls)
252252
except AttributeError:
253253
pass
@@ -261,15 +261,15 @@ def do_project_owned():
261261
populate_sub_parser_by_class(cls, object_subparsers)
262262

263263
arg = parser.parse_args()
264-
d = arg.__dict__
264+
args = arg.__dict__
265265

266266
# read the config
267-
config = ConfigParser()
267+
config = configparser.ConfigParser()
268268
config.read(['/etc/python-gitlab.cfg',
269269
os.path.expanduser('~/.python-gitlab.cfg')])
270270
gitlab_id = arg.gitlab
271271
# conflicts with "gitlab" attribute from GitlabObject class
272-
d.pop("gitlab")
272+
args.pop("gitlab")
273273
verbose = arg.verbose
274274
action = arg.action
275275
what = arg.what
@@ -312,50 +312,46 @@ def do_project_owned():
312312
except Exception:
313313
die("Unknown object: %s" % what)
314314

315-
gl = do_auth()
315+
gl = do_auth(gitlab_url, gitlab_token, ssl_verify, timeout)
316316

317317
if action == CREATE or action == GET:
318-
o = globals()['do_%s' % action.lower()](cls, d)
318+
o = globals()['do_%s' % action.lower()](cls, gl, what, args)
319319
o.display(verbose)
320320

321321
elif action == LIST:
322-
for o in do_list(cls, d):
322+
for o in do_list(cls, gl, what, args):
323323
o.display(verbose)
324324
print("")
325325

326326
elif action == DELETE or action == UPDATE:
327-
o = globals()['do_%s' % action.lower()](cls, d)
327+
o = globals()['do_%s' % action.lower()](cls, gl, what, args)
328328

329329
elif action == PROTECT or action == UNPROTECT:
330330
if cls != gitlab.ProjectBranch:
331331
die("%s objects can't be protected" % what)
332332

333-
o = do_get(cls, d)
333+
o = do_get(cls, gl, what, args)
334334
getattr(o, action)()
335335

336336
elif action == SEARCH:
337337
if cls != gitlab.Project:
338338
die("%s objects don't support this request" % what)
339339

340-
for o in do_project_search(d):
340+
for o in do_project_search(gl, what, args):
341341
o.display(verbose)
342342

343343
elif action == OWNED:
344344
if cls != gitlab.Project:
345345
die("%s objects don't support this request" % what)
346346

347-
for o in do_project_owned():
347+
for o in do_project_owned(gl, what, args):
348348
o.display(verbose)
349349

350350
elif action == ALL:
351351
if cls != gitlab.Project:
352352
die("%s objects don't support this request" % what)
353353

354-
for o in do_project_all():
354+
for o in do_project_all(gl, what, args):
355355
o.display(verbose)
356356

357-
else:
358-
die("Unknown action: %s. Use \"gitlab -h %s\" to get details." %
359-
(action, what))
360-
361357
sys.exit(0)

setup.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@ def get_version():
2020
license='LGPLv3',
2121
url='https://github.com/gpocentek/python-gitlab',
2222
packages=['gitlab'],
23-
scripts=['bin/gitlab'],
2423
install_requires=['requests', 'six'],
24+
entry_points={
25+
'console_scripts': [
26+
'gitlab = gitlab.cli:main'
27+
]
28+
},
2529
classifiers=[
2630
'Development Status :: 5 - Production/Stable',
2731
'Environment :: Console',

tox.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ commands =
1515

1616
[testenv:pep8]
1717
commands =
18-
flake8 {posargs} gitlab bin
18+
flake8 {posargs} gitlab/
1919

2020
[testenv:venv]
2121
commands = {posargs}

0 commit comments

Comments
 (0)