Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Asset packs index #1

Merged
merged 41 commits into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
3330943
Initial repo copy
Willy-JL Mar 25, 2024
ce4bf0b
Some progress on packs index structure
Willy-JL Mar 26, 2024
7aea539
Implement pack parser
Willy-JL Mar 26, 2024
c15b1bf
Differenciate API types
Willy-JL Mar 26, 2024
ab0e6f9
Formatting
Willy-JL Mar 26, 2024
1223bd9
Better differenciate API endpoints
Willy-JL Mar 26, 2024
77735e2
Format
Willy-JL Mar 26, 2024
04becae
Merge branch 'dev' of https://github.com/Next-Flip/flipper-update-ind…
Willy-JL May 15, 2024
9bdc502
Description in meta
Willy-JL May 15, 2024
a22983d
Include preview videos
Willy-JL May 15, 2024
77ba10e
Jpeg too
Willy-JL May 15, 2024
b1cd6e0
Merge branch 'dev' of https://github.com/Next-Flip/flipper-update-ind…
Willy-JL May 16, 2024
9d8000e
Merge branch 'dev' of https://github.com/Next-Flip/flipper-update-ind…
Willy-JL May 16, 2024
d0ff4e7
Merge branch 'dev' of https://github.com/Next-Flip/flipper-update-ind…
Willy-JL Jun 3, 2024
747d503
Gzipped packs
Willy-JL Jun 3, 2024
69ec15c
Change title to name
Willy-JL Jun 3, 2024
18f47db
Rebuild packs on reindex
Willy-JL Jun 3, 2024
af62e79
Format
Willy-JL Jun 4, 2024
8c08528
Refactor pack parsing with pathlib
Willy-JL Jun 5, 2024
854c979
Update asset packer from fw repo
Willy-JL Jun 5, 2024
56f3d80
Compute pack stats
Willy-JL Jun 5, 2024
429d24a
Better cleanup
Willy-JL Jun 5, 2024
1a7596a
Fix pack filetypes
Willy-JL Jun 5, 2024
cb063f1
Add some sanity checks and logging
Willy-JL Jun 8, 2024
7588fd7
Warn about too many previews too
Willy-JL Jun 9, 2024
9bb5137
Merge branch 'dev' of https://github.com/Next-Flip/flipper-update-ind…
Willy-JL Jun 25, 2024
1414c28
Merge branch 'dev' of https://github.com/Next-Flip/flipper-update-ind…
Willy-JL Jul 4, 2024
952b238
Merge remote-tracking branch 'mntm/dev' into asset-packs-index
Willy-JL Aug 24, 2024
d80de8d
Format
Willy-JL Aug 24, 2024
5e65e1a
Update asset packer
Willy-JL Aug 24, 2024
c483cf0
Improve warning messages
Willy-JL Aug 24, 2024
51ae026
Offload repacking to separate repo, only parsing and indexing here
Willy-JL Aug 24, 2024
1bcb0b3
Rename to download
Willy-JL Aug 24, 2024
2a9d495
Include files dir skeleton structure
Willy-JL Aug 24, 2024
3fe80dc
Cleanup repacking related things
Willy-JL Aug 24, 2024
d4e839d
Add asset packs submodule
Willy-JL Aug 24, 2024
f836fad
Fix for having files in indexer dirs
Willy-JL Aug 24, 2024
fa8549e
Merge remote-tracking branch 'mntm/dev' into asset-packs-index
Willy-JL Aug 24, 2024
10b8a5e
Merge remote-tracking branch 'mntm/dev' into asset-packs-index
Willy-JL Aug 24, 2024
b075862
Fixes for new FastAPI
Willy-JL Aug 24, 2024
523c662
No None fields
Willy-JL Aug 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
__pycache__
*.swp
venv
files
files/*/*
!files/*/.gitkeep
.env
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "files/asset-packs"]
path = files/asset-packs
url = https://github.com/Next-Flip/Asset-Packs.git
1 change: 1 addition & 0 deletions files/asset-packs
Submodule asset-packs added at 403186
Empty file added files/firmware/.gitkeep
Empty file.
5 changes: 5 additions & 0 deletions indexer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ async def lifespan(app: FastAPI):
os.makedirs(dir_path, exist_ok=True)
except Exception:
logging.exception(f"Failed to create {dir_path}")
logger = logging.getLogger()
prev_level = logger.level
logger.setLevel(logging.INFO)
logging.info("Startup complete")
logger.setLevel(prev_level)

yield

Expand Down
247 changes: 130 additions & 117 deletions indexer/src/directories.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from fastapi import APIRouter
from fastapi.responses import HTMLResponse, JSONResponse, RedirectResponse, FileResponse

from .repository import indexes
from .repository import indexes, RepositoryIndex, PacksCatalog


router = APIRouter()
Expand All @@ -18,123 +18,136 @@ async def root_request():
return RedirectResponse("/firmware", status_code=303)


@router.get("/{directory}/directory.json")
@router.get("/{directory}")
async def directory_request(directory):
"""
Method for obtaining indices
Args:
directory: Repository name

Returns:
Indices in json
"""
if directory not in indexes:
return JSONResponse(f"{directory} not found!", status_code=404)
return indexes.get(directory).index


@router.get(
"/{directory}/{channel}/{target}/{file_type}",
response_class=RedirectResponse,
status_code=302,
)
async def latest_request(directory, channel, target, file_type):
"""
A method for retrieving a file from the repository
of a specific version
Args:
directory: Repository name
channel: Channel type (release, dev)
target: Operating System (linux, mac, win)
file_type: File Type

Returns:
Artifact file
"""
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:
return index.get_file_from_latest_version(channel, target, file_type)
except Exception as e:
return JSONResponse(str(e), status_code=404)


@router.get("/{directory}/{channel}/{file_name}")
async def file_request(directory, channel, file_name):
"""
A method for retrieving a file from the repository
of a specific version
Args:
directory: Repository name
channel: Channel type (release, dev)
file_name: File Name

Returns:
Artifact file
"""
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:
return FileResponse(
index.get_file_path(channel, file_name),
media_type="application/octet-stream",
status_code=200,
)
except Exception as e:
return JSONResponse(str(e), status_code=404)
def setup_routes(prefix: str, index):
@router.get(prefix + "/directory.json")
@router.get(prefix)
async def directory_request():
"""
Method for obtaining indices
Args:
Nothing

Returns:
Indices in json
"""
return index.index

@router.get("/{directory}/reindex")
async def reindex_request(directory):
"""
Method for starting reindexing
Args:
directory: Repository name
if isinstance(index, RepositoryIndex):

Returns:
Reindex status
"""
if directory not in indexes:
return JSONResponse(f"{directory} not found!", status_code=404)
async with lock:
try:
indexes.get(directory).reindex()
return JSONResponse("Reindexing is done!")
except Exception as e:
logging.exception(e)
return JSONResponse("Reindexing is failed!", status_code=500)


@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,
@router.get(
prefix + "/{channel}/{target}/{file_type}",
response_class=RedirectResponse,
status_code=302,
)
except Exception as e:
return JSONResponse(str(e), status_code=404)
async def repository_latest_request(channel, target, file_type):
"""
A method for retrieving a file from the repository
of a specific version
Args:
channel: Channel type (release, dev)
target: Operating System (linux, mac, win)
file_type: File Type

Returns:
Artifact file
"""
if len(index.index["channels"]) == 0:
return JSONResponse("No channels found!", status_code=404)
try:
return index.get_file_from_latest_version(channel, target, file_type)
except Exception as e:
return JSONResponse(str(e), status_code=404)

@router.get(prefix + "/{channel}/{file_name}")
async def repository_file_request(channel, file_name):
"""
A method for retrieving a file from a specific version
Args:
channel: Channel type (release, dev)
file_name: File Name

Returns:
Artifact file
"""
if len(index.index["channels"]) == 0:
return JSONResponse("No channels found!", status_code=404)
try:
return FileResponse(
index.get_file_path(channel, file_name),
media_type="application/octet-stream",
status_code=200,
)
except Exception as e:
return JSONResponse(str(e), status_code=404)

elif isinstance(index, PacksCatalog):

@router.get(prefix + "/{pack}/{file_type}/{file_name}")
async def pack_file_request(pack, file_type, file_name):
"""
A method for retrieving a file from a specific pack
Args:
pack: Pack id
file_type: File Type (download, preview)
file_name: File Name

Returns:
Artifact file
"""
if len(index.index["packs"]) == 0:
return JSONResponse("No packs found!", status_code=404)
try:
return FileResponse(
index.get_file_path(pack, file_type, file_name),
media_type="application/octet-stream",
status_code=200,
)
except Exception as e:
return JSONResponse(str(e), status_code=404)

@router.get(prefix + "/reindex")
async def reindex_request():
"""
Method for starting reindexing
Args:
Nothing

Returns:
Reindex status
"""
async with lock:
try:
index.reindex()
return JSONResponse("Reindexing is done!")
except Exception as e:
logging.exception(e)
return JSONResponse("Reindexing is failed!", status_code=500)

if isinstance(index, RepositoryIndex):

@router.get(prefix + "/{branch}")
async def repository_branch_request(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:
branch: Branch name

Returns:
HTML links in format that ufbt understands
"""
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)


for directory, index in indexes.items():
setup_routes(f"/{directory}", index)
Loading
Loading