Skip to content

Commit 80a0cd3

Browse files
authored
Merge pull request #25 from mwestphall/feature/replace-sets-with-lists
Replace sets with lists, add manual deduplication logic
2 parents e8c8e06 + b6dd653 commit 80a0cd3

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

osg-comanage-project-usermap.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,19 @@ def parse_options(args):
117117
except PermissionError:
118118
usage("PASS required")
119119

120+
def _deduplicate_list(items):
121+
""" Deduplicate a list while maintaining order by converting it to a dictionary and then back to a list.
122+
Used to ensure a consistent ordering for output group lists, since sets are unordered.
123+
"""
124+
return list(dict.fromkeys(items))
120125

121126
def gid_pids_to_osguser_pid_gids(gid_pids, pid_osguser):
122-
pid_gids = collections.defaultdict(set)
127+
pid_gids = collections.defaultdict(list)
123128

124129
for gid in gid_pids:
125130
for pid in gid_pids[gid]:
126-
if pid_osguser[pid] is not None:
127-
pid_gids[pid].add(gid)
131+
if pid_osguser[pid] is not None and gid not in pid_gids[pid]:
132+
pid_gids[pid].append(gid)
128133

129134
return pid_gids
130135

@@ -146,7 +151,7 @@ def get_osguser_groups(filter_group_name=None):
146151
if filter_group_name is not None:
147152
pid_gids = filter_by_group(pid_gids, groups, filter_group_name)
148153

149-
return { pid_osguser[pid]: set(map(groups.get, gids))
154+
return { pid_osguser[pid]: map(groups.get, gids)
150155
for pid, gids in pid_gids.items() }
151156

152157

@@ -157,9 +162,9 @@ def parse_localmap(inputfile):
157162
# Split up 3 semantic columns
158163
split_line = line.strip().split(maxsplit=2)
159164
if split_line[0] == "*" and len(split_line) == 3:
160-
line_groups = set(re.split(r'[ ,]+', split_line[2]))
165+
line_groups = re.split(r'[ ,]+', split_line[2])
161166
if split_line[1] in user_groupmap:
162-
user_groupmap[split_line[1]] |= line_groups
167+
user_groupmap[split_line[1]] = _deduplicate_list(user_groupmap[split_line[1]] + line_groups)
163168
else:
164169
user_groupmap[split_line[1]] = line_groups
165170
return user_groupmap
@@ -170,9 +175,9 @@ def merge_maps(maps):
170175
for projectmap in maps:
171176
for key in projectmap.keys():
172177
if key in merged_map:
173-
merged_map[key] |= set(projectmap[key])
178+
merged_map[key] = _deduplicate_list(merged_map[key] + projectmap[key])
174179
else:
175-
merged_map[key] = set(projectmap[key])
180+
merged_map[key] = projectmap[key]
176181
return merged_map
177182

178183

0 commit comments

Comments
 (0)