Skip to content

Commit e8c8e06

Browse files
Merge pull request #24 from williamnswanson/INF-2273.merge-local-mapfiles
Add local mapfile merging to osg-comanage-project-usermap (INF-2273)
2 parents 0f92ac5 + ccb46b3 commit e8c8e06

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

osg-comanage-project-usermap.py

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python3
22

33
import os
4+
import re
45
import sys
56
import getopt
67
import collections
@@ -24,6 +25,7 @@
2425
(default = {ENDPOINT})
2526
-o outfile specify output file (default: write to stdout)
2627
-g filter_group filter users by group name (eg, 'ap1-login')
28+
-l localmaps specify a comma-delimited list of local HTCondor mapfiles to merge into outfile
2729
-h display this help text
2830
2931
PASS for USER is taken from the first of:
@@ -48,6 +50,7 @@ class Options:
4850
outfile = None
4951
authstr = None
5052
filtergrp = None
53+
localmaps = []
5154

5255

5356
options = Options()
@@ -87,7 +90,7 @@ def get_co_person_osguser(pid):
8790

8891
def parse_options(args):
8992
try:
90-
ops, args = getopt.getopt(args, 'u:c:d:f:g:e:o:h')
93+
ops, args = getopt.getopt(args, 'u:c:d:f:g:e:o:l:h')
9194
except getopt.GetoptError:
9295
usage()
9396

@@ -106,6 +109,7 @@ def parse_options(args):
106109
if op == '-e': options.endpoint = arg
107110
if op == '-o': options.outfile = arg
108111
if op == '-g': options.filtergrp = arg
112+
if op == '-l': options.localmaps = arg.split(",")
109113

110114
try:
111115
user, passwd = utils.getpw(options.user, passfd, passfile)
@@ -142,10 +146,36 @@ def get_osguser_groups(filter_group_name=None):
142146
if filter_group_name is not None:
143147
pid_gids = filter_by_group(pid_gids, groups, filter_group_name)
144148

145-
return { pid_osguser[pid]: sorted(map(groups.get, gids))
149+
return { pid_osguser[pid]: set(map(groups.get, gids))
146150
for pid, gids in pid_gids.items() }
147151

148152

153+
def parse_localmap(inputfile):
154+
user_groupmap = dict()
155+
with open(inputfile, 'r', encoding='utf-8') as file:
156+
for line in file:
157+
# Split up 3 semantic columns
158+
split_line = line.strip().split(maxsplit=2)
159+
if split_line[0] == "*" and len(split_line) == 3:
160+
line_groups = set(re.split(r'[ ,]+', split_line[2]))
161+
if split_line[1] in user_groupmap:
162+
user_groupmap[split_line[1]] |= line_groups
163+
else:
164+
user_groupmap[split_line[1]] = line_groups
165+
return user_groupmap
166+
167+
168+
def merge_maps(maps):
169+
merged_map = dict()
170+
for projectmap in maps:
171+
for key in projectmap.keys():
172+
if key in merged_map:
173+
merged_map[key] |= set(projectmap[key])
174+
else:
175+
merged_map[key] = set(projectmap[key])
176+
return merged_map
177+
178+
149179
def print_usermap_to_file(osguser_groups, file):
150180
for osguser, groups in sorted(osguser_groups.items()):
151181
print("* {} {}".format(osguser, ",".join(group.strip() for group in groups)), file=file)
@@ -163,7 +193,13 @@ def main(args):
163193
parse_options(args)
164194

165195
osguser_groups = get_osguser_groups(options.filtergrp)
166-
print_usermap(osguser_groups)
196+
197+
maps = [osguser_groups]
198+
for localmap in options.localmaps:
199+
maps.append(parse_localmap(localmap))
200+
osguser_groups_merged = merge_maps(maps)
201+
202+
print_usermap(osguser_groups_merged)
167203

168204

169205
if __name__ == "__main__":

0 commit comments

Comments
 (0)