Commit 0549151d authored by Samuel GAIST's avatar Samuel GAIST
Browse files

[scripts][loop_execute] pre-commit cleanup

parent 9aedbc01
......@@ -61,9 +61,6 @@ import logging
import os
import sys
import docopt
import simplejson
import pwd
import stat
import zmq
......@@ -72,25 +69,25 @@ from beat.backend.python.execution import LoopMessageHandler
from beat.backend.python.exceptions import UserError
#----------------------------------------------------------
# ----------------------------------------------------------
def process_traceback(tb, prefix):
import traceback
algorithms_prefix = os.path.join(prefix, 'algorithms') + os.sep
algorithms_prefix = os.path.join(prefix, "algorithms") + os.sep
for first_line, line in enumerate(tb):
if line[0].startswith(algorithms_prefix):
break
s = ''.join(traceback.format_list(tb[first_line:]))
s = s.replace(algorithms_prefix, '').strip()
s = "".join(traceback.format_list(tb[first_line:]))
s = s.replace(algorithms_prefix, "").strip()
return s
#----------------------------------------------------------
# ----------------------------------------------------------
def main(arguments=None):
......@@ -99,74 +96,69 @@ def main(arguments=None):
if arguments is None:
arguments = sys.argv[1:]
package = __name__.rsplit('.', 2)[0]
version = package + ' v' + \
__import__('pkg_resources').require(package)[0].version
package = __name__.rsplit(".", 2)[0]
version = package + " v" + __import__("pkg_resources").require(package)[0].version
prog = os.path.basename(sys.argv[0])
args = docopt.docopt(
__doc__ % dict(prog=prog, version=version),
argv=arguments,
version=version
__doc__ % dict(prog=prog, version=version), argv=arguments, version=version
)
# Setup the logging system
formatter = logging.Formatter(fmt="[%(asctime)s - loop_provider.py - " \
"%(name)s] %(levelname)s: %(message)s",
datefmt="%d/%b/%Y %H:%M:%S")
formatter = logging.Formatter(
fmt="[%(asctime)s - loop_provider.py - " "%(name)s] %(levelname)s: %(message)s",
datefmt="%d/%b/%Y %H:%M:%S",
)
handler = logging.StreamHandler()
handler.setFormatter(formatter)
root_logger = logging.getLogger('beat.backend.python')
root_logger = logging.getLogger("beat.backend.python")
root_logger.addHandler(handler)
if args['--debug']:
if args["--debug"]:
root_logger.setLevel(logging.DEBUG)
else:
root_logger.setLevel(logging.INFO)
logger = logging.getLogger(__name__)
# Create the message handler
message_handler = LoopMessageHandler(args['<addr>'])
message_handler = LoopMessageHandler(args["<addr>"])
context = None
db_socket = None
if args['<db_addr>']:
if args["<db_addr>"]:
context = zmq.Context()
db_socket = context.socket(zmq.PAIR)
db_socket.connect(args['<db_addr>'])
logger.debug("loop: zmq client connected to db `%s'", args['<db_addr>'])
db_socket.connect(args["<db_addr>"])
logger.debug("loop: zmq client connected to db `%s'", args["<db_addr>"])
# If necessary, change to another user (with less privileges, but has access
# to the databases)
with open(os.path.join(args['<dir>'], 'configuration.json'), 'r') as f:
cfg = simplejson.load(f)
try:
# Check the dir
if not os.path.exists(args['<dir>']):
raise IOError("Running directory `%s' not found" % args['<dir>'])
if not os.path.exists(args["<dir>"]):
raise IOError("Running directory `%s' not found" % args["<dir>"])
# Sets up the execution
try:
loop_executor = LoopExecutor(message_handler=message_handler,
directory=args['<dir>'],
cache_root=args['<cache>'],
db_socket=db_socket)
loop_executor = LoopExecutor(
message_handler=message_handler,
directory=args["<dir>"],
cache_root=args["<cache>"],
db_socket=db_socket,
)
except (MemoryError):
raise
except Exception as e:
import traceback
exc_type, exc_value, exc_traceback = sys.exc_info()
tb = traceback.extract_tb(exc_traceback)
s = process_traceback(tb, os.path.join(args['<dir>'], 'prefix'))
s = process_traceback(tb, os.path.join(args["<dir>"], "prefix"))
raise UserError("%s%s: %s" % (s, type(e).__name__, e))
try:
......@@ -177,6 +169,7 @@ def main(arguments=None):
raise
except Exception as e:
import traceback
exc_type, exc_value, exc_traceback = sys.exc_info()
tb = traceback.extract_tb(exc_traceback)
s = process_traceback(tb, loop_executor.prefix)
......@@ -191,6 +184,7 @@ def main(arguments=None):
raise
except Exception as e:
import traceback
exc_type, exc_value, exc_traceback = sys.exc_info()
tb = traceback.extract_tb(exc_traceback)
s = process_traceback(tb, loop_executor.prefix)
......@@ -205,29 +199,33 @@ def main(arguments=None):
raise
except Exception as e:
import traceback
exc_type, exc_value, exc_traceback = sys.exc_info()
tb = traceback.extract_tb(exc_traceback)
s = process_traceback(tb, os.path.join(args['<dir>'], 'prefix'))
s = process_traceback(tb, os.path.join(args["<dir>"], "prefix"))
raise UserError("%s%s: %s" % (s, type(e).__name__, e))
except UserError as e:
message_handler.send_error(str(e), 'usr')
message_handler.send_error(str(e), "usr")
message_handler.destroy()
return 1
except MemoryError as e:
except MemoryError:
# Say something meaningful to the user
msg = "The user process for this block ran out of memory. We " \
"suggest you optimise your code to reduce memory usage or, " \
"if this is not an option, choose an appropriate processing " \
"queue with enough memory."
message_handler.send_error(msg, 'usr')
msg = (
"The user process for this block ran out of memory. We "
"suggest you optimise your code to reduce memory usage or, "
"if this is not an option, choose an appropriate processing "
"queue with enough memory."
)
message_handler.send_error(msg, "usr")
message_handler.destroy()
return 1
except Exception as e:
except Exception:
import traceback
message_handler.send_error(traceback.format_exc(), 'sys')
message_handler.send_error(traceback.format_exc(), "sys")
message_handler.destroy()
return 1
......@@ -243,6 +241,5 @@ def main(arguments=None):
return 0
if __name__ == '__main__':
if __name__ == "__main__":
sys.exit(main())
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment