#!/usr/bin/env python
'''Script to clean no existing users from Perforce groups'''
# =====================================================
# Copyright (c) Miki Tebeka <miki.tebeka@gmail.com>
# This file is under the GNU Public License (GPL), see
# http://www.gnu.org/copyleft/gpl.html for more details
# =====================================================
__author__ = "Miki Tebeka <miki.tebeka@gmail.com>"
# $Id: //guest/miki_tebeka/clean_groups.py#2 $
from os import popen
# List of all users
users = {}
# Collect all users
for line in popen("p4 users"):
users[line.split()[0]] = 1
# Fix groups
for group in popen("p4 groups"):
group = group.strip()
# Read group description
lines = popen("p4 group -o %s" % group)
# Open update pipe
pipe = popen("p4 group -i", "w")
in_users = 0 # Are we in users section
for line in lines:
write = 1 # "write this line" flag
if line.startswith("Users:"):
in_users = 1
elif not in_users: # Write all line not in users
pass
elif not line.strip(): # Ignore empty lines
pass
else: # In users, check user
user = line.strip()
if user not in users:
print "removing %s from %s" % (user, group)
write = 0
else:
write = 1
# Write line to group description
if write:
pipe.write(line)