Example #1
0
    def __init__(self, target_cwd):
        report_time("Before MainBackend")
        MainBackend.__init__(self)
        report_time("After MainBackend")

        global _backend
        _backend = self

        self._ini = None
        self._object_info_tweakers = []
        self._import_handlers = {}
        self._input_queue = queue.Queue()
        self._source_preprocessors = []
        self._ast_postprocessors = []
        self._main_dir = os.path.dirname(sys.modules["thonny"].__file__)
        self._heap = {
        }  # WeakValueDictionary would be better, but can't store reference to None
        self._source_info_by_frame = {}
        site.sethelper()  # otherwise help function is not available
        self._install_fake_streams()
        self._install_repl_helper()
        self._current_executor = None
        self._io_level = 0
        self._tty_mode = True
        self._tcl = None

        update_system_path(os.environ,
                           get_augmented_system_path(get_exe_dirs()))

        # clean __main__ global scope
        for key in list(__main__.__dict__.keys()):
            if not key.startswith("__") or key in {"__file__", "__cached__"}:
                del __main__.__dict__[key]

        # unset __doc__, then exec dares to write doc of the script there
        __main__.__doc__ = None

        logger.info("Loading plugins")
        report_time("Before loading plugins")
        execute_with_frontend_sys_path(self._load_plugins)
        report_time("After loading plugins")

        # preceding code was run in the directory containing thonny module, now switch to provided
        try:
            os.chdir(os.path.expanduser(target_cwd))
        except OSError:
            try:
                os.chdir(os.path.expanduser("~"))
            except OSError:
                os.chdir("/")  # yes, this works also in Windows

        # ... and replace current-dir path item
        # start in shell mode (may be later switched to script mode)
        # required in shell mode and also required to overwrite thonny location dir
        sys.path[0] = ""
        sys.argv[:] = [""]  # empty "script name"

        if os.name == "nt":
            self._install_signal_handler()
def _open_system_shell():
    """Main task is to modify path and open terminal window.
    Bonus (and most difficult) part is executing a script in this window
    for recommending commands for running given python and related pip"""

    if inside_flatpak():
        show_command_not_available_in_flatpak_message()
        return

    cwd = get_workbench().get_local_cwd()

    proxy = get_runner().get_backend_proxy()
    if proxy and proxy.has_custom_system_shell():
        proxy.open_custom_system_shell()
        return
    if proxy and proxy.get_local_executable():
        target_executable = proxy.get_local_executable()
    else:
        target_executable = sys.executable

    exe_dirs = get_exe_dirs()
    if hasattr(proxy, "get_exe_dirs") and proxy.get_exe_dirs():
        # use both backend and frontend exe dirs
        exe_dirs = proxy.get_exe_dirs() + exe_dirs

    env_overrides = get_environment_overrides_for_python_subprocess(
        target_executable)
    env_overrides["PATH"] = get_augmented_system_path(exe_dirs)

    explainer = os.path.join(os.path.dirname(__file__),
                             "explain_environment.py")
    cmd = [target_executable, explainer]

    activate = os.path.join(
        os.path.dirname(target_executable),
        "activate.bat" if platform.system() == "Windows" else "activate",
    )

    if os.path.isfile(activate):
        del env_overrides["PATH"]
        if platform.system() == "Windows":
            cmd = [activate, "&"] + cmd
        else:
            cmd = ["source", activate, ";"] + cmd

    return terminal.run_in_terminal(cmd, cwd, env_overrides, True)
Example #3
0
 def _cmd_get_environment_info(self, cmd):
     return ToplevelResponse(
         main_dir=self._main_dir,
         sys_path=sys.path,
         usersitepackages=site.getusersitepackages()
         if site.ENABLE_USER_SITE else None,
         prefix=sys.prefix,
         welcome_text="Python " + get_python_version_string(),
         executable=sys.executable,
         exe_dirs=get_exe_dirs(),
         in_venv=(hasattr(sys, "base_prefix")
                  and sys.base_prefix != sys.prefix
                  or hasattr(sys, "real_prefix")
                  and getattr(sys, "real_prefix") != sys.prefix),
         python_version=get_python_version_string(),
         cwd=os.getcwd(),
     )