#!/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/15/2010 #Modified Date: 12/30/2010 # #Summary: A change-commit trigger which submits the compiler generated files # to the "build" folder as well as "debug" folder # #Triggers: # # changeCommit change-commit //depot/js/build/... "c:\ # //depot/js/build/..." #******************************************************************************* from subprocess import Popen,PIPE,STDOUT from datetime import date import sys,os,re,time,logging #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='p4change.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') #set envionment #in my case, I am running in an Unicode mode #and the security level is set to 3 where #user smoon belongs to a group whose timeout #value is set to "unlimited" os.environ['P4USER'] = "smoon" os.environ['P4PORT'] = "20101" os.environ['P4CHARSET'] = "utf8" os.environ['P4CLIENT'] = "smoon_ws" os.environ['P4PASSWD'] = 'CC690F2731877023275B4598827421BF' class ChangeClass(object): def __init__(self): self.descList = [] self.filesList = [] def showChange(chngList,chClass): desc = re.compile(r'^(Description:)') files = re.compile(r'^(Files:)') fileM = re.compile(r'^\s+(\/\/\w+\/.+)(\/.+)\s+(#.*)$') inDesc = 0 inFiles = 0 getChanges = ['p4','change','-o',chngList] p = Popen(getChanges,shell=True,stdout=PIPE,stderr=PIPE) outPut = p.stdout.readline() while outPut: #print outPut, outPut = p.stdout.readline() descMatch = desc.match(outPut) if descMatch is not None: inDesc = 1 filesMatch = files.match(outPut) if filesMatch is not None: inDesc = 0 inFiles = 1 if inDesc == 1 and descMatch is None and outPut.strip() != "": chClass.descList.append(outPut) if inFiles == 1 and filesMatch is None and outPut.strip() != "": noutPut = fileM.match(outPut) if noutPut: chClass.filesList.append(noutPut.group(1) + noutPut.group(2)) chngErr = p.communicate()[1] p.stdout.close() p.stderr.close() if chngErr == "": print('"p4 change -s %s" was performed successfully' % chngList) p4debug.debug('"p4 change -s %s" was performed successfully' % chngList) else: print('An error encountered during "p4 change -o %s"' % chngList) p4debug.exception('An error encountered during "p4 change -o %s"' % chngList) p4debug.exception(chngErr) exit(1) return chClass def integrateFiles(srcPath,destPath): integrateFiles = ['p4','integ','-v',srcPath,destPath] integP = Popen(integrateFiles,shell=True,stdout=PIPE,stderr=PIPE) integOut,integErr = integP.communicate() integP.stdout.close() integP.stderr.close() if integErr == "": print('"p4 integ %s %s" was performed successfully' % (srcPath,destPath)) p4debug.debug('"p4 integ %s %s" was performed successfully' % (srcPath,destPath)) else: print('An error encountered during "p4 integ %s %s"' % (srcPath,destPath)) p4debug.exception('An error encountered during "p4 integ %s %s"' % (srcPath,destPath)) p4debug.exception(integErr) exit(1) def submitFiles(chClass): descMsg = "" for line in chClass.descList: descMsg += line.strip() #print "l: " + line submitFiles= ['p4','submit','-d',descMsg] submitP = Popen(submitFiles,shell=True,stdout=PIPE,stderr=PIPE) subOut,subErr = submitP.communicate() submitP.stdout.close() submitP.stderr.close() if subErr == "": print('"p4 submit -d %s" was performed successfully' % (descMsg)) p4debug.debug('"p4 submit -d %s" was performed successfully' % (descMsg)) exit(0) else: print('An error encountered during "p4 submit -d %s"' % (descMsg)) p4debug.exception('An error encountered during "p4 submit -d %s"' % (descMsg)) p4debug.exception(subErr) exit(1) def main(): chngList = sys.argv[1] srcPath = sys.argv[2] destPath = "//depot/js/debug/..." chClass = ChangeClass() chClass = showChange(chngList,chClass) integrateFiles(srcPath,destPath) submitFiles(chClass) if __name__ == '__main__': main()