Skip to content

Commit cae8f74

Browse files
authored
Merge pull request #1 from edquist/SOFTWARE-4751.moate-updates
Updates per Moate (SOFTWARE-4751)
2 parents 0f60ea7 + 05010e5 commit cae8f74

File tree

1 file changed

+37
-12
lines changed

1 file changed

+37
-12
lines changed

osg-comanage-project-usermap.py

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@
1717
OPTIONS:
1818
-u USER[:PASS] specify USER and optionally PASS on command line
1919
-d passfd specify open fd to read PASS
20+
-f passfile specify path to file to open and read PASS
2021
-e ENDPOINT specify REST endpoint
22+
-o outfile specify output file (default: write to stdout)
23+
-h display this help text
2124
2225
PASS for USER is taken from the first of:
2326
1. -u USER:PASS
2427
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
2630
2731
ENDPOINT defaults to {ENDPOINT}
2832
"""
@@ -39,17 +43,20 @@ def usage(msg=None):
3943
class Options:
4044
endpoint = ENDPOINT
4145
user = "co_8.project_script"
46+
outfile = None
4247
authstr = None
4348

4449

4550
options = Options()
4651

4752

48-
def getpw(user, passfd=None):
53+
def getpw(user, passfd, passfile):
4954
if ':' in user:
5055
user, pw = user.split(':', 1)
5156
elif passfd is not None:
5257
pw = os.fdopen(passfd).readline().rstrip('\n')
58+
elif passfile is not None:
59+
pw = open(passfile).readline().rstrip('\n')
5360
elif 'PASS' in os.environ:
5461
pw = os.environ['PASS']
5562
else:
@@ -64,7 +71,7 @@ def mkauthstr(user, passwd):
6471

6572

6673
def mkrequest(target, **kw):
67-
url = options.endpoint + target
74+
url = os.path.join(options.endpoint, target)
6875
if kw:
6976
url += "?" + "&".join( "{}={}".format(k,v) for k,v in kw.items() )
7077
req = urllib.request.Request(url)
@@ -131,17 +138,27 @@ def get_co_person_osguser(pid):
131138

132139

133140
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))
136148

137149
passfd = None
150+
passfile = None
138151

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
142159

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)
145162

146163

147164
def gid_pids_to_osguser_pid_gids(gid_pids, pid_osguser):
@@ -167,9 +184,17 @@ def get_osguser_groups():
167184
for pid, gids in pid_gids.items() }
168185

169186

170-
def print_usermap(osguser_groups):
187+
def print_usermap_to_file(osguser_groups, file):
171188
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)
173198

174199

175200
def main(args):

0 commit comments

Comments
 (0)