Skip to content

Commit

Permalink
Create script folder
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillemdb committed Sep 1, 2024
1 parent 6bf9003 commit c87ac2d
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 3 deletions.
20 changes: 20 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
FROM ubuntu:22.04


RUN apt-get update && \
apt-get install -y --no-install-suggests --no-install-recommends \
make cmake git xvfb build-essential curl ssh wget ca-certificates swig \
python3 python3-dev && \
wget -O - https://bootstrap.pypa.io/get-pip.py | python3

RUN python3 -m pip install uv
COPY src ./src
COPY requirements.lock .
COPY pyproject.toml .
COPY src/plangym/scripts/import_retro_roms.py .
COPY LICENSE .
COPY README.md .
COPY ROMS.zip .

RUN uv pip install --no-cache --system -r requirements.lock
RUN python3 src/plangym/scripts/import_retro_roms.py
2 changes: 1 addition & 1 deletion _old_Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ENV BROWSER=/browser \
COPY Makefile.docker Makefile

RUN apt-get update && \
apt-get install -y --no-install-suggests --no-install-recommends make cmake && \
apt-get install -y --no-install-suggests --no-install-recommends make cmake curl ssh && \
make install-python3.8 && \
make install-common-dependencies && \
make install-python-libs && \
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ style = { chain = ["ruff check --fix-only --unsafe-fixes tests src", "ruff form
check = { chain = ["ruff check --diff tests src", "ruff format --diff tests src"]} #,"mypy src tests" ] }
test = { chain = ["test:doctest", "test:parallel", "test:singlecore"] }
codecov = { chain = ["codecov:parallel", "codecov:singlecore"] }
import-roms = { cmd = "python3 import_retro_roms.py" }
import-roms = { cmd = "python3 src/plangym/scripts/import_retro_roms.py" }
"test:parallel" = { cmd = "pytest -n auto -s -o log_cli=true -o log_cli_level=info tests", env-file = ".multicore.env" }
"test:singlecore" = { cmd = "pytest -s -o log_cli=true -o log_cli_level=info tests/control/test_classic_control.py", env-file = ".onecore.env" }
"test:doctest" = { cmd = "pytest --doctest-modules -n 0 -s -o log_cli=true -o log_cli_level=info src", env-file = ".multicore.env" }
Expand Down
Empty file added src/plangym/scripts/__init__.py
Empty file.
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import os
import sys
import zipfile
import logging

import retro.data

logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")


def _check_zipfile(f, process_f):
with zipfile.ZipFile(f) as zf:
Expand Down Expand Up @@ -36,14 +39,18 @@ def main():
}
EMU_EXTENSIONS.update(emu_extensions)
paths = sys.argv[1:] or ["."]
logging.info(f"Importing ROMs from: {paths}")
logging.info("Fetching known hashes")
known_hashes = retro.data.get_known_hashes()
logging.info(f"Found {len(known_hashes)} known hashes")
imported_games = 0

def save_if_matches(filename, f):
nonlocal imported_games
try:
data, hash = retro.data.groom_rom(filename, f)
except (OSError, ValueError):
logging.warning(f"Failed to process file: {filename}")
return
if hash in known_hashes:
game, ext, curpath = known_hashes[hash]
Expand All @@ -53,20 +60,23 @@ def save_if_matches(filename, f):
with open(rompath, "wb") as file: # noqa: FURB103
file.write(data)
imported_games += 1
logging.info(f"Imported game: {game} to {rompath}")

for path in paths: # noqa: PLR1702
for root, dirs, files in os.walk(path):
for filename in files:
logging.info(f"Processing file: {root}/{filename}")
filepath = os.path.join(root, filename) # noqa: PTH118
with open(filepath, "rb") as f:
_root, ext = os.path.splitext(filename) # noqa: PTH122
if ext == ".zip":
try:
_check_zipfile(f, save_if_matches)
except (zipfile.BadZipFile, RuntimeError, OSError):
pass
logging.error(f"Failed to process zip file: {filepath}")
else:
save_if_matches(filename, f)
logging.info(f"Total imported games: {imported_games}")


if __name__ == "__main__":
Expand Down

0 comments on commit c87ac2d

Please sign in to comment.