1616
1717
1818ENVIRONMENT_DIR = 'py_env'
19+ HEALTH_MODS = ('datetime' , 'io' , 'os' , 'ssl' , 'weakref' )
1920
2021
2122def bin_dir (venv ):
@@ -32,15 +33,6 @@ def get_env_patch(venv):
3233 )
3334
3435
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-
4436def _find_by_py_launcher (version ): # pragma: no cover (windows only)
4537 if version .startswith ('python' ):
4638 try :
@@ -98,15 +90,6 @@ def get_default_version():
9890 return get_default_version ()
9991
10092
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-
11093def norm_version (version ):
11194 if os .name == 'nt' : # pragma: no cover (windows)
11295 # Try looking up by name
@@ -123,30 +106,53 @@ def norm_version(version):
123106 if version .startswith ('python' ):
124107 return r'C:\{}\python.exe' .format (version .replace ('.' , '' ))
125108
126- # Otherwise assume it is a path
109+ # Otherwise assume it is a path
127110 return os .path .expanduser (version )
128111
129112
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 ,
113+ def py_interface (_dir , _make_venv ):
114+ @contextlib .contextmanager
115+ def in_env (prefix , language_version ):
116+ envdir = prefix .path (helpers .environment_dir (_dir , language_version ))
117+ with envcontext (get_env_patch (envdir )):
118+ yield
119+
120+ def healthy (prefix , language_version ):
121+ with in_env (prefix , language_version ):
122+ retcode , _ , _ = cmd_output (
123+ 'python' , '-c' , 'import {}' .format (',' .join (HEALTH_MODS )),
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