Skip to content

Commit 72a9cfc

Browse files
Add local mapfile merging to osg-comanage-project-usermap
Initial pass at adding merging of local mapfiles into the mapfile generated by osg-comanage-project-usermap. Use option -i and provide a comma-separated list of paths/filenames for the local mapfiles you want to be merged in.
1 parent 0f92ac5 commit 72a9cfc

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

osg-comanage-project-usermap.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
(default = {ENDPOINT})
2525
-o outfile specify output file (default: write to stdout)
2626
-g filter_group filter users by group name (eg, 'ap1-login')
27+
-i inputfiles specify a comma-delineated list of local mapfiles to merge into outfile
2728
-h display this help text
2829
2930
PASS for USER is taken from the first of:
@@ -48,6 +49,7 @@ class Options:
4849
outfile = None
4950
authstr = None
5051
filtergrp = None
52+
inputfiles = []
5153

5254

5355
options = Options()
@@ -87,7 +89,7 @@ def get_co_person_osguser(pid):
8789

8890
def parse_options(args):
8991
try:
90-
ops, args = getopt.getopt(args, 'u:c:d:f:g:e:o:h')
92+
ops, args = getopt.getopt(args, 'u:c:d:f:g:e:o:i:h')
9193
except getopt.GetoptError:
9294
usage()
9395

@@ -106,6 +108,7 @@ def parse_options(args):
106108
if op == '-e': options.endpoint = arg
107109
if op == '-o': options.outfile = arg
108110
if op == '-g': options.filtergrp = arg
111+
if op == '-i': options.inputfiles = arg.split(",")
109112

110113
try:
111114
user, passwd = utils.getpw(options.user, passfd, passfile)
@@ -146,6 +149,35 @@ def get_osguser_groups(filter_group_name=None):
146149
for pid, gids in pid_gids.items() }
147150

148151

152+
def parse_inputfile(inputfile: str):
153+
user_groupmap = dict()
154+
with open(inputfile) as file:
155+
for line in file:
156+
split_line = line.split()
157+
if split_line[0] == "*" and len(split_line) >= 3:
158+
user_groupmap[split_line[1]] = split_line[2].split(",")
159+
return user_groupmap
160+
161+
162+
def merge_maps(maps: list[dict[str, list]]):
163+
while len(maps) > 1:
164+
merge_target = maps[0]
165+
merge_material = maps.pop(1)
166+
for key in merge_material.keys():
167+
if key in merge_target:
168+
merge_target[key].extend(merge_material[key])
169+
merge_target[key] = list(set(merge_target[key]))
170+
else:
171+
merge_target[key] = merge_material[key]
172+
return maps[0]
173+
174+
175+
def merge_inputfiles(osguser_groups):
176+
maps = [osguser_groups]
177+
for inputfile in options.inputfiles:
178+
maps.append(parse_inputfile(inputfile))
179+
return merge_maps(maps)
180+
149181
def print_usermap_to_file(osguser_groups, file):
150182
for osguser, groups in sorted(osguser_groups.items()):
151183
print("* {} {}".format(osguser, ",".join(group.strip() for group in groups)), file=file)
@@ -163,7 +195,8 @@ def main(args):
163195
parse_options(args)
164196

165197
osguser_groups = get_osguser_groups(options.filtergrp)
166-
print_usermap(osguser_groups)
198+
osguser_groups_merged = merge_inputfiles(osguser_groups)
199+
print_usermap(osguser_groups_merged)
167200

168201

169202
if __name__ == "__main__":

0 commit comments

Comments
 (0)