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')
28
+ -l localmaps specify a comma-delimited list of local HTCondor mapfiles to merge into outfile
27
29
-h display this help text
28
30
29
31
PASS for USER is taken from the first of:
@@ -48,6 +50,7 @@ class Options:
48
50
outfile = None
49
51
authstr = None
50
52
filtergrp = None
53
+ localmaps = []
51
54
52
55
53
56
options = Options ()
@@ -87,7 +90,7 @@ def get_co_person_osguser(pid):
87
90
88
91
def parse_options (args ):
89
92
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' )
91
94
except getopt .GetoptError :
92
95
usage ()
93
96
@@ -106,6 +109,7 @@ def parse_options(args):
106
109
if op == '-e' : options .endpoint = arg
107
110
if op == '-o' : options .outfile = arg
108
111
if op == '-g' : options .filtergrp = arg
112
+ if op == '-l' : options .localmaps = arg .split ("," )
109
113
110
114
try :
111
115
user , passwd = utils .getpw (options .user , passfd , passfile )
@@ -142,10 +146,36 @@ def get_osguser_groups(filter_group_name=None):
142
146
if filter_group_name is not None :
143
147
pid_gids = filter_by_group (pid_gids , groups , filter_group_name )
144
148
145
- return { pid_osguser [pid ]: sorted (map (groups .get , gids ))
149
+ return { pid_osguser [pid ]: set (map (groups .get , gids ))
146
150
for pid , gids in pid_gids .items () }
147
151
148
152
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
+
149
179
def print_usermap_to_file (osguser_groups , file ):
150
180
for osguser , groups in sorted (osguser_groups .items ()):
151
181
print ("* {} {}" .format (osguser , "," .join (group .strip () for group in groups )), file = file )
@@ -163,7 +193,13 @@ def main(args):
163
193
parse_options (args )
164
194
165
195
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 )
167
203
168
204
169
205
if __name__ == "__main__" :
0 commit comments