|
# #############################
# 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
# Use this to get Domain Name where Control Script is executing
CONTROL_SCRIPT_DOMAIN = socket.gethostname()
# Use this to get Control Script IP address
CONTROL_SCRIPT_IP = socket.gethostbyname(CONTROL_SCRIPT_DOMAIN)
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\db1.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\\new_dir\\rose-3.pzh", \
"c:\\Users\\Administrator\\pzh_gld\\rose-3.pzh")
if z is 0:
flog.write('\nFiles rose-3.pzh and pzh_gld\rose-3.pzh are the same.\n\n')
z = os.spawnl(os.P_WAIT, "c:\\Users\\Administrator\\diff.exe", "diff", "-b", \
"c:\\Users\\Administrator\\new_dir\\ppr-es.pzh", \
"c:\\Users\\Administrator\\pzh_gld\\ppr-es.pzh")
if z is 0:
flog.write('\nFiles ppr-es.pzh to pzh_gld\ppr-es.pzh are the same.\n\n')
z = os.spawnl(os.P_WAIT, "c:\\Users\\Administrator\\diff.exe", "diff", "-b", \
"c:\\Users\\Administrator\\new_dir\\r.pzh", \
"c:\\Users\\Administrator\\pzh_gld\\r.pzh")
if z is 0:
flog.write('\nr.pzh and pzh_gld\r.pzh are the same.\n\n')
z = os.spawnl(os.P_WAIT, "c:\\Users\\Administrator\\diff.exe", "diff", "-b", \
"c:\\Users\\Administrator\\new_dir\\timrom41.pzh", \
"c:\\Users\\Administrator\\pzh_gld\\timrom41.pzh")
if z is 0:
flog.write('\n timrom41.pzh and pzh_gld\timrom41.pzh are the same.\n\n')
z = os.spawnl(os.P_WAIT, "c:\\Users\\Administrator\\diff.exe", "diff", "-b", \
"c:\\Users\\Administrator\\new_dir\\mul-AB.pzh", \
"c:\\Users\\Administrator\\pzh_gld\\mul-AB.pzh")
if z is 0:
flog.write('\n mul-AB.pzh and pzh_gld\mul-AB.pzh are the same.\n\n')
z = os.spawnl(os.P_WAIT, "c:\\Users\\Administrator\\diff.exe", "diff", "-b", \
"c:\\Users\\Administrator\\new_dir\\mul-AD.pzh", \
"c:\\Users\\Administrator\\pzh_gld\\mul-AD.pzh")
if z is 0:
flog.write('\n mul-AD.pzh and pzh_gld\mul-AD.pzh are the same.\n\n')
# Test2.pzm
# ---------
# Example of the macro file test2.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\db2.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\\new_dir\\A_ARL16.pzl", \
"c:\\Users\\Administrator\\pzh_gld\\A_ARL16.pzl")
if z is 0:
flog.write('\n A_ARL16.pzl and pzh_gld\A_ARL16.pzl are the same.\n\n')
z = os.spawnl(os.P_WAIT, "c:\\Users\\Administrator\\diff.exe", "diff", "-b", \
"c:\\Users\\Administrator\\new_dir\\AB_ARL16.pzl", \
"c:\\Users\\Administrator\\pzh_gld\\AB_ARL16.pzl")
if z is 0:
flog.write('\n AB_ARL16.pzl and pzh_gld\AB_ARL16.pzl are the same.\n\n')
z = os.spawnl(os.P_WAIT, "c:\\Users\\Administrator\\diff.exe", "diff", "-b", \
"c:\\Users\\Administrator\\new_dir\\timrom41.pzl", \
"c:\\Users\\Administrator\\pzh_gld\\timrom41.pzl")
if z is 0:
flog.write('\n timrom41.pzl and pzh_gld\timrom41.pzl are the same.\n\n')
# -------------------------------------------------------------------------
# Test3.pzm
# ---------
# Example of the macro file test3.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\db3.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\\new_dir\\rose-3g.pzh", \
"c:\\Users\\Administrator\\pzh_gld\\rose-3g.pzh")
if z is 0:
flog.write('\n rose-3g.pzh and pzh_gld\rose-3g.pzh are the same.\n\n')
z = os.spawnl(os.P_WAIT, "c:\\Users\\Administrator\\diff.exe", "diff", "-b", \
"c:\\Users\\Administrator\\new_dir\\e-gld.pzh", \
"c:\\Users\\Administrator\\pzh_gld\\e-gld.pzh")
if z is 0:
flog.write('\n e-gld.pzh and pzh_gld\e-gld.pzh are the same.\n\n')
z = os.spawnl(os.P_WAIT, "c:\\Users\\Administrator\\diff.exe", "diff", "-b", \
"c:\\Users\\Administrator\\new_dir\\r-gld.pzh", \
"c:\\Users\\Administrator\\pzh_gld\\r-gld.pzh")
if z is 0:
flog.write('\n r-gld.pzh and pzh_gld\r-gld.pzh are the same.\n\n')
z = os.spawnl(os.P_WAIT, "c:\\Users\\Administrator\\diff.exe", "diff", "-b", \
"c:\\Users\\Administrator\\new_dir\\dh_1.pzh", \
"c:\\Users\\Administrator\\pzh_gld\\dh_1.pzh")
if z is 0:
flog.write('\n dh_1.pzh and pzh_gld\dh_1.pzh are the same.\n\n')
z = os.spawnl(os.P_WAIT, "c:\\Users\\Administrator\\diff.exe", "diff", "-b", \
"c:\\Users\\Administrator\\new_dir\\dh_1-10.pzh", \
"c:\\Users\\Administrator\\pzh_gld\\dh_1-10.pzh")
if z is 0:
flog.write('\n dh_1-10.pzh and pzh_gld\dh_1-10.pzh are the same.\n\n')
ScriptExit(z)
ScriptExit(z)
ScriptExit(0)
|
|
|
|