Ejemplo n.º 1
0
    def _create_process(
        self,
        installer: InstallerTypes = "pip",
    ):
        process = QProcess()
        if installer != "pip":
            process.setProgram(installer)
        else:
            process.setProgram(self._sys_executable_or_bundled_python())

        process.setProcessChannelMode(QProcess.MergedChannels)
        process.readyReadStandardOutput.connect(
            lambda process=process: self._on_stdout_ready(process)
        )

        # setup process path
        env = QProcessEnvironment()
        combined_paths = os.pathsep.join(
            [user_site_packages(), env.systemEnvironment().value("PYTHONPATH")]
        )
        env.insert("PYTHONPATH", combined_paths)
        # use path of parent process
        env.insert(
            "PATH", QProcessEnvironment.systemEnvironment().value("PATH")
        )
        process.setProcessEnvironment(env)
        self.set_output_widget(self._output_widget)
        process.finished.connect(
            lambda ec, es: self._on_process_finished(process, ec, es)
        )  # FIXME connecting lambda to finished signal is bug creating and may end with segfault when garbage
        # collection will consume Installer object before process end.
        return process
Ejemplo n.º 2
0
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setup_ui()
        self.setAttribute(Qt.WA_DeleteOnClose)

        # create install process
        self.process = QProcess(self)
        self.process.setProgram(sys.executable)
        self.process.setProcessChannelMode(QProcess.MergedChannels)
        # setup process path
        env = QProcessEnvironment()
        combined_paths = os.pathsep.join(
            [user_site_packages(), env.systemEnvironment().value("PYTHONPATH")]
        )
        env.insert("PYTHONPATH", combined_paths)
        self.process.setProcessEnvironment(env)

        # connections
        self.install_button.clicked.connect(self._install)
        self.uninstall_button.clicked.connect(self._uninstall)
        self.process.readyReadStandardOutput.connect(self._on_stdout_ready)

        from ..plugins import plugin_manager

        self.process.finished.connect(plugin_manager.discover)
        self.process.finished.connect(plugin_manager.prune)
Ejemplo n.º 3
0
    def _create_process(
        self,
        installer: InstallerTypes = "pip",
    ):
        process = QProcess()
        process.setProcessChannelMode(QProcess.MergedChannels)
        process.readyReadStandardOutput.connect(
            lambda process=process: self._on_stdout_ready(process))
        env = QProcessEnvironment.systemEnvironment()

        if installer == "pip":
            process.setProgram(self._sys_executable_or_bundled_python())
            # patch process path
            combined_paths = os.pathsep.join([
                user_site_packages(),
                env.systemEnvironment().value("PYTHONPATH"),
            ])
            env.insert("PYTHONPATH", combined_paths)
        else:
            process.setProgram(installer)

        if installer == "mamba":
            from ..._version import version_tuple

            # To avoid napari version changing when installing a plugin, we
            # add a pin to the current napari version, that way we can
            # restrict any changes to the actual napari application.
            # Conda/mamba also pin python by default, so we effectively
            # constrain python and napari versions from changing, when
            # installing plugins inside the constructor bundled application.
            # See: https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-pkgs.html#preventing-packages-from-updating-pinning
            napari_version = ".".join(str(v) for v in version_tuple[:3])
            if env.contains("CONDA_PINNED_PACKAGES"):
                # str delimiter is '&'
                system_pins = f"&{env.value('CONDA_PINNED_PACKAGES')}"
            else:
                system_pins = ""
            env.insert(
                "CONDA_PINNED_PACKAGES",
                f"napari={napari_version}{system_pins}",
            )
            if os.name == "nt":
                # workaround https://github.com/napari/napari/issues/4247, 4484
                if not env.contains("TEMP"):
                    temp = gettempdir()
                    env.insert("TMP", temp)
                    env.insert("TEMP", temp)
                if not env.contains("USERPROFILE"):
                    env.insert("HOME", os.path.expanduser("~"))
                    env.insert("USERPROFILE", os.path.expanduser("~"))

        process.setProcessEnvironment(env)
        self.set_output_widget(self._output_widget)
        process.finished.connect(
            lambda ec, es: self._on_process_finished(process, ec, es)
        )  # FIXME connecting lambda to finished signal is bug creating and may end with segfault when garbage
        # collection will consume Installer object before process end.
        return process
Ejemplo n.º 4
0
    def start_server(self, filename, interpreter):
        """
        Start a notebook server asynchronously.

        Start a server which can render the given notebook and return
        immediately. Assume the server uses the given interpreter. The manager
        will check periodically whether the server is accepting requests and
        emit `sig_server_started` or `sig_server_timed_out` when appropriate.

        Parameters
        ----------
        filename : str
            File name of notebook to be rendered by the server.
        interpreter : str
            File name of Python interpreter to be used.
        """
        home_dir = get_home_dir()
        if filename.startswith(home_dir):
            nbdir = home_dir
        else:
            nbdir = osp.dirname(filename)

        logger.debug('Starting new notebook server for %s', nbdir)
        process = QProcess(None)
        serverscript = osp.join(osp.dirname(__file__), '../server/main.py')
        serverscript = osp.normpath(serverscript)
        arguments = [
            serverscript, '--no-browser', '--notebook-dir={}'.format(nbdir),
            '--NotebookApp.password='******'--KernelSpecManager.kernel_spec_class={}'.format(KERNELSPEC)
        ]
        if self.dark_theme:
            arguments.append('--dark')
        logger.debug('Arguments: %s', repr(arguments))

        if DEV:
            env = QProcessEnvironment.systemEnvironment()
            env.insert('PYTHONPATH', osp.dirname(get_module_path('spyder')))
            process.setProcessEnvironment(env)

        server_process = ServerProcess(process,
                                       notebook_dir=nbdir,
                                       interpreter=interpreter)
        process.setProcessChannelMode(QProcess.MergedChannels)
        process.readyReadStandardOutput.connect(
            lambda: self.read_server_output(server_process))
        process.errorOccurred.connect(
            lambda error: self.handle_error(server_process, error))
        process.finished.connect(lambda code, status: self.handle_finished(
            server_process, code, status))

        process.start(sys.executable, arguments)
        self.servers.append(server_process)

        self._check_server_started(server_process)
Ejemplo n.º 5
0
def _get_environment():
    env = QProcessEnvironment.systemEnvironment()
    env.insert("PYTHONUNBUFFERED", "y")
    env.insert("PYTHONPATH", dirname(SCRIPT_DIRECTORY))
    if os.name == 'nt':
        anaconda_home = conda_prefix()
        path = env.value('PATH')
        new_path = os.pathsep.join([
            join(anaconda_home, 'Library', 'mingw-w64', 'bin'),
            join(anaconda_home, 'Library', 'usr', 'bin'), path
        ])
        env.insert("PATH", new_path)
    return env
Ejemplo n.º 6
0
    def __init__(self, output_widget: QTextEdit = None):
        from ...plugins import plugin_manager

        # create install process
        self._output_widget = None
        self.process = QProcess()
        self.process.setProgram(sys.executable)
        self.process.setProcessChannelMode(QProcess.MergedChannels)
        self.process.readyReadStandardOutput.connect(self._on_stdout_ready)
        # setup process path
        env = QProcessEnvironment()
        combined_paths = os.pathsep.join([
            user_site_packages(),
            env.systemEnvironment().value("PYTHONPATH")
        ])
        env.insert("PYTHONPATH", combined_paths)
        # use path of parent process
        env.insert("PATH",
                   QProcessEnvironment.systemEnvironment().value("PATH"))
        self.process.setProcessEnvironment(env)
        self.process.finished.connect(lambda: plugin_manager.discover())
        self.process.finished.connect(lambda: plugin_manager.prune())
        self.set_output_widget(output_widget)
Ejemplo n.º 7
0
    def _install(
            self,
            pkg_list: Sequence[str],
            installer: Optional[InstallerTypes] = None,
            channels: Sequence[str] = ("conda-forge", ),
    ):
        installer = installer or self._installer_type
        process_environment = QProcessEnvironment.systemEnvironment()
        if installer != "pip":
            from ..._version import version_tuple

            # To avoid napari version changing when installing a plugin, we
            # add a pin to the current napari version, that way we can
            # restrict any changes to the actual napari application.
            # Conda/mamba also pin python by default, so we effectively
            # constrain python and napari versions from changing, when
            # installing plugins inside the constructor bundled application.
            # See: https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-pkgs.html#preventing-packages-from-updating-pinning
            napari_version = ".".join(str(v) for v in version_tuple[:3])
            process_environment.insert("CONDA_PINNED_PACKAGES",
                                       f"napari={napari_version}")
            cmd = [
                'install',
                '-y',
                '--prefix',
                self._conda_env_path,
            ]
            for channel in channels:
                cmd.extend(["-c", channel])
        else:
            cmd = ['-m', 'pip', 'install', '--upgrade']

        if (running_as_bundled_app() and sys.platform.startswith('linux')
                and not self._conda_env_path):
            cmd += [
                '--no-warn-script-location',
                '--prefix',
                user_plugin_dir(),
            ]

        process = self._create_process(installer)
        process.setProcessEnvironment(process_environment)
        process.setArguments(cmd + list(pkg_list))
        if self._output_widget and self._queue:
            self._output_widget.clear()

        process.start()
        return process
Ejemplo n.º 8
0
    def _prepare_process(self, config, pythonpath):
        """
        Prepare and return process for running the unit test suite.

        This sets the working directory and environment.
        """
        process = QProcess(self)
        process.setProcessChannelMode(QProcess.MergedChannels)
        process.setWorkingDirectory(config.wdir)
        process.finished.connect(self.finished)
        if pythonpath:
            env = QProcessEnvironment.systemEnvironment()
            old_python_path = env.value('PYTHONPATH', None)
            python_path_str = os.pathsep.join(pythonpath)
            if old_python_path:
                python_path_str += os.pathsep + old_python_path
            env.insert('PYTHONPATH', python_path_str)
            process.setProcessEnvironment(env)
        return process
Ejemplo n.º 9
0
    def spawn_flag_process(self, stat, solver):

        log.debug('Feature testing MFiX %s', solver)
        key = (stat, solver)

        proc = self.flag_processes.get(key, None)
        if proc is not None:
            proc.kill()

        exe = self.solver_list.get(solver)
        exe_dir = os.path.dirname(exe)
        proc = self.flag_processes[key] = QProcess()
        proc.setProcessEnvironment(QProcessEnvironment.systemEnvironment())
        proc.readyReadStandardOutput.connect(
            lambda k=key: self.flag_process_out(k))
        proc.readyReadStandardError.connect(
            lambda k=key: self.flag_process_error(k))
        proc.finished.connect(lambda ecode, estat, k=key: self.
                              flag_process_finished(k, ecode, estat))
        proc.error.connect(lambda e, k=key: self.flag_process_error(k, e))
        proc.setWorkingDirectory(exe_dir)
        proc.start(exe, ["--print-flags"])