Skip to content

Commit ccb46b3

Browse files
Feedback from Brian L. and cleanup
1 parent ffe8ece commit ccb46b3

File tree

1 file changed

+29
-26
lines changed

1 file changed

+29
-26
lines changed

osg-comanage-project-usermap.py

Lines changed: 29 additions & 26 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,7 +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')
27-
-i inputfiles specify a comma-delineated list of local mapfiles to merge into outfile
28+
-l localmaps specify a comma-delimited list of local HTCondor mapfiles to merge into outfile
2829
-h display this help text
2930
3031
PASS for USER is taken from the first of:
@@ -49,7 +50,7 @@ class Options:
4950
outfile = None
5051
authstr = None
5152
filtergrp = None
52-
inputfiles = []
53+
localmaps = []
5354

5455

5556
options = Options()
@@ -89,7 +90,7 @@ def get_co_person_osguser(pid):
8990

9091
def parse_options(args):
9192
try:
92-
ops, args = getopt.getopt(args, 'u:c:d:f:g:e:o:i:h')
93+
ops, args = getopt.getopt(args, 'u:c:d:f:g:e:o:l:h')
9394
except getopt.GetoptError:
9495
usage()
9596

@@ -108,7 +109,7 @@ def parse_options(args):
108109
if op == '-e': options.endpoint = arg
109110
if op == '-o': options.outfile = arg
110111
if op == '-g': options.filtergrp = arg
111-
if op == '-i': options.inputfiles = arg.split(",")
112+
if op == '-l': options.localmaps = arg.split(",")
112113

113114
try:
114115
user, passwd = utils.getpw(options.user, passfd, passfile)
@@ -145,39 +146,36 @@ def get_osguser_groups(filter_group_name=None):
145146
if filter_group_name is not None:
146147
pid_gids = filter_by_group(pid_gids, groups, filter_group_name)
147148

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

151152

152-
def parse_inputfile(inputfile):
153+
def parse_localmap(inputfile):
153154
user_groupmap = dict()
154-
with open(inputfile) as file:
155+
with open(inputfile, 'r', encoding='utf-8') as file:
155156
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(",")
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
159165
return user_groupmap
160166

161167

162168
def merge_maps(maps):
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]))
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])
170174
else:
171-
merge_target[key] = merge_material[key]
172-
return maps[0]
175+
merged_map[key] = set(projectmap[key])
176+
return merged_map
173177

174178

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-
181179
def print_usermap_to_file(osguser_groups, file):
182180
for osguser, groups in sorted(osguser_groups.items()):
183181
print("* {} {}".format(osguser, ",".join(group.strip() for group in groups)), file=file)
@@ -195,7 +193,12 @@ def main(args):
195193
parse_options(args)
196194

197195
osguser_groups = get_osguser_groups(options.filtergrp)
198-
osguser_groups_merged = merge_inputfiles(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+
199202
print_usermap(osguser_groups_merged)
200203

201204

0 commit comments

Comments
 (0)