1616# You should have received a copy of the GNU Lesser General Public License
1717# along with this program. If not, see <http://www.gnu.org/licenses/>.
1818
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
2022import argparse
23+ from inspect import getmro
2124import os
22- import sys
2325import re
24- import gitlab
25- from inspect import getmro
26+ import sys
2627try :
2728 from ConfigParser import ConfigParser
28- except :
29+ except ImportError :
2930 from configparser import ConfigParser
3031
32+ import gitlab
33+
3134camel_re = re .compile ('(.)([A-Z])' )
3235LIST = 'list'
3336GET = 'get'
@@ -44,12 +47,10 @@ EXTRA_ACTION = [PROTECT, UNPROTECT, SEARCH, OWNED, ALL]
4447
4548extra_actions = {
4649 gitlab .ProjectBranch : {PROTECT : {'requiredAttrs' : ['id' , 'project-id' ]},
47- UNPROTECT : {'requiredAttrs' : ['id' , 'project-id' ]}
48- },
50+ UNPROTECT : {'requiredAttrs' : ['id' , 'project-id' ]}},
4951 gitlab .Project : {SEARCH : {'requiredAttrs' : ['query' ]},
5052 OWNED : {'requiredAttrs' : []},
51- ALL : {'requiredAttrs' : []}
52- },
53+ ALL : {'requiredAttrs' : []}},
5354}
5455
5556
@@ -77,36 +78,51 @@ def populate_sub_parser_by_class(cls, sub_parser):
7778 attr = 'can' + action_name .capitalize ()
7879 try :
7980 y = cls .__dict__ [attr ]
80- except :
81+ except AttributeError :
8182 y = gitlab .GitlabObject .__dict__ [attr ]
8283 if not y :
8384 continue
8485 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+
8690 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 ]
8894 sub_parser_action .add_argument ("--page" , required = False )
8995 sub_parser_action .add_argument ("--per-page" , required = False )
96+
9097 elif action_name in [GET , DELETE ]:
9198 if cls not in [gitlab .CurrentUser ]:
9299 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+
94104 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+
99112 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 ]
104119
105120 if cls in extra_actions :
106121 for action_name in sorted (extra_actions [cls ]):
107122 sub_parser_action = sub_parser_class .add_parser (action_name )
108123 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' ]]
110126
111127
112128def do_auth ():
@@ -115,14 +131,14 @@ def do_auth():
115131 ssl_verify = ssl_verify , timeout = timeout )
116132 gl .auth ()
117133 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 )) )
120136
121137
122138def get_id ():
123139 try :
124140 id = d .pop ('id' )
125- except :
141+ except Exception :
126142 die ("Missing --id argument" )
127143
128144 return id
@@ -219,34 +235,42 @@ def do_project_owned():
219235if __name__ == "__main__" :
220236 ssl_verify = True
221237 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" )
224243 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 )
227249 subparsers = parser .add_subparsers (
228250 dest = 'what' ,
229251 title = "positional argument" ,
230252 description = 'GitLab object' ,
231253 help = 'GitLab object'
232254 )
233- #populate argparse for all Gitlab Object
255+
256+ # populate argparse for all Gitlab Object
234257 for cls in gitlab .__dict__ .values ():
235258 try :
236259 if gitlab .GitlabObject in getmro (cls ):
237260 sub_parser = subparsers .add_parser (clsToWhat (cls ))
238261 populate_sub_parser_by_class (cls , sub_parser )
239- except :
262+ except Exception :
240263 pass
241264
242265 arg = parser .parse_args ()
243266 d = arg .__dict__
267+
244268 # read the config
245269 config = ConfigParser ()
246270 config .read (['/etc/python-gitlab.cfg' ,
247271 os .path .expanduser ('~/.python-gitlab.cfg' )])
248272 gitlab_id = arg .gitlab
249- #conflicts with "gitlab" attribute with GitlabObject class
273+ # conflicts with "gitlab" attribute from GitlabObject class
250274 d .pop ("gitlab" )
251275 verbose = arg .verbosity
252276 action = arg .action
@@ -255,38 +279,39 @@ if __name__ == "__main__":
255279 if gitlab_id is None :
256280 try :
257281 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)" )
260285
261286 try :
262287 gitlab_url = config .get (gitlab_id , 'url' )
263288 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 )
267292
268293 try :
269294 ssl_verify = config .getboolean ('global' , 'ssl_verify' )
270- except :
295+ except Exception :
271296 pass
272297 try :
273298 ssl_verify = config .getboolean (gitlab_id , 'ssl_verify' )
274- except :
299+ except Exception :
275300 pass
276301
277302 try :
278303 timeout = config .getint ('global' , 'timeout' )
279- except :
304+ except Exception :
280305 pass
281306 try :
282307 timeout = config .getint (gitlab_id , 'timeout' )
283- except :
308+ except Exception :
284309 pass
285310
286311 cls = None
287312 try :
288313 cls = gitlab .__dict__ [whatToCls (what )]
289- except :
314+ except Exception :
290315 die ("Unknown object: %s" % what )
291316
292317 gl = do_auth ()
@@ -335,4 +360,4 @@ if __name__ == "__main__":
335360 die ("Unknown action: %s. Use \" gitlab -h %s\" to get details." %
336361 (action , what ))
337362
338- sys .exit (0 )
363+ sys .exit (0 )
0 commit comments