Пример #1
0
def main(rawArguments):

	# Project/root directory can be set on a different ways.
	# On the first place from command line arguments.
	#
	# Search directory in array of arguments.
	# Examined cases:
	#	1. runapp ~/dir -opts -- -appOpts  => ['-opts', '--', '-appOpts']
	#   2. runapp -opts ~/dir -- -appOpts  => ['-opts', '--', '-appOpts'] 
	#   3. runapp -opts -- ~/dir -appOpts  => ['-opts', '--', '~/dir', '-appOpts']
	#   4. runapp -opts -- -appOpts ~/dir  => ['-opts', '--', '-appOpts', '~/dir']
	#   5. runapp -opts ~/dir              => ['-opts']
	#	6. runapp  ... ( without dir )
	#	7. runapp -opts ~/dir -appOpts     => ['-opts', '--', '-appOpts']
	#
	#   Try find first encountered non-option value and split them.
	#		Then will be wrong in case 7 (-appOpts will be joined to -opts )
	#		sol: Replace found value with end mark char(separator) only then if end mark will not found.
	#			bug: runapp ~/dir -opts => runapp -- -opts !!!
	#		And its index is greater then zero.
	#			e.q  runapp -opts ~/dir ...
	#
	# to case 1. Default parser recognize it as a the arguments for the external application (all to appArguments)
	#		conclusion is that non-option value will be treated as end mark
	#

	val = resolveNonOptionFromList(rawArguments)

	# If path has been found in command line arguments then initialize it.
	# If true then overwrite current system env var
	if val:
		initConfigurationFile(val[0])
	else:
		# If PROJECT_DIR is set in sys env then apply it.
		os.chdir(FSUtil.check_path(Config.getProjectDir()))

	appOptions, appArguments = parseArguments(rawArguments)

	# Read config should be before cmdl parsing
	# Allow us to overriding parameters defined in config file
	# Is useful for testing app, when i wants quick change variable

	if Config.isJar() == False:
		readConfig(Config.getConfigFName())

		# After loaded configuration, some environment can be changed
		# synchronize configuration with env.
		Config.update()

	initializeCMDLOptions(appOptions)

	appArguments = Keys.USER_ARGS_TO_APP.fromEnv()+' '+Utils.toString(appArguments)

	dependency=None
	jvmArgs = None

	if Config.isJar() == False:
		dependency = toCommandLineString(readConfig(Config.getDependencyFName(),"bash-path-parser",False))

		if PRINT_DEPENDENCIES_AND_EXIT.fromEnv() :
			print dependency.replace(":","\n")
			exitScript(0)

		jvmArgs = toCommandLineString(readConfig(Config.getJVMArgsFP(),auto_update=False),' ')

	if not Config.getJavaBinPath():
		LOG.error("Java not found, set JAVA_HOME in envirioment variables")
		exitScript()

	if Config.isJar() == False:

		if not Config.getMainClass():
			LOG.error("Main class not found. Please set 'MAINCLASS' variable.")
			exitScript(2)

	provider = Config.getProvider().lower()

	execPath = None
	execArgs = None

	if provider == "java" or Config.isJar() == True:

		# Fixed: When path contains white space. C:\\Program Files\... 
		execPath = "\"" + Config.getJavaBinPath() + "\""

		if Config.isJar() == False:
			execArgs = jvmArgs

			if len(dependency) > 1:
				execArgs += " -cp "+dependency

			execArgs += " "+Config.getMainClass()+" "+appArguments
		else:
			execArgs = "-jar "+Config.getConfigFName()+" "+appArguments

	elif provider == "maven":
		LOG.warn("Maven provider not implenebted yet !")
		exitScript(0)
	else:
		LOG.error("Unrecongnized provider %s !",provider)
		exitScript(2)

	globalVariables = env.getExportedVars()

	LOG.info("%s" % Timer.toString(START_TIME_MS,"Elapsed time of preparing of boot application"))
	
	if LOG.isDebug():
		LOG.debug("Exec tool: '%s'",Config.getExecTool())
		LOG.debug("Exec path: '%s'",execPath)
		LOG.debug("JVM Parameters: '%s'",jvmArgs)
		LOG.debug("Main class: '%s'",Config.getMainClass())
		LOG.debug("User args: '%s'",appArguments)

		if LOG.isTrace():
			LOG.trace("%s" % commands.getoutput(Config.getJavaBinPath() + " -version") )
			LOG.trace("ClassPath: %s" % dependency )

	if not Config.isTestingMode():

		workingTime = Timer.time()
		cmd = Utils.toString( (Config.getExecTool(),execPath,execArgs))

		process = subprocess.Popen(cmd, shell=True,env=globalVariables)

		if LOG.isDebug():
			LOG.debug("Created new process with PID:%d.",process.pid)

		signal.signal(signal.SIGINT,trapHandler)

		process.wait()

		LOG.info(Timer.toString(workingTime,"Application working time"))

		if LOG.isDebug():
			LOG.debug("The application has finished work ! [exitcode:%d]",process.returncode)