24
24
(default = { ENDPOINT } )
25
25
-o outfile specify output file (default: write to stdout)
26
26
-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
27
28
-h display this help text
28
29
29
30
PASS for USER is taken from the first of:
@@ -48,6 +49,7 @@ class Options:
48
49
outfile = None
49
50
authstr = None
50
51
filtergrp = None
52
+ inputfiles = []
51
53
52
54
53
55
options = Options ()
@@ -87,7 +89,7 @@ def get_co_person_osguser(pid):
87
89
88
90
def parse_options (args ):
89
91
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' )
91
93
except getopt .GetoptError :
92
94
usage ()
93
95
@@ -106,6 +108,7 @@ def parse_options(args):
106
108
if op == '-e' : options .endpoint = arg
107
109
if op == '-o' : options .outfile = arg
108
110
if op == '-g' : options .filtergrp = arg
111
+ if op == '-i' : options .inputfiles = arg .split ("," )
109
112
110
113
try :
111
114
user , passwd = utils .getpw (options .user , passfd , passfile )
@@ -146,6 +149,35 @@ def get_osguser_groups(filter_group_name=None):
146
149
for pid , gids in pid_gids .items () }
147
150
148
151
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
+
149
181
def print_usermap_to_file (osguser_groups , file ):
150
182
for osguser , groups in sorted (osguser_groups .items ()):
151
183
print ("* {} {}" .format (osguser , "," .join (group .strip () for group in groups )), file = file )
@@ -163,7 +195,8 @@ def main(args):
163
195
parse_options (args )
164
196
165
197
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 )
167
200
168
201
169
202
if __name__ == "__main__" :
0 commit comments