def is_venv_python(interpreter: os.PathLike) -> bool: """Check if the given interpreter path is from a virtualenv""" interpreter = Path(interpreter) if interpreter.parent.parent.joinpath("pyvenv.cfg").exists(): return True if os.getenv("VIRTUAL_ENV"): try: interpreter.relative_to(os.getenv("VIRTUAL_ENV")) except ValueError: pass else: return True return False
def encode_path(path: PathLike) -> str: ''' Convert a path-like object to a artifact-root-directory-relative path string (a string starting with "@/"). ''' root = active_root.get().resolve() dots: List[str] = [] path = Path(path).resolve() while root not in (*path.parents, path): root = root.parent dots.append('..') return '@/' + '/'.join(Path(*dots, path.relative_to(root)).parts)
def create_symlink(source_filename: PathLike, symlink_filename: PathLike, relative: bool = False): """ :param source_filename: path to point to :param symlink_filename: path at which to create symlink :param relative: make symlink relative to source location """ if not isinstance(source_filename, Path): source_filename = Path(source_filename) if not isinstance(symlink_filename, Path): symlink_filename = Path(symlink_filename) if symlink_filename.is_symlink(): logging.debug(f'removing symlink "{symlink_filename}"') os.remove(symlink_filename) symlink_filename = symlink_filename.parent.absolute().resolve( ) / symlink_filename.name starting_directory = None if relative: starting_directory = Path().cwd().resolve() os.chdir(symlink_filename.parent) if source_filename.is_absolute(): try: source_filename = source_filename.relative_to( symlink_filename.parent) except ValueError as error: warnings.warn(error) os.chdir(starting_directory) else: source_filename = source_filename.absolute() try: symlink_filename.symlink_to(source_filename) except Exception as error: warnings.warn(f'could not create symbolic link: {error}') shutil.copyfile(source_filename, symlink_filename) if starting_directory is not None: os.chdir(starting_directory)