16
16
# You should have received a copy of the GNU Lesser General Public License
17
17
# along with this program. If not, see <http://www.gnu.org/licenses/>.
18
18
19
- from __future__ import print_function , division , absolute_import
19
+ from __future__ import print_function
20
+ from __future__ import division
21
+ from __future__ import absolute_import
20
22
import argparse
23
+ from inspect import getmro
21
24
import os
22
- import sys
23
25
import re
24
- import gitlab
25
- from inspect import getmro
26
+ import sys
26
27
try :
27
28
from ConfigParser import ConfigParser
28
- except :
29
+ except ImportError :
29
30
from configparser import ConfigParser
30
31
32
+ import gitlab
33
+
31
34
camel_re = re .compile ('(.)([A-Z])' )
32
35
LIST = 'list'
33
36
GET = 'get'
@@ -44,12 +47,10 @@ EXTRA_ACTION = [PROTECT, UNPROTECT, SEARCH, OWNED, ALL]
44
47
45
48
extra_actions = {
46
49
gitlab .ProjectBranch : {PROTECT : {'requiredAttrs' : ['id' , 'project-id' ]},
47
- UNPROTECT : {'requiredAttrs' : ['id' , 'project-id' ]}
48
- },
50
+ UNPROTECT : {'requiredAttrs' : ['id' , 'project-id' ]}},
49
51
gitlab .Project : {SEARCH : {'requiredAttrs' : ['query' ]},
50
52
OWNED : {'requiredAttrs' : []},
51
- ALL : {'requiredAttrs' : []}
52
- },
53
+ ALL : {'requiredAttrs' : []}},
53
54
}
54
55
55
56
@@ -77,36 +78,51 @@ def populate_sub_parser_by_class(cls, sub_parser):
77
78
attr = 'can' + action_name .capitalize ()
78
79
try :
79
80
y = cls .__dict__ [attr ]
80
- except :
81
+ except AttributeError :
81
82
y = gitlab .GitlabObject .__dict__ [attr ]
82
83
if not y :
83
84
continue
84
85
sub_parser_action = sub_parser_class .add_parser (action_name )
85
- [sub_parser_action .add_argument ("--%s" % x .replace ('_' , '-' ), required = True ) for x in cls .requiredUrlAttrs ]
86
+ [sub_parser_action .add_argument ("--%s" % x .replace ('_' , '-' ),
87
+ required = True )
88
+ for x in cls .requiredUrlAttrs ]
89
+
86
90
if action_name == LIST :
87
- [sub_parser_action .add_argument ("--%s" % x .replace ('_' , '-' ), required = True ) for x in cls .requiredListAttrs ]
91
+ [sub_parser_action .add_argument ("--%s" % x .replace ('_' , '-' ),
92
+ required = True )
93
+ for x in cls .requiredListAttrs ]
88
94
sub_parser_action .add_argument ("--page" , required = False )
89
95
sub_parser_action .add_argument ("--per-page" , required = False )
96
+
90
97
elif action_name in [GET , DELETE ]:
91
98
if cls not in [gitlab .CurrentUser ]:
92
99
sub_parser_action .add_argument ("--id" , required = True )
93
- [sub_parser_action .add_argument ("--%s" % x .replace ('_' , '-' ), required = True ) for x in cls .requiredGetAttrs ]
100
+ [sub_parser_action .add_argument ("--%s" % x .replace ('_' , '-' ),
101
+ required = True )
102
+ for x in cls .requiredGetAttrs ]
103
+
94
104
elif action_name == CREATE :
95
- [sub_parser_action .add_argument ("--%s" % x .replace ('_' , '-' ), required = True ) for x in
96
- cls .requiredCreateAttrs ]
97
- [sub_parser_action .add_argument ("--%s" % x .replace ('_' , '-' ), required = False ) for x in
98
- cls .optionalCreateAttrs ]
105
+ [sub_parser_action .add_argument ("--%s" % x .replace ('_' , '-' ),
106
+ required = True )
107
+ for x in cls .requiredCreateAttrs ]
108
+ [sub_parser_action .add_argument ("--%s" % x .replace ('_' , '-' ),
109
+ required = False )
110
+ for x in cls .optionalCreateAttrs ]
111
+
99
112
elif action_name == UPDATE :
100
- [sub_parser_action .add_argument ("--%s" % x .replace ('_' , '-' ), required = True ) for x in
101
- cls .requiredCreateAttrs ]
102
- [sub_parser_action .add_argument ("--%s" % x .replace ('_' , '-' ), required = False ) for x in
103
- cls .optionalCreateAttrs ]
113
+ [sub_parser_action .add_argument ("--%s" % x .replace ('_' , '-' ),
114
+ required = True )
115
+ for x in cls .requiredCreateAttrs ]
116
+ [sub_parser_action .add_argument ("--%s" % x .replace ('_' , '-' ),
117
+ required = False )
118
+ for x in cls .optionalCreateAttrs ]
104
119
105
120
if cls in extra_actions :
106
121
for action_name in sorted (extra_actions [cls ]):
107
122
sub_parser_action = sub_parser_class .add_parser (action_name )
108
123
d = extra_actions [cls ][action_name ]
109
- [sub_parser_action .add_argument ("--%s" % arg , required = True ) for arg in d ['requiredAttrs' ]]
124
+ [sub_parser_action .add_argument ("--%s" % arg , required = True )
125
+ for arg in d ['requiredAttrs' ]]
110
126
111
127
112
128
def do_auth ():
@@ -115,14 +131,14 @@ def do_auth():
115
131
ssl_verify = ssl_verify , timeout = timeout )
116
132
gl .auth ()
117
133
return gl
118
- except :
119
- die ("Could not connect to GitLab (%s)" % gitlab_url )
134
+ except Exception as e :
135
+ die ("Could not connect to GitLab %s (%s)" % ( gitlab_url , str ( e )) )
120
136
121
137
122
138
def get_id ():
123
139
try :
124
140
id = d .pop ('id' )
125
- except :
141
+ except Exception :
126
142
die ("Missing --id argument" )
127
143
128
144
return id
@@ -219,34 +235,42 @@ def do_project_owned():
219
235
if __name__ == "__main__" :
220
236
ssl_verify = True
221
237
timeout = 60
222
- parser = argparse .ArgumentParser (description = 'Useful GitLab Command Line Interface' )
223
- parser .add_argument ("-v" , "--verbosity" , "--fancy" , help = "increase output verbosity" , action = "store_true" )
238
+ parser = argparse .ArgumentParser (
239
+ description = 'GitLab API Command Line Interface' )
240
+ parser .add_argument ("-v" , "--verbosity" , "--fancy" ,
241
+ help = "increase output verbosity" ,
242
+ action = "store_true" )
224
243
parser .add_argument ("--gitlab" , metavar = 'gitlab' ,
225
- help = "Specifies which python-gitlab.cfg configuration section should be used. "
226
- "If not defined, the default selection will be used." , required = False )
244
+ help = ("Specifies which python-gitlab.cfg "
245
+ "configuration section should be used. "
246
+ "If not defined, the default selection "
247
+ "will be used." ),
248
+ required = False )
227
249
subparsers = parser .add_subparsers (
228
250
dest = 'what' ,
229
251
title = "positional argument" ,
230
252
description = 'GitLab object' ,
231
253
help = 'GitLab object'
232
254
)
233
- #populate argparse for all Gitlab Object
255
+
256
+ # populate argparse for all Gitlab Object
234
257
for cls in gitlab .__dict__ .values ():
235
258
try :
236
259
if gitlab .GitlabObject in getmro (cls ):
237
260
sub_parser = subparsers .add_parser (clsToWhat (cls ))
238
261
populate_sub_parser_by_class (cls , sub_parser )
239
- except :
262
+ except Exception :
240
263
pass
241
264
242
265
arg = parser .parse_args ()
243
266
d = arg .__dict__
267
+
244
268
# read the config
245
269
config = ConfigParser ()
246
270
config .read (['/etc/python-gitlab.cfg' ,
247
271
os .path .expanduser ('~/.python-gitlab.cfg' )])
248
272
gitlab_id = arg .gitlab
249
- #conflicts with "gitlab" attribute with GitlabObject class
273
+ # conflicts with "gitlab" attribute from GitlabObject class
250
274
d .pop ("gitlab" )
251
275
verbose = arg .verbosity
252
276
action = arg .action
@@ -255,38 +279,39 @@ if __name__ == "__main__":
255
279
if gitlab_id is None :
256
280
try :
257
281
gitlab_id = config .get ('global' , 'default' )
258
- except :
259
- die ("Impossible to get the gitlab id (not specified in config file)" )
282
+ except Exception :
283
+ die ("Impossible to get the gitlab id "
284
+ "(not specified in config file)" )
260
285
261
286
try :
262
287
gitlab_url = config .get (gitlab_id , 'url' )
263
288
gitlab_token = config .get (gitlab_id , 'private_token' )
264
- except :
265
- die ("Impossible to get gitlab informations from configuration (%s)" %
266
- gitlab_id )
289
+ except Exception :
290
+ die ("Impossible to get gitlab informations from configuration "
291
+ "(%s)" % gitlab_id )
267
292
268
293
try :
269
294
ssl_verify = config .getboolean ('global' , 'ssl_verify' )
270
- except :
295
+ except Exception :
271
296
pass
272
297
try :
273
298
ssl_verify = config .getboolean (gitlab_id , 'ssl_verify' )
274
- except :
299
+ except Exception :
275
300
pass
276
301
277
302
try :
278
303
timeout = config .getint ('global' , 'timeout' )
279
- except :
304
+ except Exception :
280
305
pass
281
306
try :
282
307
timeout = config .getint (gitlab_id , 'timeout' )
283
- except :
308
+ except Exception :
284
309
pass
285
310
286
311
cls = None
287
312
try :
288
313
cls = gitlab .__dict__ [whatToCls (what )]
289
- except :
314
+ except Exception :
290
315
die ("Unknown object: %s" % what )
291
316
292
317
gl = do_auth ()
@@ -335,4 +360,4 @@ if __name__ == "__main__":
335
360
die ("Unknown action: %s. Use \" gitlab -h %s\" to get details." %
336
361
(action , what ))
337
362
338
- sys .exit (0 )
363
+ sys .exit (0 )
0 commit comments