#!/usr/bin/env python #################################################### # findPList.py # # Written by David Jeske # # This code is in the Public Domain. Pretend there is # a fancy legal disclaimer here limiting liability. # # You probably want to put an entry in crontab to have # this script run once a week. # import os import string import smtplib import time gOldAgeSecs = 86400 * 60 # NUMBER OF DAYS FOR CLIENTS TO BE "OLD" gMailhost = "mail" # PUT THE NAME OF YOUR MAILHOST HERE gAdmin = "root" # ADMINISTRATION EMAIL ADDRESS gMailHead = """ ** OLD P4-CLIENT NOTIFICATION\r \r This is an automatically generated email to inform you that you have\r old clients in Perforce which have not been accessed in the last\r several weeks. If you don't delete these old clients, they keep\r taking up space in the server, and you will keep receiving this\r email. Normally you can delete these old clients if they are\r not in use by typing "p4 client -d ". If there are still files open in this client, you either need to find the client and close the files, or you can type 'p4 client -df '.\r \r\n \r\n However, sometimes there are more complicated problems. If you have\r trouble deleting an old unused client, drop an email to\r %s.\r\n """ % gAdmin # :$Id: # User: The user's user name. # Email: The user's email address; for email review. # Update: The date this specification was last modified. # Access: The date this user was last active. Read only. # FullName: The user's real name. # JobView: Selects jobs for inclusion during changelist creation. # Password: If set, user must have matching $P4PASSWD on client. # Reviews: Listing of depot files to be reviewed by user. def get_user_info(user_name): in_part = "lines" reviews = [] userspec = {} lines = os.popen("p4 user -o %s" % user_name).readlines() for a_line in lines: stripped_line = string.strip(a_line) if not (len(stripped_line)==0 or stripped_line[0] == "#"): line_parts = string.split(stripped_line,":") if line_parts[0] in ['User','Email','Update','Access','FullName', 'JobView','Password','Reviews']: in_part = "lines" if in_part == "reviews": reviews.append(stripped_line) else: if line_parts[0] == "Reviews": in_part = "reviews" else: userspec[line_parts[0]] = string.strip(line_parts[1]) userspec['Reviews'] = reviews return userspec def get_list_of_clients(): clients = [] lines = os.popen("p4 clients").readlines() for a_line in lines: clients.append(string.split(a_line)[1]) return clients def get_client_info(client_name): in_part = "lines" viewspec = [] desc = [] clientspec = {} lines = os.popen("p4 client -o %s" % client_name).readlines() for a_line in lines: stripped_line = string.strip(a_line) if not (len(stripped_line)==0 or stripped_line[0] == "#"): line_parts = string.split(stripped_line,":") if line_parts[0] in ['Client', 'Owner','Update', 'Access','Root','Options','Description','View']: in_part = "lines" if in_part == "desc": desc.append(stripped_line) elif in_part == "view": viewspec.append(stripped_line) else: if line_parts[0] == "View": in_part = "view" elif line_parts[0] == "Description": in_part = "desc" desc.append(string.strip(line_parts[1])) else: clientspec[line_parts[0]] = string.strip(line_parts[1]) clientspec["View"] = viewspec return clientspec # decode_date(date_str) # # date_str is of the form: 2001/05/18 05 # YYYY/MM/DD HH def decode_date(date_str): (y,m,d) = string.split(string.split(date_str,' ')[0],'/') date_sec = time.mktime( (int(y),int(m),int(d),0,0,0,0,0,0) ) return date_sec def main(): mailport = smtplib.SMTP(gMailhost) users = {} client_list = get_list_of_clients() for a_client in client_list: clientspec = get_client_info(a_client) access_date_sec = decode_date(clientspec['Access']) now = time.time() if (now - gOldAgeSecs) > access_date_sec: print "OLD: %s" % a_client client_desc = "Client: %-15s Accessed: %-15s Root: %s" % (a_client,clientspec['Access'],clientspec['Root']) if users.has_key(clientspec['Owner']): users[clientspec['Owner']].append(client_desc) else: users[clientspec['Owner']] = [client_desc] else: print " : %s" % a_client for a_user,client_list in users.items(): user_info = get_user_info(a_user) em = user_info['Email'] email_parts = ["From: %s" % gAdmin, "To: %s" % em, "Subject: (%s) old P4 clients for %s\r\b\r\n" % (len(client_list),a_user), gMailHead, string.join(client_list,"\r\n")] message = string.join(email_parts,"\r\n") try: mailport.sendmail(gAdmin, [em], message) print "notified: %s, %s old clients" % (em,len(client_list)) except: print "trouble deliverying to: %s " % em mailport.quit() if __name__ == "__main__": main()