Ejemplo n.º 1
0
def run_command(args):
	# Check if the stack command has been quoted.

	module = None
        if not args:
                return
        
	cmd = args[0].split()
	if len(cmd) > 1:
		s = 'stack.commands.%s' % string.join(cmd, '.')
		try:
			__import__(s)
			module = eval(s)
			i = 1
		except:
			module = None

	# Treat the entire command line as if it were a python
	# command module and keep popping arguments off the end
	# until we get a match.  If no match is found issue an
	# error

	if not module:
		for i in range(len(args), 0, -1):
			s = 'stack.commands.%s' % string.join(args[:i], '.')
			try:
				__import__(s)
				module = eval(s)
				if module:
					break
			except ImportError:
				continue

	if not module:
		sys.stderr.write('Error - Invalid stack command "%s"\n' % args[0])
		return -1

	name = string.join(string.split(s, '.')[2:], ' ')

        import stack.exception

	# If we can load the command object then fall through and invoke the run()
	# method.  Otherwise the user did not give a complete command line and
	# we call the help command based on the partial command given.

	if not hasattr(module,'Command'):
		import stack.commands.list.help
		help = stack.commands.list.help.Command(Database)
		fullmodpath = s.split('.')
		submodpath  = string.join(fullmodpath[2:], '/')
                try:
                        help.run({'subdir': submodpath}, [])
                except stack.exception.CommandError, e:
                        sys.stderr.write('%s\n' % e)
                        return -1
		print help.getText()
		return -1
Ejemplo n.º 2
0
def run_command(args, debug=False):
	# Check if the stack command has been quoted.

	module = None
	if not args:
		return

	cmd = args[0].split()
	if len(cmd) > 1:
		s = 'stack.commands.%s' % '.'.join(cmd)
		try:
			__import__(s)
			module = eval(s)
			i = 1
		except:
			module = None

	# Treat the entire command line as if it were a python
	# command module and keep popping arguments off the end
	# until we get a match.	 If no match is found issue an
	# error
	if not module:
		for i in range(len(args), 0, -1):
			s = 'stack.commands.%s' % '.'.join(args[:i])
			try:
				__import__(s)
				module = eval(s)
				if module:
					break
			except ImportError:
				continue

	if not module:
		sys.stderr.write('Error - Invalid stack command "%s"\n' % args[0])
		return -1

	name = ' '.join(s.split('.')[2:])

	# If we can load the command object then fall through and invoke the run()
	# method.  Otherwise the user did not give a complete command line and
	# we call the help command based on the partial command given.

	if not hasattr(module, 'Command'):
		import stack.commands.list.help
		help = stack.commands.list.help.Command(db)
		fullmodpath = s.split('.')
		submodpath = '/'.join(fullmodpath[2:])
		try:
			help.run({'subdir': submodpath}, [])
		except CommandError as e:
			sys.stderr.write('%s\n' % e)
			return -1
		print(help.getText())
		return -1

	try:
		command = getattr(module, 'Command')(db, debug=debug)
		rc = command.runWrapper(name, args[i:])
	except CommandError as e:
		sys.stderr.write('%s\n' % e)
		syslog.syslog(syslog.LOG_ERR, '%s' % e)
		return -1
	except Exception as e:
		# Sanitize Exceptions, and log them.
		exc, msg, tb = sys.exc_info()
		for line in traceback.format_tb(tb):
			syslog.syslog(syslog.LOG_DEBUG, '%s' % line)
			sys.stderr.write(line)
		error = '%s: %s -- %s' % (module.__name__, exc.__name__, msg)
		sys.stderr.write('%s\n' % error)
		syslog.syslog(syslog.LOG_ERR, error)
		return -1

	text = command.getText()

	# set the SIGPIPE to the system default (instead of python default)
	# before trying to print; prevents a stacktrace when exiting a pipe'd stack command
	signal.signal(signal.SIGPIPE, signal.SIG_DFL)

	if text and len(text) > 0:
		print(text, end='')
		if text[len(text) - 1] != '\n':
			print()
	syslog.closelog()
	if rc is True:
		return 0
	return -1
Ejemplo n.º 3
0
def run_command(args):
	# Check if the stack command has been quoted.

	module = None
        if not args:
                return
        
	cmd = args[0].split()
	if len(cmd) > 1:
		s = 'stack.commands.%s' % string.join(cmd, '.')
		try:
			__import__(s)
			module = eval(s)
			i = 1
		except:
			module = None

	# Treat the entire command line as if it were a python
	# command module and keep popping arguments off the end
	# until we get a match.  If no match is found issue an
	# error

	if not module:
		for i in range(len(args), 0, -1):
			s = 'stack.commands.%s' % string.join(args[:i], '.')
			try:
				__import__(s)
				module = eval(s)
				if module:
					break
			except ImportError:
				continue

	if not module:
		print 'error - invalid stack command "%s"' % args[0]
		return -1

	name = string.join(string.split(s, '.')[2:], ' ')


	# If we can load the command object then fall through and invoke the run()
	# method.  Otherwise the user did not give a complete command line and
	# we call the help command based on the partial command given.

	try:
		command   = getattr(module, 'Command')(Database)
	except AttributeError:
		import stack.commands.list.help
		help = stack.commands.list.help.Command(Database)
		fullmodpath = s.split('.')
		submodpath  = string.join(fullmodpath[2:], '/')
		help.run({'subdir': submodpath}, [])
		print help.getText()
		return -1

	# Try to use SUDO

#	if command.MustBeRoot and not (command.isRootUser() or command.isApacheUser()):
#		rc = os.system('sudo %s' % string.join(sys.argv,' '))
#		syslog.closelog()
#		return rc

	# Run as current user

	try:
		t0 = time.time()
		rc = command.runWrapper(name, args[i:])
		syslog.syslog(syslog.LOG_INFO, 'runtime %.3f' % (time.time() - t0))
	except SystemExit, (rc, msg, usage):
		print 'error - %s' % msg
		syslog.syslog(syslog.LOG_ERR, 'error - %s' % msg)
		if usage:
			print usage
		return rc