1
1
#!/usr/bin/env python3
2
2
3
3
import os
4
+ import re
4
5
import sys
5
6
import getopt
6
7
import collections
24
25
(default = { ENDPOINT } )
25
26
-o outfile specify output file (default: write to stdout)
26
27
-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
28
29
-h display this help text
29
30
30
31
PASS for USER is taken from the first of:
@@ -49,7 +50,7 @@ class Options:
49
50
outfile = None
50
51
authstr = None
51
52
filtergrp = None
52
- inputfiles = []
53
+ localmaps = []
53
54
54
55
55
56
options = Options ()
@@ -89,7 +90,7 @@ def get_co_person_osguser(pid):
89
90
90
91
def parse_options (args ):
91
92
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' )
93
94
except getopt .GetoptError :
94
95
usage ()
95
96
@@ -108,7 +109,7 @@ def parse_options(args):
108
109
if op == '-e' : options .endpoint = arg
109
110
if op == '-o' : options .outfile = arg
110
111
if op == '-g' : options .filtergrp = arg
111
- if op == '-i ' : options .inputfiles = arg .split ("," )
112
+ if op == '-l ' : options .localmaps = arg .split ("," )
112
113
113
114
try :
114
115
user , passwd = utils .getpw (options .user , passfd , passfile )
@@ -145,39 +146,36 @@ def get_osguser_groups(filter_group_name=None):
145
146
if filter_group_name is not None :
146
147
pid_gids = filter_by_group (pid_gids , groups , filter_group_name )
147
148
148
- return { pid_osguser [pid ]: sorted (map (groups .get , gids ))
149
+ return { pid_osguser [pid ]: set (map (groups .get , gids ))
149
150
for pid , gids in pid_gids .items () }
150
151
151
152
152
- def parse_inputfile (inputfile ):
153
+ def parse_localmap (inputfile ):
153
154
user_groupmap = dict ()
154
- with open (inputfile ) as file :
155
+ with open (inputfile , 'r' , encoding = 'utf-8' ) as file :
155
156
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
159
165
return user_groupmap
160
166
161
167
162
168
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 ])
170
174
else :
171
- merge_target [key ] = merge_material [key ]
172
- return maps [ 0 ]
175
+ merged_map [key ] = set ( projectmap [key ])
176
+ return merged_map
173
177
174
178
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
-
181
179
def print_usermap_to_file (osguser_groups , file ):
182
180
for osguser , groups in sorted (osguser_groups .items ()):
183
181
print ("* {} {}" .format (osguser , "," .join (group .strip () for group in groups )), file = file )
@@ -195,7 +193,12 @@ def main(args):
195
193
parse_options (args )
196
194
197
195
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
+
199
202
print_usermap (osguser_groups_merged )
200
203
201
204
0 commit comments