Source code for cmd_rc

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# 
# Copyright 2012-2017 Frédéric Magniette, Miguel Rubio-Roy
# This file is part of Pyrame.
# 
# Pyrame is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# Pyrame is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
# 
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrame.  If not, see <http://www.gnu.org/licenses/>

import sys,os,time,subprocess,signal,json,base64,threading,bindpyrame
rc_script=None

######################################################################
# RC SCRIPT functions
######################################################################

[docs]def set_script_param_rc(name,value): "Set value of script parameter *name* to *value*" if rc_script==None: return 0,"load a script first" retcode,res=submod_execcmd("set_param@rc_script",name,value) if retcode==0: return 0,"error setting script parameter <- %s"%(res) return 1,res
######################################################################
[docs]def get_script_params_rc(): "Get parameters of loaded script" if rc_script==None: return 0,"load a script first" retcode,res=submod_execcmd("get_params@rc_script") if retcode==0: return 0,"error getting script parameters <- %s"%(res) return 1,res
######################################################################
[docs]def get_scripts_rc(): "Get list of scripts in /opt/pyrame/rc_scripts" res=[] for root, dirs, files in os.walk("/opt/pyrame/rc_scripts",followlinks=True): for f in files: if f.endswith(".py"): res.append("%s/%s"%(root,f)) return 1,",".join(res)
###################################################################### def kill_rc_script(): global rc_script if rc_script!=None: print("killing pid %d"%(rc_script["process"].pid)) os.killpg(rc_script["process"].pid,signal.SIGKILL) os.waitpid(rc_script["process"].pid,0) rc_script["process"]=None def wait_until_awake(port): i=0 waittime=0.1 while i<30: if bindpyrame.open_socket("localhost",port)==-1: print("waiting %fs for localhost:%d to open ... "%(waittime,port)) time.sleep(waittime) else: return True i+=1 return False def launch_rc_script(): global rc_script rc_script["process"]=subprocess.Popen(["/usr/bin/cmdmod","/opt/pyrame/cmd_rc_script.xml"],preexec_fn=os.setpgrp) if not wait_until_awake(submod_getport("RC_SCRIPT")): return 0,"error launching RC_SCRIPT. unable to open socket" retcode,res=submod_execcmd("load@rc_script",rc_script["filename"]) if retcode==0: return 0,"error loading script <- %s"%(res) return 1,"ok" ######################################################################
[docs]def load_script_rc(filename): "Load run script *filename*" global rc_script kill_rc_script() rc_script={} rc_script["filename"]=filename retcode,res=launch_rc_script() if retcode==0: return 0,res return 1,filename
######################################################################
[docs]def get_loaded_script_rc(): "Get the path to the currently loaded script" if rc_script==None: #GUIs depend on this exact message return 1,"None" retcode,res=submod_execcmd("get_loaded@rc_script") if retcode==0: return 0,"error getting loaded script <- %s"%(res) return 1,res
######################################################################
[docs]def get_last_script_error_rc(): "Get last error catched from a running script" if rc_script==None: return 0,"load a script first" retcode,res=submod_execcmd("get_last_error@rc_script") if retcode==0: return 0,"error getting last error <- %s"%(res) return 1,res
######################################################################
[docs]def start_script_rc(): "Launch run() function of script. This function is asynchronous and returns as soon as the function is launched." if rc_script==None: return 0,"load a script first" retcode,res=submod_execcmd("start@rc_script") if retcode==0: return 0,"error starting script <- %s"%(res) return 1,res
###################################################################### def stop_script_t(bg_context,timeout): # sleep so that calling function has time to return KEEPOPEN time.sleep(0.5) retcode,res=submod_execcmd("stop@rc_script",timeout) if retcode==0: print("########## rc_script did not stop ##############") print("################# KILLING ######################") kill_rc_script() launch_rc_script() submod_sendres(bg_context,1,"ok")
[docs]def stop_script_rc(timeout="10"): "Signal running script to stop. If after *timeout* seconds the script continues running, terminate the process." if rc_script==None: return 0,"load a script first" retcode,res=submod_bgcontext() t=threading.Thread(target=stop_script_t,args=(res,timeout)) t.start() return KEEPOPEN,"ok"
######################################################################
[docs]def get_script_progress_rc(): "Get the progress, or degree of completion, of a script. Scripts are expected to implement a get_progress function returning a number from 0 to 100 describing the degree of completion of the run." if rc_script==None: return 0,"load a script first" retcode,res=submod_execcmd("get_progress@rc_script") if retcode==0: return 0,"error getting script progress <- %s"%(res) return 1,res