Skip to content

Commit

Permalink
Fix bugs (#324)
Browse files Browse the repository at this point in the history
- close #298
- close #285
- close #274
  • Loading branch information
aguschin authored Dec 22, 2022
1 parent f835358 commit cb83eec
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 34 deletions.
30 changes: 21 additions & 9 deletions gto/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
VERSION,
VERSIONS_PER_STAGE,
VersionSort,
is_hexsha,
mark_artifact_unregistered,
parse_shortcut,
)
Expand Down Expand Up @@ -275,7 +276,7 @@ def deprecate(
name=name,
message=message,
stdout=stdout,
simple=simple if simple is not None else True,
simple=simple,
force=force,
delete=delete,
push=push or is_url_of_remote_repo(repo),
Expand Down Expand Up @@ -332,6 +333,7 @@ def show(
all_commits=False,
truncate_hexsha=False,
registered_only=False,
deprecated=False,
assignments_per_version=ASSIGNMENTS_PER_VERSION,
versions_per_stage=VERSIONS_PER_STAGE,
sort=VersionSort.Timestamp,
Expand All @@ -344,6 +346,7 @@ def show(
all_branches=all_branches,
all_commits=all_commits,
registered_only=registered_only,
deprecated=deprecated,
assignments_per_version=assignments_per_version,
versions_per_stage=versions_per_stage,
sort=sort,
Expand All @@ -356,6 +359,7 @@ def show(
all_branches=all_branches,
all_commits=all_commits,
registered_only=registered_only,
deprecated=deprecated,
assignments_per_version=assignments_per_version,
versions_per_stage=versions_per_stage,
sort=sort,
Expand All @@ -370,6 +374,7 @@ def _show_registry(
all_branches=False,
all_commits=False,
registered_only=False,
deprecated=False,
assignments_per_version: int = None,
versions_per_stage: int = None,
sort: VersionSort = None,
Expand Down Expand Up @@ -406,11 +411,13 @@ def format_hexsha(hexsha):
for name in stages
},
"registered": o.is_registered,
"active": o.is_active,
}
for o in reg.get_artifacts(
all_branches=all_branches,
all_commits=all_commits,
).values()
if o.is_active or deprecated
}

if not table:
Expand All @@ -427,7 +434,7 @@ def format_hexsha(hexsha):
+ [d["stage"][name] for name in stages],
),
)
for name, d in models_state.items()
for name, d in sorted(models_state.items())
], "keys"


Expand All @@ -438,6 +445,7 @@ def _show_versions( # pylint: disable=too-many-locals
all_branches=False,
all_commits=False,
registered_only=False,
deprecated=False,
assignments_per_version: int = None,
versions_per_stage: int = None,
sort: VersionSort = None,
Expand All @@ -447,7 +455,7 @@ def _show_versions( # pylint: disable=too-many-locals
"""List versions of artifact"""

def format_hexsha(hexsha):
return hexsha[:7] if truncate_hexsha else hexsha
return hexsha[:7] if truncate_hexsha and is_hexsha(hexsha) else hexsha

shortcut = parse_shortcut(name)

Expand All @@ -468,7 +476,9 @@ def format_hexsha(hexsha):
)
versions = []
for v in artifact.get_versions(
include_non_explicit=not registered_only, include_discovered=True
active_only=not deprecated,
include_non_explicit=not registered_only,
include_discovered=True,
):
v = v.dict_state()
v["stages"] = [
Expand All @@ -477,13 +487,14 @@ def format_hexsha(hexsha):
for vstage in vstages
if vstage.version == v["version"]
]
versions.append(v)
if artifact.is_active or deprecated:
versions.append(v)

if shortcut.latest:
versions = versions[:1]
if shortcut.version:
elif shortcut.version:
versions = [v for v in versions if shortcut.version == v["version"]]
if shortcut.stage:
elif shortcut.stage:
versions = [
v for v in versions for a in v["stages"] if shortcut.stage == a["stage"]
]
Expand All @@ -506,6 +517,7 @@ def format_hexsha(hexsha):
)
)
v["commit_hexsha"] = format_hexsha(v["commit_hexsha"])
v["ref"] = format_hexsha(v["ref"])
if len(v["registrations"]) > 1:
raise NotImplementedInGTO(
"Multiple registrations are not supported currently. How you got in here?"
Expand All @@ -523,7 +535,7 @@ def format_hexsha(hexsha):

def history(
repo: Union[str, Repo],
artifact: str = None,
artifact: Optional[str] = None,
# action: str = None,
all_branches=False,
all_commits=False,
Expand All @@ -538,7 +550,7 @@ def history(
)

def format_hexsha(hexsha):
return hexsha[:7] if truncate_hexsha else hexsha
return hexsha[:7] if truncate_hexsha and is_hexsha(hexsha) else hexsha

events = [
OrderedDict(
Expand Down
6 changes: 4 additions & 2 deletions gto/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ def activated_at(self):

@property
def is_registered(self):
"""Tells if this is an a registered artifact - i.e. there Git tags for it"""
"""Tells if this is an a registered artifact - i.e. there are Git tags for it"""
return not all(
isinstance(e, Commit) for e in self.get_events(direct=True, indirect=True)
)
Expand All @@ -442,6 +442,7 @@ def __repr__(self) -> str:

def get_versions(
self,
active_only=True,
include_non_explicit=False,
include_discovered=False,
sort=VersionSort.SemVer,
Expand All @@ -450,7 +451,8 @@ def get_versions(
versions = [
v
for v in self.versions
if v.is_active
if not active_only
or v.is_active
and (
(v.is_registered and not v.discovered)
or (include_discovered and v.discovered)
Expand Down
14 changes: 12 additions & 2 deletions gto/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def GTOGroupSection(section):
arg_version = Argument(..., help="Artifact version")
arg_stage = Argument(..., help="Stage to assign")
option_version = Option(None, "--version", help="Version to register")
option_stage = Option(None, "--stage", help="Stage to assign")
option_stage = Option(..., "--stage", help="Stage to assign")
option_to_version = Option(
None, "--to-version", help="Version to use for stage assignment"
)
Expand Down Expand Up @@ -293,11 +293,18 @@ def callback_sort( # pylint: disable=inconsistent-return-statements
option_all = Option(False, "--all", "-a", help="Return all versions sorted")
option_registered_only = Option(
False,
"--registered-only",
"--ro",
"--registered-only",
is_flag=True,
help="Show only registered versions",
)
option_deprecated = Option(
False,
"-d",
"--deprecated",
is_flag=True,
help="Include deprecated in output",
)
option_expected = Option(
False,
"-e",
Expand Down Expand Up @@ -794,6 +801,7 @@ def show( # pylint: disable=too-many-locals
show_stage: bool = option_show_stage,
show_ref: bool = option_show_ref,
registered_only: bool = option_registered_only,
deprecated: bool = option_deprecated,
assignments_per_version: int = option_assignments_per_version,
versions_per_stage: int = option_versions_per_stage,
sort: str = option_sort,
Expand All @@ -810,6 +818,7 @@ def show( # pylint: disable=too-many-locals
all_branches=all_branches,
all_commits=all_commits,
registered_only=registered_only,
deprecated=deprecated,
assignments_per_version=assignments_per_version,
versions_per_stage=versions_per_stage,
sort=sort,
Expand All @@ -823,6 +832,7 @@ def show( # pylint: disable=too-many-locals
all_branches=all_branches,
all_commits=all_commits,
registered_only=registered_only,
deprecated=deprecated,
assignments_per_version=assignments_per_version,
versions_per_stage=versions_per_stage,
sort=sort,
Expand Down
15 changes: 10 additions & 5 deletions gto/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,22 @@ class Action(Enum):
name = "[a-z][a-z0-9-/]*[a-z0-9]"
semver = r"(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?"
counter = "?P<counter>[0-9]+"
name_regexp = re.compile(f"^{name}$")
tag_regexp = re.compile(
name_re = re.compile(f"^{name}$")
tag_re = re.compile(
f"^(?P<artifact>{name})(((#(?P<stage>{name})|@(?P<version>v{semver}))(?P<cancel>!?))|@((?P<deprecated>deprecated)|(?P<created>created)))(#({counter}))?$"
)
shortcut_regexp = re.compile(
shortcut_re = re.compile(
f"^(?P<artifact>{name})(#(?P<stage>{name})|@(?P<version>latest|greatest|v{semver}))$"
)
git_hexsha_re = re.compile(r"^[0-9a-fA-F]{40}$")


def is_hexsha(value):
return bool(git_hexsha_re.search(value))


def check_name_is_valid(value):
return bool(re.search(name_regexp, value))
return bool(name_re.search(value))


def assert_name_is_valid(value):
Expand All @@ -62,7 +67,7 @@ class Shortcut(BaseModel):


def parse_shortcut(value):
match = re.search(shortcut_regexp, value)
match = re.search(shortcut_re, value)
if match:
value = match["artifact"]
if match["stage"]:
Expand Down
8 changes: 7 additions & 1 deletion gto/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,13 @@ def deprecate(
author: Optional[str] = None,
author_email: Optional[str] = None,
) -> Optional[Deprecation]:
if not force:
if force:
if simple:
raise WrongArgs("Can't use 'force' with 'simple=True'")
simple = False
else:
if simple is None:
simple = True
found_artifact = self.find_artifact(name)
if not found_artifact.is_active:
raise WrongArgs("Artifact was deprecated already")
Expand Down
4 changes: 2 additions & 2 deletions gto/tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
TAG,
VERSION,
Action,
tag_regexp,
tag_re,
)
from .exceptions import (
InvalidTagName,
Expand Down Expand Up @@ -79,7 +79,7 @@ def name_tag(

def parse_name(name: str, raise_on_fail: bool = True):

match = re.search(tag_regexp, name)
match = re.search(tag_re, name)
if raise_on_fail and not match:
raise InvalidTagName(name)
if match:
Expand Down
21 changes: 8 additions & 13 deletions tests/resources/sample_remote_repo_expected_registry.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
{
"deprecated-model": {
"version": "v0.0.1",
"stage": {
"dev": null,
"prod": null,
"staging": null
},
"registered": true
},
"churn": {
"version": "v3.1.0",
"stage": {
"dev": null,
"prod": "v3.0.0",
"staging": "v3.1.0"
},
"registered": true
"registered": true,
"active": true
},
"segment": {
"version": "v0.4.1",
Expand All @@ -24,7 +16,8 @@
"prod": null,
"staging": null
},
"registered": true
"registered": true,
"active": true
},
"cv-class": {
"version": "v0.1.13",
Expand All @@ -33,7 +26,8 @@
"prod": null,
"staging": null
},
"registered": true
"registered": true,
"active": true
},
"model-a": {
"version": "v0.0.2",
Expand All @@ -42,6 +36,7 @@
"prod": null,
"staging": null
},
"registered": true
"registered": true,
"active": true
}
}
Loading

0 comments on commit cb83eec

Please sign in to comment.