Skip to content

Commit

Permalink
Pass script files without using rewriting stdin to allow working with…
Browse files Browse the repository at this point in the history
… stdin in scripts
  • Loading branch information
fkantelberg committed Mar 11, 2024
1 parent 643c756 commit a2fc8a2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/doblib/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,19 @@ def load_shell_arguments(args):
default=utils.get_config_file(),
help="Configuration file to use. Default: %(default)s",
)
parser.add_argument("file", nargs="?", help="File to execute")
parser.add_argument("file", default=None, nargs="?", help="File to execute")
return parser.parse_known_args(args)


def wrapped_console(script_file):
def console(local_vars):
local_vars["__name__"] = "__main__"
with open(script_file, "r", encoding="utf-8") as fp:
exec(fp.read(), local_vars)

return console


class RunEnvironment(env.Environment):
"""Class to the environment"""

Expand All @@ -34,13 +43,12 @@ def shell(self, args=None):
# pylint: disable=C0415,E0401
from odoo.cli.shell import Shell

sys.argv = [args.file] + left if args.file else [""]
shell = Shell()

if args.file:
sys.stdin = open(args.file, "r", encoding="utf-8")
sys.argv = [args.file] + left
else:
sys.argv = [""]
shell.console = wrapped_console(args.file)

shell = Shell()
return shell.run(["-c", base.ODOO_CONFIG, "--no-http"])

def start(self, args=None):
Expand Down
2 changes: 2 additions & 0 deletions tests/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ def test_shell(env):

with NamedTemporaryFile() as fp:
shell.Shell.reset_mock()
console_mock = shell.Shell.return_value.console
env.shell([fp.name])
shell.Shell.assert_called_once()
assert sys.argv == [fp.name]
assert console_mock != shell.Shell.return_value.console


@mock.patch("doblib.utils.call", return_value=42)
Expand Down

0 comments on commit a2fc8a2

Please sign in to comment.