#!/usr/bin/env python # #******************************************************************************* # #Copyright (c) 2009, Perforce Software, Inc. All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL PERFORCE SOFTWARE, INC. BE LIABLE FOR ANY # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #******************************************************************************* # #Author: Stephen Moon #Date: 7/17/2010 #Summary: Delete old clients # #******************************************************************************* from subprocess import Popen,PIPE,STDOUT from datetime import date import sys,os,re,time,logging,smtplib #Enable logging of the backup script logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s', datefmt='%m-%d %H:%M', filename='p4client.log', filemode='w') # define a Handler which writes INFO messages or higher to the sys.stderr console = logging.StreamHandler() console.setLevel(logging.INFO) # set a format which is simpler for console use formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s') # tell the handler to use this format console.setFormatter(formatter) # add the handler to the root logger logging.getLogger('').addHandler(console) #define all the environmental variables p4debug = logging.getLogger('p4debug') p4error = logging.getLogger('p4error') p4 = "c:\program files\perforce\p4.exe" mailhost = 'smtp.perforce.com' server = 'localhost' port = '20101' P4USER = 'smoon' P4CHARSET = 'utf8' #set it to empty string if your server is not Unicode P4PORT = server + ':' + port os.environ['P4PORT'] = P4PORT os.environ['P4USER'] = P4USER os.environ['P4CHARSET'] = P4CHARSET class Client(object): def __init__(self): self.name = "" # update = self.update self.access = "" self.owner = "" # host = self.host # root = self.root # submitOption = self.submitOption # lineEnd = self.lineEnd def getName(self): return self.name class ClientDict(dict): def addClient(self,obj): self[obj.getName()] = obj def parseClientSpec(client,clientObj): chkName = re.compile(r'^Client:\s(\S+)\s.*$') chkAccess = re.compile(r'^Access:\s(\S+)\s(\S+).*$') chkOwner = re.compile(r'^Owner:\s(\S+)\s.*$') getClientSpec = [p4,'client','-o',client.group(1)] p = Popen(getClientSpec,shell=True,stdout=PIPE,stderr=PIPE) outClient = p.stdout.readline() #print outClient while outClient: #print outClient nameMatch = chkName.match(outClient) accessMatch = chkAccess.match(outClient) ownerMatch = chkOwner.match(outClient) if nameMatch is not None: #print nameMatch.group(1) clientObj.name = nameMatch.group(1) if accessMatch is not None: #print accessMatch.group(1) clientObj.access = accessMatch.group(1) if ownerMatch is not None: #print ownerMatch.group(1) clientObj.owner = ownerMatch.group(1) outClient = p.stdout.readline() outErr = p.communicate()[1] p.stdout.close p.stderr.close if outErr == "": print('"p4 client -o %s" was performed successfully\n' % client.group(1)) else: print('An error encountered during "p4 client -o %s"' % client.group(1)) return clientObj def getClientInfo(chkClient,clientList): clients = [chkClient.match(x) for x in clientList] clientList = ClientDict() for eachClient in clients: clientObj = Client() clientObj = parseClientSpec(eachClient,clientObj) clientList.addClient(clientObj) return clientList def getClients(): chkClient = re.compile(r'^Client\s(\S+)\s.*$') getClients = [p4,'clients'] p = Popen(getClients,shell=True,stdout=PIPE,stderr=PIPE) clients = getClientInfo(chkClient,p.stdout.readlines()) clientErr = p.communicate()[1] p.stdout.close() p.stderr.close() if clientErr == "": for k,v in clients.items(): print "%s, %s, %s" % (k,v.access,v.owner) else: sys.stderr.write(clientErr) p4error.exception('Unable to list clients: \n%s' % clientErr) #emailAdmin(1) exit(1) def main(): getClients() if __name__ == '__main__': main()