import os
import sys
import subprocess
import stat
import __main__

__all__ = 'VmmError', 'main', 'find_program', 'run_program'

class VmmError(Exception):
	def __init__(self, message):
		self.message = message
	def __str__(self):
		return self.message

def _init():
	global VMNAME

	VMNAME = "UnknownVMM"
	for var in 'NAME', 'CFGDIR', 'UUID', 'DISK', 'SECTORS', 'MEM', \
				'FULLSCREEN', 'SUSPENDED', 'COMMAND':
		if os.environ.has_key(var):
			exec('global ' + var + ';' + var + ' = "' + \
						os.environ[var] + '"')

def main():
	if len(sys.argv) <= 1:
		print >>sys.stderr, "No mode specified"
		sys.exit(1)
	elif sys.argv[1] == "info":
		try:
			__main__.info()
		except Exception, inst:
			print "VMM=%s" % VMNAME
			print "RUNNABLE=no"
			print "RUNNABLE_REASON=%s: %s" % \
						(inst.__class__.__name__, inst)
		else:
			print "VMM=%s" % VMNAME
			print "RUNNABLE=yes"
	elif sys.argv[1] == "run":
		try:
			__main__.run()
		except Exception, inst:
			print "SUSPENDED=%s" % SUSPENDED
			print "SUCCESS=no"
			print "ERROR=%s: %s" % (inst.__class__.__name__, inst)
		else:
			print "SUSPENDED=%s" % SUSPENDED
			print "SUCCESS=yes"
	else:
		print >>sys.stderr, "Unknown mode specified"
		sys.exit(1)
	sys.exit(0)

def _executable(path):
	try:
		st = os.stat(path)
	except OSError:
		return False
	return st[stat.ST_MODE] & stat.S_IXUSR|stat.S_IXGRP|stat.S_IXOTH > 0

# If prog is an absolute path and executable, return it.  If it is a relative
# path and executable via PATH, return the absolute path to the executable.
# If no executable is found, return false.
def find_program(prog):
	if prog[0] == '/':
		if _executable(prog):
			return prog
		else:
			return False
	for dirname in os.environ['PATH'].split(':'):
		path = dirname + '/' + prog
		if _executable(path):
			return path
	return False

# Run a process and wait for it to complete, redirecting its stdout to stderr
# so that the child can't write key-value pairs back to our calling process
def run_program(*args):
	return subprocess.call(args, stdout = sys.stderr)

_init()

@@ -44,16 +44,33 @@
 rt stat%0A
+import traceback%0A
 import _
@@ -77,16 +77,16 @@
 _main__%0A
-
 %0A__all__
@@ -139,16 +139,53 @@
 ogram'%0A%0A
+VMNAME = %22UnknownVMM%22%0ADEBUG = False%0A%0A
 class Vm
@@ -318,47 +318,8 @@
 ():%0A
-%09global VMNAME%0A%0A%09VMNAME = %22UnknownVMM%22%0A
 %09for
@@ -696,32 +696,71 @@
 xception, inst:%0A
+%09%09%09if DEBUG:%0A%09%09%09%09traceback.print_exc()%0A
 %09%09%09print %22VMM=%25s
@@ -981,16 +981,16 @@
 _.run()%0A
-
 %09%09except
@@ -1007,16 +1007,55 @@
 , inst:%0A
+%09%09%09if DEBUG:%0A%09%09%09%09traceback.print_exc()%0A
 %09%09%09print

Add debug mechanism for printing Python exception tracebacks