# Module osce.py
# This file is read and integrated by os.py
# Call these funcs from os
#
# Telion

# CE patch for execv etc..

#import win32process
#import string
#import win32event

def execv (path, args):
	import os
	if not type(args) in (tuple, list):
		raise TypeError, "execv() arg 2 must be a tuple or list"
	p=os.path.abspath(path)
	#if os.path.exists(p):
	import win32process
	import string
	hPro,hThr,dwPro,dwThr=win32process.CreateProcess(p,string.join(args,' '),None,None,0,0,None,None,None)
	#del string
	#del win32process
	#del os

def execve (path, args, env):
	# Currently, env is simply ignored. Sorry if you were trying to use that.
	import os
	if not type(args) in (tuple, list):
		raise TypeError, "execve() arg 2 must be a tuple or list"
	p=os.path.abspath(path)
	import win32process
	import string
	hPro,hThr,dwPro,dwThr=win32process.CreateProcess(p,string.join(args,' '),None,None,0,0,env,None,None)
	#del string
	#del win32process
	#del os


def system(cmd):
	# This function will not start program in the directory with space(s)
	# It is the same for other Windows version of Python.
	# I made os.sytema(cmd, arg) for your convenience.
	# Note: the retrun value maybe unliable...

	import win32process
	import string
	import os
	import win32event
	#import re
	a = string.find(cmd, ' ')
	arg = ''
	if a >0:
		p=cmd[:a]
		arg = cmd[a+1:]
	else:
		p=cmd
	p=os.path.abspath(p)
	hPro,hThr,dwPro,dwThr=win32process.CreateProcess(p,arg,None,None,0,0,None,None,None)
	if hPro and hThr:
		win32event.WaitForSingleObject(hPro,win32event.INFINITE)
		r = win32process.GetExitCodeProcess(hPro) # This returns always 0?
		#r2 = win32process.GetExitCodeThread(hThr)
	#del re
	#del string
	#del win32process
	#del win32event
	#del os
	return r

def systema(cmd,arg):
	# This function is for CE only...
	#
	import win32process
	import string
	import os
	import win32event
	#import re
	p=os.path.abspath(cmd)
	hPro,hThr,dwPro,dwThr=win32process.CreateProcess(p,arg,None,None,0,0,None,None,None)
	if hPro and hThr:
		win32event.WaitForSingleObject(hPro,win32event.INFINITE)
		r = win32process.GetExitCodeProcess(hPro) 
	#del re
	#del string
	#del win32process
	#del win32event
	#del os
	return r



def syscmd(icmd, wait=0):
	# call up a command through CMD.EXE and let it stay open after executeion
	# Python does not wait for the command uness "wait" is True.
	import win32process
	import win32event
	hPro,hThr,dwPro,dwThr=win32process.CreateProcess('\\windows\\cmd','/k '+icmd,None,None,0,0,None,None,None)
	if wait and hPro and hThr:
		win32event.WaitForSingleObject(hPro,win32event.INFINITE)
	#del win32event
	#del win32process


def syscmdc(icmd, wait=0):
	# call up a command through CMD.EXE and close it after return
	# Python does not wait for the command uness "wait" is True.
	import win32process
	import win32event
	hPro,hThr,dwPro,dwThr=win32process.CreateProcess('\\windows\\cmd','/c '+icmd,None,None,0,0,None,None,None)
	if wait and hPro and hThr:
		win32event.WaitForSingleObject(hPro,win32event.INFINITE)
	#del win32event
	#del win32process

