Skip to content

Commit

Permalink
Add feature to specify directory name (#7)
Browse files Browse the repository at this point in the history
* Add feature to specify directory name

* Fix typing and add defaults
  • Loading branch information
mnaser authored Sep 21, 2024
1 parent efb06f1 commit 39ede16
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 21 deletions.
44 changes: 29 additions & 15 deletions chart_vendor/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import asyncio
import os
import textwrap
import typing
from datetime import datetime, timezone

import aiohttp
Expand Down Expand Up @@ -95,45 +96,53 @@ def repositories(self):

async def _fetch_chart(
self,
session: aiohttp_client_cache.CachedSession,
session: aiohttp_retry.RetryClient,
chart: models.Chart,
path: str,
):
charts_path: aiopath.AsyncPath = aiopath.AsyncPath(path)
chart_path = charts_path / chart.name
chart_path = charts_path / chart.directory

try:
await aioshutil.rmtree(f"{path}/{chart.name}-{chart.version}")
await aioshutil.rmtree(f"{path}/{chart.directory}-{chart.version}")
except FileNotFoundError:
pass

try:
try:
os.rename(
f"{path}/{chart.name}", f"{path}/{chart.name}-{chart.version}"
f"{path}/{chart.directory}",
f"{path}/{chart.directory}-{chart.version}",
)
except FileNotFoundError:
pass

await parsers.fetch_chart(
session, str(chart.repository.url), chart.name, chart.version, path
session,
str(chart.repository.url),
chart.name,
chart.version,
path,
chart.directory,
)
except Exception:
os.rename(f"{path}/{chart.name}-{chart.version}", f"{path}/{chart.name}")
os.rename(
f"{path}/{chart.directory}-{chart.version}", f"{path}/{chart.directory}"
)
raise

try:
await aioshutil.rmtree(f"{path}/{chart.name}-{chart.version}")
await aioshutil.rmtree(f"{path}/{chart.directory}-{chart.version}")
except FileNotFoundError:
pass

if chart.dependencies:
requirements = models.ChartRequirements(dependencies=chart.dependencies)
to_yaml_file(f"{path}/{chart.name}/requirements.yaml", requirements)
to_yaml_file(f"{path}/{chart.directory}/requirements.yaml", requirements)

await asyncio.gather(
*[
aioshutil.rmtree(f"{path}/{chart.name}/charts/{req.name}")
aioshutil.rmtree(f"{path}/{chart.directory}/charts/{req.name}")
for req in chart.dependencies
]
)
Expand All @@ -145,7 +154,8 @@ async def _fetch_chart(
str(req.repository),
req.name,
req.version,
f"{path}/{chart.name}/charts",
f"{path}/{chart.directory}/charts",
req.name,
)
for req in chart.dependencies
]
Expand All @@ -154,16 +164,17 @@ async def _fetch_chart(
for req in chart.dependencies:
lock = parse_yaml_file_as(
models.ChartLock,
f"{path}/{chart.name}/charts/{req.name}/requirements.lock",
f"{path}/{chart.directory}/charts/{req.name}/requirements.lock",
)
lock.generated = datetime.min.replace(tzinfo=timezone.utc)
to_yaml_file(
f"{path}/{chart.name}/charts/{req.name}/requirements.lock", lock
f"{path}/{chart.directory}/charts/{req.name}/requirements.lock",
lock,
)

# Reset the generated time in the lock file to make things reproducible
to_yaml_file(
f"{path}/{chart.name}/requirements.lock", chart.requirements_lock
f"{path}/{chart.directory}/requirements.lock", chart.requirements_lock
)

for gerrit, changes in chart.patches.gerrit.items():
Expand All @@ -187,7 +198,10 @@ async def _fetch_chart(


async def _main(
config_file: str, charts_root: str, check: bool, chart_name: str = None
config_file: str,
charts_root: str,
check: bool,
chart_name: typing.Optional[str] = None,
):
config = parse_yaml_file_as(Config, config_file)

Expand All @@ -204,7 +218,7 @@ async def _main(
else config.charts
)
if not charts_to_fetch:
logger.warn("No chart configured to fetch.")
logger.warning("No chart configured to fetch.")
return
await asyncio.gather(
*[
Expand Down
9 changes: 8 additions & 1 deletion chart_vendor/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from datetime import datetime, timezone
from typing import Annotated

from pydantic import AfterValidator, BaseModel, HttpUrl
from pydantic import AfterValidator, BaseModel, HttpUrl, model_validator
from pydantic.json import pydantic_encoder

HttpUrlString = Annotated[HttpUrl, AfterValidator(str)]
Expand Down Expand Up @@ -49,9 +49,16 @@ class Chart(BaseModel):
name: str
version: str
repository: ChartRepository
directory: str
dependencies: list[ChartDependency] = []
patches: ChartPatches = ChartPatches()

@model_validator(mode='before')
def set_defaults(cls, values: dict):
if "directory" not in values:
values["directory"] = values["name"]
return values

@property
def requirements_lock(self):
data = json.dumps(
Expand Down
13 changes: 8 additions & 5 deletions chart_vendor/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
# SPDX-License-Identifier: Apache-2.0

import io
import os
import tarfile

import aiohttp_client_cache
import aiohttp_retry
import yaml # type: ignore
from aiohttp import client_exceptions as aiohttp_exceptions
from async_lru import alru_cache
Expand All @@ -18,9 +19,7 @@


@alru_cache(maxsize=32)
async def parse_remote_repository(
session: aiohttp_client_cache.CachedSession, url: str
):
async def parse_remote_repository(session: aiohttp_retry.RetryClient, url: str):
repo_url = str(url) + "/index.yaml"

async with session.get(repo_url) as resp:
Expand Down Expand Up @@ -58,11 +57,12 @@ def fetch_entry(index: dict, index_url: str, name: str, version: str):
stop=stop_after_attempt(10),
)
async def fetch_chart(
session: aiohttp_client_cache.CachedSession,
session: aiohttp_retry.RetryClient,
index_url: str,
name: str,
version: str,
path: str,
directory: str,
):
index = await parse_remote_repository(session, index_url)
entry = fetch_entry(index, index_url, name, version)
Expand All @@ -76,3 +76,6 @@ async def fetch_chart(
tar_bytes = io.BytesIO(data)
with tarfile.open(fileobj=tar_bytes) as tar:
tar.extractall(path=path)

if name != directory:
os.rename(f"{path}/{name}", f"{path}/{directory}")

0 comments on commit 39ede16

Please sign in to comment.