Skip to content

Commit

Permalink
Added --fixture option to init and clone
Browse files Browse the repository at this point in the history
This way, we can clone the sub components even when the fixture is not a git repo
CAUTION: many mepo commands, including status, will fail
  • Loading branch information
pchakraborty committed Aug 18, 2024
1 parent 2ed8d4a commit 50e941c
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 33 deletions.
12 changes: 12 additions & 0 deletions src/mepo/cmdline/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ def __init(self):
choices=["naked", "prefix", "postfix"],
help="Style of directory file, default: prefix, allowed options: %(choices)s",
)
init.add_argument(
"--fixture",
metavar="url",
default=None,
help="Remote URL of fixture (default: None)",
)

def __clone(self):
clone = self.subparsers.add_parser(
Expand Down Expand Up @@ -129,6 +135,12 @@ def __clone(self):
choices=["off", "blobless", "treeless"],
help='Style of partial clone, default: None, allowed options: %(choices)s. Off means a "normal" full git clone, blobless means cloning with "--filter=blob:none" and treeless means cloning with "--filter=tree:0". NOTE: We do *not* recommend using "treeless" as it is very aggressive and will cause problems with many git commands.',
)
clone.add_argument(
"--fixture",
metavar="url",
default=None,
help="Remote URL of fixture (default: None)",
)

def __list(self):
listcomps = self.subparsers.add_parser(
Expand Down
2 changes: 1 addition & 1 deletion src/mepo/command/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def run(args):
else:
style = None

_ = MepoState.initialize(args.registry, style)
_ = MepoState.initialize(args.registry, style, args.fixture)

if not style:
print(f"Initializing mepo using {args.registry}")
Expand Down
71 changes: 41 additions & 30 deletions src/mepo/component.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import shlex

from urllib.parse import urlparse
from urllib.parse import urlparse, urljoin

from .utilities import shellcmd
from .utilities.version import MepoVersion
Expand Down Expand Up @@ -55,33 +55,40 @@ def __repr__(self):
f" ignore_submodules: {_ignore_submodules}"
)

def __set_original_version(self, comp_details):
def __set_original_version(self, comp_details, fixture_url):
if self.fixture:
cmd_if_branch = "git symbolic-ref HEAD"
# Have to use 'if not' since 0 is a good status
if not shellcmd.run(cmd_if_branch.split(), status=True):
output = shellcmd.run(cmd_if_branch.split(), output=True).rstrip()
ver_name = output.replace("refs/heads/", "")
ver_type = "b"
is_detached = False
if fixture_url is not None:
ver_name = "UNKNOWN"
ver_type = "NOT APPLICABLE"
is_detached = "NOT APPLICABLE"
else:
# On some CI systems, git is handled oddly. As such, sometimes
# tags aren't found due to shallow clones
cmd_for_tag = "git describe --tags"
cmd_if_branch = "git symbolic-ref HEAD"
# Have to use 'if not' since 0 is a good status
if not shellcmd.run(cmd_for_tag.split(), status=True):
ver_name = shellcmd.run(cmd_for_tag.split(), output=True).rstrip()
ver_type = "t"
is_detached = True
if not shellcmd.run(cmd_if_branch.split(), status=True):
output = shellcmd.run(cmd_if_branch.split(), output=True).rstrip()
ver_name = output.replace("refs/heads/", "")
ver_type = "b"
is_detached = False
else:
# Per internet, describe always should always work, though mepo
# will return weirdness (a grafted branch, probably a hash)
cmd_for_always = "git describe --always"
ver_name = shellcmd.run(
cmd_for_always.split(), output=True
).rstrip()
ver_type = "h"
is_detached = True
# On some CI systems, git is handled oddly. As such, sometimes
# tags aren't found due to shallow clones
cmd_for_tag = "git describe --tags"
# Have to use 'if not' since 0 is a good status
if not shellcmd.run(cmd_for_tag.split(), status=True):
ver_name = shellcmd.run(
cmd_for_tag.split(), output=True
).rstrip()
ver_type = "t"
is_detached = True
else:
# Per internet, describe always should always work, though mepo
# will return weirdness (a grafted branch, probably a hash)
cmd_for_always = "git describe --always"
ver_name = shellcmd.run(
cmd_for_always.split(), output=True
).rstrip()
ver_type = "h"
is_detached = True
else:
if comp_details.get("branch", None):
# SPECIAL HANDLING of 'detached head' branches
Expand All @@ -100,16 +107,18 @@ def __set_original_version(self, comp_details):
is_detached = True
self.version = MepoVersion(ver_name, ver_type, is_detached)

def registry_to_component(self, comp_name, comp_details, comp_style):
def registry_to_component(self, comp_name, comp_details, comp_style, fixture_url):
self.name = comp_name
self.fixture = comp_details.get("fixture", False)
# local/remote - start
if self.fixture:
self.local = "."
repo_url = get_current_remote_url()
p = urlparse(repo_url)
last_url_node = p.path.rsplit("/")[-1]
self.remote = "../" + last_url_node
self.remote = fixture_url
if self.remote is None:
repo_url = get_current_remote_url()
p = urlparse(repo_url)
last_url_node = p.path.rsplit("/")[-1]
self.remote = "../" + last_url_node
else:
# Assume the flag for repostories is commercial-at
repo_flag = "@"
Expand Down Expand Up @@ -141,6 +150,8 @@ def registry_to_component(self, comp_name, comp_details, comp_style):
# print(f'final self.local: {self.local}')

self.remote = comp_details["remote"]
if fixture_url and self.remote.startswith("../"):
self.remote = urljoin(fixture_url, os.path.basename(self.remote))
# local/remote - end
self.sparse = comp_details.get("sparse", None) # sparse is optional
self.develop = comp_details.get("develop", None) # develop is optional
Expand All @@ -150,7 +161,7 @@ def registry_to_component(self, comp_name, comp_details, comp_style):
self.ignore_submodules = comp_details.get(
"ignore_submodules", None
) # ignore_submodules is optional
self.__set_original_version(comp_details)
self.__set_original_version(comp_details, fixture_url)
return self

def to_registry_format(self):
Expand Down
6 changes: 4 additions & 2 deletions src/mepo/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,16 @@ def state_exists(cls, old_style=False):
return False

@classmethod
def initialize(cls, project_registry, directory_style):
def initialize(cls, project_registry, directory_style, fixture_url):
if cls.state_exists():
raise StateAlreadyInitializedError("Error! mepo state already exists")
input_components = Registry(project_registry).read_file()
complist = list()
for name, comp in input_components.items():
complist.append(
MepoComponent().registry_to_component(name, comp, directory_style)
MepoComponent().registry_to_component(
name, comp, directory_style, fixture_url
)
)
cls.write_state(complist)

Expand Down
1 change: 1 addition & 0 deletions tests/test_mepo_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def __mepo_clone(cls):
branch=None,
directory=None,
partial="blobless",
fixture=None,
)
mepo_clone.run(args)
print(flush=True)
Expand Down

0 comments on commit 50e941c

Please sign in to comment.