Skip to content

Commit

Permalink
fix bugs on conda env
Browse files Browse the repository at this point in the history
  • Loading branch information
kalefranz authored and HugoTian committed Jul 27, 2016
1 parent 22261d5 commit 043ce41
Show file tree
Hide file tree
Showing 63 changed files with 116 additions and 35 deletions.
2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
include CHANGELOG.txt
include CHANGELOG.md
include LICENSE.txt
include MANIFEST.in
include README.rst
Expand Down
2 changes: 2 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ install:
- conda install -q pytest requests mock
- conda install -q pycrypto pyflakes pycosat
- conda install -q git menuinst
- conda install yaml
- conda install anaconda-client
- conda install -q enum34 || WHOAMI
- pip install flake8 pytest-cov pytest-timeout responses
- python --version
Expand Down
1 change: 0 additions & 1 deletion conda-env/tests/support/notebook.ipynb

This file was deleted.

5 changes: 4 additions & 1 deletion conda/base/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,10 @@ def get_prefix(ctx, args, search=True):
if args.name == ROOT_ENV_NAME:
return ctx.root_dir
if search:
prefix = find_prefix_name(ctx, args.name)
if getattr(args, 'clone', False):
prefix = find_prefix_name(ctx, args.clone)
else:
prefix = find_prefix_name(ctx, args.name)
if prefix:
return prefix
return join(ctx.envs_dirs[0], args.name)
Expand Down
1 change: 1 addition & 0 deletions conda/cli/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,3 +568,4 @@ def disp_env(prefix):

if output:
print()

1 change: 0 additions & 1 deletion conda/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,5 @@ def completer(prefix, **kwargs):
def main():
return conda_exception_handler(_main)


if __name__ == '__main__':
main()
File renamed without changes.
File renamed without changes.
68 changes: 68 additions & 0 deletions conda_env/cli/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import sys
from os.path import abspath, join, isdir, expanduser
from conda.config import root_dir, default_prefix
import os
from conda.base.context import context

root_env_name = 'root'
envs_dirs = context.envs_dirs
def stdout_json(d):
import json

json.dump(d, sys.stdout, indent=2, sort_keys=True)
sys.stdout.write('\n')


def error_and_exit(message, json=False, newline=False, error_text=True,
error_type=None):
"""
Function used in conda info
"""
if json:
stdout_json(dict(error=message, error_type=error_type))
sys.exit(1)
else:
if newline:
print()

if error_text:
sys.exit("Error: " + message)
else:
sys.exit(message)


def exception_and_exit(exc, **kwargs):
if 'error_type' not in kwargs:
kwargs['error_type'] = exc.__class__.__name__
error_and_exit('; '.join(map(str, exc.args)), **kwargs)


def get_prefix(args, search=True):
if args.name:
if '/' in args.name:
error_and_exit("'/' not allowed in environment name: %s" %
args.name,
json=getattr(args, 'json', False),
error_type="ValueError")
if args.name == root_env_name:
return root_dir
if search:
prefix = find_prefix_name(args.name)
if prefix:
return prefix
return join(envs_dirs[0], args.name)

if args.prefix:
return abspath(expanduser(args.prefix))

return default_prefix

def find_prefix_name(name):
if name == root_env_name:
return root_dir
# always search cwd in addition to envs dirs (for relative path access)
for envs_dir in envs_dirs + [os.getcwd(), ]:
prefix = join(envs_dir, name)
if isdir(prefix):
return prefix
return None
4 changes: 2 additions & 2 deletions conda-env/conda_env/cli/main.py → conda_env/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import sys

try:
from conda.cli.main import args_func
from conda.exceptions import conda_exception_handler
except ImportError as e:
if 'CONDA_DEFAULT_ENV' in os.environ:
sys.stderr.write("""
Expand Down Expand Up @@ -65,7 +65,7 @@ def create_parser():
def main():
parser = create_parser()
args = parser.parse_args()
return args_func(args, parser)
return conda_exception_handler(args.func, args, parser)


if __name__ == '__main__':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from ..utils.notebooks import Notebook
from conda.cli import common
from ..env import from_environment


# conda env import
from conda_env.cli.common import get_prefix
description = """
Embeds information describing your conda environment
into the notebook metadata
Expand Down Expand Up @@ -70,7 +70,7 @@ def configure_parser(sub_parsers):
def execute(args, parser):

if args.prefix is None:
prefix = common.get_prefix(args)
prefix = get_prefix(args)
else:
prefix = args.prefix

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import sys
import textwrap


from conda.cli import common
from conda.cli import install as cli_install
from conda.install import rm_rf
Expand All @@ -14,6 +15,9 @@
from .. import exceptions
from .. import specs

# for conda env import
from conda_env.cli.common import error_and_exit, get_prefix

description = """
Create an environment based on an environment file
"""
Expand All @@ -28,6 +32,7 @@
conda env create -f=/path/to/requirements.txt -p /home/user/software/deathstar
"""


def configure_parser(sub_parsers):
p = sub_parsers.add_parser(
'create',
Expand Down Expand Up @@ -77,13 +82,14 @@ def execute(args, parser):
env = spec.environment

# FIXME conda code currently requires args to have a name or prefix
if args.prefix is None:
# don't overwrite name if it's given. gh-254
if args.prefix is None and args.name is None:
args.name = env.name

except exceptions.SpecNotFound as e:
common.error_and_exit(str(e), json=args.json)
error_and_exit(str(e), json=args.json)

prefix = common.get_prefix(args, search=False)
prefix = get_prefix(args, search=False)

if args.force and not is_root_prefix(prefix) and os.path.exists(prefix):
rm_rf(prefix)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
import os
import textwrap
from argparse import RawDescriptionHelpFormatter

from conda import config
from conda.cli import common

from ..env import from_environment

# conda env import
from conda_env.cli.common import error_and_exit, get_prefix
description = """
Export a given environment
"""
Expand Down Expand Up @@ -79,11 +77,11 @@ def execute(args, parser):
* Provide an environment name via --name or -n
* Re-run this command inside an activated conda environment.""").lstrip()
# TODO Add json support
common.error_and_exit(msg, json=False)
error_and_exit(msg, json=False)
args.name = name
else:
name = args.name
prefix = common.get_prefix(args)
prefix = get_prefix(args)
env = from_environment(name, prefix, no_builds=args.no_builds)

if args.override_channels:
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
from conda.cli import common
from conda.cli import install as cli_install
from conda.misc import touch_nonadmin

from ..installers.base import get_installer, InvalidInstaller
from .. import specs as install_specs
from .. import exceptions

# for conda env
from conda_env.cli.common import error_and_exit, get_prefix
description = """
Update the current environment based on environment file
"""
Expand Down Expand Up @@ -76,7 +76,7 @@ def execute(args, parser):
directory=os.getcwd())
env = spec.environment
except exceptions.SpecNotFound as e:
common.error_and_exit(str(e), json=args.json)
error_and_exit(str(e), json=args.json)

if not args.name:
if not env.name:
Expand All @@ -91,14 +91,14 @@ def execute(args, parser):
* Provide an environment name via --name or -n
* Re-run this command inside an activated conda environment.""").lstrip()
# TODO Add json support
common.error_and_exit(msg, json=False)
error_and_exit(msg, json=False)

# Note: stubbing out the args object as all of the
# conda.cli.common code thinks that name will always
# be specified.
args.name = env.name

prefix = common.get_prefix(args, search=False)
prefix = get_prefix(args, search=False)
# CAN'T Check with this function since it assumes we will create prefix.
# cli_install.check_prefix(prefix, json=args.json)

Expand Down
File renamed without changes.
File renamed without changes.
7 changes: 4 additions & 3 deletions conda-env/conda_env/env.py → conda_env/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from conda.cli import common
from conda import install
from conda import config
from conda.base import context

from . import compat
from . import exceptions
Expand Down Expand Up @@ -50,8 +50,9 @@ def from_environment(name, prefix, no_builds=False):
dependencies = ['='.join(a.rsplit('-', 2)) for a in sorted(conda_pkgs)]
if len(pip_pkgs) > 0:
dependencies.append({'pip': ['=='.join(a.rsplit('-', 2)[:2]) for a in pip_pkgs]})

channels = config.get_rc_urls()
# conda uses ruamel_yaml which returns a ruamel_yaml.comments.CommentedSeq
# this doesn't dump correctly using pyyaml
channels = context.channels

return Environment(name=name, dependencies=dependencies, channels=channels, prefix=prefix)

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from __future__ import absolute_import
import subprocess

from conda.cli import common
from conda_env.pip_util import pip_args
from conda_env.cli import common


def install(prefix, specs, args, env, prune=False):
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ markers =
[pep8]
max-line-length = 99
ignore = E116,E121,E123,E126,E133,E226,E241,E242,E302,E704
exclude = build/*,.tox/*,tests/*,ve/*,*/_vendor/*,conda/compat.py,conda-env/*
exclude = build/*,.tox/*,tests/*,ve/*,*/_vendor/*,conda/compat.py,conda-env/*, conda_env/*


[flake8]
max-line-length = 99
ignore = E116,E121,E123,E126,E133,E226,E241,E242,E302,E704
exclude = build/*,.tox/*,tests/*,ve/*,*/_vendor/*,conda/compat.py,conda-env/*
exclude = build/*,.tox/*,tests/*,ve/*,*/_vendor/*,conda/compat.py,conda-env/*, conda_env/*


[coverage:run]
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@
"Programming Language :: Python :: 3.4",
"Programming Language :: Python :: 3.5",
],
packages=conda._vendor.auxlib.packaging.find_packages(exclude=("tests", "conda-env",
"build", "utils", ".tox")),
packages=conda._vendor.auxlib.packaging.find_packages(exclude=("tests", "build",
"utils", ".tox")),
cmdclass={
'build_py': conda._vendor.auxlib.BuildPyCommand,
'sdist': conda._vendor.auxlib.SDistCommand,
Expand Down
File renamed without changes.
5 changes: 5 additions & 0 deletions conda-env/tests/cli.py → tests/conda_env/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ def test_update(self):
parsed = json.loads(o)
self.assertNotEqual(len(parsed), 0)

def test_name(self):
# smoke test for gh-254
create_env(environment_1)
o, e, s = run('conda env create -n new-env create')
self.assertStatusOk(s)

if __name__ == '__main__':
unittest.main()
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions tests/conda_env/support/notebook.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"nbformat": 3, "nbformat_minor": 0, "worksheets": [{"cells": []}], "metadata": {"name": "", "signature": ""}}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 1 addition & 2 deletions tests/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,9 +372,8 @@ def test_remove_all(self):
assert not exists(prefix)

@pytest.mark.skipif(on_win and bits == 32, reason="no 32-bit windows python on conda-forge")
@pytest.mark.xfail(reason="pending resolution of #2926")
@pytest.mark.timeout(600)
def ash_c_usage_replacing_python(self):
def test_dash_c_usage_replacing_python(self):
# Regression test for #2606
with make_temp_env("-c conda-forge python=3.5") as prefix:
assert exists(join(prefix, PYTHON_BINARY))
Expand Down
2 changes: 1 addition & 1 deletion utils/travis-run-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ flake8_extras() {


test_extras() {
python -m pip install -U mock pytest pytest-cov pytest-timeout radon responses
python -m pip install -U mock pytest pytest-cov pytest-timeout radon responses anaconda-client nbformat
}


Expand Down

0 comments on commit 043ce41

Please sign in to comment.