[r34]: / trunk / cforge / cforge / Package / CFORGE / Scripts / cds_script.py  Maximize  Restore  History

Download this file

78 lines (64 with data), 2.2 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# imports 
import sys
import os

#
# this is a general helper module providing some basic functions to 
# e.g. run CODESYS or another process with certain arguments
#
#

from System.Diagnostics import Process
from System.Net import WebClient

def RunProcess(workdir, process, args):
	exitcode = -1
	print("run process: " + process + " " + args)
	if not os.path.exists(workdir):
		print("Path does not exist: " + workdir)
	if not os.path.exists(os.path.join(workdir, process)):
		print("File does not exist: " + process)
	else:
		p = Process()
		p.StartInfo.WorkingDirectory=workdir
		p.StartInfo.FileName = process
		p.StartInfo.Arguments =  args
		p.Start()
		p.WaitForExit()
		exitcode = p.ExitCode
	print("done: " + str(exitcode))
	return exitcode


def RunCodesysWithScript(scriptfilename, scriptargs=None, ui=False):
	print(scriptfilename)
	# common paths
	workingdir = os.path.dirname(sys.argv[0])
	cdsdir = os.path.abspath(os.path.join(workingdir, os.path.pardir, os.path.pardir))
	#print(cdsdir)

	# profile stuff
	profiledir=os.path.join(cdsdir,"Profiles")
	#print(profiledir)
	lastprofile=""
	for file in os.listdir(profiledir):
		if file.endswith(".profile"):
			lastprofile = file.replace(".profile","")
	#print(lastprofile)

	# exe
	codesys_exe = os.path.join(cdsdir, "Common", "CODESYS.exe")
	packageman_exe = os.path.join(cdsdir, "Common", "PackageManager.exe")
	workdir = os.path.join(cdsdir, "Common")
	exitcode = -1
	if not os.path.exists(os.path.join(workingdir,scriptfilename)):
		print("File does not exist: " + scriptfilename)

	else:
		p = Process()
		p.StartInfo.WorkingDirectory=workdir
		p.StartInfo.FileName = codesys_exe
		
		processargs = "--profile='" + lastprofile + "'  --runscript='" + os.path.join(workingdir,scriptfilename) + "'" 
		if not ui:
			processargs += " --noUI"
		if scriptargs != None:
			processargs += " --scriptargs:'" + scriptargs + "'"
		print("running codesys with script " + scriptfilename)
		p.StartInfo.Arguments =  processargs
		p.Start()
		p.WaitForExit()
		
		exitcode = p.ExitCode 

	print("done: " + str(exitcode))
	return exitcode