change version comparsion, handling of venv and startup

This commit is contained in:
Ondrej Samohel 2021-01-15 22:42:16 +01:00
parent 2d3bb6a4d9
commit 10686e06c5
No known key found for this signature in database
GPG key ID: 02376E18990A97C6
13 changed files with 1295 additions and 114 deletions

View file

@ -14,8 +14,7 @@ from zipfile import ZipFile, BadZipFile
from appdirs import user_data_dir
from speedcopy import copyfile
from pype.lib import PypeSettingsRegistry
from pype.version import __version__
from .user_settings import PypeSettingsRegistry
from .tools import load_environments
@ -24,23 +23,23 @@ class PypeVersion:
"""Class for storing information about Pype version.
Attributes:
major (int): [1].2.3-variant-client
minor (int): 1.[2].3-variant-client
subversion (int): 1.2.[3]-variant-client
variant (str): 1.2.3-[variant]-client
client (str): 1.2.3-variant-[client]
major (int): [1].2.3-client-variant
minor (int): 1.[2].3-client-variant
subversion (int): 1.2.[3]-client-variant
client (str): 1.2.3-[client]-variant
variant (str): 1.2.3-client-[variant]
path (str): path to Pype
"""
major = 0
minor = 0
subversion = 0
variant = "production"
variant = ""
client = None
path = None
_version_regex = re.compile(
r"(?P<major>\d+)\.(?P<minor>\d+)\.(?P<sub>\d+)(-?((?P<variant>staging)|(?P<client>.+))(-(?P<cli>.+))?)?") # noqa: E501
r"(?P<major>\d+)\.(?P<minor>\d+)\.(?P<sub>\d+)(-(?P<var1>staging)|-(?P<client>.+)(-(?P<var2>staging)))?") # noqa: E501
@property
def version(self):
@ -58,12 +57,12 @@ class PypeVersion:
def __init__(self, major: int = None, minor: int = None,
subversion: int = None, version: str = None,
variant: str = "production", client: str = None,
variant: str = "", client: str = None,
path: Path = None):
self.path = path
if (
major is None or minor is None or subversion is None
major is None or minor is None or subversion is None
) and version is None:
raise ValueError("Need version specified in some way.")
if version:
@ -85,12 +84,13 @@ class PypeVersion:
def _compose_version(self):
version = "{}.{}.{}".format(self.major, self.minor, self.subversion)
if self.variant == "staging":
version = "{}-{}".format(version, self.variant)
if self.client:
version = "{}-{}".format(version, self.client)
if self.variant == "staging":
version = "{}-{}".format(version, self.variant)
return version
@classmethod
@ -101,10 +101,10 @@ class PypeVersion:
"Cannot parse version string: {}".format(version_string))
variant = None
if m.group("variant") == "staging":
if m.group("var1") == "staging" or m.group("var2") == "staging":
variant = "staging"
client = m.group("client") or m.group("cli")
client = m.group("client")
return (int(m.group("major")), int(m.group("minor")),
int(m.group("sub")), variant, client)
@ -125,26 +125,48 @@ class PypeVersion:
return hash(self.version)
def __lt__(self, other):
if self.major < other.major:
if (self.major, self.minor, self.subversion) < \
(other.major, other.minor, other.subversion):
return True
if self.major <= other.major and self.minor < other.minor:
return True
if self.major <= other.major and self.minor <= other.minor and \
self.subversion < other.subversion:
# 1.2.3-staging < 1.2.3-client-staging
if self.get_main_version() == other.get_main_version() and \
not self.client and self.variant and \
other.client and other.variant:
return True
# Directory takes precedence over file
if (
self.path
and other.path
and other.path.is_dir()
and self.path.is_file()
):
# 1.2.3 < 1.2.3-staging
if self.get_main_version() == other.get_main_version() and \
not self.client and self.variant and \
not other.client and not other.variant:
return True
return self.major == other.major and self.minor == other.minor and \
self.subversion == other.subversion and self.variant == "staging"
# 1.2.3 < 1.2.3-client
if self.get_main_version() == other.get_main_version() and \
not self.client and not self.variant and \
other.client and not other.variant:
return True
# 1.2.3 < 1.2.3-client-staging
if self.get_main_version() == other.get_main_version() and \
not self.client and not self.variant and other.client:
return True
# 1.2.3-client-staging < 1.2.3-client
if self.get_main_version() == other.get_main_version() and \
self.client and self.variant and \
other.client and not other.variant:
return True
# prefer path over no path
if self.version == other.version and \
not self.path and other.path:
return True
# prefer path with dir over path with file
return self.version == other.version and self.path and \
other.path and self.path.is_file() and \
other.path.is_dir()
def is_staging(self) -> bool:
"""Test if current version is staging one."""
@ -252,7 +274,12 @@ class BootstrapRepos:
@staticmethod
def get_local_live_version() -> str:
"""Get version of local Pype."""
return __version__
version = {}
path = Path(os.path.dirname(__file__)).parent / "pype" / "version.py"
with open(path, "r") as fp:
exec(fp.read(), version)
return version["__version__"]
@staticmethod
def get_version(repo_dir: Path) -> Union[str, None]:
@ -494,6 +521,7 @@ class BootstrapRepos:
def find_pype(
self,
pype_path: Path = None,
staging: bool = False,
include_zips: bool = False) -> Union[List[PypeVersion], None]:
"""Get ordered dict of detected Pype version.
@ -505,6 +533,8 @@ class BootstrapRepos:
Args:
pype_path (Path, optional): Try to find Pype on the given path.
staging (bool, optional): Filter only staging version, skip them
otherwise.
include_zips (bool, optional): If set True it will try to find
Pype in zip files in given directory.
@ -549,6 +579,7 @@ class BootstrapRepos:
result = PypeVersion.version_in_str(name)
if result[0]:
detected_version: PypeVersion
detected_version = result[1]
if file.is_dir():
@ -614,7 +645,11 @@ class BootstrapRepos:
continue
detected_version.path = file
_pype_versions.append(detected_version)
if staging and detected_version.is_staging():
_pype_versions.append(detected_version)
if not staging and not detected_version.is_staging():
_pype_versions.append(detected_version)
return sorted(_pype_versions)