Commit 72ce6adc authored by LE GAC Renaud's avatar LE GAC Renaud
Browse files

Polish the script run.

parent ebdec501
#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" NAME
run -- run a script or test within track_publications
run -- run a script or python tests within the application framework
SYNOPSIS
run [options] <command> [args]
DESCRIPTION
The run command has to be run in the scripts directory
It can execute two commands script and pytest.
The command has to be run in the scripts directory
It can execute two subcommands 'script' and 'pytest'.
The first command, script, run a given script in the application
The first subcommand runs a given script in the application
context (model, modules and database are available).
The second command, pytest, run pythons test within the pytest
framework as well as the application context.
The second subcommand runs python tests within the pytest
framework and the application context.
The python tests are located in the directory myapplication/tests.
The scripts are located in myapplication/scripts.
The path of the test are relative to that directory.
OPTIONS
......@@ -38,7 +39,7 @@
> ./run pytest -h
> ./run pytest
> ./run pytest -xq test_foo/test_faa.py
> ./run pytest -xq test_foo/test_faa.py::test_fii
> ...
AUTHOR
......@@ -69,6 +70,33 @@ def get_application():
return os.getcwd().split(os.sep)[-2]
def process(application, script, args):
"""Process the script within the framework of the application
Args:
application (str): the name of the application
script (str): filename for the script
args (str): optional argument to be passed to the script
Returns:
int: return code of the subprocess
"""
script_path = os.path.join(os.getcwd(), script)
cmd = [WEB2PY,
'--no-banner',
'--shell', application,
'--import_models',
'--run', script_path]
if args:
cmd.extend(['--args', args])
return subprocess.call(cmd)
def run_pytest(args):
"""Run python test.
......@@ -95,16 +123,7 @@ def run_pytest(args):
cmd = "%s %s" % (cmd, path)
# run pytest script in the application context
script_path = os.path.join(os.getcwd(), PYTEST_SCRIPT)
cmd = [WEB2PY,
'--no-banner',
'--shell', args.shell,
'--import_models',
'--run', script_path,
'--args', cmd]
return_code = subprocess.call(cmd)
return_code = process(args.shell, PYTEST_SCRIPT, cmd)
sys.exit(return_code)
......@@ -112,20 +131,7 @@ def run_script(args):
"""Run the given script.
"""
# run the script in the application context
script_path = os.path.join(os.getcwd(), args.filename)
cmd = [WEB2PY,
'--no-banner',
'--shell', args.shell,
'--import_models',
'--run', script_path]
if args.args:
cmd.append('--ARGS')
cmd.extend(args.args)
return_code = subprocess.call(cmd)
return_code = process(args.shell, args.filename, args.args)
sys.exit(return_code)
......@@ -187,8 +193,8 @@ if __name__ == "__main__":
help="the name of the python script.")
SCRIPT.add_argument("args",
nargs="*",
help="additional arguments to run the script.")
nargs="?",
help="additional arguments to be passed to the script.")
SCRIPT.set_defaults(func=run_script)
......
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