FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-gitlab/gitlab/v4/cli.py at master · movermeyer/python-gitlab · GitHub
movermeyer
python-gitlab
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
python-gitlab
/
gitlab
/
v4
/
cli.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
337 lines (279 loc) · 12.9 KB
Breadcrumbs
python-gitlab
/
gitlab
/
v4
/
cli.py
Copy path
File metadata and controls
337 lines (279 loc) · 12.9 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2013-2017 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/>.
from
__future__
import
print_function
import
inspect
import
operator
import
six
import
gitlab
import
gitlab
.
base
from
gitlab
import
cli
import
gitlab
.
v4
.
objects
class
GitlabCLI
(
object
):
def
__init__
(
self
,
gl
,
what
,
action
,
args
):
self
.
cls_name
=
cli
.
what_to_cls
(
what
)
self
.
cls
=
gitlab
.
v4
.
objects
.
__dict__
[
self
.
cls_name
]
self
.
what
=
what
.
replace
(
'-'
,
'_'
)
self
.
action
=
action
.
lower
()
self
.
gl
=
gl
self
.
args
=
args
self
.
mgr_cls
=
getattr
(
gitlab
.
v4
.
objects
,
self
.
cls
.
__name__
+
'Manager'
)
# We could do something smart, like splitting the manager name to find
# parents, build the chain of managers to get to the final object.
# Instead we do something ugly and efficient: interpolate variables in
# the class _path attribute, and replace the value with the result.
self
.
mgr_cls
.
_path
=
self
.
mgr_cls
.
_path
%
self
.
args
self
.
mgr
=
self
.
mgr_cls
(
gl
)
def
__call__
(
self
):
method
=
'do_%s'
%
self
.
action
if
hasattr
(
self
,
method
):
return
getattr
(
self
,
method
)()
else
:
return
self
.
do_custom
()
def
do_custom
(
self
):
in_obj
=
cli
.
custom_actions
[
self
.
cls_name
][
self
.
action
][
2
]
# Get the object (lazy), then act
if
in_obj
:
data
=
{}
if
hasattr
(
self
.
mgr
,
'_from_parent_attrs'
):
for
k
in
self
.
mgr
.
_from_parent_attrs
:
data
[
k
]
=
self
.
args
[
k
]
if
gitlab
.
mixins
.
GetWithoutIdMixin
not
in
inspect
.
getmro
(
self
.
cls
):
data
[
self
.
cls
.
_id_attr
]
=
self
.
args
.
pop
(
self
.
cls
.
_id_attr
)
o
=
self
.
cls
(
self
.
mgr
,
data
)
method_name
=
self
.
action
.
replace
(
'-'
,
'_'
)
return
getattr
(
o
,
method_name
)(
**
self
.
args
)
else
:
return
getattr
(
self
.
mgr
,
self
.
action
)(
**
self
.
args
)
def
do_create
(
self
):
try
:
return
self
.
mgr
.
create
(
self
.
args
)
except
Exception
as
e
:
cli
.
die
(
"Impossible to create object"
,
e
)
def
do_list
(
self
):
try
:
return
self
.
mgr
.
list
(
**
self
.
args
)
except
Exception
as
e
:
cli
.
die
(
"Impossible to list objects"
,
e
)
def
do_get
(
self
):
id
=
None
if
gitlab
.
mixins
.
GetWithoutIdMixin
not
in
inspect
.
getmro
(
self
.
mgr_cls
):
id
=
self
.
args
.
pop
(
self
.
cls
.
_id_attr
)
try
:
return
self
.
mgr
.
get
(
id
,
**
self
.
args
)
except
Exception
as
e
:
cli
.
die
(
"Impossible to get object"
,
e
)
def
do_delete
(
self
):
id
=
self
.
args
.
pop
(
self
.
cls
.
_id_attr
)
try
:
self
.
mgr
.
delete
(
id
,
**
self
.
args
)
except
Exception
as
e
:
cli
.
die
(
"Impossible to destroy object"
,
e
)
def
do_update
(
self
):
id
=
None
if
gitlab
.
mixins
.
GetWithoutIdMixin
not
in
inspect
.
getmro
(
self
.
mgr_cls
):
id
=
self
.
args
.
pop
(
self
.
cls
.
_id_attr
)
try
:
return
self
.
mgr
.
update
(
id
,
self
.
args
)
except
Exception
as
e
:
cli
.
die
(
"Impossible to update object"
,
e
)
def
_populate_sub_parser_by_class
(
cls
,
sub_parser
):
mgr_cls_name
=
cls
.
__name__
+
'Manager'
mgr_cls
=
getattr
(
gitlab
.
v4
.
objects
,
mgr_cls_name
)
for
action_name
in
[
'list'
,
'get'
,
'create'
,
'update'
,
'delete'
]:
if
not
hasattr
(
mgr_cls
,
action_name
):
continue
sub_parser_action
=
sub_parser
.
add_parser
(
action_name
)
sub_parser_action
.
add_argument
(
"--sudo"
,
required
=
False
)
if
hasattr
(
mgr_cls
,
'_from_parent_attrs'
):
[
sub_parser_action
.
add_argument
(
"--%s"
%
x
.
replace
(
'_'
,
'-'
),
required
=
True
)
for
x
in
mgr_cls
.
_from_parent_attrs
]
if
action_name
==
"list"
:
if
hasattr
(
mgr_cls
,
'_list_filters'
):
[
sub_parser_action
.
add_argument
(
"--%s"
%
x
.
replace
(
'_'
,
'-'
),
required
=
False
)
for
x
in
mgr_cls
.
_list_filters
]
sub_parser_action
.
add_argument
(
"--page"
,
required
=
False
)
sub_parser_action
.
add_argument
(
"--per-page"
,
required
=
False
)
sub_parser_action
.
add_argument
(
"--all"
,
required
=
False
,
action
=
'store_true'
)
if
action_name
==
'delete'
:
id_attr
=
cls
.
_id_attr
.
replace
(
'_'
,
'-'
)
sub_parser_action
.
add_argument
(
"--%s"
%
id_attr
,
required
=
True
)
if
action_name
==
"get"
:
if
gitlab
.
mixins
.
GetWithoutIdMixin
not
in
inspect
.
getmro
(
cls
):
if
cls
.
_id_attr
is
not
None
:
id_attr
=
cls
.
_id_attr
.
replace
(
'_'
,
'-'
)
sub_parser_action
.
add_argument
(
"--%s"
%
id_attr
,
required
=
True
)
if
hasattr
(
mgr_cls
,
'_optional_get_attrs'
):
[
sub_parser_action
.
add_argument
(
"--%s"
%
x
.
replace
(
'_'
,
'-'
),
required
=
False
)
for
x
in
mgr_cls
.
_optional_get_attrs
]
if
action_name
==
"create"
:
if
hasattr
(
mgr_cls
,
'_create_attrs'
):
[
sub_parser_action
.
add_argument
(
"--%s"
%
x
.
replace
(
'_'
,
'-'
),
required
=
True
)
for
x
in
mgr_cls
.
_create_attrs
[
0
]
if
x
!=
cls
.
_id_attr
]
[
sub_parser_action
.
add_argument
(
"--%s"
%
x
.
replace
(
'_'
,
'-'
),
required
=
False
)
for
x
in
mgr_cls
.
_create_attrs
[
1
]
if
x
!=
cls
.
_id_attr
]
if
action_name
==
"update"
:
if
cls
.
_id_attr
is
not
None
:
id_attr
=
cls
.
_id_attr
.
replace
(
'_'
,
'-'
)
sub_parser_action
.
add_argument
(
"--%s"
%
id_attr
,
required
=
True
)
if
hasattr
(
mgr_cls
,
'_update_attrs'
):
[
sub_parser_action
.
add_argument
(
"--%s"
%
x
.
replace
(
'_'
,
'-'
),
required
=
True
)
for
x
in
mgr_cls
.
_update_attrs
[
0
]
if
x
!=
cls
.
_id_attr
]
[
sub_parser_action
.
add_argument
(
"--%s"
%
x
.
replace
(
'_'
,
'-'
),
required
=
False
)
for
x
in
mgr_cls
.
_update_attrs
[
1
]
if
x
!=
cls
.
_id_attr
]
if
cls
.
__name__
in
cli
.
custom_actions
:
name
=
cls
.
__name__
for
action_name
in
cli
.
custom_actions
[
name
]:
sub_parser_action
=
sub_parser
.
add_parser
(
action_name
)
# Get the attributes for URL/path construction
if
hasattr
(
mgr_cls
,
'_from_parent_attrs'
):
[
sub_parser_action
.
add_argument
(
"--%s"
%
x
.
replace
(
'_'
,
'-'
),
required
=
True
)
for
x
in
mgr_cls
.
_from_parent_attrs
]
sub_parser_action
.
add_argument
(
"--sudo"
,
required
=
False
)
# We need to get the object somehow
if
gitlab
.
mixins
.
GetWithoutIdMixin
not
in
inspect
.
getmro
(
cls
):
if
cls
.
_id_attr
is
not
None
:
id_attr
=
cls
.
_id_attr
.
replace
(
'_'
,
'-'
)
sub_parser_action
.
add_argument
(
"--%s"
%
id_attr
,
required
=
True
)
required
,
optional
,
dummy
=
cli
.
custom_actions
[
name
][
action_name
]
[
sub_parser_action
.
add_argument
(
"--%s"
%
x
.
replace
(
'_'
,
'-'
),
required
=
True
)
for
x
in
required
if
x
!=
cls
.
_id_attr
]
[
sub_parser_action
.
add_argument
(
"--%s"
%
x
.
replace
(
'_'
,
'-'
),
required
=
False
)
for
x
in
optional
if
x
!=
cls
.
_id_attr
]
if
mgr_cls
.
__name__
in
cli
.
custom_actions
:
name
=
mgr_cls
.
__name__
for
action_name
in
cli
.
custom_actions
[
name
]:
sub_parser_action
=
sub_parser
.
add_parser
(
action_name
)
if
hasattr
(
mgr_cls
,
'_from_parent_attrs'
):
[
sub_parser_action
.
add_argument
(
"--%s"
%
x
.
replace
(
'_'
,
'-'
),
required
=
True
)
for
x
in
mgr_cls
.
_from_parent_attrs
]
sub_parser_action
.
add_argument
(
"--sudo"
,
required
=
False
)
required
,
optional
,
dummy
=
cli
.
custom_actions
[
name
][
action_name
]
[
sub_parser_action
.
add_argument
(
"--%s"
%
x
.
replace
(
'_'
,
'-'
),
required
=
True
)
for
x
in
required
if
x
!=
cls
.
_id_attr
]
[
sub_parser_action
.
add_argument
(
"--%s"
%
x
.
replace
(
'_'
,
'-'
),
required
=
False
)
for
x
in
optional
if
x
!=
cls
.
_id_attr
]
def
extend_parser
(
parser
):
subparsers
=
parser
.
add_subparsers
(
title
=
'object'
,
dest
=
'what'
,
help
=
"Object to manipulate."
)
subparsers
.
required
=
True
# populate argparse for all Gitlab Object
classes
=
[]
for
cls
in
gitlab
.
v4
.
objects
.
__dict__
.
values
():
try
:
if
gitlab
.
base
.
RESTManager
in
inspect
.
getmro
(
cls
):
if
cls
.
_obj_cls
is
not
None
:
classes
.
append
(
cls
.
_obj_cls
)
except
AttributeError
:
pass
classes
.
sort
(
key
=
operator
.
attrgetter
(
"__name__"
))
for
cls
in
classes
:
arg_name
=
cli
.
cls_to_what
(
cls
)
object_group
=
subparsers
.
add_parser
(
arg_name
)
object_subparsers
=
object_group
.
add_subparsers
(
dest
=
'action'
,
help
=
"Action to execute."
)
_populate_sub_parser_by_class
(
cls
,
object_subparsers
)
object_subparsers
.
required
=
True
return
parser
class
JSONPrinter
(
object
):
def
display
(
self
,
d
,
**
kwargs
):
import
json
# noqa
print
(
json
.
dumps
(
d
))
class
YAMLPrinter
(
object
):
def
display
(
self
,
d
,
**
kwargs
):
import
yaml
# noqa
print
(
yaml
.
safe_dump
(
d
,
default_flow_style
=
False
))
class
LegacyPrinter
(
object
):
def
display
(
self
,
d
,
**
kwargs
):
verbose
=
kwargs
.
get
(
'verbose'
,
False
)
padding
=
kwargs
.
get
(
'padding'
,
0
)
obj
=
kwargs
.
get
(
'obj'
)
def
display_dict
(
d
,
padding
):
for
k
in
sorted
(
d
.
keys
()):
v
=
d
[
k
]
if
isinstance
(
v
,
dict
):
print
(
'%s%s:'
%
(
' '
*
padding
,
k
.
replace
(
'_'
,
'-'
)))
new_padding
=
padding
+
2
self
.
display
(
v
,
verbose
=
True
,
padding
=
new_padding
,
obj
=
v
)
continue
print
(
'%s%s: %s'
%
(
' '
*
padding
,
k
.
replace
(
'_'
,
'-'
),
v
))
if
verbose
:
if
isinstance
(
obj
,
dict
):
display_dict
(
obj
,
padding
)
return
# not a dict, we assume it's a RESTObject
if
obj
.
_id_attr
:
id
=
getattr
(
obj
,
obj
.
_id_attr
,
None
)
print
(
'%s: %s'
%
(
obj
.
_id_attr
,
id
))
attrs
=
obj
.
attributes
if
obj
.
_id_attr
:
attrs
.
pop
(
obj
.
_id_attr
)
display_dict
(
attrs
,
padding
)
else
:
if
obj
.
_id_attr
:
id
=
getattr
(
obj
,
obj
.
_id_attr
)
print
(
'%s: %s'
%
(
obj
.
_id_attr
.
replace
(
'_'
,
'-'
),
id
))
if
hasattr
(
obj
,
'_short_print_attr'
):
value
=
getattr
(
obj
,
obj
.
_short_print_attr
)
print
(
'%s: %s'
%
(
obj
.
_short_print_attr
,
value
))
PRINTERS
=
{
'json'
:
JSONPrinter
,
'legacy'
:
LegacyPrinter
,
'yaml'
:
YAMLPrinter
,
}
def
run
(
gl
,
what
,
action
,
args
,
verbose
,
output
,
fields
):
g_cli
=
GitlabCLI
(
gl
,
what
,
action
,
args
)
ret_val
=
g_cli
()
printer
=
PRINTERS
[
output
]()
def
get_dict
(
obj
):
if
fields
:
return
{
k
:
v
for
k
,
v
in
obj
.
attributes
.
items
()
if
k
in
fields
}
return
obj
.
attributes
if
isinstance
(
ret_val
,
dict
):
printer
.
display
(
ret_val
,
verbose
=
True
,
obj
=
ret_val
)
elif
isinstance
(
ret_val
,
list
):
for
obj
in
ret_val
:
if
isinstance
(
obj
,
gitlab
.
base
.
RESTObject
):
printer
.
display
(
get_dict
(
obj
),
verbose
=
verbose
,
obj
=
obj
)
else
:
print
(
obj
)
print
(
''
)
elif
isinstance
(
ret_val
,
dict
):
printer
.
display
(
ret_val
,
verbose
=
verbose
,
obj
=
ret_val
)
elif
isinstance
(
ret_val
,
gitlab
.
base
.
RESTObject
):
printer
.
display
(
get_dict
(
ret_val
),
verbose
=
verbose
,
obj
=
ret_val
)
elif
isinstance
(
ret_val
,
six
.
string_types
):
print
(
ret_val
)
Back
|
FazBrowse Home
|
New Git URL