18
18
19
19
import os
20
20
import sys
21
+ import re
21
22
22
23
try :
23
24
from ConfigParser import ConfigParser
24
25
except :
25
26
from configparser import ConfigParser
26
27
27
- from inspect import getmro
28
+ from inspect import getmro , getmembers , isclass
28
29
29
30
import gitlab
30
31
32
+ camel_re = re .compile ('(.)([A-Z])' )
33
+
31
34
def die (msg ):
32
35
sys .stderr .write (msg + "\n " )
33
36
sys .exit (1 )
34
37
38
+ def whatToCls (what ):
39
+ return "" .join ([s .capitalize () for s in what .split ("-" )])
40
+
41
+ def clsToWhat (cls ):
42
+ return camel_re .sub (r'\1-\2' , cls .__name__ ).lower ()
43
+
44
+ def actionHelpList (cls ):
45
+ l = []
46
+ for action in 'list' , 'get' , 'create' , 'update' , 'delete' :
47
+ attr = 'can' + action .capitalize ()
48
+ try :
49
+ y = cls .__dict__ [attr ]
50
+ except :
51
+ y = gitlab .GitlabObject .__dict__ [attr ]
52
+ if not y :
53
+ continue
54
+
55
+ detail = ''
56
+ if action == 'list' :
57
+ detail = " " .join (["--%s=ARG" % x for x in cls .requiredListAttrs ])
58
+ elif action in ['get' , 'delete' ]:
59
+ detail = "--id=ARG "
60
+ detail += " " .join (["--%s=ARG" % x for x in cls .requiredGetAttrs ])
61
+ elif action == 'create' :
62
+ detail = " " .join (["--%s=ARG" % x for x in cls .requiredCreateAttrs ])
63
+ if detail :
64
+ detail += " "
65
+ detail += " " .join (["[--%s=ARG]" % x for x in cls .optionalCreateAttrs ])
66
+ elif action == 'update' :
67
+ detail = " " .join (["[--%s=ARG]" % x for x in cls .requiredCreateAttrs ])
68
+ if detail :
69
+ detail += " "
70
+ detail += " " .join (["[--%s=ARG]" % x for x in cls .optionalCreateAttrs ])
71
+ l .append ("%s %s" % (action , detail ))
72
+
73
+ return (l )
74
+
75
+ def usage ():
76
+ print ("usage: gitlab [--help] [--gitlab=GITLAB] what action [options]" )
77
+ print ("" )
78
+ print ("--gitlab=GITLAB: Specifies which python-gitlab.cfg configuration section should be used." )
79
+ print (" If not defined, the default selection will be used." )
80
+ print ("" )
81
+ print ("--help : Displays this message." )
82
+ print ("" )
83
+ print ("Available `options` depend on which what/action couple is used." )
84
+ print ("If `action` is \" help\" , available actions and options will be listed for `what`." )
85
+ print ("" )
86
+ print ("Available `what` values are:" )
87
+
88
+ classes = []
89
+ for name , o in getmembers (gitlab ):
90
+ if not isclass (o ):
91
+ continue
92
+ if gitlab .GitlabObject in getmro (o ) and o != gitlab .GitlabObject :
93
+ classes .append (o )
94
+
95
+ def s (a , b ):
96
+ if a .__name__ < b .__name__ :
97
+ return - 1
98
+ elif a .__name__ > b .__name__ :
99
+ return 1
100
+
101
+ classes .sort (cmp = s )
102
+ for cls in classes :
103
+ print (" %s" % clsToWhat (cls ))
104
+
105
+
35
106
gitlab_id = None
36
107
37
108
args = []
38
109
d = {}
39
110
for arg in sys .argv [1 :]:
40
111
if arg .startswith ('--' ):
112
+ arg = arg [2 :]
113
+
114
+ if arg == 'help' :
115
+ usage ()
116
+ sys .exit (0 )
117
+
41
118
k , v = arg .split ('=' , 2 )
42
- k = k [ 2 :] .strip ()
119
+ k = k .strip ()
43
120
v = v .strip ()
44
121
45
122
if k == 'gitlab' :
@@ -66,23 +143,14 @@ try:
66
143
except :
67
144
die ("Impossible to get gitlab informations from configuration (%s)" % gitlab_id )
68
145
69
- try :
70
- gl = gitlab .Gitlab (gitlab_url , private_token = gitlab_token )
71
- gl .auth ()
72
- except :
73
- die ("Could not connect to GitLab (%s)" % gitlab_url )
74
-
75
146
try :
76
147
what = args .pop (0 )
77
148
action = args .pop (0 )
78
149
except :
79
150
die ("Missing arguments" )
80
151
81
- if action not in ['get' , 'list' , 'update' , 'create' , 'delete' ]:
82
- die ("Unknown action: %s" % action )
83
-
84
- def whatToCls (what ):
85
- return "" .join ([s .capitalize () for s in what .split ("-" )])
152
+ if action not in ['get' , 'list' , 'update' , 'create' , 'delete' , 'help' ]:
153
+ die ("Unknown action: %s. Use \" gitlab %s help\" to get details." % (action , what ))
86
154
87
155
try :
88
156
cls = gitlab .__dict__ [whatToCls (what )]
@@ -92,6 +160,19 @@ except:
92
160
if gitlab .GitlabObject not in getmro (cls ):
93
161
die ("Unknown object: %s" % what )
94
162
163
+ if action == "help" :
164
+ print ("%s options:" % what )
165
+ for item in actionHelpList (cls ):
166
+ print (" %s %s" % (what , item ))
167
+
168
+ sys .exit (0 )
169
+
170
+ try :
171
+ gl = gitlab .Gitlab (gitlab_url , private_token = gitlab_token )
172
+ gl .auth ()
173
+ except :
174
+ die ("Could not connect to GitLab (%s)" % gitlab_url )
175
+
95
176
if action == "create" :
96
177
if not cls .canCreate :
97
178
die ("%s objects can't be created" % what )
@@ -117,7 +198,7 @@ elif action == "list":
117
198
118
199
for o in l :
119
200
o .pretty_print ()
120
- print
201
+ print ( "" )
121
202
122
203
sys .exit (0 )
123
204
0 commit comments