diff --git a/instld/cli/main.py b/instld/cli/main.py index 991ae3a..5df2061 100644 --- a/instld/cli/main.py +++ b/instld/cli/main.py @@ -11,6 +11,7 @@ from instld.cli.parsing_comments.get_options_from_comments import get_options_from_comments from instld.cli.parsing_arguments.get_python_file import get_python_file from instld.cli.traceback_cutting.cutting import set_cutting_excepthook +from instld.errors import CommentFormatError def main(): @@ -56,6 +57,14 @@ def import_wrapper(name, *args, **kwargs): if 'version' in options: package_name = f'{package_name}=={options.pop("version")}' + catch_output = options.pop('catch_output', 'no').lower() + if catch_output in ('yes', 'on', 'true'): + catch_output = True + elif catch_output in ('no', 'off', 'false'): + catch_output = False + else: + raise CommentFormatError('For option "catch_output" you can use the following values: "yes", "on", "true", "no", "off", "false".') + current_context = get_current_context(options.pop('where', None)) with lock: @@ -63,7 +72,7 @@ def import_wrapper(name, *args, **kwargs): try: result = __import__(name, *args, **kwargs) except (ModuleNotFoundError, ImportError) as e: - current_context.install(package_name) + current_context.install(package_name, catch_output=catch_output, **options) result = current_context.import_here(base_name) sys.modules[base_name] = result diff --git a/instld/errors.py b/instld/errors.py index 5fa6a34..764e682 100644 --- a/instld/errors.py +++ b/instld/errors.py @@ -6,3 +6,6 @@ class RestartingCommandError(Exception): class RunningCommandError(Exception): pass + +class CommentFormatError(Exception): + pass diff --git a/instld/module/context.py b/instld/module/context.py index 16205d9..2d43b88 100644 --- a/instld/module/context.py +++ b/instld/module/context.py @@ -42,10 +42,10 @@ def new_path(self, module_name): yield sys.path = old_path - def install(self, *package_names, **options): + def install(self, *package_names, catch_output=False, **options): if not package_names: raise ValueError('You need to pass at least one package name.') options = convert_options(options) - with self.installer(package_names, options=options): + with self.installer(package_names, catch_output=catch_output, options=options): pass diff --git a/tests/cli/test_cli.py b/tests/cli/test_cli.py index 4546e7a..dafcf9e 100644 --- a/tests/cli/test_cli.py +++ b/tests/cli/test_cli.py @@ -125,3 +125,27 @@ def test_exceptions_are_similar_with_just_python_command_2(): assert result_1.stderr == result_2.stderr os.remove(script) + + +def test_install_package_from_another_repository(main_runner): + strings = [ + 'import super_test # instld: package super_test_project, version 0.0.1, index_url https://test.pypi.org/simple/, catch_output true', + 'print(super_test.function(2, 3))', + ] + + script = os.path.join('tests', 'cli', 'data', 'main.py') + with open(script, 'w') as file: + file.write('\n'.join(strings)) + + for runner in (subprocess.run, main_runner): + result = runner(['instld', script], stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=100, universal_newlines=True) + + result.check_returncode() + + print(result.stderr) + print(result.stdout) + #assert result.stderr == '' + assert result.stdout == '5\n' + + + os.remove(script)