defl/defl/pyenv_.py
2024-09-11 11:14:03 -04:00

120 lines
4.6 KiB
Python

#!/usr/bin/env python #! depricate
#! depricate
import itertools #! depricate
import os #! depricate
import sys #! depricate
from dataclasses import dataclass #! depricate
#! depricate
import defl #! depricate
from defl import Assert, CLIError, Null, Path, Undefined, cl, log #! depricate
from defl._typing_ import * #! depricate
from defl._typing_ import false, none, true #! depricate
#! depricate
@dataclass(slots=True, kw_only=false, frozen=False) #! depricate
class PyVersion: #! depricate
# TODO generalize and format parse #! depricate
py: int #! depricate
major: int #! depricate
minor: int #! depricate
_asInt: int | defl.PropertyNotSet = defl.PropertyNotSet #! depricate
#! depricate
asInt = defl.propertyFunc('asInt') #! depricate
#! depricate
@classmethod #! depricate
def FromStr(cls, string: str) -> 'PyVersion': #! depricate
spl = string.split('.') # TODO format parse #! depricate
assert len(spl) == 3 #! depricate
return cls(py=spl[0], major=spl[1], minor=spl[2]) #! depricate
#! depricate
@classmethod #! depricate
def FromRunning(cls) -> 'PyVersion': #! depricate
return cls(
py=sys.version_info[0], major=sys.version_info[1], minor=sys.version_info[2]
) #! depricate
#! depricate
def _get_asInt(_) -> int: #! depricate
return int(_.py) * 999**2 + int(_.major) * 999 + int(_.minor) #! depricate
#! depricate
def __lt__(_, other: 'PyVersion') -> None: #! depricate
assert isinstance(other, PyVersion) #! depricate
return _.asInt < other.asInt #! depricate
#! depricate
def __gt__(_, other: 'PyVersion') -> None: #! depricate
assert isinstance(other, PyVersion) #! depricate
return _.asInt > other.asInt #! depricate
#! depricate
def __le__(_, other: 'PyVersion') -> None: #! depricate
assert isinstance(other, PyVersion) #! depricate
return _.asInt <= other.asInt #! depricate
#! depricate
def __ge__(_, other: 'PyVersion') -> None: #! depricate
assert isinstance(other, PyVersion) #! depricate
return _.asInt >= other.asInt #! depricate
#! depricate
def __eq__(_, other: 'PyVersion') -> None: #! depricate
assert isinstance(other, PyVersion) #! depricate
return _.asInt == other.asInt #! depricate
#! depricate
def __repr__(_) -> None: #! depricate
return f'PyVersion({_.py}.{_.major}.{_.minor})' #! depricate
#! depricate
def __str__(_) -> None: #! depricate
return f'{_.py}.{_.major}.{_.minor}' #! depricate
#! depricate
@dataclass(slots=True, kw_only=True, frozen=False) #! depricate
class PyEnv: #! depricate
version: str #! depricate
pyEnvRoot: Path = Path('~/.pyenv') #! depricate
#! depricate
@property #! depricate
def versionPath(_) -> Path: #! depricate
return _.pyEnvRoot / 'versions' #! depricate
#! depricate
def versions(_) -> None: #! depricate
return [PyVersion.FromStr(x.name) for x in _.versionPath.glob('*')] #! depricate
#! depricate
@property #! depricate
def python(_) -> Path: #! depricate
return _.pyEnvRoot / f'versions/{_.version}/bin/python' #! depricate
#! depricate
def __post_init__(_): #! depricate
if _.version is not None: #! depricate
assert _.version in _.versions() #! depricate
#! depricate
@staticmethod #! depricate
def rerunAsPythonVersionLessThen(version: PyVersion, scriptPath: Path) -> None: #! depricate
raise NotImplementedError('') #! depricate
maxVer = PyVersion(3, 12, 0) #! depricate
if PyVersion.FromRunning() >= maxVer: #! depricate
pyEnv = PyEnv(version=none) #! depricate
newestSupported = max([x for x in pyEnv.versions() if x < maxVer]) #! depricate
pyEnv.versionPath / str(newestSupported) / 'bin' #! depricate
log.debug('path', lambda x: x.path) #! depricate
log.info('rerunning as python', newestSupported) #! depricate
name = Path(scriptPath).name #! depricate
[x.endswith(name) for x in sys.argv].index(True) #! depricate
newestSupported = PyEnv(version=newestSupported) #! depricate
pathEnv = os.environ['PATH'] #! depricate
env = os.environ | {'PATH': f'{newestSupported}:{pathEnv}'} #! depricate
print(os.environ['PATH']) #! depricate
adad #! depricate
res = defl.RunCom(sys.argv, pipe=False, env=env, raiseOnFail=False).status #! depricate
sys.exit(res) #! depricate
#! depricate