Created
August 26, 2020 17:02
-
-
Save globau/3bd60d41fe44a97b24c90ddbafbe0451 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def venv_broken(self, *, full_check=False): | |
# check if a venv is broken (eg. after a python upgrade) | |
# full_check validates the #! line of each bin/ script | |
logger.debug("checking venv %s", self.venv_path) | |
bin_path = self.venv_path / "bin" | |
for bin_name in ("python3", "pip3"): | |
if not (bin_path / bin_name).exists(): | |
logger.debug("%s not found", bin_path / bin_name) | |
return True | |
p = process.run( | |
[bin_path / bin_name, "--version"], | |
check=False, | |
capture_stdout=True, | |
capture_stderr=True, | |
) | |
if p.returncode != 0: | |
return True | |
if not full_check: | |
return False | |
for script_filename in [ | |
f for f in bin_path.glob("*") if f.is_file() and is_executable(f) | |
]: | |
try: | |
with script_filename.open("rb") as f: | |
block = f.read(80) | |
block.index(10) | |
first_line = block.splitlines()[0].decode("utf8") | |
if first_line.startswith("#!"): | |
executable = Path(first_line.split(" ")[0][2:]) | |
if not executable.exists(): | |
return True | |
except (IOError, ValueError): | |
pass | |
return False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment