@@ -32,15 +32,6 @@ def get_env_patch(venv):
3232 )
3333
3434
35- @contextlib .contextmanager
36- def in_env (prefix , language_version ):
37- envdir = prefix .path (
38- helpers .environment_dir (ENVIRONMENT_DIR , language_version ),
39- )
40- with envcontext (get_env_patch (envdir )):
41- yield
42-
43-
4435def _find_by_py_launcher (version ): # pragma: no cover (windows only)
4536 if version .startswith ('python' ):
4637 try :
@@ -98,15 +89,6 @@ def get_default_version():
9889 return get_default_version ()
9990
10091
101- def healthy (prefix , language_version ):
102- with in_env (prefix , language_version ):
103- retcode , _ , _ = cmd_output (
104- 'python' , '-c' , 'import ctypes, datetime, io, os, ssl, weakref' ,
105- retcode = None ,
106- )
107- return retcode == 0
108-
109-
11092def norm_version (version ):
11193 if os .name == 'nt' : # pragma: no cover (windows)
11294 # Try looking up by name
@@ -123,30 +105,54 @@ def norm_version(version):
123105 if version .startswith ('python' ):
124106 return r'C:\{}\python.exe' .format (version .replace ('.' , '' ))
125107
126- # Otherwise assume it is a path
108+ # Otherwise assume it is a path
127109 return os .path .expanduser (version )
128110
129111
130- def install_environment (prefix , version , additional_dependencies ):
131- additional_dependencies = tuple (additional_dependencies )
132- directory = helpers .environment_dir (ENVIRONMENT_DIR , version )
133-
134- # Install a virtualenv
135- env_dir = prefix .path (directory )
136- with clean_path_on_failure (env_dir ):
137- venv_cmd = [sys .executable , '-m' , 'virtualenv' , env_dir ]
138- if version != 'default' :
139- venv_cmd .extend (['-p' , norm_version (version )])
140- else :
141- venv_cmd .extend (['-p' , os .path .realpath (sys .executable )])
142- venv_env = dict (os .environ , VIRTUALENV_NO_DOWNLOAD = '1' )
143- cmd_output (* venv_cmd , cwd = '/' , env = venv_env )
144- with in_env (prefix , version ):
145- helpers .run_setup_cmd (
146- prefix , ('pip' , 'install' , '.' ) + additional_dependencies ,
112+ def py_interface (_dir , _make_venv ):
113+ @contextlib .contextmanager
114+ def in_env (prefix , language_version ):
115+ envdir = prefix .path (helpers .environment_dir (_dir , language_version ))
116+ with envcontext (get_env_patch (envdir )):
117+ yield
118+
119+ def healthy (prefix , language_version ):
120+ with in_env (prefix , language_version ):
121+ retcode , _ , _ = cmd_output (
122+ 'python' , '-c' ,
123+ 'import ctypes, datetime, io, os, ssl, weakref' ,
124+ retcode = None ,
147125 )
126+ return retcode == 0
127+
128+ def run_hook (prefix , hook , file_args ):
129+ with in_env (prefix , hook ['language_version' ]):
130+ return xargs (helpers .to_cmd (hook ), file_args )
131+
132+ def install_environment (prefix , version , additional_dependencies ):
133+ additional_dependencies = tuple (additional_dependencies )
134+ directory = helpers .environment_dir (_dir , version )
135+
136+ env_dir = prefix .path (directory )
137+ with clean_path_on_failure (env_dir ):
138+ if version != 'default' :
139+ python = norm_version (version )
140+ else :
141+ python = os .path .realpath (sys .executable )
142+ _make_venv (env_dir , python )
143+ with in_env (prefix , version ):
144+ helpers .run_setup_cmd (
145+ prefix , ('pip' , 'install' , '.' ) + additional_dependencies ,
146+ )
147+
148+ return in_env , healthy , run_hook , install_environment
149+
150+
151+ def make_venv (envdir , python ):
152+ env = dict (os .environ , VIRTUALENV_NO_DOWNLOAD = '1' )
153+ cmd = (sys .executable , '-mvirtualenv' , envdir , '-p' , python )
154+ cmd_output (* cmd , env = env , cwd = '/' )
148155
149156
150- def run_hook (prefix , hook , file_args ):
151- with in_env (prefix , hook ['language_version' ]):
152- return xargs (helpers .to_cmd (hook ), file_args )
157+ _interface = py_interface (ENVIRONMENT_DIR , make_venv )
158+ in_env , healthy , run_hook , install_environment = _interface
0 commit comments