Skip to content

Commit

Permalink
Support ufbt --branch
Browse files Browse the repository at this point in the history
  • Loading branch information
Willy-JL committed Jun 25, 2024
1 parent 7b655e1 commit c4ba45e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
30 changes: 29 additions & 1 deletion indexer/src/directories.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
import asyncio
from fastapi import APIRouter
from fastapi.responses import JSONResponse, RedirectResponse, FileResponse
from fastapi.responses import HTMLResponse, JSONResponse, RedirectResponse, FileResponse

from .repository import indexes

Expand All @@ -11,6 +11,7 @@


@router.get("/{directory}/directory.json")
@router.get("/{directory}")
async def directory_request(directory):
"""
Method for obtaining indices
Expand All @@ -25,6 +26,33 @@ async def directory_request(directory):
return indexes.get(directory).index


@router.get("/{directory}/{branch}")
async def repository_branch_request(directory, branch):
"""
A method for retrieving the list of files from a specific branch
Made for support of `ufbt update --index-url {base_url}/firmware --branch {branch}`
Args:
directory: Repository name
branch: Branch name
Returns:
HTML links in format that ufbt understands
"""
if directory not in indexes:
return JSONResponse(f"{directory} not found!", status_code=404)
index = indexes.get(directory)
if len(index.index["channels"]) == 0:
return JSONResponse("No channels found!", status_code=404)
try:
branch_files = index.get_branch_file_names(branch)
response = "\n".join(f'<a href="{file}"></a>' for file in branch_files)
return HTMLResponse(
response,
status_code=200,
)
except Exception as e:
return JSONResponse(str(e), status_code=404)

@router.get(
"/{directory}/{channel}/{target}/{file_type}",
response_class=RedirectResponse,
Expand Down
15 changes: 15 additions & 0 deletions indexer/src/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,21 @@ def reindex(self):
logging.exception(e)
raise e

def get_branch_file_names(self: str, branch: str) -> list[str]:
"""
A method to get a list of file names in the specified branch
Args:
branch: Branch name
Returns:
The list of file names
"""
branch_path = os.path.join(settings.files_dir, self.directory, branch)
if not os.path.isdir(branch_path):
raise FileNotFoundError("Branch not found!")
files = os.listdir(branch_path)
return filter(lambda file: not file.startswith("."), files)

def get_file_from_latest_version(
self: str, channel: str, target: str, file_type: str
) -> str:
Expand Down

0 comments on commit c4ba45e

Please sign in to comment.