Skip to content

Commit

Permalink
new options in special comment language
Browse files Browse the repository at this point in the history
  • Loading branch information
pomponchik committed Sep 6, 2023
1 parent 5197fa6 commit 86f8221
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
11 changes: 10 additions & 1 deletion instld/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -56,14 +57,22 @@ 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:
with set_import():
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

Expand Down
3 changes: 3 additions & 0 deletions instld/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ class RestartingCommandError(Exception):

class RunningCommandError(Exception):
pass

class CommentFormatError(Exception):
pass
4 changes: 2 additions & 2 deletions instld/module/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 24 additions & 0 deletions tests/cli/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 86f8221

Please sign in to comment.