def health_check(prefix: Prefix, language_version: str) -> str | None: directory = helpers.environment_dir(ENVIRONMENT_DIR, language_version) envdir = prefix.path(directory) pyvenv_cfg = os.path.join(envdir, 'pyvenv.cfg') # created with "old" virtualenv if not os.path.exists(pyvenv_cfg): return 'pyvenv.cfg does not exist (old virtualenv?)' exe_name = win_exe('python') py_exe = prefix.path(bin_dir(envdir), exe_name) cfg = _read_pyvenv_cfg(pyvenv_cfg) if 'version_info' not in cfg: return "created virtualenv's pyvenv.cfg is missing `version_info`" # always use uncached lookup here in case we replaced an unhealthy env virtualenv_version = _version_info.__wrapped__(py_exe) if virtualenv_version != cfg['version_info']: return (f'virtualenv python version did not match created version:\n' f'- actual version: {virtualenv_version}\n' f'- expected version: {cfg["version_info"]}\n') # made with an older version of virtualenv? skip `base-executable` check if 'base-executable' not in cfg: return None base_exe_version = _version_info(cfg['base-executable']) if base_exe_version != cfg['version_info']: return ( f'base executable python version does not match created version:\n' f'- base-executable version: {base_exe_version}\n' f'- expected version: {cfg["version_info"]}\n') else: return None
def test_unhealthy_then_replaced(python_dir): prefix, tmpdir = python_dir python.install_environment(prefix, C.DEFAULT, ()) # simulate an exe which returns an old version exe_name = win_exe('python') py_exe = prefix.path(python.bin_dir('py_env-default'), exe_name) os.rename(py_exe, f'{py_exe}.tmp') with open(py_exe, 'w') as f: f.write('#!/usr/bin/env bash\necho 1.2.3\n') make_executable(py_exe) # should be unhealthy due to version mismatch ret = python.health_check(prefix, C.DEFAULT) assert ret == ( f'virtualenv python version did not match created version:\n' f'- actual version: 1.2.3\n' f'- expected version: {python._version_info(sys.executable)}\n') # now put the exe back and it should be healthy again os.replace(f'{py_exe}.tmp', py_exe) assert python.health_check(prefix, C.DEFAULT) is None
def test_unhealthy_python_goes_missing(python_dir): prefix, tmpdir = python_dir python.install_environment(prefix, C.DEFAULT, ()) exe_name = win_exe('python') py_exe = prefix.path(python.bin_dir('py_env-default'), exe_name) os.remove(py_exe) assert python.healthy(prefix, C.DEFAULT) is False
def test_unhealthy_python_goes_missing(python_dir): prefix, tmpdir = python_dir python.install_environment(prefix, C.DEFAULT, ()) exe_name = win_exe('python') py_exe = prefix.path(python.bin_dir('py_env-default'), exe_name) os.remove(py_exe) ret = python.health_check(prefix, C.DEFAULT) assert ret == ( f'virtualenv python version did not match created version:\n' f'- actual version: <<error retrieving version from {py_exe}>>\n' f'- expected version: {python._version_info(sys.executable)}\n')
def healthy(prefix: Prefix, language_version: str) -> bool: directory = helpers.environment_dir(ENVIRONMENT_DIR, language_version) envdir = prefix.path(directory) pyvenv_cfg = os.path.join(envdir, 'pyvenv.cfg') # created with "old" virtualenv if not os.path.exists(pyvenv_cfg): return False exe_name = win_exe('python') py_exe = prefix.path(bin_dir(envdir), exe_name) cfg = _read_pyvenv_cfg(pyvenv_cfg) return ( 'version_info' in cfg and # always use uncached lookup here in case we replaced an unhealthy env _version_info.__wrapped__(py_exe) == cfg['version_info'] and ('base-executable' not in cfg or _version_info(cfg['base-executable']) == cfg['version_info']))
def test_unhealthy_then_replaced(python_dir): prefix, tmpdir = python_dir python.install_environment(prefix, C.DEFAULT, ()) # simulate an exe which returns an old version exe_name = win_exe('python') py_exe = prefix.path(python.bin_dir('py_env-default'), exe_name) os.rename(py_exe, f'{py_exe}.tmp') with open(py_exe, 'w') as f: f.write('#!/usr/bin/env bash\necho 1.2.3\n') make_executable(py_exe) # should be unhealthy due to version mismatch assert python.healthy(prefix, C.DEFAULT) is False # now put the exe back and it should be healthy again os.replace(f'{py_exe}.tmp', py_exe) assert python.healthy(prefix, C.DEFAULT) is True
def _install_dir(prefix_p: Prefix, pub_cache: str) -> None: dart_env = {**os.environ, 'PUB_CACHE': pub_cache} with open(prefix_p.path('pubspec.yaml')) as f: pubspec_contents = yaml_load(f) helpers.run_setup_cmd(prefix_p, ('dart', 'pub', 'get'), env=dart_env) for executable in pubspec_contents['executables']: helpers.run_setup_cmd( prefix_p, ( 'dart', 'compile', 'exe', '--output', os.path.join(bin_dir, win_exe(executable)), prefix_p.path('bin', f'{executable}.dart'), ), env=dart_env, )