Repository: conda/conda

Issue:
activate on master is broken in OSX
@msarahan I'm on master, and I get:

```
$ source activate koo
path:usage: conda [-h] [-V] [--debug] command ...
conda: error: argument command: invalid choice: '..changeps1' (choose from 'info', 'help', 'list', 'search', 'create', 'install', 'update', 'upgrade', 'remove', 'uninstall', 'run', 'config', 'init', 'clean', 'package', 'bundle')
prepending /Users/ilan/python/envs/koo and /Users/ilan/python/envs/koo/cmd and /Users/ilan/python/envs/koo/bin to PATH
-bash: awk: command not found
-bash: dirname: command not found
Traceback (most recent call last):
  File "/Users/ilan/python/bin/conda", line 4, in <module>
    from conda.cli.main import main
  File "/Users/ilan/conda/conda/__init__.py", line 19, in <module>
    __version__ = get_version(__file__, __name__)
  File "/Users/ilan/python/lib/python2.7/site-packages/auxlib/packaging.py", line 93, in get_version
    if is_git_repo(here):
  File "/Users/ilan/python/lib/python2.7/site-packages/auxlib/packaging.py", line 70, in is_git_repo
    return call(('git', 'rev-parse'), cwd=path) == 0
  File "/Users/ilan/python/lib/python2.7/subprocess.py", line 522, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/Users/ilan/python/lib/python2.7/subprocess.py", line 710, in __init__
    errread, errwrite)
  File "/Users/ilan/python/lib/python2.7/subprocess.py", line 1335, in _execute_child
    raise child_exception
```

It is true that the new activate scripts depend on `awk` and `dirname`?



Discussion:
> the new activate scripts

The usage of awk and dirname are not new:

https://github.com/conda/conda-env/blame/develop/bin/activate#L32

That's conda-env, of course, but that's where the scripts that are now in conda came from.

Something else is going wrong.  It might be a bug in the path translation stuff.  It is supposed to just be a pass-through on *nix to *nix, but I did see some issues like this in fixing the tests.  I'll look into it and see if I can improve the tests to catch whatever is happening here.


Resolving patch:
diff --git a/conda/cli/activate.py b/conda/cli/activate.py
--- a/conda/cli/activate.py
+++ b/conda/cli/activate.py
@@ -124,10 +124,13 @@ def main():
             if rootpath:
                 path = path.replace(translate_stream(rootpath, win_path_to_cygwin), "")
         else:
-            path = translate_stream(path, win_path_to_unix)
+            if sys.platform == 'win32':
+                path = translate_stream(path, win_path_to_unix)
+                if rootpath:
+                    rootpath = translate_stream(rootpath, win_path_to_unix)
             # Clear the root path if it is present
             if rootpath:
-                path = path.replace(translate_stream(rootpath, win_path_to_unix), "")
+                path = path.replace(rootpath, "")
 
 
     elif sys.argv[1] == '..deactivate':
diff --git a/conda/install.py b/conda/install.py
--- a/conda/install.py
+++ b/conda/install.py
@@ -66,11 +66,12 @@ def win_path_to_unix(path, root_prefix=""):
 
         Does not add cygdrive.  If you need that, set root_prefix to "/cygdrive"
         """
-        import re
-        path_re = '[a-zA-Z]:[/\\\\]+(?:[^:*?"<>|]+[\/\\\\]+)*[^:*?"<>|;/\\\\]*'
-        converted_paths = [root_prefix + "/" + _path.replace("\\", "/").replace(":", "")
-                        for _path in re.findall(path_re, path)]
-        return ":".join(converted_paths)
+        path_re = '(?<![:/^a-zA-Z])([a-zA-Z]:[\/\\\\]+(?:[^:*?"<>|]+[\/\\\\]+)*[^:*?"<>|;\/\\\\]+?(?![a-zA-Z]:))'
+        translation = lambda found_path: root_prefix + "/" + found_path.groups()[0].replace("\\", "/")\
+            .replace(":", "")
+        translation = re.sub(path_re, translation, path)
+        translation = translation.replace(";/", ":/")
+        return translation
 
 
 on_win = bool(sys.platform == "win32")
diff --git a/conda/utils.py b/conda/utils.py
--- a/conda/utils.py
+++ b/conda/utils.py
@@ -81,10 +81,12 @@ def win_path_to_unix(path, root_prefix=""):
 
     Does not add cygdrive.  If you need that, set root_prefix to "/cygdrive"
     """
-    path_re = '(?<![:/])([a-zA-Z]:[\/\\\\]+(?:[^:*?"<>|]+[\/\\\\]+)*[^:*?"<>|;\/\\\\]+?(?![a-zA-Z]:))'
+    path_re = '(?<![:/^a-zA-Z])([a-zA-Z]:[\/\\\\]+(?:[^:*?"<>|]+[\/\\\\]+)*[^:*?"<>|;\/\\\\]+?(?![a-zA-Z]:))'
     translation = lambda found_path: root_prefix + "/" + found_path.groups()[0].replace("\\", "/")\
-        .replace(":", "").replace(";", ":")
-    return re.sub(path_re, translation, path)
+        .replace(":", "")
+    translation = re.sub(path_re, translation, path)
+    translation = translation.replace(";/", ":/")
+    return translation
 
 
 def unix_path_to_win(path, root_prefix=""):
@@ -100,7 +102,7 @@ def unix_path_to_win(path, root_prefix=""):
     translation = lambda found_path: found_path.group(0)[len(root_prefix)+1] + ":" + \
                   found_path.group(0)[len(root_prefix)+2:].replace("/", "\\")
     translation = re.sub(path_re, translation, path)
-    translation = re.sub(":([a-zA-Z]):", lambda match: ";" + match.group(0)[1] + ":", translation)
+    translation = re.sub(":?([a-zA-Z]):\\\\", lambda match: ";" + match.group(0)[1] + ":\\", translation)
     return translation
 
 


Test patch:
diff --git a/tests/test_activate.py b/tests/test_activate.py
--- a/tests/test_activate.py
+++ b/tests/test_activate.py
@@ -226,12 +226,12 @@ def _format_vars(shell):
 
 
 def test_path_translation():
-    test_cygwin_path = "/usr/bin:/cygdrive/z/documents (x86)/code/conda/tests/envskhkzts/test1:/cygdrive/z/documents/code/conda/tests/envskhkzts/test1/cmd"
-    test_unix_path = "/usr/bin:/z/documents (x86)/code/conda/tests/envskhkzts/test1:/z/documents/code/conda/tests/envskhkzts/test1/cmd"
-    test_win_path = "z:\\documents (x86)\\code\\conda\\tests\\envskhkzts\\test1;z:\\documents\\code\\conda\\tests\\envskhkzts\\test1\\cmd"
+    test_cygwin_path = "test dummy text /usr/bin:/cygdrive/z/documents (x86)/code/conda/tests/envskhkzts/test1:/cygdrive/z/documents/code/conda/tests/envskhkzts/test1/cmd more dummy text"
+    test_unix_path = "test dummy text /usr/bin:/z/documents (x86)/code/conda/tests/envskhkzts/test1:/z/documents/code/conda/tests/envskhkzts/test1/cmd more dummy text"
+    test_win_path = "test dummy text /usr/bin;z:\\documents (x86)\\code\\conda\\tests\\envskhkzts\\test1;z:\\documents\\code\\conda\\tests\\envskhkzts\\test1\\cmd more dummy text"
     assert_equals(test_win_path, unix_path_to_win(test_unix_path))
-    assert_equals(test_unix_path.replace("/usr/bin:", ""), win_path_to_unix(test_win_path))
-    assert_equals(test_cygwin_path.replace("/usr/bin:", ""), win_path_to_cygwin(test_win_path))
+    assert_equals(test_unix_path, win_path_to_unix(test_win_path))
+    assert_equals(test_cygwin_path, win_path_to_cygwin(test_win_path))
     assert_equals(test_win_path, cygwin_path_to_win(test_cygwin_path))