@@ -32,26 +32,28 @@ import gitlab
32
32
camel_re = re .compile ('(.)([A-Z])' )
33
33
34
34
extra_actions = {
35
- gitlab .ProjectBranch : {
36
- 'protect' : {'requiredAttrs' : ['id' , 'project-id' ]},
37
- 'unprotect' : {'requiredAttrs' : ['id' , 'project-id' ]}
38
- },
39
- gitlab .Project : {
40
- 'search' : {'requiredAttrs' : ['query' ]},
41
- 'owned' : {'requiredAttrs' : []},
42
- 'all' : {'requiredAttrs' : []}
43
- },
35
+ gitlab .ProjectBranch : {'protect' : {'requiredAttrs' : ['id' , 'project-id' ]},
36
+ 'unprotect' : {'requiredAttrs' : ['id' , 'project-id' ]}
37
+ },
38
+ gitlab .Project : {'search' : {'requiredAttrs' : ['query' ]},
39
+ 'owned' : {'requiredAttrs' : []},
40
+ 'all' : {'requiredAttrs' : []}
41
+ },
44
42
}
45
43
44
+
46
45
def die (msg ):
47
46
sys .stderr .write (msg + "\n " )
48
47
sys .exit (1 )
49
48
49
+
50
50
def whatToCls (what ):
51
51
return "" .join ([s .capitalize () for s in what .split ("-" )])
52
52
53
+
53
54
def clsToWhat (cls ):
54
- return camel_re .sub (r'\1-\2' , cls .__name__ ).lower ()
55
+ return camel_re .sub (r'\1-\2' , cls .__name__ ).lower ()
56
+
55
57
56
58
def actionHelpList (cls ):
57
59
l = []
@@ -66,39 +68,48 @@ def actionHelpList(cls):
66
68
67
69
detail = ''
68
70
if action == 'list' :
69
- detail = " " .join (["--%s=ARG" % x .replace ('_' , '-' ) for x in cls .requiredListAttrs ])
71
+ detail = " " .join (["--%s=ARG" % x .replace ('_' , '-' )
72
+ for x in cls .requiredListAttrs ])
70
73
if detail :
71
74
detail += " "
72
75
detail += "--page=ARG --per-page=ARG"
73
76
elif action in ['get' , 'delete' ]:
74
77
if cls not in [gitlab .CurrentUser ]:
75
78
detail = "--id=ARG "
76
- detail += " " .join (["--%s=ARG" % x .replace ('_' , '-' ) for x in cls .requiredGetAttrs ])
79
+ detail += " " .join (["--%s=ARG" % x .replace ('_' , '-' )
80
+ for x in cls .requiredGetAttrs ])
77
81
elif action == 'create' :
78
- detail = " " .join (["--%s=ARG" % x .replace ('_' , '-' ) for x in cls .requiredCreateAttrs ])
82
+ detail = " " .join (["--%s=ARG" % x .replace ('_' , '-' )
83
+ for x in cls .requiredCreateAttrs ])
79
84
if detail :
80
85
detail += " "
81
- detail += " " .join (["[--%s=ARG]" % x .replace ('_' , '-' ) for x in cls .optionalCreateAttrs ])
86
+ detail += " " .join (["[--%s=ARG]" % x .replace ('_' , '-' )
87
+ for x in cls .optionalCreateAttrs ])
82
88
elif action == 'update' :
83
- detail = " " .join (["[--%s=ARG]" % x .replace ('_' , '-' ) for x in cls .requiredCreateAttrs ])
89
+ detail = " " .join (["[--%s=ARG]" % x .replace ('_' , '-' )
90
+ for x in cls .requiredCreateAttrs ])
84
91
if detail :
85
92
detail += " "
86
- detail += " " .join (["[--%s=ARG]" % x .replace ('_' , '-' ) for x in cls .optionalCreateAttrs ])
93
+ detail += " " .join (["[--%s=ARG]" % x .replace ('_' , '-' )
94
+ for x in cls .optionalCreateAttrs ])
87
95
l .append ("%s %s" % (action , detail ))
88
96
89
- if extra_actions . has_key ( cls ) :
97
+ if cls in extra_actions :
90
98
for action in sorted (extra_actions [cls ]):
91
99
d = extra_actions [cls ][action ]
92
100
detail = " " .join (["--%s=ARG" % arg for arg in d ['requiredAttrs' ]])
93
101
l .append ("%s %s" % (action , detail ))
94
102
95
103
return (l )
96
104
105
+
97
106
def usage ():
98
- print ("usage: gitlab [--help|-h] [--fancy|--verbose|-v] [--gitlab=GITLAB] WHAT ACTION [options]" )
107
+ print ("usage: gitlab [--help|-h] [--fancy|--verbose|-v] [--gitlab=GITLAB] "
108
+ "WHAT ACTION [options]" )
99
109
print ("" )
100
110
print ("--gitlab=GITLAB" )
101
- print (" Specifies which python-gitlab.cfg configuration section should be used." )
111
+ print (" Specifies which python-gitlab.cfg configuration section should "
112
+ "be used." )
102
113
print (" If not defined, the default selection will be used." )
103
114
print ("" )
104
115
print ("--fancy, --verbose, -v" )
@@ -108,7 +119,8 @@ def usage():
108
119
print (" Displays this message." )
109
120
print ("" )
110
121
print ("Available `options` depend on which WHAT/ACTION couple is used." )
111
- print ("If `ACTION` is \" help\" , available actions and options will be listed for `ACTION`." )
122
+ print ("If `ACTION` is \" help\" , available actions and options will be "
123
+ "listed for `ACTION`." )
112
124
print ("" )
113
125
print ("Available `WHAT` values are:" )
114
126
@@ -129,15 +141,18 @@ def usage():
129
141
for cls in classes :
130
142
print (" %s" % clsToWhat (cls ))
131
143
144
+
132
145
def do_auth ():
133
146
try :
134
- gl = gitlab .Gitlab (gitlab_url , private_token = gitlab_token , ssl_verify = ssl_verify )
147
+ gl = gitlab .Gitlab (gitlab_url , private_token = gitlab_token ,
148
+ ssl_verify = ssl_verify )
135
149
gl .auth ()
136
150
except :
137
151
die ("Could not connect to GitLab (%s)" % gitlab_url )
138
152
139
153
return gl
140
154
155
+
141
156
def get_id ():
142
157
try :
143
158
id = d .pop ('id' )
@@ -146,6 +161,7 @@ def get_id():
146
161
147
162
return id
148
163
164
+
149
165
def do_create (cls , d ):
150
166
if not cls .canCreate :
151
167
die ("%s objects can't be created" % what )
@@ -158,6 +174,7 @@ def do_create(cls, d):
158
174
159
175
return o
160
176
177
+
161
178
def do_list (cls , d ):
162
179
if not cls .canList :
163
180
die ("%s objects can't be listed" % what )
@@ -169,6 +186,7 @@ def do_list(cls, d):
169
186
170
187
return l
171
188
189
+
172
190
def do_get (cls , d ):
173
191
if not cls .canGet :
174
192
die ("%s objects can't be retrieved" % what )
@@ -184,6 +202,7 @@ def do_get(cls, d):
184
202
185
203
return o
186
204
205
+
187
206
def do_delete (cls , d ):
188
207
if not cls .canDelete :
189
208
die ("%s objects can't be deleted" % what )
@@ -194,6 +213,7 @@ def do_delete(cls, d):
194
213
except Exception as e :
195
214
die ("Impossible to destroy object (%s)" % str (e ))
196
215
216
+
197
217
def do_update (cls , d ):
198
218
if not cls .canUpdate :
199
219
die ("%s objects can't be updated" % what )
@@ -208,22 +228,25 @@ def do_update(cls, d):
208
228
209
229
return o
210
230
231
+
211
232
def do_project_search (d ):
212
233
try :
213
234
return gl .search_projects (d ['query' ])
214
- except :
235
+ except Exception as e :
215
236
die ("Impossible to search projects (%s)" % str (e ))
216
237
238
+
217
239
def do_project_all ():
218
240
try :
219
241
return gl .all_projects ()
220
242
except Exception as e :
221
243
die ("Impossible to list all projects (%s)" % str (e ))
222
244
245
+
223
246
def do_project_owned ():
224
247
try :
225
248
return gl .owned_projects ()
226
- except :
249
+ except Exception as e :
227
250
die ("Impossible to list owned projects (%s)" % str (e ))
228
251
229
252
294
317
gitlab_url = config .get (gitlab_id , 'url' )
295
318
gitlab_token = config .get (gitlab_id , 'private_token' )
296
319
except :
297
- die ("Impossible to get gitlab informations from configuration (%s)" % gitlab_id )
320
+ die ("Impossible to get gitlab informations from configuration (%s)" %
321
+ gitlab_id )
298
322
299
323
try :
300
324
ssl_verify = config .getboolean ('global' , 'ssl_verify' )
@@ -383,6 +407,7 @@ elif action == "all":
383
407
o .display (verbose )
384
408
385
409
else :
386
- die ("Unknown action: %s. Use \" gitlab %s help\" to get details." % (action , what ))
410
+ die ("Unknown action: %s. Use \" gitlab %s help\" to get details." %
411
+ (action , what ))
387
412
388
413
sys .exit (0 )
0 commit comments