# #############################
# Example Python Control Script
# #############################
# ================
# Import Libraries
# ================
import socket
import sys
import os
from time import strftime, sleep
# ===========
# Define Data
# ===========
# --------------------------
# Message Control Characters
# --------------------------
ExecuteTaskMessage = '#'
TaskCompletedMessage = '@'
OKMessage = '?'
# -------------
# Program Names
# -------------
PROCESS_CONTROLLER = 'ProcessController'
PAC_N_ZOOM = 'Pac-n-Zoom'
# --------------------
# Program Socket Ports
# --------------------
PC_PORT = '5098' # Character Script-ProcCont Port Number
I_PC_PORT = 5098 # Integer Script-ProcCont Port Number
PZ_PORT = '5099' # Character ProcCont-Pac-N-Zoom Port Number
CONTROL_SCRIPT_DOMAIN = socket.gethostname() # Use this to
# get Domain
# Name where
# Control
# Script is
# executing
CONTROL_SCRIPT_IP = socket.gethostbyname(CONTROL_SCRIPT_DOMAIN) # Use this to
# get Control
# Script IP
# address
pid = os.getpid()
PythonHeader = ': Python Script PID=%(pid)04d: ' % vars()
FileName = 'py__%(pid)04d.log' % vars()
flog = open(FileName, 'w')
# =========
# Functions
# =========
# -----------------------
def ScriptExit(ExitCode):
# -----------------------
# -----------------------------------------------------------------
LogHeader = strftime('%X') + PythonHeader
flog.write('%(LogHeader)sPython Shutting Down\n' % vars())
flog.flush()
# -----------------------------------------------------------------
s.close()
flog.close()
sys.exit(ExitCode)
# -----------------------------------------
def PCSend(ProcessName, SendData, DoPrint):
# -----------------------------------------
while 1:
try:
# -------------------------------------------------------------------------------
if DoPrint is 1:
LogHeader = strftime('%X') + PythonHeader
flog.write('%(LogHeader)sSending \"%(SendData)s\" to %(ProcessName)s\n' % vars())
# -------------------------------------------------------------------------------
s.send(SendData)
except socket.error, msg:
# ---------------------------------------------------------------------------------------
LogHeader = strftime('%X') + PythonHeader
flog.write('%(LogHeader)sProcess Controller Socket send() Error - Shutting Down\n' % vars())
# ---------------------------------------------------------------------------------------
ScriptExit(1)
return
# ---------------------------------------
def PCSendMessage(ProcessName, SendData):
# ---------------------------------------
PCSend(ProcessName, SendData, 1)
RecvOKMessage(ProcessName)
# ---------------------------------------
def PCSendMacro(MacroName):
# ---------------------------------------
fmac = open(MacroName, 'r')
while 1:
string = fmac.readline()
if len(string) is 0:
fmac.close()
return
PCSendMessage(PAC_N_ZOOM, string)
# ---------------------------
def ExecuteTask(ProcessName):
# ---------------------------
# ------------------------------------------------------------------------------
LogHeader = strftime('%X') + PythonHeader
flog.write('%(LogHeader)sSending ExecuteTaskMessage to %(ProcessName)s\n' % vars())
# ------------------------------------------------------------------------------
PCSend(ProcessName, ExecuteTaskMessage, 0)
# ----------------------------------------------------------------------------------------
LogHeader = strftime('%X') + PythonHeader
flog.write('%(LogHeader)sListening for TaskCompletedMessage from %(ProcessName)s\n' % vars())
# ----------------------------------------------------------------------------------------
RecvData = ' '
while 1:
try:
RecvData = s.recv(1024)
if RecvData[0] is not TaskCompletedMessage[0]:
# -------------------------------------------------------------------------------
LogHeader = strftime('%X') + PythonHeader
flog.write('%(LogHeader)sReceived \"%(RecvData)s\" from %(ProcessName)s\n' % vars())
flog.write('%(LogHeader)sExpected TaskCompletedMessage - Shutting Down\n' % vars())
# -------------------------------------------------------------------------------
ScriptExit(1)
# -----------------------------------------------------------------------------------
LogHeader = strftime('%X') + PythonHeader
flog.write('%(LogHeader)sReceived TaskCompletedMessage from %(ProcessName)s\n' % vars())
# -----------------------------------------------------------------------------------
except socket.error, msg:
# ---------------------------------------------------------------------------------------
LogHeader = strftime('%X') + PythonHeader
flog.write('%(LogHeader)sProcess Controller Socket recv() Error - Shutting Down\n' % vars())
# ---------------------------------------------------------------------------------------
ScriptExit(1)
return
# -----------------------------
def RecvOKMessage(ProcessName):
# -----------------------------
# -----------------------------------------------------------------------------
LogHeader = strftime('%X') + PythonHeader
flog.write('%(LogHeader)sListening for OKMessage from %(ProcessName)s\n' % vars())
# -----------------------------------------------------------------------------
RecvData = ' '
while 1:
try:
RecvData = s.recv(1024)
if RecvData[0] is not OKMessage[0]:
# -------------------------------------------------------------------------------
LogHeader = strftime('%X') + PythonHeader
flog.write('%(LogHeader)sReceived \"%(RecvData)s\" from %(ProcessName)s\n' % vars())
flog.write('%(LogHeader)sExpected OKMessage - Shutting Down\n' % vars())
# -------------------------------------------------------------------------------
ScriptExit(1)
# ------------------------------------------------------------------------
LogHeader = strftime('%X') + PythonHeader
flog.write('%(LogHeader)sReceived OKMessage from %(ProcessName)s\n' % vars())
# ------------------------------------------------------------------------
except socket.error, msg:
# ---------------------------------------------------------------------------------------
LogHeader = strftime('%X') + PythonHeader
flog.write('%(LogHeader)sProcess Controller Socket recv() Error - Shutting Down\n' % vars())
# ---------------------------------------------------------------------------------------
ScriptExit(1)
return
# ----------------------
def SendRecvOKMessage():
# ----------------------
RecvData = ' '
while 1:
try:
# ------------------------------------------------------------------------
LogHeader = strftime('%X') + PythonHeader
flog.write('%(LogHeader)sSending OKMessage to Process Controller\n' % vars())
# ------------------------------------------------------------------------
s.send(OKMessage)
# --------------------------------------------------------------------------------
LogHeader = strftime('%X') + PythonHeader
flog.write('%(LogHeader)sListening for OKMessage from Process Controller\n' % vars())
# --------------------------------------------------------------------------------
RecvData = s.recv(1024)
if RecvData[0] is not OKMessage[0]:
# ----------------------------------------------------------------------------------
LogHeader = strftime('%X') + PythonHeader
flog.write('%(LogHeader)sReceived \"%(RecvData)s\" from Process Controller\n' % vars())
flog.write('%(LogHeader)sExpected OKMessage - Shutting Down\n' % vars())
# ----------------------------------------------------------------------------------
ScriptExit(1)
break
except socket.error, msg:
# --------------------------------------------------------------------------------------------------------
LogHeader = strftime('%X') + PythonHeader
flog.write('%(LogHeader)sSleeping to give the Process Controller time to Open Socket and Respond\n' % vars())
# --------------------------------------------------------------------------------------------------------
sleep(1)
# ---------------------------------------------------------------------------
LogHeader = strftime('%X') + PythonHeader
flog.write('%(LogHeader)sReceived OKMessage from Process Controller\n' % vars())
# ---------------------------------------------------------------------------
# ================
# End of Functions
# ================
# ==================
# Python Main Script
# ==================
# --------------------------------------------------------------------------------------------------
LogHeader = strftime('%X') + PythonHeader
flog.write('%(LogHeader)sCreating Socket to communicate with the %(PROCESS_CONTROLLER)s\n' % vars())
# --------------------------------------------------------------------------------------------------
s = None
while 1:
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error, msg:
s = None
continue
try:
s.connect((CONTROL_SCRIPT_IP, I_PC_PORT))
except socket.error, msg:
s = None
continue
break
if s is None:
# ----------------------------------------------------------------------------
LogHeader = strftime('%X') + PythonHeader
flog.write('%(LogHeader)sPython Socket Open Error - Shutting Down\n' % vars())
# ----------------------------------------------------------------------------
sys.exit(1)
# ---------------------------------------------
LogHeader = strftime('%X') + PythonHeader
flog.write('%(LogHeader)sConnected\n' % vars())
# ---------------------------------------------
#-----------------------------------------------------------
# Send and then Receive OKMessage to/from Process Controller
#-----------------------------------------------------------
SendRecvOKMessage()
# ---------------------------------------------------------------------------------------------------
LogHeader = strftime('%X') + PythonHeader
flog.write('%(LogHeader)s%(PROCESS_CONTROLLER)s has opened the socket for communications\n' % vars())
# ---------------------------------------------------------------------------------------------------
# -------------------------------------------------------------------------
LogHeader = strftime('%X') + PythonHeader
flog.write('%(LogHeader)sPython can now talk to %(PAC_N_ZOOM)s\n' % vars())
# -------------------------------------------------------------------------
# ====================================================
# All processes are working, all sockets are open, and
# every process is listening on the proper socket.
# ====================================================
# -------------------------------------------------------------------------
LogHeader = strftime('%X') + PythonHeader
flog.write('%(LogHeader)sSending %(PAC_N_ZOOM)s Macro Messages\n' % vars())
# -------------------------------------------------------------------------
# ------------------------------------------------
os.system("dir") # example of a built-in dos call
# ------------------------------------------------
# -------------------------------------------------------------------------
# Test1.pzm
# ---------
# Example of the macro file test1.pzm passed through the Process Controller
# and then the dos executable diff.exe is called to compare the results.
# -------------------------------------------------------------------------
PCSendMacro("c:\progra~1\colorcom\Pac-n-Zoom\dcc1.pzm")
# ------------------------------------
# Tell Pac-n-Zoom to execute the Macro
# ------------------------------------
ExecuteTask(PAC_N_ZOOM) # --> Pac-N-Zoom ExecuteTaskMessage
# <-- Pac-N-Zoom TaskCompletedMessage
z = os.spawnl(os.P_WAIT, "c:\\Users\\Administrator\\diff.exe", "diff", "-b", \
"c:\\Users\\Administrator\\cdl_tst\\rose-3.CDL", \
"c:\\Users\\Administrator\\cdl_gld\\rose-3.CDL")
if z is 0:
flog.write('\nFiles rose-3.CDL and pzh_gld\rose-3.CDL are the same.\n\n')
else:
exit(z)
z = os.spawnl(os.P_WAIT, "c:\\Users\\Administrator\\diff.exe", "diff", "-b", \
"c:\\Users\\Administrator\\cdl_tst\\ppr-es.CDL", \
"c:\\Users\\Administrator\\cdl_gld\\ppr-es.CDL")
if z is 0:
flog.write('\nFiles ppr-es.CDL to pzh_gld\ppr-es.CDL are the same.\n\n')
else:
exit(z)
z = os.spawnl(os.P_WAIT, "c:\\Users\\Administrator\\diff.exe", "diff", "-b", \
"c:\\Users\\Administrator\\cdl_tst\\r.CDL", \
"c:\\Users\\Administrator\\cdl_gld\\r.CDL")
if z is 0:
flog.write('\n r.CDL and pzh_gld\r.CDL are the same.\n\n')
else:
exit(z)
z = os.spawnl(os.P_WAIT, "c:\\Users\\Administrator\\diff.exe", "diff", "-b", \
"c:\\Users\\Administrator\\cdl_tst\\timrom41.CDL", \
"c:\\Users\\Administrator\\cdl_gld\\timrom41.CDL")
if z is 0:
flog.write('\n timrom.CDL and pzh_gld\timrom.CDL are the same.\n\n')
z = os.spawnl(os.P_WAIT, "c:\\Users\\Administrator\\diff.exe", "diff", "-b", \
"c:\\Users\\Administrator\\cdl_tst\\mul-AB-A.CDL", \
"c:\\Users\\Administrator\\cdl_gld\\mul-AB-A.CDL")
if z is 0:
flog.write('\n mul-AB-A.CDL and pzh_gld\mul-AB-A.CDL are the same.\n\n')
else:
ScriptExit(z)
z = os.spawnl(os.P_WAIT, "c:\\Users\\Administrator\\diff.exe", "diff", "-b", \
"c:\\Users\\Administrator\\cdl_tst\\mul-AB-B.CDL", \
"c:\\Users\\Administrator\\cdl_gld\\mul-AB-B.CDL")
if z is 0:
flog.write('\n mul-AB-B.CDL and pzh_gld\mul-AB-B.CDL are the same.\n\n')
else:
exit(z)
z = os.spawnl(os.P_WAIT, "c:\\Users\\Administrator\\diff.exe", "diff", "-b", \
"c:\\Users\\Administrator\\cdl_tst\\mul-AD-A.CDL", \
"c:\\Users\\Administrator\\cdl_gld\\mul-AD-A.CDL")
if z is 0:
flog.write('\n mul-AD-A.CDL and pzh_gld\mul-AD-A.CDL are the same.\n\n')
else:
exit(z)
z = os.spawnl(os.P_WAIT, "c:\\Users\\Administrator\\diff.exe", "diff", "-b", \
"c:\\Users\\Administrator\\cdl_tst\\mul-AD-B.CDL", \
"c:\\Users\\Administrator\\cdl_gld\\mul-AD-B.CDL")
if z is 0:
flog.write('\n mul-AD-B.CDL and pzh_gld\mul-AD-B.CDL are the same.\n\n')
else:
exit(z)
z = os.spawnl(os.P_WAIT, "c:\\Users\\Administrator\\diff.exe", "diff", "-b", \
"c:\\Users\\Administrator\\cdl_tst\\mul-AD-C.CDL", \
"c:\\Users\\Administrator\\cdl_gld\\mul-AD-C.CDL")
if z is 0:
flog.write('\n mul-AD-C.CDL and pzh_gld\mul-AD-C.CDLare the same.\n\n')
else:
exit(z)
z = os.spawnl(os.P_WAIT, "c:\\Users\\Administrator\\diff.exe", "diff", "-b", \
"c:\\Users\\Administrator\\cdl_tst\\mul-AD-D.CDL", \
"c:\\Users\\Administrator\\cdl_gld\\mul-AD-D.CDL")
if z is 0:
flog.write('\n mul-AD-D.CDL and pzh_gld\mul-AD-D.CDL are the same.\n\n')
else:
ScriptExit(z)
PCSendMacro("c:\progra~1\colorcom\Pac-n-Zoom\dcc2.pzm")
# ------------------------------------
# Tell Pac-n-Zoom to execute the Macro
# ------------------------------------
ExecuteTask(PAC_N_ZOOM) # --> Pac-N-Zoom ExecuteTaskMessage
# <-- Pac-N-Zoom TaskCompletedMessage
z = os.spawnl(os.P_WAIT, "c:\\Users\\Administrator\\diff.exe", "diff", "-b", \
"c:\\Users\\Administrator\\cdl_tst\\rose-3g.CDL", \
"c:\\Users\\Administrator\\cdl_gld\\rose-3g.CDL")
if z is 0:
flog.write('\nFiles rose-3g.CDL and cdl_gld\rose-3g.CDL are the same.\n\n')
else:
exit(z)
z = os.spawnl(os.P_WAIT, "c:\\Users\\Administrator\\diff.exe", "diff", "-b", \
"c:\\Users\\Administrator\\cdl_tst\\e-gld.CDL", \
"c:\\Users\\Administrator\\cdl_gld\\e-gld.CDL")
if z is 0:
flog.write('\nFiles e-gld.CDL to cdl_gld\e-gld.CDL are the same.\n\n')
else:
exit(z)
z = os.spawnl(os.P_WAIT, "c:\\Users\\Administrator\\diff.exe", "diff", "-b", \
"c:\\Users\\Administrator\\cdl_tst\\r-gld.CDL", \
"c:\\Users\\Administrator\\r-gld.CDL")
if z is 0:
flog.write('\n r-gld.CDL and cdl_gld\r-gld.CDL are the same.\n\n')
else:
exit(z)
z = os.spawnl(os.P_WAIT, "c:\\Users\\Administrator\\diff.exe", "diff", "-b", \
"c:\\Users\\Administrator\\cdl_tst\\dh_1.CDL", \
"c:\\Users\\Administrator\\cdl_gld\\dh_1.CDL")
if z is 0:
flog.write('\n dh_1.CDL and cdl_gld\dh_1.CDL are the same.\n\n')
z = os.spawnl(os.P_WAIT, "c:\\Users\\Administrator\\diff.exe", "diff", "-b", \
"c:\\Users\\Administrator\\cdl_tst\\dh_1-1.CDL", \
"c:\\Users\\Administrator\\cdl_gld\\dh_1-1.CDL")
if z is 0:
flog.write('\n dh_1-1.CDL and cdl_gld\dh_1-1.CDL are the same.\n\n')
else:
ScriptExit(z)
z = os.spawnl(os.P_WAIT, "c:\\Users\\Administrator\\diff.exe", "diff", "-b", \
"c:\\Users\\Administrator\\cdl_tst\\dh_1-2.CDL", \
"c:\\Users\\Administrator\\cdl_gld\\dh_1-2.CDL")
if z is 0:
flog.write('\n dh_1-2.CDL and cdl_gld\dh_1-2.CDL are the same.\n\n')
else:
exit(z)
z = os.spawnl(os.P_WAIT, "c:\\Users\\Administrator\\diff.exe", "diff", "-b", \
"c:\\Users\\Administrator\\cdl_tst\\dh_1-3.CDL", \
"c:\\Users\\Administrator\\cdl_gld\\dh_1-3.CDL")
if z is 0:
flog.write('\n dh_1-3.CDL and cdl_gld\dh_1-3.CDL are the same.\n\n')
else:
exit(z)
z = os.spawnl(os.P_WAIT, "c:\\Users\\Administrator\\diff.exe", "diff", "-b", \
"c:\\Users\\Administrator\\cdl_tst\\dh_1-4.CDL", \
"c:\\Users\\Administrator\\cdl_gld\\dh_1-4.CDL")
if z is 0:
flog.write('\n dh_1-4.CDL and cdl_gld\dh_1-4.CDL are the same.\n\n')
else:
exit(z)
z = os.spawnl(os.P_WAIT, "c:\\Users\\Administrator\\diff.exe", "diff", "-b", \
"c:\\Users\\Administrator\\cdl_tst\\dh_1-5.CDL", \
"c:\\Users\\Administrator\\cdl_gld\\dh_1-5.CDL")
if z is 0:
flog.write('\n dh_1-5.CDL and cdl_gld\dh_1-5.CDL are the same.\n\n')
else:
exit(z)
z = os.spawnl(os.P_WAIT, "c:\\Users\\Administrator\\diff.exe", "diff", "-b", \
"c:\\Users\\Administrator\\cdl_tst\\dh_1-6.CDL", \
"c:\\Users\\Administrator\\cdl_gld\\dh_1-6.CDL")
if z is 0:
flog.write('\n mul-AD-D.CDL and cdl_gld\mul-AD-D.CDL are the same.\n\n')
else:
exit(z)
z = os.spawnl(os.P_WAIT, "c:\\Users\\Administrator\\diff.exe", "diff", "-b", \
"c:\\Users\\Administrator\\cdl_tst\\dh_1-7.CDL", \
"c:\\Users\\Administrator\\cdl_gld\\dh_1-7.CDL")
if z is 0:
flog.write('\n dh_1-7.CDL and cdl_gld\dh_1-7.CDL are the same.\n\n')
else:
exit(z)
z = os.spawnl(os.P_WAIT, "c:\\Users\\Administrator\\diff.exe", "diff", "-b", \
"c:\\Users\\Administrator\\cdl_tst\\dh_1-8.CDL", \
"c:\\Users\\Administrator\\cdl_gld\\dh_1-8.CDL")
if z is 0:
flog.write('\n dh_1-8.CDL and cdl_gld\dh_1-8.CDL are the same.\n\n')
else:
exit(z)
z = os.spawnl(os.P_WAIT, "c:\\Users\\Administrator\\diff.exe", "diff", "-b", \
"c:\\Users\\Administrator\\cdl_tst\\dh_1-9.CDL", \
"c:\\Users\\Administrator\\cdl_gld\\dh_1-9.CDL")
if z is 0:
flog.write('\n dh_1-9.CDL and cdl_gld\dh_1-9.CDL are the same.\n\n')
else:
exit(z)
z = os.spawnl(os.P_WAIT, "c:\\Users\\Administrator\\diff.exe", "diff", "-b", \
"c:\\Users\\Administrator\\dh_1-10.CDL", \
"c:\\Users\\Administrator\\cdl_gld\\dh_1-10.CDL")
if z is 0:
flog.write('\n dh_1-10.CDL and cdl_gld\dh_1-10.CDL are the same.\n\n')
else:
exit(z)
ScriptExit(z)
ScriptExit(0)
|
|