1010import os
1111import sys
1212import traceback
13- from os .path import isfile , join , exists
13+ from os .path import isdir , isfile , join , exists
1414
1515# Install an exception hook which pops up a message box.
1616# Ideally, exceptions will get returned to NSIS and logged there,
@@ -41,12 +41,14 @@ def write(x):
4141 err = write
4242
4343
44- def mk_menus (remove = False ):
44+ def mk_menus (remove = False , prefix = None ):
4545 try :
4646 import menuinst
4747 except (ImportError , OSError ):
4848 return
49- menu_dir = join (sys .prefix , 'Menu' )
49+ if prefix is None :
50+ prefix = sys .prefix
51+ menu_dir = join (prefix , 'Menu' )
5052 if not os .path .isdir (menu_dir ):
5153 return
5254 pkg_names = [s .strip () for s in sys .argv [2 :]]
@@ -57,7 +59,7 @@ def mk_menus(remove=False):
5759 continue
5860 shortcut = join (menu_dir , fn )
5961 try :
60- menuinst .install (shortcut , remove )
62+ menuinst .install (shortcut , remove , prefix = prefix )
6163 except Exception as e :
6264 out ("Failed to process %s...\n " % shortcut )
6365 err ("Error: %s\n " % str (e ))
@@ -66,6 +68,60 @@ def mk_menus(remove=False):
6668 out ("Processed %s successfully.\n " % shortcut )
6769
6870
71+ def get_conda_envs_from_python_api ():
72+ try :
73+ from conda .cli .python_api import run_command , Commands
74+ except (ImportError , OSError ):
75+ return
76+ from json import loads
77+ c_stdout , c_stderr , return_code = run_command (Commands .INFO , "--json" )
78+ json_conda_info = loads (c_stdout )
79+ return json_conda_info ["envs" ]
80+
81+
82+ def get_conda_envs_from_libconda ():
83+ # alternative implementation using libconda
84+ # adapted from conda.misc.list_prefixes
85+ try :
86+ from libconda .config import envs_dirs
87+ except (ImportError , OSError ):
88+ return
89+ # Lists all the prefixes that conda knows about.
90+ for envs_dir in envs_dirs :
91+ if not isdir (envs_dir ):
92+ continue
93+ for dn in sorted (os .listdir (envs_dir )):
94+ if dn .startswith ('.' ):
95+ continue
96+ prefix = join (envs_dir , dn )
97+ if isdir (prefix ):
98+ prefix = join (envs_dir , dn )
99+ yield prefix
100+
101+
102+ get_conda_envs = get_conda_envs_from_python_api
103+
104+
105+ def rm_menus ():
106+ mk_menus (remove = True )
107+ try :
108+ import menuinst
109+ menuinst
110+ except (ImportError , OSError ):
111+ return
112+ try :
113+ envs = get_conda_envs ()
114+ envs = list (envs ) # make sure `envs` is iterable
115+ except Exception as e :
116+ out ("Failed to get conda environments list\n " )
117+ err ("Error: %s\n " % str (e ))
118+ err ("Traceback:\n %s\n " % traceback .format_exc (20 ))
119+ return
120+ for env in envs :
121+ env = str (env ) # force `str` so that `os.path.join` doesn't fail
122+ mk_menus (remove = True , prefix = env )
123+
124+
69125def run_post_install ():
70126 """
71127 call the post install script, if the file exists
@@ -115,7 +171,7 @@ def main():
115171 elif cmd == 'post_install' :
116172 run_post_install ()
117173 elif cmd == 'rmmenus' :
118- mk_menus ( remove = True )
174+ rm_menus ( )
119175 elif cmd == 'addpath' :
120176 add_to_path ()
121177 elif cmd == 'rmpath' :
0 commit comments