Skip to content

Commit 93ebaad

Browse files
authored
[IMP] Scripts with working stdin (#20)
* Pass script files without using rewriting stdin to allow working with stdin in scripts * Bump version to 0.18.0
1 parent 643c756 commit 93ebaad

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

src/doblib/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
VERSION = "0.17.2"
1+
VERSION = "0.18.0"

src/doblib/run.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,19 @@ def load_shell_arguments(args):
1818
default=utils.get_config_file(),
1919
help="Configuration file to use. Default: %(default)s",
2020
)
21-
parser.add_argument("file", nargs="?", help="File to execute")
21+
parser.add_argument("file", default=None, nargs="?", help="File to execute")
2222
return parser.parse_known_args(args)
2323

2424

25+
def wrapped_console(script_file):
26+
def console(local_vars):
27+
local_vars["__name__"] = "__main__"
28+
with open(script_file, "r", encoding="utf-8") as fp:
29+
exec(fp.read(), local_vars)
30+
31+
return console
32+
33+
2534
class RunEnvironment(env.Environment):
2635
"""Class to the environment"""
2736

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

46+
sys.argv = [args.file] + left if args.file else [""]
47+
shell = Shell()
48+
3749
if args.file:
38-
sys.stdin = open(args.file, "r", encoding="utf-8")
39-
sys.argv = [args.file] + left
40-
else:
41-
sys.argv = [""]
50+
shell.console = wrapped_console(args.file)
4251

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

4654
def start(self, args=None):

tests/test_run.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,11 @@ def test_shell(env):
4040

4141
with NamedTemporaryFile() as fp:
4242
shell.Shell.reset_mock()
43+
console_mock = shell.Shell.return_value.console
4344
env.shell([fp.name])
4445
shell.Shell.assert_called_once()
4546
assert sys.argv == [fp.name]
47+
assert console_mock != shell.Shell.return_value.console
4648

4749

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

0 commit comments

Comments
 (0)