<commit_before># Filename: main.py
# Module:	main
# Date:		4th August 2004
# Author:	<NAME>, prologic at shortcircuit dot net dot au

"""Main

This is the main module. Everything starts from here
after the command-line options have been parsed and
passed to here by the cli module.
"""

import os
import signal
import optparse

from pymills.utils import getProgName, \
		writePID, daemonize

from core import Core
from env import Environment
from kdb import __name__ as systemName
from kdb import __version__ as systemVersion

USAGE = """"%prog [options] <endPath> <command>

commands:
  start    Start %prog
  stop     Stop %prog
  rehash   Rehash (reload environment)
  initenv  Create a new empty environment for %prog.
  upgrade  Upgrade an existing environment."""

VERSION = "%prog v" + systemVersion

def parse_options():
	"""parse_options() -> opts, args

	Parse any command-line options given returning both
	the parsed options and arguments.
	"""

	parser = optparse.OptionParser(usage=USAGE, version=VERSION)

	parser.add_option("-n", "--no-fork",
			action="store_true", default=False, dest="nofork",
			help="Don't fork to background")

	opts, args = parser.parse_args()
	if len(args) != 2:
		parser.print_help()
		raise SystemExit, 1
	return opts, args

def run():
	"""run() -> None

	Parse all command-line arguments and options
	determine what command to run. The environment
	path must be the first argument specified.
	If no valid command is given as the second
	argument, an error is printed and the system
	is terminated with an error code of 1.
	"""

	opts, args = parse_options()

	envPath = args[0]
	command = args[1].upper()

	if command == "START":
		start(envPath, not opts.nofork)
	elif command == "STOP":
		stop(envPath)
	elif command == "RESTART":
		restart(envPath)
	elif command == "REHASH":
		rehash(envPath)
	elif command == "INITENV":
		initEnv(envPath)
	elif command == "UPGRADE":
		upgrade(envPath)
	else:
		print "ERROR: Invalid Command %s" % args[0]
		raise SystemExit, 1

def start(envPath, daemon=True):
	"""start(envPath, daemon=True) -> None

	Start the system. Check if the given envPath is
	valid and doesn't need upgrading. Daemonize if the
	daemon option (default is True) is given. Write the
	pid of this process and run the core.
	"""

	if not os.path.exists(envPath):
		print "ERROR: Path not found '%s'" % envPath
		raise SystemExit, 1

	env = Environment(envPath)
	if env.needsUpgrade():
		print "ERROR: %s Environment '%s' needs upgrading" % (
				systemName, envPath)
		print "Run: %s %s upgrade" % (getProgName, envPath)
		raise SystemExit, 1

	print "-- Starting %s...\n" % systemName

	if daemon:
		daemonize(envPath)

	writePID(env.config.get(systemName, "pidfile"))

	core = Core(env.event, env)
	core.run()
	pidfile = env.config.get(systemName, "pidfile")
	os.remove(pidfile)

def stop(envPath):
	"""stop(envPath) -> None

	Stop the system by sending the KILL signal to the
	pid found in the environment given by envPath.
	Check if the given environment is valid before
	attempting this. If an error occurs while trying
	to do this, the error is printed and exitcode 1
	is returned.
	"""

	if not os.path.exists(envPath):
		print "ERROR: Path not found '%s'" % envPath
		raise SystemExit, 1

	env = Environment(envPath)

	try:
		os.kill(int(open(env.config.get(
			systemName, "pidfile")).read()),
			signal.SIGTERM)
		print "-- %s Stopped" % systemName
	except Exception, e:<commit_msg>main.py: Wrong arg used - must be kwarg 'path='<commit_after># Filename: main.py
# Module:	main
# Date:		4th August 2004
# Author:	<NAME>, prologic at shortcircuit dot net dot au

"""Main

This is the main module. Everything starts from here
after the command-line options have been parsed and
passed to here by the cli module.
"""

import os
import signal
import optparse

from pymills.utils import getProgName, \
		writePID, daemonize

from core import Core
from env import Environment
from kdb import __name__ as systemName
from kdb import __version__ as systemVersion

USAGE = """"%prog [options] <endPath> <command>

commands:
  start    Start %prog
  stop     Stop %prog
  rehash   Rehash (reload environment)
  initenv  Create a new empty environment for %prog.
  upgrade  Upgrade an existing environment."""

VERSION = "%prog v" + systemVersion

def parse_options():
	"""parse_options() -> opts, args

	Parse any command-line options given returning both
	the parsed options and arguments.
	"""

	parser = optparse.OptionParser(usage=USAGE, version=VERSION)

	parser.add_option("-n", "--no-fork",
			action="store_true", default=False, dest="nofork",
			help="Don't fork to background")

	opts, args = parser.parse_args()
	if len(args) != 2:
		parser.print_help()
		raise SystemExit, 1
	return opts, args

def run():
	"""run() -> None

	Parse all command-line arguments and options
	determine what command to run. The environment
	path must be the first argument specified.
	If no valid command is given as the second
	argument, an error is printed and the system
	is terminated with an error code of 1.
	"""

	opts, args = parse_options()

	envPath = args[0]
	command = args[1].upper()

	if command == "START":
		start(envPath, not opts.nofork)
	elif command == "STOP":
		stop(envPath)
	elif command == "RESTART":
		restart(envPath)
	elif command == "REHASH":
		rehash(envPath)
	elif command == "INITENV":
		initEnv(envPath)
	elif command == "UPGRADE":
		upgrade(envPath)
	else:
		print "ERROR: Invalid Command %s" % args[0]
		raise SystemExit, 1

def start(envPath, daemon=True):
	"""start(envPath, daemon=True) -> None

	Start the system. Check if the given envPath is
	valid and doesn't need upgrading. Daemonize if the
	daemon option (default is True) is given. Write the
	pid of this process and run the core.
	"""

	if not os.path.exists(envPath):
		print "ERROR: Path not found '%s'" % envPath
		raise SystemExit, 1

	env = Environment(envPath)
	if env.needsUpgrade():
		print "ERROR: %s Environment '%s' needs upgrading" % (
				systemName, envPath)
		print "Run: %s %s upgrade" % (getProgName, envPath)
		raise SystemExit, 1

	print "-- Starting %s...\n" % systemName

	if daemon:
		daemonize(path=envPath)

	writePID(env.config.get(systemName, "pidfile"))

	core = Core(env.event, env)
	core.run()
	pidfile = env.config.get(systemName, "pidfile")
	os.remove(pidfile)

def stop(envPath):
	"""stop(envPath) -> None

	Stop the system by sending the KILL signal to the
	pid found in the environment given by envPath.
	Check if the given environment is valid before
	attempting this. If an error occurs while trying
	to do this, the error is printed and exitcode 1
	is returned.
	"""

	if not os.path.exists(envPath):
		print "ERROR: Path not found '%s'" % envPath
		raise SystemExit, 1

	env = Environment(envPath)

	try:
		os.kill(int(open(env.config.get(
			systemName, "pidfile")).read()),
			signal.SIGTERM)
		print "-- %s Stopped" % systemName
	except Exception, e:
{'fraction_non_alphanumeric': 0.10835377290434281, 'fraction_numerical': 0.004616938392728322, 'mean_word_length': 4.06355003652301, 'pattern_counts': {'":': 12, '<': 9, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '166793', 'n_tokens_mistral': 2378, 'n_tokens_neox': 2208, 'n_words': 830}