Ejemplo n.º 1
0
def main():
    from conda.base.context import context
    from conda.base.constants import ROOT_ENV_NAME
    from conda.utils import shells
    if '-h' in sys.argv or '--help' in sys.argv:
        # all execution paths sys.exit at end.
        help(sys.argv[1], sys.argv[2])

    if len(sys.argv) > 2:
        shell = sys.argv[2]
        shelldict = shells[shell]
    if sys.argv[1] == '..activate':
        if len(sys.argv) != 4:
            raise ArgumentError("..activate expected exactly two arguments: shell and env name")
        binpath = binpath_from_arg(sys.argv[3], shelldict=shelldict)

        # prepend our new entries onto the existing path and make sure that the separator is native
        path = shelldict['pathsep'].join(binpath)

    # deactivation is handled completely in shell scripts - it restores backups of env variables.
    #    It is done in shell scripts because they handle state much better than we can here.

    elif sys.argv[1] == '..checkenv':
        if len(sys.argv) < 4:
            raise ArgumentError("Invalid arguments to checkenv.  Need shell and env name/path")
        if len(sys.argv) > 4:
            raise ArgumentError("did not expect more than one argument.")
        if sys.argv[3].lower() == ROOT_ENV_NAME.lower():
            # no need to check root env and try to install a symlink there
            sys.exit(0)
            # raise CondaSystemExit

        # this should throw an error and exit if the env or path can't be found.
        try:
            prefix = prefix_from_arg(sys.argv[3], shelldict=shelldict)
        except ValueError as e:
            raise CondaValueError(e)

        # Make sure an env always has the conda symlink
        try:
            import conda.install
            conda.install.symlink_conda(prefix, context.root_dir, shell)
        except (IOError, OSError) as e:
            if e.errno == errno.EPERM or e.errno == errno.EACCES:
                msg = ("Cannot activate environment {0}.\n"
                       "User does not have write access for conda symlinks."
                       .format(sys.argv[2]))
                raise CondaEnvironmentError(msg, e)
            raise
        sys.exit(0)
        # raise CondaSystemExit
    elif sys.argv[1] == '..changeps1':
        from conda.base.context import context
        path = int(context.changeps1)

    else:
        # This means there is a bug in main.py
        raise CondaValueError("unexpected command")

    # This print is actually what sets the PATH or PROMPT variable.  The shell
    # script gets this value, and finishes the job.
    print(path)
Ejemplo n.º 2
0
def main():
    from conda.base.context import context
    from conda.base.constants import ROOT_ENV_NAME
    from conda.utils import shells

    # possible cases:
    #   > conda ..activate <SHELL> <ENV>
    #   > conda ..checkenv <SHELL> <ENV>
    #   > conda ..changeps1

    received = len(sys.argv)
    if received >= 2:
        mode = sys.argv[1].strip()
    if received >= 3:
        shell = sys.argv[2].strip()
    if received >= 4:
        env = sys.argv[3].strip()

    if '-h' in sys.argv or '--help' in sys.argv:
        # all unknown values will be listed after the -h/--help flag
        try:
            i = sys.argv.index("-h")
        except ValueError:
            i = sys.argv.index("--help")
        unknown = list(map(lambda s: s.strip(), sys.argv[i+1:]))

        help(mode, shell, unknown)
        # note: will never return from the help method

    if mode == '..activate':
        # conda ..activate <SHELL> <ENV>

        # don't count conda and ..activate
        received -= 2
        if received != 2:
            expected = 2
            offset = sys.argv[2:]
            opt_msg = "..activate expected exactly two arguments: SHELL and ENV"
            if received < 2:
                raise TooFewArgumentsError(expected, received, opt_msg)
            if received > 2:
                raise TooManyArgumentsError(expected, received, offset, opt_msg)

        binpath = binpath_from_arg(sys.argv[3], shelldict=shells[shell])

        # prepend our new entries onto the existing path and make sure that
        # the separator is native
        path = shells[shell]['path_delim'].join(binpath)

    # deactivation is handled completely in shell scripts - it restores backups
    # of env variables it is done in shell scripts because they handle state
    # much better than we can here

    elif mode == '..checkenv':
        # conda ..checkenv <SHELL> <ENV>

        # dont't count conda and ..checkenv
        received -= 2
        if received != 2:
            expected = 2
            offset = sys.argv[2:]
            opt_msg = "..checkenv expected exactly two arguments: SHELL and ENV"
            if received < 2:
                raise TooFewArgumentsError(expected, received, opt_msg)
            if received > 2:
                raise TooManyArgumentsError(expected, received, offset, opt_msg)

        if env.lower() == ROOT_ENV_NAME.lower():
            # no need to check root env and try to install a symlink there
            sys.exit(0)
            # raise CondaSystemExit

        # this should throw an error and exit if the env or path can't be found.
        try:
            prefix = prefix_from_arg(env, shelldict=shells[shell])
        except ValueError as e:
            raise CondaValueError(text_type(e))

        # Make sure an env always has the conda symlink
        try:
            import conda.install
            conda.install.symlink_conda(prefix, context.root_dir, shell)
        except (IOError, OSError) as e:
            if e.errno == errno.EPERM or e.errno == errno.EACCES:
                raise CondaEnvironmentError(dedent("""\
                    Cannot activate environment {env}
                    User does not have write access for conda symlinks
                    """).format(env=env))
            raise
        sys.exit(0)
        # raise CondaSystemExit
    elif mode == '..changeps1':
        from conda.base.context import context
        path = int(context.changeps1)

    else:
        # This means there is a bug in main.py
        raise CondaValueError("unexpected command")

    # This print is actually what sets the PATH or PROMPT variable. The shell
    # script gets this value, and finishes the job.
    #
    # Must use sys.stdout.write(str(path)) to properly write to the console
    # cross platform, print(path) incorrectly prints integers on Windows
    sys.stdout.write(str(path))
Ejemplo n.º 3
0
def main():
    from conda.base.context import context
    from conda.base.constants import ROOT_ENV_NAME
    from conda.utils import shells

    # possible cases:
    #   > conda ..activate <SHELL> <ENV>
    #   > conda ..checkenv <SHELL> <ENV>
    #   > conda ..changeps1

    received = len(sys.argv)
    if received >= 2:
        mode = sys.argv[1].strip()
    if received >= 3:
        shell = sys.argv[2].strip()
    if received >= 4:
        env = sys.argv[3].strip()

    if '-h' in sys.argv or '--help' in sys.argv:
        # all unknown values will be listed after the -h/--help flag
        try:
            i = sys.argv.index("-h")
        except ValueError:
            i = sys.argv.index("--help")
        unknown = list(map(lambda s: s.strip(), sys.argv[i+1:]))

        help(mode, shell, unknown)
        # note: will never return from the help method

    if mode == '..activate':
        # conda ..activate <SHELL> <ENV>

        # don't count conda and ..activate
        received -= 2
        if received != 2:
            expected = 2
            offset = sys.argv[2:]
            opt_msg = "..activate expected exactly two arguments: SHELL and ENV"
            if received < 2:
                raise TooFewArgumentsError(expected, received, opt_msg)
            if received > 2:
                raise TooManyArgumentsError(expected, received, offset, opt_msg)

        binpath = binpath_from_arg(sys.argv[3], shelldict=shells[shell])

        # prepend our new entries onto the existing path and make sure that
        # the separator is native
        path = shells[shell]['path_delim'].join(binpath)

    # deactivation is handled completely in shell scripts - it restores backups
    # of env variables it is done in shell scripts because they handle state
    # much better than we can here

    elif mode == '..checkenv':
        # conda ..checkenv <SHELL> <ENV>

        # don't count conda and ..checkenv
        received -= 2
        if received != 2:
            expected = 2
            offset = sys.argv[2:]
            opt_msg = "..checkenv expected exactly two arguments: SHELL and ENV"
            if received < 2:
                raise TooFewArgumentsError(expected, received, opt_msg)
            if received > 2:
                raise TooManyArgumentsError(expected, received, offset, opt_msg)

        if env.lower() == ROOT_ENV_NAME.lower():
            # no need to check root env and try to install a symlink there
            sys.exit(0)
            # raise CondaSystemExit

        # this should throw an error and exit if the env or path can't be found.
        try:
            prefix = prefix_from_arg(env, shelldict=shells[shell])
        except ValueError as e:
            raise CondaValueError(text_type(e))

        # Make sure an env always has the conda symlink
        try:
            import conda.install
            conda.install.symlink_conda(prefix, context.root_dir, shell)
        except (IOError, OSError) as e:
            if e.errno == errno.EPERM or e.errno == errno.EACCES:
                raise CondaEnvironmentError(dedent("""\
                    Cannot activate environment {env}
                    User does not have write access for conda symlinks
                    """).format(env=env))
            raise
        sys.exit(0)
        # raise CondaSystemExit
    elif mode == '..changeps1':
        from conda.base.context import context
        path = int(context.changeps1)

    else:
        # This means there is a bug in main.py
        raise CondaValueError("unexpected command")

    # This print is actually what sets the PATH or PROMPT variable. The shell
    # script gets this value, and finishes the job.
    #
    # Must use sys.stdout.write(str(path)) to properly write to the console
    # cross platform, print(path) incorrectly prints integers on Windows
    sys.stdout.write(str(path))
Ejemplo n.º 4
0
def main():
    from conda.base.context import context
    from conda.base.constants import ROOT_ENV_NAME
    from conda.utils import shells
    if '-h' in sys.argv or '--help' in sys.argv:
        # all execution paths sys.exit at end.
        help(sys.argv[1], sys.argv[2])

    if len(sys.argv) > 2:
        shell = sys.argv[2]
        shelldict = shells[shell]
    if sys.argv[1] == '..activate':
        if len(sys.argv) != 4:
            raise ArgumentError(
                "..activate expected exactly two arguments: shell and env name"
            )
        binpath = binpath_from_arg(sys.argv[3], shelldict=shelldict)

        # prepend our new entries onto the existing path and make sure that the separator is native
        path = shelldict['pathsep'].join(binpath)

    # deactivation is handled completely in shell scripts - it restores backups of env variables.
    #    It is done in shell scripts because they handle state much better than we can here.

    elif sys.argv[1] == '..checkenv':
        if len(sys.argv) < 4:
            raise ArgumentError(
                "Invalid arguments to checkenv.  Need shell and env name/path")
        if len(sys.argv) > 4:
            raise ArgumentError("did not expect more than one argument.")
        if sys.argv[3].lower() == ROOT_ENV_NAME.lower():
            # no need to check root env and try to install a symlink there
            sys.exit(0)
            # raise CondaSystemExit

        # this should throw an error and exit if the env or path can't be found.
        try:
            prefix = prefix_from_arg(sys.argv[3], shelldict=shelldict)
        except ValueError as e:
            raise CondaValueError(e)

        # Make sure an env always has the conda symlink
        try:
            import conda.install
            conda.install.symlink_conda(prefix, context.root_dir, shell)
        except (IOError, OSError) as e:
            if e.errno == errno.EPERM or e.errno == errno.EACCES:
                msg = ("Cannot activate environment {0}.\n"
                       "User does not have write access for conda symlinks.".
                       format(sys.argv[2]))
                raise CondaEnvironmentError(msg, e)
            raise
        sys.exit(0)
        # raise CondaSystemExit
    elif sys.argv[1] == '..changeps1':
        from conda.base.context import context
        path = int(context.changeps1)

    else:
        # This means there is a bug in main.py
        raise CondaValueError("unexpected command")

    # This print is actually what sets the PATH or PROMPT variable.  The shell
    # script gets this value, and finishes the job.
    print(path)
Ejemplo n.º 5
0
def main():
    from conda.base.constants import ROOT_ENV_NAME
    from conda.utils import shells
    if '-h' in sys.argv or '--help' in sys.argv:
        # all execution paths sys.exit at end.
        help(sys.argv[1], sys.argv[2])

    if len(sys.argv) > 2:
        shell = sys.argv[2]
        shelldict = shells[shell]
    else:
        shelldict = {}

    if sys.argv[1] == '..activate':
        print(get_activate_path(shelldict))
        sys.exit(0)

    elif sys.argv[1] == '..deactivate.path':
        import re
        activation_path = get_activate_path(shelldict)

        if os.getenv('_CONDA_HOLD'):
            new_path = re.sub(r'%s(:?)' % re.escape(activation_path),
                              r'CONDA_PATH_PLACEHOLDER\1',
                              os.environ[str('PATH')], 1)
        else:
            new_path = re.sub(r'%s(:?)' % re.escape(activation_path), r'',
                              os.environ[str('PATH')], 1)

        print(new_path)
        sys.exit(0)

    elif sys.argv[1] == '..checkenv':
        if len(sys.argv) < 4:
            raise ArgumentError(
                "Invalid arguments to checkenv.  Need shell and env name/path")
        if len(sys.argv) > 4:
            raise ArgumentError("did not expect more than one argument.")
        if sys.argv[3].lower() == ROOT_ENV_NAME.lower():
            # no need to check root env and try to install a symlink there
            sys.exit(0)
            # raise CondaSystemExit

        # this should throw an error and exit if the env or path can't be found.
        try:
            prefix = prefix_from_arg(sys.argv[3], shelldict=shelldict)
        except ValueError as e:
            raise CondaValueError(text_type(e))

        # Make sure an env always has the conda symlink
        try:
            from conda.base.context import context
            import conda.install
            conda.install.symlink_conda(prefix, context.root_prefix, shell)
        except (IOError, OSError) as e:
            if e.errno == errno.EPERM or e.errno == errno.EACCES:
                msg = ("Cannot activate environment {0}.\n"
                       "User does not have write access for conda symlinks.".
                       format(sys.argv[2]))
                raise CondaEnvironmentError(msg)
            raise
        sys.exit(0)
        # raise CondaSystemExit
    elif sys.argv[1] == '..changeps1':
        from conda.base.context import context
        path = int(context.changeps1)

    else:
        # This means there is a bug in main.py
        raise CondaValueError("unexpected command")

    # This print is actually what sets the PATH or PROMPT variable.  The shell
    # script gets this value, and finishes the job.
    print(path)