17
17
OPTIONS:
18
18
-u USER[:PASS] specify USER and optionally PASS on command line
19
19
-d passfd specify open fd to read PASS
20
+ -f passfile specify path to file to open and read PASS
20
21
-e ENDPOINT specify REST endpoint
22
+ -o outfile specify output file (default: write to stdout)
23
+ -h display this help text
21
24
22
25
PASS for USER is taken from the first of:
23
26
1. -u USER:PASS
24
27
2. -d passfd (read from fd)
25
- 3. read from $PASS env var
28
+ 3. -f passfile (read from file)
29
+ 4. read from $PASS env var
26
30
27
31
ENDPOINT defaults to {ENDPOINT}
28
32
"""
@@ -39,17 +43,20 @@ def usage(msg=None):
39
43
class Options :
40
44
endpoint = ENDPOINT
41
45
user = "co_8.project_script"
46
+ outfile = None
42
47
authstr = None
43
48
44
49
45
50
options = Options ()
46
51
47
52
48
- def getpw (user , passfd = None ):
53
+ def getpw (user , passfd , passfile ):
49
54
if ':' in user :
50
55
user , pw = user .split (':' , 1 )
51
56
elif passfd is not None :
52
57
pw = os .fdopen (passfd ).readline ().rstrip ('\n ' )
58
+ elif passfile is not None :
59
+ pw = open (passfile ).readline ().rstrip ('\n ' )
53
60
elif 'PASS' in os .environ :
54
61
pw = os .environ ['PASS' ]
55
62
else :
@@ -64,7 +71,7 @@ def mkauthstr(user, passwd):
64
71
65
72
66
73
def mkrequest (target , ** kw ):
67
- url = options .endpoint + target
74
+ url = os . path . join ( options .endpoint , target )
68
75
if kw :
69
76
url += "?" + "&" .join ( "{}={}" .format (k ,v ) for k ,v in kw .items () )
70
77
req = urllib .request .Request (url )
@@ -131,17 +138,27 @@ def get_co_person_osguser(pid):
131
138
132
139
133
140
def parse_options (args ):
134
- ops , args = getopt .getopt (args , 'u:d:' )
135
- ops = dict (ops )
141
+ try :
142
+ ops , args = getopt .getopt (args , 'u:d:f:e:o:h' )
143
+ except getopt .GetoptError :
144
+ usage ()
145
+
146
+ if args :
147
+ usage ("Extra arguments: %s" % repr (args ))
136
148
137
149
passfd = None
150
+ passfile = None
138
151
139
- if '-u' in ops : options .user = ops ['-u' ]
140
- if '-d' in ops : passfd = int (ops ['-d' ])
141
- if '-e' in ops : options .endpoint = ops ['-e' ]
152
+ for op , arg in ops :
153
+ if op == '-h' : usage ()
154
+ if op == '-u' : options .user = arg
155
+ if op == '-d' : passfd = int (arg )
156
+ if op == '-f' : passfile = arg
157
+ if op == '-e' : options .endpoint = arg
158
+ if op == '-o' : options .outfile = arg
142
159
143
- options . user , passwd = getpw (options .user , passfd )
144
- options .authstr = mkauthstr (options . user , passwd )
160
+ user , passwd = getpw (options .user , passfd , passfile )
161
+ options .authstr = mkauthstr (user , passwd )
145
162
146
163
147
164
def gid_pids_to_osguser_pid_gids (gid_pids , pid_osguser ):
@@ -167,9 +184,17 @@ def get_osguser_groups():
167
184
for pid , gids in pid_gids .items () }
168
185
169
186
170
- def print_usermap (osguser_groups ):
187
+ def print_usermap_to_file (osguser_groups , file ):
171
188
for osguser , groups in osguser_groups .items ():
172
- print ("* {} {}" .format (osguser , "," .join (groups )))
189
+ print ("* {} {}" .format (osguser , "," .join (groups )), file = file )
190
+
191
+
192
+ def print_usermap (osguser_groups ):
193
+ if options .outfile :
194
+ with open (options .outfile , "w" ) as w :
195
+ print_usermap_to_file (osguser_groups , w )
196
+ else :
197
+ print_usermap_to_file (osguser_groups , sys .stdout )
173
198
174
199
175
200
def main (args ):
0 commit comments