Skip to content

Commit

Permalink
fix: recipe names are not updated (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
nichmor authored Jul 4, 2024
1 parent 2a97590 commit 5a356e5
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 76 deletions.
10 changes: 4 additions & 6 deletions src/repror/cli/build_recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from repror.internals.db import get_latest_builds, save, Recipe, RemoteRecipe
from repror.internals.rattler_build import rattler_build_hash
from repror.internals.build import BuildStatus
from repror.internals.patcher import save_patch, save_recipe_patch
from repror.internals.patcher import save_patch
from rich.table import Table
from rich import print

Expand All @@ -28,18 +28,18 @@ def recipes_for_names(
all_recipes = load_all_recipes(config_path)
if recipe_names:
recipes_to_build = []
all_recipes_names = [recipe.name for recipe in all_recipes]
all_recipes_names = [recipe.name for recipe in all_recipes.all_recipes]
for recipe_to_filter in recipe_names:
if recipe_to_filter not in all_recipes_names:
raise ValueError(
f"Recipe {recipe_to_filter} not found in the configuration file"
)
recipes_to_build.append(
all_recipes[all_recipes_names.index(recipe_to_filter)]
all_recipes.all_recipes[all_recipes_names.index(recipe_to_filter)]
)

else:
recipes_to_build = all_recipes
recipes_to_build = all_recipes.all_recipes

return recipes_to_build

Expand Down Expand Up @@ -119,8 +119,6 @@ def build_recipes(
if patch:
print(f"Saving patch for {build_result.build.recipe_name}")
save_patch(build_result.build)
if isinstance(recipe, RemoteRecipe):
save_recipe_patch(recipe)

# We need to save the rebuild result to the database
# even though we are using patches because we might invoke
Expand Down
15 changes: 4 additions & 11 deletions src/repror/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from .utils import pixi_root_cli, platform_name, reproducible_table

from ..internals.options import global_options
from ..internals.config import load_all_recipes


app = typer.Typer(no_args_is_help=True, pretty_exceptions_enable=False)
Expand Down Expand Up @@ -148,10 +149,7 @@ def rebuild_recipe(
@app.command()
def merge_patches(update_remote: Annotated[bool, typer.Option()] = False):
"""Merge database patches after CI jobs run to the database."""
num_builds_patches, num_recipes_patches = (
patch_database.patch_builds_to_db(),
patch_database.patch_recipes_to_db(),
)
num_builds_patches = patch_database.patch_builds_to_db()

if num_builds_patches > 0:
print(
Expand All @@ -160,14 +158,9 @@ def merge_patches(update_remote: Annotated[bool, typer.Option()] = False):
else:
print(":man_shrugging: No build patches to merge.")

if num_recipes_patches > 0:
print(
f":red_car: Database patched. {num_builds_patches} recipe patches applied."
)
else:
print(":man_shrugging: No recipe patches to merge.")
all_recipes = load_all_recipes()

if update_remote and (num_builds_patches > 0 or num_recipes_patches > 0):
if update_remote and (num_builds_patches > 0 or all_recipes.saved_recipes > 0):
print(":globe_with_meridians: Database patches merged to remote database.")
patch_database.write_database_to_remote()

Expand Down
2 changes: 1 addition & 1 deletion src/repror/cli/generate_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def rerender_html(

builds = get_rebuild_data()

total_recipes = len(load_all_recipes(str(config_path)))
total_recipes = len(load_all_recipes(str(config_path)).all_recipes)

# Statistics for graph
counts_per_platform = {}
Expand Down
10 changes: 7 additions & 3 deletions src/repror/cli/generate_recipes.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ def _generate_recipes(

if not all_:
# Get the name and hash of all the recipes
name_and_hash = [(recipe.name, recipe.content_hash) for recipe in all_recipes]
name_and_hash = [
(recipe.name, recipe.content_hash) for recipe in all_recipes.all_recipes
]
# Current rattler hash
# Latest build with rebuild, a.k.a. finished recipes
finished_recipes = get_latest_build_with_rebuild(
Expand All @@ -25,10 +27,12 @@ def _generate_recipes(

# Get the recipes that are not finished yet
return [
recipe.name for recipe in all_recipes if recipe.name not in finished_recipes
recipe.name
for recipe in all_recipes.all_recipes
if recipe.name not in finished_recipes
]
else:
return [recipe.name for recipe in all_recipes]
return [recipe.name for recipe in all_recipes.all_recipes]


def generate_recipes(
Expand Down
18 changes: 15 additions & 3 deletions src/repror/internals/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,15 @@ def load_remote_recipes(
return remote_recipes


def load_all_recipes(config_path: str = "config.yaml") -> list[RecipeDB | RemoteRecipe]:
class LoadedRecipes(BaseModel):
all_recipes: list[RecipeDB | RemoteRecipe]
saved_recipes: int


def load_all_recipes(config_path: str = "config.yaml") -> LoadedRecipes:
config = load_config(config_path)
recipes = []
saved_recipes = 0
with tempfile.TemporaryDirectory() as clone_dir:
# iterate over existing recipes
# this is done to avoid not-so-intuitive setup of the :memory: database
Expand All @@ -108,7 +114,13 @@ def load_all_recipes(config_path: str = "config.yaml") -> list[RecipeDB | Remote
for repo, recipes in recipes_to_fetch.items()
],
)
[save(recipe) for repo_recipes in remote_recipes for recipe in repo_recipes]
saved_recipes = len(
[
save(recipe)
for repo_recipes in remote_recipes
for recipe in repo_recipes
]
)
[recipes.extend(recipe_list) for recipe_list in remote_recipes]

for local in config.local:
Expand All @@ -124,4 +136,4 @@ def load_all_recipes(config_path: str = "config.yaml") -> list[RecipeDB | Remote
)
recipes.append(recipe)

return recipes
return LoadedRecipes(all_recipes=recipes, saved_recipes=saved_recipes)
23 changes: 1 addition & 22 deletions src/repror/internals/patch_database.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from repror.internals.db import PROD_DB, get_recipe, get_session
from repror.internals.db import PROD_DB
from repror.internals.git import github_api
from repror.internals.print import print
from repror.internals.patcher import (
aggregate_build_patches,
load_patch,
aggregate_recipe_patches,
)


Expand All @@ -28,26 +27,6 @@ def patch_builds_to_db(build_dir: str = "build_info") -> int:
return len(patches)


def patch_recipes_to_db(recipes_dir: str = "recipe_info") -> int:
patches = aggregate_recipe_patches(recipes_dir)
with get_session() as session:
for recipe_obj in patches:
exisiting_recipe = get_recipe(
recipe_obj.url, recipe_obj.path, recipe_obj.rev
)
if not exisiting_recipe:
print(f":running: Writing {recipe_obj.name} to the database")
recipe_obj.id = None
session.add(recipe_obj)
else:
print(
f":running: Recipe {recipe_obj.name} already exists in the database. Skipping."
)

session.commit()
return len(patches)


def write_database_to_remote():
"""Update the remote repro.db file with the new data"""
print(":running: Updating repro.db")
Expand Down
28 changes: 1 addition & 27 deletions src/repror/internals/patcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import Any, Literal


from repror.internals.db import Build, Rebuild, RemoteRecipe, get_session
from repror.internals.db import Build, Rebuild, get_session


def find_patches(folder_path: str) -> list[Path]:
Expand Down Expand Up @@ -40,21 +40,6 @@ def aggregate_build_patches(
return patches


def aggregate_recipe_patches(
folder_path: str,
) -> list[RemoteRecipe]:
"""
Aggregate all found patches, and return list of what should be saved
"""
recipes_patches = list()
json_files = find_patches(folder_path)

for file_path in json_files:
recipes_patches.append(RemoteRecipe.model_validate_json(file_path.read_bytes()))

return recipes_patches


def save_patch(model: Build | Rebuild):
"""
Save the patch to a file
Expand All @@ -66,17 +51,6 @@ def save_patch(model: Build | Rebuild):
file.write(model.model_dump_json())


def save_recipe_patch(recipe: RemoteRecipe):
"""
Save the patch to a file
"""
patch_file = f"recipe_info/{recipe.name}/{recipe.__class__.__name__.lower()}.json"
os.makedirs(os.path.dirname(patch_file), exist_ok=True)

with open(patch_file, "w") as file:
file.write(recipe.model_dump_json())


# Load the patch data
def load_patch(patch_data: dict[Literal["build", "rebuild"], Any]):
build = patch_data["build"]
Expand Down
3 changes: 1 addition & 2 deletions src/repror/internals/print.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import platform

from rich import print as rich_print
from rich.table import Table
from typing import Any, IO, Optional
from .options import global_options

Expand Down Expand Up @@ -59,7 +58,7 @@ def print(
logging.debug("Disabled output")
return

if objects and isinstance(objects[0], Table):
if objects and not isinstance(objects[0], (str, bytes)):
return rich_print(*objects, sep=sep, end=end, file=file, flush=flush)

if platform.system() == "Windows":
Expand Down
2 changes: 1 addition & 1 deletion tests/test_recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def test_load_all_recipes_test():
"""Check to see if all recipes can be loaded"""
recipes = load_all_recipes(Path(__file__).parent.parent / "config.yaml")
remote_recipe = next(
filter(lambda recipe: isinstance(recipe, RemoteRecipe), recipes)
filter(lambda recipe: isinstance(recipe, RemoteRecipe), recipes.all_recipes)
)
with remote_recipe.local_path as local_path:
assert Path(local_path).exists()
Expand Down

0 comments on commit 5a356e5

Please sign in to comment.