Exemplo n.º 1
0
    def remove(self, name=None, prefix=None, pkgs=None, all_=False):
        """
        Remove a package (from an environment) by name.

        Returns {
            success: bool, (this is always true),
            (other information)
        }
        """
        logger.debug(str((prefix, pkgs)))

        cmd_list = ['remove', '--json', '--yes']

        if not pkgs and not all_:
            raise TypeError("Must specify at least one package to remove, or "
                            "all=True.")

        if name:
            cmd_list.extend(['--name', name])
        elif prefix:
            cmd_list.extend(['--prefix', prefix])
        else:
            raise TypeError('must specify either an environment name or a '
                            'path for package removal')

        if all_:
            cmd_list.extend(['--all'])
        else:
            cmd_list.extend(pkgs)

        return self._call_and_parse(cmd_list)
Exemplo n.º 2
0
def launch(
    prefix,
    command,
    leave_path_alone,
    working_directory=HOME_PATH,
    package_name=None,
    root_prefix=None,
):
    """Handle launching commands from projects."""
    logger.debug(str((prefix, command)))
    command = command.replace('\\', '/')
    prefix = prefix.replace('\\', '/')
    root_prefix = root_prefix.replace('\\', '/')

    pid = -1

    if os.name == 'nt' and not leave_path_alone:
        command = command.replace('/bin', '/Scripts')

    if MAC or LINUX:
        pid = run_app_on_unix(prefix=prefix,
                              command=command,
                              package_name=package_name,
                              root_prefix=root_prefix)
    else:
        pid = run_app_on_win(prefix=prefix,
                             command=command,
                             package_name=package_name,
                             root_prefix=root_prefix)
    return pid
Exemplo n.º 3
0
    def linked(prefix, apps=False):
        """Return set of canonical names of linked packages in `prefix`."""
        logger.debug(str(prefix))

        if not os.path.isdir(prefix):
            return set()

        packages = set()
        meta_dir = join(prefix, 'conda-meta')

        if isdir(meta_dir):
            meta_files = set(fname for fname in os.listdir(meta_dir)
                             if fname.endswith('.json'))
            if apps:
                for fname in meta_files:
                    fpath = os.path.join(meta_dir, fname)

                    if os.path.isfile(fpath):
                        with open(fpath) as f:
                            data = f.read()

                        if 'app_entry' in data or 'app_type' in data:
                            packages.add(fname[:-5])
            else:
                packages = set(fname[:-5] for fname in meta_files)

        return packages
Exemplo n.º 4
0
    def info(self, abspath=True):
        """
        Return a dictionary with configuration information.

        No guarantee is made about which keys exist.  Therefore this function
        should only be used for testing and debugging.
        """
        logger.debug(str(''))
        return self._call_and_parse(['info', '--json'], abspath=abspath)
Exemplo n.º 5
0
    def start(self):
        """Start process."""
        logger.debug(str(' '.join(self._cmd_list)))

        if not self._fired:
            self._partial_ouput = None
            self._process.start(self._cmd_list[0], self._cmd_list[1:])
            self._timer.start()
        else:
            raise CondaProcessWorker('A Conda ProcessWorker can only run once '
                                     'per method call.')
Exemplo n.º 6
0
def launch(
    prefix,
    command,
    leave_path_alone,
    working_directory=HOME_PATH,
    package_name=None,
    root_prefix=None,
    environment=None,
    non_conda=False,
):
    """Handle launching commands from projects."""
    logger.debug(str((prefix, command)))
    new_command = []
    for cmd in command:
        new_cmd = cmd.replace('\\', '/')
        new_command.append(new_cmd)
    prefix = prefix.replace('\\', '/')
    root_prefix = root_prefix.replace('\\', '/')

    pid = -1

    # if os.name == 'nt' and not leave_path_alone:
    #     command = command.replace('/bin', '/Scripts')

    if MAC or LINUX:
        popen_dict = get_command_on_unix(
            prefix=prefix,
            command=new_command,
            package_name=package_name,
            root_prefix=root_prefix,
            environment=environment,
            non_conda=non_conda,
        )

    else:
        popen_dict = get_command_on_win(
            prefix=prefix,
            command=new_command,
            package_name=package_name,
            root_prefix=root_prefix,
            environment=environment,
            non_conda=non_conda,
        )

    args = popen_dict.pop('args')
    id_ = popen_dict.pop('id')
    cmd = popen_dict.pop('cmd')
    cmd
    p = subprocess.Popen(args, **popen_dict).pid
    pid = p, id_
    return pid
Exemplo n.º 7
0
    def install(self,
                name=None,
                prefix=None,
                pkgs=None,
                dep=True,
                channels=None,
                token=None):
        """
        Install a set of packages into an environment by name or path.

        If token is specified, the channels different from the defaults will
        get the token appended.
        """
        logger.debug(str((prefix, pkgs, channels)))

        # TODO: Fix temporal hack
        if not pkgs or not isinstance(pkgs, (list, tuple, str)):
            raise TypeError('must specify a list of one or more packages to '
                            'install into existing environment')

        cmd_list = ['install', '--yes', '--json', '--force-pscheck']
        if name:
            cmd_list.extend(['--name', name])
        elif prefix:
            cmd_list.extend(['--prefix', prefix])
        else:
            # Just install into the current environment, whatever that is
            pass

        # TODO: Check if correct
        if channels:
            cmd_list.extend(['--override-channels'])

            for channel in channels:
                cmd_list.extend(['--channel'])
                channel = self.parse_token_channel(channel, token)
                cmd_list.extend([channel])

        # TODO: Fix temporal hack
        if isinstance(pkgs, (list, tuple)):
            cmd_list.extend(pkgs)
        elif isinstance(pkgs, str):
            cmd_list.extend(['--file', pkgs])

        if not dep:
            cmd_list.extend(['--no-deps'])

        return self._call_and_parse(cmd_list)
Exemplo n.º 8
0
    def get_envs(self, log=True):
        """Return environment list of absolute path to their prefixes."""
        if log:
            logger.debug('')

        all_envs = []
        for env in self.envs_dirs:
            if os.path.isdir(env):
                envs_names = os.listdir(env)
                all_envs += [os.sep.join([env, i]) for i in envs_names]

        valid_envs = [
            e for e in all_envs
            if os.path.isdir(e) and self.environment_exists(prefix=e)
        ]
        return valid_envs
Exemplo n.º 9
0
    def create(self, name=None, prefix=None, pkgs=None, channels=None):
        """Create an environment with a specified set of packages."""
        logger.debug(str((prefix, pkgs, channels)))

        # TODO: Fix temporal hack
        if (not pkgs or
            (not isinstance(pkgs,
                            (list, tuple)) and not is_text_string(pkgs))):
            raise TypeError('must specify a list of one or more packages to '
                            'install into new environment')

        cmd_list = ['create', '--yes', '--json', '--mkdir']
        if name:
            ref = name
            search = [
                os.path.join(d, name)
                for d in self.info().communicate()[0]['envs_dirs']
            ]
            cmd_list.extend(['--name', name])
        elif prefix:
            ref = prefix
            search = [prefix]
            cmd_list.extend(['--prefix', prefix])
        else:
            raise TypeError('must specify either an environment name or a '
                            'path for new environment')

        if any(os.path.exists(prefix) for prefix in search):
            raise CondaEnvExistsError('Conda environment {0} already '
                                      'exists'.format(ref))

        # TODO: Fix temporal hack
        if isinstance(pkgs, (list, tuple)):
            cmd_list.extend(pkgs)
        elif is_text_string(pkgs):
            cmd_list.extend(['--file', pkgs])

        # TODO: Check if correct
        if channels:
            cmd_list.extend(['--override-channels'])

            for channel in channels:
                cmd_list.extend(['--channel'])
                cmd_list.extend([channel])

        return self._call_and_parse(cmd_list)
Exemplo n.º 10
0
    def create_from_yaml(self, name, yamlfile):
        """
        Create new environment using conda-env via a yaml specification file.

        Unlike other methods, this calls conda-env, and requires a named
        environment and uses channels as defined in rcfiles.

        Parameters
        ----------
        name : string
            Environment name
        yamlfile : string
            Path to yaml file with package spec (as created by conda env export
        """
        logger.debug(str((name, yamlfile)))
        cmd_list = ['env', 'create', '-n', name, '-f', yamlfile, '--json']
        return self._call_and_parse(cmd_list)
Exemplo n.º 11
0
    def environment_exists(self,
                           name=None,
                           prefix=None,
                           abspath=True,
                           log=True):
        """Check if an environment exists by 'name' or by 'prefix'.

        If query is by 'name' only the default conda environments directory is
        searched.
        """
        if log:
            logger.debug(str((name, prefix)))

        if name and prefix or (name is None and prefix is None):
            raise TypeError("Exactly one of 'name' or 'prefix' is required.")

        if name:
            prefix = self.get_prefix_envname(name)

        if prefix is None:
            prefix = self.ROOT_PREFIX

        return os.path.isdir(os.path.join(prefix, 'conda-meta'))