#!/usr/bin/env python3 #============================================================================== # Copyright and license info is available in the LICENSE file included with # the Server Deployment Package (SDP), and also available online: # https://swarm.workshop.perforce.com/projects/perforce-software-sdp/view/main/LICENSE #------------------------------------------------------------------------------ # MANAGED BY ANSIBLE (perforce-sdp-install / group_limits.yml) -- do not edit by # hand. The limits group name is templated from host_vars (p4_baseline_group). #------------------------------------------------------------------------------ """ This script makes sure that all Perforce users are in the limits group. """ import sys, os, re, subprocess, tempfile if len(sys.argv) < 2: print("Usage: update_limits.py ", file=sys.stderr) sys.exit(1) instance = sys.argv[1] if not re.match(r'^[A-Za-z0-9_]+$', instance): print("Error: instance must be alphanumeric (plus underscore) only.", file=sys.stderr) sys.exit(1) tmpdir = tempfile.mkdtemp(prefix="update_limits_") users_file = os.path.join(tmpdir, "users.txt") limits_file = os.path.join(tmpdir, "limits.txt") newlimits_file = os.path.join(tmpdir, "newlimits.txt") try: subprocess.run(["/p4/common/bin/p4login", instance], check=True) with open(limits_file, "w") as fh: subprocess.run( ["/p4/common/bin/p4master_run", instance, "/p4/{0}/bin/p4_{0}".format(instance), "group", "-o", "{{ p4_baseline_group }}"], stdout=fh, check=True) with open(users_file, "w") as fh: subprocess.run( ["/p4/common/bin/p4master_run", instance, "/p4/{0}/bin/p4_{0}".format(instance), "-ztag", "-F", "%User%", "users"], stdout=fh, check=True) userlist = [] try: with open(users_file, 'r', errors='replace') as u: for user in u.readlines(): user = user.strip() if user != "": userlist.append(user) except Exception: sys.exit(0) # Deduplicate while preserving order seen = set() unique_userlist = [] for user in userlist: if user not in seen: seen.add(user) unique_userlist.append(user) userlist = unique_userlist output = open(newlimits_file, "w") try: with open(limits_file, 'r', errors='replace') as l: for line in l.readlines(): if line != "\n": output.write(line) except Exception: output.close() sys.exit(0) for user in userlist: output.write("\t%s\n" % user) output.close() with open(newlimits_file, "r") as fh: subprocess.run( ["/p4/common/bin/p4master_run", instance, "/p4/{0}/bin/p4_{0}".format(instance), "group", "-i"], stdin=fh, stdout=subprocess.DEVNULL, check=True) finally: for f in [users_file, limits_file, newlimits_file]: if os.path.exists(f): os.remove(f) if os.path.isdir(tmpdir): os.rmdir(tmpdir)