Skip to content

Commit

Permalink
REPL (without comments reading)
Browse files Browse the repository at this point in the history
  • Loading branch information
pomponchik committed Sep 9, 2023
1 parent 86f8221 commit 6e14303
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 41 deletions.
43 changes: 35 additions & 8 deletions instld/cli/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import sys
import code
import builtins
import importlib
import inspect
Expand All @@ -8,7 +9,7 @@
from threading import RLock

import instld
from instld.cli.parsing_comments.get_options_from_comments import get_options_from_comments
from instld.cli.parsing_comments.get_options_from_comments import get_options_from_comments_by_frame
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
Expand Down Expand Up @@ -50,7 +51,8 @@ def import_wrapper(name, *args, **kwargs):
last_name = splitted_name[-1]

current_frame = inspect.currentframe()
options = get_options_from_comments(current_frame.f_back)
options = get_options_from_comments_by_frame(current_frame.f_back)
#print('OPTIONS:', options)

package_name = options.pop('package', base_name)

Expand All @@ -72,6 +74,7 @@ def import_wrapper(name, *args, **kwargs):
try:
result = __import__(name, *args, **kwargs)
except (ModuleNotFoundError, ImportError) as e:
#print('OPTIONS>', options, 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 All @@ -87,13 +90,37 @@ def import_wrapper(name, *args, **kwargs):

return result

builtins.__import__ = import_wrapper

spec = importlib.util.spec_from_file_location('kek', os.path.abspath(python_file))
module = importlib.util.module_from_spec(spec)
sys.modules['__main__'] = module
set_cutting_excepthook(4)
spec.loader.exec_module(module)

if python_file is None:
try:
import readline
except ImportError:
pass

builtins.__import__ = import_wrapper

class REPL(code.InteractiveConsole):
pass


banner_strings = [
'⚡ INSTLD REPL based on\n'
'Python %s on %s\n' % (sys.version, sys.platform),
'Type "help", "copyright", "credits" or "license" for more information.\n',
]
banner = ''.join(banner_strings)

REPL().interact(banner=banner)


else:
builtins.__import__ = import_wrapper
spec = importlib.util.spec_from_file_location('kek', os.path.abspath(python_file))
module = importlib.util.module_from_spec(spec)
sys.modules['__main__'] = module
set_cutting_excepthook(4)
spec.loader.exec_module(module)


if __name__ == "__main__":
Expand Down
7 changes: 2 additions & 5 deletions instld/cli/parsing_arguments/get_python_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,5 @@


def get_python_file():
if len(sys.argv) < 2:
print('usage: instld python_file.py [argv ...]', file=sys.stderr)
sys.exit(1)

return sys.argv[1]
if len(sys.argv) >= 2:
return sys.argv[1]
2 changes: 1 addition & 1 deletion instld/cli/parsing_comments/get_comment_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def get_comment_string_from_file(line_number, file_name):
except (FileNotFoundError, OSError):
return None

def get_comment_string(frame):
def get_comment_string_by_frame(frame):
line_number = frame.f_lineno
code = frame.f_code
file_name = code.co_filename
Expand Down
10 changes: 6 additions & 4 deletions instld/cli/parsing_comments/get_options_from_comments.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from instld.errors import InstallingPackageError
from instld.cli.parsing_comments.get_comment_string import get_comment_string
from instld.cli.parsing_comments.get_comment_string import get_comment_string_by_frame


def get_options_from_comments(frame):
comment_string = get_comment_string(frame)

def get_options_from_comments(comment_string):
result = {}

if comment_string is not None:
Expand All @@ -25,3 +23,7 @@ def get_options_from_comments(frame):
result.pop('comment', None)

return result

def get_options_from_comments_by_frame(frame):
comment_string = get_comment_string_by_frame(frame)
return get_options_from_comments(comment_string)
2 changes: 2 additions & 0 deletions instld/module/context_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ def create_temp_directory():
new_error = InstallingPackageError(f'{str(e)} It occurred when installing one of the following packages: {", ".join(packages_names)}.')
new_error.stdout = e.stdout
new_error.stderr = e.stderr
#print('STDOUT', e.stdout)
#print('STDERR', e.stderr)
raise new_error from e

yield Context(where, logger, catch_output, options, partial(pip_context, logger=logger, runner=runner, catch_output=catch_output, where=directory))
13 changes: 1 addition & 12 deletions tests/cli/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,6 @@ def test_cli_where(main_runner):
os.remove(script)


def test_run_command_without_arguments(main_runner):
for runner in (main_runner, subprocess.run):
result = runner(['instld'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=100, universal_newlines=True)

assert result.returncode == 1
assert result.stdout == ''
assert result.stderr == f'usage: instld python_file.py [argv ...]\n'


def test_run_command_with_arguments(main_runner):
strings = [
'import json, sys',
Expand Down Expand Up @@ -140,11 +131,9 @@ def test_install_package_from_another_repository(main_runner):
for runner in (subprocess.run, main_runner):
result = runner(['instld', script], stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=100, universal_newlines=True)

print(result.stderr)
result.check_returncode()

print(result.stderr)
print(result.stdout)
#assert result.stderr == ''
assert result.stdout == '5\n'


Expand Down
10 changes: 5 additions & 5 deletions tests/units/cli/parsing_comments/test_get_comment_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@
import pytest

from instld.errors import InstallingPackageError
from instld.cli.parsing_comments.get_comment_string import get_comment_string
from instld.cli.parsing_comments.get_comment_string import get_comment_string_by_frame


def test_get_comment_started_with_instld():
comment = get_comment_string(inspect.currentframe()) # instld: lol kek cheburek
comment = get_comment_string_by_frame(inspect.currentframe()) # instld: lol kek cheburek
assert comment == 'lol kek cheburek'


def test_get_comment_not_started_with_instld():
comment = get_comment_string(inspect.currentframe()) # lol kek cheburek
comment = get_comment_string_by_frame(inspect.currentframe()) # lol kek cheburek
assert comment is None


def test_get_comment_without_comment():
comment = get_comment_string(inspect.currentframe())
comment = get_comment_string_by_frame(inspect.currentframe())
assert comment is None


def test_get_comment_wrong():
with pytest.raises(InstallingPackageError):
comment = get_comment_string(inspect.currentframe()) # instld:
comment = get_comment_string_by_frame(inspect.currentframe()) # instld:
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import pytest

from instld.errors import InstallingPackageError
from instld.cli.parsing_comments.get_options_from_comments import get_options_from_comments
from instld.cli.parsing_comments.get_options_from_comments import get_options_from_comments_by_frame


def test_get_normal_options():
options = get_options_from_comments(inspect.currentframe()) # instld: lol kek, cheburek mek
options = get_options_from_comments_by_frame(inspect.currentframe()) # instld: lol kek, cheburek mek

assert isinstance(options, dict)
assert len(options) == 2
Expand All @@ -18,15 +18,15 @@ def test_get_normal_options():

def test_get_wrong_options():
with pytest.raises(InstallingPackageError):
options = get_options_from_comments(inspect.currentframe()) # instld: lol kek cheburek, cheburek mek
options = get_options_from_comments_by_frame(inspect.currentframe()) # instld: lol kek cheburek, cheburek mek
with pytest.raises(InstallingPackageError):
options = get_options_from_comments(inspect.currentframe()) # instld: lol
options = get_options_from_comments_by_frame(inspect.currentframe()) # instld: lol
with pytest.raises(InstallingPackageError):
options = get_options_from_comments(inspect.currentframe()) # instld:
options = get_options_from_comments_by_frame(inspect.currentframe()) # instld:


def test_get_empty_options():
options = get_options_from_comments(inspect.currentframe())
options = get_options_from_comments_by_frame(inspect.currentframe())

assert isinstance(options, dict)
assert len(options) == 0

0 comments on commit 6e14303

Please sign in to comment.