#!/usr/bin/python
#------------------------------------------------------------------------------
# Copyright (c) Perforce Software, Inc., 2011-2015. 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.
#------------------------------------------------------------------------------
#
import sys
import os
import marshal
import subprocess
from subprocess import Popen, PIPE
# EDIT THIS LINE TO CONFIGURE THE DEPOT LOCATION OF THE AUTOMERGE DIRECTORY
baseDepotPath = "//devtools/p4/dev/automerge"
backgroundUser = "automerge"
passwordFileName = "/p4/common/bin/backgroundpass"
# Store baseDepotPath as an environment variable for this session
os.environ["automerge.baseDepotPath"] = baseDepotPath
scriptPath = "{0}/automerge_impl.py".format(baseDepotPath)
# containsError
# utility function to check for any error code in the results array
def containsError(results=[]):
foundError = False
for r in results:
if 'code' in r:
if r['code'] == 'error':
foundError = True
return foundError
# login
# log into the server as the background user
def login():
loginCommand = "p4 -u {0} -p {1} login < {2}".format(backgroundUser,os.environ.get("P4PORT"),passwordFileName)
returnCode = subprocess.call(loginCommand, stdout=open('/dev/null', 'w'), stderr=subprocess.STDOUT, shell=True)
# check that the user is logged in, if not raise an exception
cmd = ["login","-s"]
result = p4MarshalCmd(cmd)
if containsError(result):
print "ERROR: User {0} is not logged in".format(backgroundUser)
sys.exit(1)
# p4MarshalCmd
# executes the p4 command, results sent to a list
def p4MarshalCmd(cmd):
list = []
pipe = Popen(["p4", "-G"] + cmd, stdout=PIPE).stdout
try:
while 1:
record = marshal.load(pipe)
list.append(record)
except EOFError:
pass
pipe.close()
return list
###########################################################################
##### MAIN PROGRAM STARTS HERE
#####
if os.environ.get("P4USER") == backgroundUser:
login()
output = p4MarshalCmd(["where",scriptPath])
if output[0]['code'] == 'error':
print "Error: You must have " + scriptPath + " mapped in your current workspace.\n"
print output
sys.exit(1)
automerge_impl = output[0]['path']
output = p4MarshalCmd(["sync","-f",scriptPath])
if output[0]['code'] == 'error':
if not "file(s) up-to-date" in output[0]['data']:
print "Error: Sync error for " + scriptPath + "\n"
print output
sys.exit(1)
import automerge_impl
automerge_impl.main()