Skip to content

Commit

Permalink
fix: ensure stp package create outputs correct PVEPs (#361)
Browse files Browse the repository at this point in the history
Signed-off-by: Alessandro Pomponio <[email protected]>
  • Loading branch information
AlessandroPomponio authored and GitHub Enterprise committed Aug 13, 2024
1 parent 5f3f7f7 commit 93e15bc
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 39 deletions.
19 changes: 12 additions & 7 deletions python/experiment/cli/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@


def get_git_toplevel_path(path: Path):
if path.is_file():
path = path.parent

try:
toplevel_path = (
subprocess.check_output(
["git", "rev-parse", "--show-toplevel"], cwd=path.parent
)
subprocess.check_output(["git", "rev-parse", "--show-toplevel"], cwd=path)
.decode()
.strip()
)
Expand All @@ -36,11 +37,12 @@ def get_git_toplevel_path(path: Path):


def get_git_origin_url(path: Path):
if path.is_file():
path = path.parent

try:
origin_url = (
subprocess.check_output(
["git", "remote", "get-url", "origin"], cwd=path.parent
)
subprocess.check_output(["git", "remote", "get-url", "origin"], cwd=path)
.decode()
.strip()
)
Expand Down Expand Up @@ -75,9 +77,12 @@ def get_alternative_git_url(original_url: str):


def get_git_head_commit(path: Path):
if path.is_file():
path = path.parent

try:
head_commit = (
subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=path.parent)
subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=path)
.decode()
.strip()
)
Expand Down
69 changes: 37 additions & 32 deletions python/experiment/cli/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ def update_definition(
)
def create_package(
ctx: typer.Context,
path: Path = typer.Option(
input_path: Path = typer.Option(
Path("."),
"--from",
help="Path to the experiment configuration file or a directory that contains a standalone project",
Expand Down Expand Up @@ -485,35 +485,32 @@ def create_package(
if not config.settings.verbose:
config.settings.verbose = verbose

# If we're given a dir, we offer support for
# - standalone projects (conf/flowir_package.yaml)
# - flowir.conf files
if path.is_dir():
standalone_flowir_path = path / Path("conf/flowir_package.yaml")
flowir_conf_path = path / Path("flowir.conf")
if standalone_flowir_path.is_file():
path = standalone_flowir_path
elif flowir_conf_path.is_file():
path = flowir_conf_path
else:
is_standalone = False

# If we're given a dir, we are dealing with a standalone project
# standalone projects (conf/flowir_package.yaml)
if input_path.is_dir():
is_standalone = True
# standalone projects must have a conf dir
conf_dir = input_path / Path("conf")
if not conf_dir.exists():
stderr.print(
"[red]Error:[/red]"
"\tThe path provided to [blue]--from[/blue] does not point to a file or to a standalone project folder"
'\tThe path provided to [blue]--from[/blue] points to a directory without a "conf" subdirectory, '
"which is required in the standalone project structure.\n"
"[b magenta]HINT[/b magenta]\t"
"Refer to https://st4sd.github.io/overview/packaging-workflows/ for more in-depth information"
)
raise typer.Exit(code=STPExitCodes.INPUT_ERROR)

from experiment.model.storage import ExperimentPackage
if manifest is None:
experiment_package: ExperimentPackage = ExperimentPackage.packageFromLocation(
location=str(path)
)
else:
experiment_package: ExperimentPackage = ExperimentPackage.packageFromLocation(
location=str(path)
)

experiment_package: ExperimentPackage = ExperimentPackage.packageFromLocation(
location=str(input_path), manifest=str(manifest) if manifest else None
)

# Base package
origin_url = get_git_origin_url(path)
origin_url = get_git_origin_url(input_path)
# We only support https URLs for cloning
if origin_url.startswith("git@"):
origin_url = get_alternative_git_url(origin_url)
Expand All @@ -522,8 +519,8 @@ def create_package(
"ST4SD does not support SSH URLs for cloning packages.\n"
f"\tThe clone URL for the PVEP has been converted to: {origin_url}"
)
commit_id = get_git_head_commit(path)
toplevel_git_path = Path(get_git_toplevel_path(path))
commit_id = get_git_head_commit(input_path)
toplevel_git_path = Path(get_git_toplevel_path(input_path))
name = origin_url.split("/")[-1]

pvep = {
Expand All @@ -534,19 +531,23 @@ def create_package(
"source": {
"git": {"location": {"url": origin_url, "commit": commit_id}}
},
"config": {
"path": str(path.relative_to(toplevel_git_path)),
"manifestPath": str(
manifest.relative_to(toplevel_git_path)
if manifest is not None
else ""
),
},
}
],
}
}

# packages.config
config = {}
if str(input_path.relative_to(toplevel_git_path)) != ".":
config["path"] = str(input_path.relative_to(toplevel_git_path))

# Manifests can only be used in standalone projects
if not is_standalone and manifest is not None:
config["manifestPath"] = str(manifest.relative_to(toplevel_git_path))

if len(config.keys()) != 0:
pvep["base"]["packages"][0]["config"] = config

# Metadata
pvep["metadata"] = {
"package": {
Expand Down Expand Up @@ -575,6 +576,10 @@ def create_package(
parameterisation["presets"] = {"platform": platforms[0]}
else:
parameterisation["executionOptions"] = {"platform": platforms}
stderr.print(
"[b yellow]WARN[/b yellow]:\tEnsure the order of the platforms is correct, "
"as the experiment will use the first one of the list if the user does not provide one."
)

# If some variables from the default platform are redefined in other
# platforms, we want to ignore them
Expand Down

0 comments on commit 93e15bc

Please sign in to comment.