diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..f59bd99 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,74 @@ +name: Quality control and automated release +on: + pull_request: # Pull request events (default: open, synchronized, reopened) in any branch triggers the workflow. + push: + branches: + - main # Pushes to main (i.e. after a merged PR) +jobs: + checks_and_release: + runs-on: ubuntu-latest + steps: + - name: Check out repository code + uses: actions/checkout@v3 + - name: Installing Poetry globally + run: pipx install poetry + - name: Installing Python + id: setup-python + uses: actions/setup-python@v4 + with: + python-version: 3.8 + - uses: actions/cache@v3 + with: + path: /home/runner/.cache/pypoetry/virtualenvs + key: poetry-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('poetry.lock') }} + restore-keys: | + poetry-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('poetry.lock') }} + poetry-${{ steps.setup-python.outputs.python-version }}- + + - name: Install dependencies + run: | + sudo apt update + sudo apt install -y \ + dpkg-dev \ + build-essential \ + freeglut3-dev \ + libgl1-mesa-dev \ + libglu1-mesa-dev \ + libgstreamer-plugins-base1.0-dev \ + libgtk-3-dev \ + libjpeg-dev \ + libnotify-dev \ + libpng-dev \ + libsdl2-dev \ + libsm-dev \ + libunwind-dev \ + libtiff-dev \ + libwebkit2gtk-4.0-dev \ + libxtst-dev \ + libgtk2.0-dev + + - name: Installing Poetry environment + run: poetry install + - name: Running pytest + id: pytest + run: poetry run pytest -v + - name: Running mypy + id: mypy + run: poetry run mypy libretro_finder/ config/ tests/ + - name: Running pylint + id: pylint + run: poetry run pylint libretro_finder/ config/ tests/ --fail-under=8 + - name: Checking code coverage + id: coverage + run: poetry run pytest --cov=config --cov=libretro_finder --cov-fail-under=75 + + - name: Build source and .whl archives with Poetry + id: build + run: poetry build + if: steps.pytest.outcome == 'success' && steps.mypy.outcome == 'success' && steps.pylint.outcome == 'success' && steps.coverage.outcome == 'success' + - name: Authorize GitHub Actions to publish on PYPI + run: poetry config pypi-token.pypi ${{ secrets.PYPI_API_TOKEN }} + if: steps.build.outcome == 'success' && github.event_name == 'push' + - name: Publish on PYPI + run: poetry publish + if: steps.build.outcome == 'success' && github.event_name == 'push' diff --git a/.github/workflows/test_release.yml b/.github/workflows/test_release.yml new file mode 100644 index 0000000..02949da --- /dev/null +++ b/.github/workflows/test_release.yml @@ -0,0 +1,74 @@ +name: Build and release on TestPyPi +on: + workflow_dispatch: # Manual trigger (dev) +jobs: + checks_and_release: + runs-on: ubuntu-latest + steps: + - name: Check out repository code + uses: actions/checkout@v3 + - name: Installing Poetry globally + run: pipx install poetry + - name: Installing Python + id: setup-python + uses: actions/setup-python@v4 + with: + python-version: 3.8 + - uses: actions/cache@v3 + with: + path: /home/runner/.cache/pypoetry/virtualenvs + key: poetry-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('poetry.lock') }} + restore-keys: | + poetry-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('poetry.lock') }} + poetry-${{ steps.setup-python.outputs.python-version }}- + + - name: Install dependencies + run: | + sudo apt update + sudo apt install -y \ + dpkg-dev \ + build-essential \ + freeglut3-dev \ + libgl1-mesa-dev \ + libglu1-mesa-dev \ + libgstreamer-plugins-base1.0-dev \ + libgtk-3-dev \ + libjpeg-dev \ + libnotify-dev \ + libpng-dev \ + libsdl2-dev \ + libsm-dev \ + libunwind-dev \ + libtiff-dev \ + libwebkit2gtk-4.0-dev \ + libxtst-dev \ + libgtk2.0-dev + + - name: Installing Poetry environment + run: poetry install + - name: Running pytest + id: pytest + run: poetry run pytest -v + - name: Running mypy + id: mypy + run: poetry run mypy libretro_finder/ config/ tests/ + - name: Running pylint + id: pylint + run: poetry run pylint libretro_finder/ config/ tests/ --fail-under=8 + - name: Checking code coverage + id: coverage + run: poetry run pytest --cov=config --cov=libretro_finder --cov-fail-under=75 + + - name: Build source and .whl archives with Poetry + id: build + run: poetry build + if: steps.pytest.outcome == 'success' && steps.mypy.outcome == 'success' && steps.pylint.outcome == 'success' && steps.coverage.outcome == 'success' + + - name: Authorize GitHub Actions to publish on PYPI + run: | + poetry config repositories.test-pypi https://test.pypi.org/legacy/ + poetry config pypi-token.test-pypi ${{ secrets.TESTPYPI_API_TOKEN }} + if: steps.build.outcome == 'success' + - name: Publish on PYPI + run: poetry publish -r test-pypi + if: steps.build.outcome == 'success' diff --git a/README.md b/README.md index bd776a6..e6ae314 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,8 @@ [![PyPI](https://img.shields.io/pypi/v/libretro-finder)](https://pypi.org/project/libretro-finder/) ![PyPI - Python Version](https://img.shields.io/pypi/pyversions/libretro-finder) ![PyPI - License](https://img.shields.io/pypi/l/libretro-finder) +[![Build passing (main)](https://github.com/jaspersiebring/libretro_finder/actions/workflows/main.yml/badge.svg?branch=main&event=push)](https://github.com/jaspersiebring/libretro_finder/actions/workflows/main.yml) + Simple tool that finds and prepares your BIOS files for usage with Libretro (or its RetroArch frontend). @@ -89,5 +91,5 @@ If `libretro_finder` is called without any additional arguments, LibretroFinder ### Missing features? Have some feedback? Let me know! -- [My Reddit account](https://www.reddit.com/user/qtieb/) -- [My Github account](https://github.com/jaspersiebring) \ No newline at end of file +- [Open a Github issue](https://github.com/jaspersiebring) +- [Message me on Reddit ](https://www.reddit.com/user/qtieb/) \ No newline at end of file diff --git a/config/__init__.py b/config/__init__.py index cef5678..db3f6c2 100644 --- a/config/__init__.py +++ b/config/__init__.py @@ -20,7 +20,8 @@ # Parsing Libretro's system.dat and formatting as pandas dataframe index = 0 # pylint: disable=invalid-name -SYSTEMS = [] + +system_series = [] with open(FILE_PATH, "r", encoding="utf-8") as file: for line in file: line = line.strip() @@ -31,27 +32,27 @@ r"name (\S+)(?: size (\S+))?(?: crc (\S+))?(?: md5 (\S+))?(?: sha1 (\S+))?", line, ) - data = { - "system": current_system, - "name": match.group(1).replace('"', "").replace("'", ""), - "size": match.group(2) if match.group(2) else None, - "crc": match.group(3) if match.group(3) else None, - "md5": match.group(4) if match.group(4) else None, - "sha1": match.group(5) if match.group(5) else None, - } - SYSTEMS.append(pd.DataFrame(data, index=[index])) - index += 1 + if match: + data = { + "system": current_system, + "name": match.group(1).replace('"', "").replace("'", ""), + "size": match.group(2) if match.group(2) else None, + "crc": match.group(3) if match.group(3) else None, + "md5": match.group(4) if match.group(4) else None, + "sha1": match.group(5) if match.group(5) else None, + } + system_series.append(pd.DataFrame(data, index=[index])) + index += 1 # join dfs and drop features without checksums -SYSTEMS = pd.concat(SYSTEMS) +SYSTEMS = pd.concat(system_series) SYSTEMS = SYSTEMS[~SYSTEMS["md5"].isnull()].reset_index(drop=True) # path to retroarch/system (if found) RETROARCH_PATH = find_retroarch() - # 'cli' if user passes arguments else 'start gui' # Needs to be present before the @Gooey decorator (https://github.com/chriskiehl/Gooey/issues/449) if len(sys.argv) >= 2: - if not "--ignore-gooey" in sys.argv: + if "--ignore-gooey" not in sys.argv: sys.argv.append("--ignore-gooey") diff --git a/libretro_finder/main.py b/libretro_finder/main.py index 0e8bed8..531f843 100644 --- a/libretro_finder/main.py +++ b/libretro_finder/main.py @@ -1,11 +1,10 @@ import shutil import pathlib +from typing import List, Optional import numpy as np -from gooey import Gooey, GooeyParser - +from gooey import Gooey, GooeyParser # type: ignore from config import SYSTEMS as system_df from config import RETROARCH_PATH -from typing import List, Optional from libretro_finder.utils import match_arrays, recursive_hash @@ -17,7 +16,6 @@ def organize(search_dir: pathlib.Path, output_dir: pathlib.Path) -> None: :param search_dir: starting location of recursive search :param output_dir: path to output directory (will be created if it doesn't exist) - :return: """ # Indexing files to be checked for matching MD5 checksums @@ -100,7 +98,7 @@ def main(argv: Optional[List[str]] = None) -> None: if not search_directory.exists(): raise FileNotFoundError("Search directory does not exist..") - elif not search_directory.is_dir(): + if not search_directory.is_dir(): raise NotADirectoryError("Search directory needs to be a directory..") organize(search_dir=search_directory, output_dir=output_directory) diff --git a/libretro_finder/utils.py b/libretro_finder/utils.py index 6bfee9a..29963aa 100644 --- a/libretro_finder/utils.py +++ b/libretro_finder/utils.py @@ -2,12 +2,13 @@ import concurrent.futures import hashlib import pathlib -from typing import Tuple, Optional, List, Union -from tqdm import tqdm -import numpy as np -import vdf +from typing import Tuple, Optional, List import platform from string import ascii_uppercase +from tqdm import tqdm +import numpy as np +import vdf # type: ignore + # not expecting BIOS files over 15mb MAX_BIOS_BYTES = 15728640 @@ -34,7 +35,7 @@ def recursive_hash( :param directory: Starting directory for the glob pattern matching :param glob: The glob pattern to match files. Defaults to "*". - :return: An array with the file_paths to selected files and an array with the corresponding MD5 hashes + :return: array with file_paths to selected files and an array with corresponding MD5 hashes """ file_paths = list(directory.rglob(pattern=glob)) @@ -65,8 +66,8 @@ def match_arrays( :param array_a: The first array to compare. :param array_b: The second array to compare. - :return: A tuple of three numpy arrays: unique matching values, indices of the matching values in - array_a and indices of the matching values in array_b. + :return: A tuple of three numpy arrays: unique matching values, indices of the matching values + in array_a and indices of the matching values in array_b. """ # expecting 1D arrays @@ -123,10 +124,10 @@ def find_retroarch() -> Optional[pathlib.Path]: env_vars = ["PROGRAMFILES(X86)", "PROGRAMFILES"] for env_var in env_vars: - env_var = os.environ.get(env_var) - if not env_var: + env_value = os.environ.get(env_var) + if not env_value: continue - env_path = pathlib.Path(env_var) + env_path = pathlib.Path(env_value) if env_path.exists(): paths_to_check.append(env_path) @@ -166,7 +167,7 @@ def find_retroarch() -> Optional[pathlib.Path]: # checking for retroarch/system (one level down) for path_to_check in paths_to_check: - # glob is needed for inconsistent parent naming (e.g. RetroArch-Win32, RetroArch-Win64, retroarch) + # glob is needed for inconsistent parent naming (e.g. RetroArch-Win32, retroarch) for path in path_to_check.glob(system_glob): return path return None diff --git a/poetry.lock b/poetry.lock index 425cdce..9cc48a2 100644 --- a/poetry.lock +++ b/poetry.lock @@ -117,6 +117,82 @@ files = [ {file = "colored-1.4.4.tar.gz", hash = "sha256:04ff4d4dd514274fe3b99a21bb52fb96f2688c01e93fba7bef37221e7cb56ce0"}, ] +[[package]] +name = "coverage" +version = "7.2.7" +description = "Code coverage measurement for Python" +category = "dev" +optional = false +python-versions = ">=3.7" +files = [ + {file = "coverage-7.2.7-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:d39b5b4f2a66ccae8b7263ac3c8170994b65266797fb96cbbfd3fb5b23921db8"}, + {file = "coverage-7.2.7-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:6d040ef7c9859bb11dfeb056ff5b3872436e3b5e401817d87a31e1750b9ae2fb"}, + {file = "coverage-7.2.7-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:ba90a9563ba44a72fda2e85302c3abc71c5589cea608ca16c22b9804262aaeb6"}, + {file = "coverage-7.2.7-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:e7d9405291c6928619403db1d10bd07888888ec1abcbd9748fdaa971d7d661b2"}, + {file = "coverage-7.2.7-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:31563e97dae5598556600466ad9beea39fb04e0229e61c12eaa206e0aa202063"}, + {file = "coverage-7.2.7-cp310-cp310-musllinux_1_1_aarch64.whl", hash = "sha256:ebba1cd308ef115925421d3e6a586e655ca5a77b5bf41e02eb0e4562a111f2d1"}, + {file = "coverage-7.2.7-cp310-cp310-musllinux_1_1_i686.whl", hash = "sha256:cb017fd1b2603ef59e374ba2063f593abe0fc45f2ad9abdde5b4d83bd922a353"}, + {file = "coverage-7.2.7-cp310-cp310-musllinux_1_1_x86_64.whl", hash = "sha256:d62a5c7dad11015c66fbb9d881bc4caa5b12f16292f857842d9d1871595f4495"}, + {file = "coverage-7.2.7-cp310-cp310-win32.whl", hash = "sha256:ee57190f24fba796e36bb6d3aa8a8783c643d8fa9760c89f7a98ab5455fbf818"}, + {file = "coverage-7.2.7-cp310-cp310-win_amd64.whl", hash = "sha256:f75f7168ab25dd93110c8a8117a22450c19976afbc44234cbf71481094c1b850"}, + {file = "coverage-7.2.7-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:06a9a2be0b5b576c3f18f1a241f0473575c4a26021b52b2a85263a00f034d51f"}, + {file = "coverage-7.2.7-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:5baa06420f837184130752b7c5ea0808762083bf3487b5038d68b012e5937dbe"}, + {file = "coverage-7.2.7-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:fdec9e8cbf13a5bf63290fc6013d216a4c7232efb51548594ca3631a7f13c3a3"}, + {file = "coverage-7.2.7-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:52edc1a60c0d34afa421c9c37078817b2e67a392cab17d97283b64c5833f427f"}, + {file = "coverage-7.2.7-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:63426706118b7f5cf6bb6c895dc215d8a418d5952544042c8a2d9fe87fcf09cb"}, + {file = "coverage-7.2.7-cp311-cp311-musllinux_1_1_aarch64.whl", hash = "sha256:afb17f84d56068a7c29f5fa37bfd38d5aba69e3304af08ee94da8ed5b0865833"}, + {file = "coverage-7.2.7-cp311-cp311-musllinux_1_1_i686.whl", hash = "sha256:48c19d2159d433ccc99e729ceae7d5293fbffa0bdb94952d3579983d1c8c9d97"}, + {file = "coverage-7.2.7-cp311-cp311-musllinux_1_1_x86_64.whl", hash = "sha256:0e1f928eaf5469c11e886fe0885ad2bf1ec606434e79842a879277895a50942a"}, + {file = "coverage-7.2.7-cp311-cp311-win32.whl", hash = "sha256:33d6d3ea29d5b3a1a632b3c4e4f4ecae24ef170b0b9ee493883f2df10039959a"}, + {file = "coverage-7.2.7-cp311-cp311-win_amd64.whl", hash = "sha256:5b7540161790b2f28143191f5f8ec02fb132660ff175b7747b95dcb77ac26562"}, + {file = "coverage-7.2.7-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:f2f67fe12b22cd130d34d0ef79206061bfb5eda52feb6ce0dba0644e20a03cf4"}, + {file = "coverage-7.2.7-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:a342242fe22407f3c17f4b499276a02b01e80f861f1682ad1d95b04018e0c0d4"}, + {file = "coverage-7.2.7-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:171717c7cb6b453aebac9a2ef603699da237f341b38eebfee9be75d27dc38e01"}, + {file = "coverage-7.2.7-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:49969a9f7ffa086d973d91cec8d2e31080436ef0fb4a359cae927e742abfaaa6"}, + {file = "coverage-7.2.7-cp312-cp312-musllinux_1_1_aarch64.whl", hash = "sha256:b46517c02ccd08092f4fa99f24c3b83d8f92f739b4657b0f146246a0ca6a831d"}, + {file = "coverage-7.2.7-cp312-cp312-musllinux_1_1_i686.whl", hash = "sha256:a3d33a6b3eae87ceaefa91ffdc130b5e8536182cd6dfdbfc1aa56b46ff8c86de"}, + {file = "coverage-7.2.7-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:976b9c42fb2a43ebf304fa7d4a310e5f16cc99992f33eced91ef6f908bd8f33d"}, + {file = "coverage-7.2.7-cp312-cp312-win32.whl", hash = "sha256:8de8bb0e5ad103888d65abef8bca41ab93721647590a3f740100cd65c3b00511"}, + {file = "coverage-7.2.7-cp312-cp312-win_amd64.whl", hash = "sha256:9e31cb64d7de6b6f09702bb27c02d1904b3aebfca610c12772452c4e6c21a0d3"}, + {file = "coverage-7.2.7-cp37-cp37m-macosx_10_9_x86_64.whl", hash = "sha256:58c2ccc2f00ecb51253cbe5d8d7122a34590fac9646a960d1430d5b15321d95f"}, + {file = "coverage-7.2.7-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:d22656368f0e6189e24722214ed8d66b8022db19d182927b9a248a2a8a2f67eb"}, + {file = "coverage-7.2.7-cp37-cp37m-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:a895fcc7b15c3fc72beb43cdcbdf0ddb7d2ebc959edac9cef390b0d14f39f8a9"}, + {file = "coverage-7.2.7-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:e84606b74eb7de6ff581a7915e2dab7a28a0517fbe1c9239eb227e1354064dcd"}, + {file = "coverage-7.2.7-cp37-cp37m-musllinux_1_1_aarch64.whl", hash = "sha256:0a5f9e1dbd7fbe30196578ca36f3fba75376fb99888c395c5880b355e2875f8a"}, + {file = "coverage-7.2.7-cp37-cp37m-musllinux_1_1_i686.whl", hash = "sha256:419bfd2caae268623dd469eff96d510a920c90928b60f2073d79f8fe2bbc5959"}, + {file = "coverage-7.2.7-cp37-cp37m-musllinux_1_1_x86_64.whl", hash = "sha256:2aee274c46590717f38ae5e4650988d1af340fe06167546cc32fe2f58ed05b02"}, + {file = "coverage-7.2.7-cp37-cp37m-win32.whl", hash = "sha256:61b9a528fb348373c433e8966535074b802c7a5d7f23c4f421e6c6e2f1697a6f"}, + {file = "coverage-7.2.7-cp37-cp37m-win_amd64.whl", hash = "sha256:b1c546aca0ca4d028901d825015dc8e4d56aac4b541877690eb76490f1dc8ed0"}, + {file = "coverage-7.2.7-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:54b896376ab563bd38453cecb813c295cf347cf5906e8b41d340b0321a5433e5"}, + {file = "coverage-7.2.7-cp38-cp38-macosx_11_0_arm64.whl", hash = "sha256:3d376df58cc111dc8e21e3b6e24606b5bb5dee6024f46a5abca99124b2229ef5"}, + {file = "coverage-7.2.7-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:5e330fc79bd7207e46c7d7fd2bb4af2963f5f635703925543a70b99574b0fea9"}, + {file = "coverage-7.2.7-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:1e9d683426464e4a252bf70c3498756055016f99ddaec3774bf368e76bbe02b6"}, + {file = "coverage-7.2.7-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:8d13c64ee2d33eccf7437961b6ea7ad8673e2be040b4f7fd4fd4d4d28d9ccb1e"}, + {file = "coverage-7.2.7-cp38-cp38-musllinux_1_1_aarch64.whl", hash = "sha256:b7aa5f8a41217360e600da646004f878250a0d6738bcdc11a0a39928d7dc2050"}, + {file = "coverage-7.2.7-cp38-cp38-musllinux_1_1_i686.whl", hash = "sha256:8fa03bce9bfbeeef9f3b160a8bed39a221d82308b4152b27d82d8daa7041fee5"}, + {file = "coverage-7.2.7-cp38-cp38-musllinux_1_1_x86_64.whl", hash = "sha256:245167dd26180ab4c91d5e1496a30be4cd721a5cf2abf52974f965f10f11419f"}, + {file = "coverage-7.2.7-cp38-cp38-win32.whl", hash = "sha256:d2c2db7fd82e9b72937969bceac4d6ca89660db0a0967614ce2481e81a0b771e"}, + {file = "coverage-7.2.7-cp38-cp38-win_amd64.whl", hash = "sha256:2e07b54284e381531c87f785f613b833569c14ecacdcb85d56b25c4622c16c3c"}, + {file = "coverage-7.2.7-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:537891ae8ce59ef63d0123f7ac9e2ae0fc8b72c7ccbe5296fec45fd68967b6c9"}, + {file = "coverage-7.2.7-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:06fb182e69f33f6cd1d39a6c597294cff3143554b64b9825d1dc69d18cc2fff2"}, + {file = "coverage-7.2.7-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:201e7389591af40950a6480bd9edfa8ed04346ff80002cec1a66cac4549c1ad7"}, + {file = "coverage-7.2.7-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:f6951407391b639504e3b3be51b7ba5f3528adbf1a8ac3302b687ecababf929e"}, + {file = "coverage-7.2.7-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:6f48351d66575f535669306aa7d6d6f71bc43372473b54a832222803eb956fd1"}, + {file = "coverage-7.2.7-cp39-cp39-musllinux_1_1_aarch64.whl", hash = "sha256:b29019c76039dc3c0fd815c41392a044ce555d9bcdd38b0fb60fb4cd8e475ba9"}, + {file = "coverage-7.2.7-cp39-cp39-musllinux_1_1_i686.whl", hash = "sha256:81c13a1fc7468c40f13420732805a4c38a105d89848b7c10af65a90beff25250"}, + {file = "coverage-7.2.7-cp39-cp39-musllinux_1_1_x86_64.whl", hash = "sha256:975d70ab7e3c80a3fe86001d8751f6778905ec723f5b110aed1e450da9d4b7f2"}, + {file = "coverage-7.2.7-cp39-cp39-win32.whl", hash = "sha256:7ee7d9d4822c8acc74a5e26c50604dff824710bc8de424904c0982e25c39c6cb"}, + {file = "coverage-7.2.7-cp39-cp39-win_amd64.whl", hash = "sha256:eb393e5ebc85245347950143969b241d08b52b88a3dc39479822e073a1a8eb27"}, + {file = "coverage-7.2.7-pp37.pp38.pp39-none-any.whl", hash = "sha256:b7b4c971f05e6ae490fef852c218b0e79d4e52f79ef0c8475566584a8fb3e01d"}, + {file = "coverage-7.2.7.tar.gz", hash = "sha256:924d94291ca674905fe9481f12294eb11f2d3d3fd1adb20314ba89e94f44ed59"}, +] + +[package.dependencies] +tomli = {version = "*", optional = true, markers = "python_full_version <= \"3.11.0a6\" and extra == \"toml\""} + +[package.extras] +toml = ["tomli"] + [[package]] name = "dill" version = "0.3.7" @@ -446,6 +522,22 @@ sql-other = ["SQLAlchemy (>=1.4.16)"] test = ["hypothesis (>=6.34.2)", "pytest (>=7.3.2)", "pytest-asyncio (>=0.17.0)", "pytest-xdist (>=2.2.0)"] xml = ["lxml (>=4.6.3)"] +[[package]] +name = "pandas-stubs" +version = "2.0.2.230605" +description = "Type annotations for pandas" +category = "dev" +optional = false +python-versions = ">=3.8" +files = [ + {file = "pandas_stubs-2.0.2.230605-py3-none-any.whl", hash = "sha256:39106b602f3cb6dc5f728b84e1b32bde6ecf41ee34ee714c66228009609fbada"}, + {file = "pandas_stubs-2.0.2.230605.tar.gz", hash = "sha256:624c7bb06d38145a44b61be459ccd19b038e0bf20364a025ecaab78fea65e858"}, +] + +[package.dependencies] +numpy = ">=1.24.3" +types-pytz = ">=2022.1.1" + [[package]] name = "pathspec" version = "0.11.1" @@ -710,6 +802,25 @@ tomli = {version = ">=1.0.0", markers = "python_version < \"3.11\""} [package.extras] testing = ["argcomplete", "attrs (>=19.2.0)", "hypothesis (>=3.56)", "mock", "nose", "pygments (>=2.7.2)", "requests", "setuptools", "xmlschema"] +[[package]] +name = "pytest-cov" +version = "4.1.0" +description = "Pytest plugin for measuring coverage." +category = "dev" +optional = false +python-versions = ">=3.7" +files = [ + {file = "pytest-cov-4.1.0.tar.gz", hash = "sha256:3904b13dfbfec47f003b8e77fd5b589cd11904a21ddf1ab38a64f204d6a10ef6"}, + {file = "pytest_cov-4.1.0-py3-none-any.whl", hash = "sha256:6ba70b9e97e69fcc3fb45bfeab2d0a138fb65c4d0d6a41ef33983ad114be8c3a"}, +] + +[package.dependencies] +coverage = {version = ">=5.2.1", extras = ["toml"]} +pytest = ">=4.6" + +[package.extras] +testing = ["fields", "hunter", "process-tests", "pytest-xdist", "six", "virtualenv"] + [[package]] name = "pytest-mock" version = "3.11.1" @@ -841,6 +952,30 @@ notebook = ["ipywidgets (>=6)"] slack = ["slack-sdk"] telegram = ["requests"] +[[package]] +name = "types-pytz" +version = "2023.3.0.0" +description = "Typing stubs for pytz" +category = "dev" +optional = false +python-versions = "*" +files = [ + {file = "types-pytz-2023.3.0.0.tar.gz", hash = "sha256:ecdc70d543aaf3616a7e48631543a884f74205f284cefd6649ddf44c6a820aac"}, + {file = "types_pytz-2023.3.0.0-py3-none-any.whl", hash = "sha256:4fc2a7fbbc315f0b6630e0b899fd6c743705abe1094d007b0e612d10da15e0f3"}, +] + +[[package]] +name = "types-tqdm" +version = "4.65.0.2" +description = "Typing stubs for tqdm" +category = "dev" +optional = false +python-versions = "*" +files = [ + {file = "types-tqdm-4.65.0.2.tar.gz", hash = "sha256:ad07ee08e758cad04299543b2586ef1d3fd5a10c54271457de13e0cba009cc5d"}, + {file = "types_tqdm-4.65.0.2-py3-none-any.whl", hash = "sha256:956d92e921651309ffe36a783cc10d41d753f107c84e82d84373c84d68737246"}, +] + [[package]] name = "typing-extensions" version = "4.7.1" @@ -996,4 +1131,4 @@ six = "*" [metadata] lock-version = "2.0" python-versions = ">=3.8,<3.13" -content-hash = "d32e3cc909ccd2fc2758726be5fba2cc9988663ff7b9fe7287c2ae1fd90c9eb8" +content-hash = "c85aecbb131d55e7e08808afd66c161ab98f2ad62a1686bbb3f664bb76479462" diff --git a/pyproject.toml b/pyproject.toml index 934165e..c2e8e42 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,7 +1,7 @@ [tool.poetry] name = "libretro-finder" -version = "0.2.1" -description = "Command line utility that looks for specific BIOS files for libretro cores and, if found, refactors them to the expected format (i.e. name and directory structure)." +version = "0.2.2" +description = "Simple tool that finds and prepares your BIOS files for usage with Libretro (or its RetroArch frontend)." authors = ["Jasper Siebring "] license = "GNU General Public License v3.0" homepage = "https://github.com/jaspersiebring/libretro_finder" @@ -29,10 +29,14 @@ black = "^23.7.0" mypy = "^1.4.1" pyinstaller = "^5.13.0" pytest-mock = "^3.11.1" +pandas-stubs = "^2.0.2.230605" +types-tqdm = "^4.65.0.2" +pytest-cov = "^4.1.0" [build-system] requires = ["poetry-core"] build-backend = "poetry.core.masonry.api" [tool.pylint.'MESSAGES CONTROL'] -disable = "C0114" \ No newline at end of file +disable = "C0114, R0912, R0914" +max-line-length = 100 \ No newline at end of file diff --git a/tests/fixtures.py b/tests/fixtures.py index 5e4a446..dd06009 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -3,7 +3,6 @@ import os import pathlib from typing import Tuple -import tempfile import pandas as pd import pytest @@ -17,10 +16,10 @@ @pytest.fixture(scope="function") def setup_files(tmp_path_factory: TempPathFactory) -> Tuple[pathlib.Path, pd.DataFrame]: """ - Pytest fixture that creates a temporary directory, populates it with fake BIOS files and - returns the path to said directory to be used in tests that expect BIOS files. The function - also updates the reference pandas dataFrame with the CRC32, MD5, and SHA1 hashes of the - dummy files (needed since hashes for the dummy files won't match the checksums for the actual + Pytest fixture that creates a temporary directory, populates it with fake BIOS files and + returns the path to said directory to be used in tests that expect BIOS files. The function + also updates the reference pandas dataFrame with the CRC32, MD5, and SHA1 hashes of the + dummy files (needed since hashes for the dummy files won't match the checksums for the actual BIOS files) :param tmp_path_factory: A pytest fixture for creating temporary directories. diff --git a/tests/test_main.py b/tests/test_main.py index 556b486..7433b98 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -1,22 +1,29 @@ -import os +# pylint: disable=redefined-outer-name import pathlib -import stat import pytest import numpy as np -from pytest import MonkeyPatch, TempdirFactory, TempPathFactory -from pytest_mock import MockerFixture -from pytest_mock import mocker +from pytest import MonkeyPatch +from pytest_mock import MockerFixture, mocker # pylint: disable=unused-import from libretro_finder.main import organize, main from libretro_finder.utils import hash_file from tests import TEST_SAMPLE_SIZE -from tests.fixtures import setup_files +from tests.fixtures import setup_files # pylint: disable=unused-import -class Test_organize: +class TestOrganize: + """Bundle of pytest asserts for main.organize""" + def test_matching( self, setup_files, tmp_path: pathlib.Path, monkeypatch: MonkeyPatch ) -> None: + """main.organize with (only) matching files + + :param setup_files: A pytest fixture that generates fake BIOS files and reference dataframe + :param tmp_path: A pytest fixture that creates a temporary directory unique to this test + :param monkeypatch: A pytest fixture that allows us to set certain testing conditions + """ + bios_dir, bios_lut = setup_files assert bios_dir.exists() @@ -55,7 +62,12 @@ def test_matching( assert np.all(np.isin(bios_lut["name"].values, output_names)) def test_non_matching(self, setup_files, tmp_path: pathlib.Path) -> None: - # pretty much matching but we don't monkeypatch so we're comparing different hashes + """main.organize without matching files + + :param setup_files: A pytest fixture that generates fake BIOS files and reference dataframe + :param tmp_path: A pytest fixture that creates a temporary directory unique to this test + """ + # same as 'matching' test but without monkeypatching (i.e. different hashes so no matches) bios_dir, _ = setup_files assert bios_dir.exists() @@ -80,6 +92,11 @@ def test_non_matching(self, setup_files, tmp_path: pathlib.Path) -> None: assert len(list(output_dir.rglob("*"))) == 0 def test_empty(self, tmp_path: pathlib.Path) -> None: + """main.organize with empty search_dir + + :param tmp_path: A pytest fixture that creates a temporary directory unique to this test + """ + input_dir = tmp_path / "input" output_dir = tmp_path / "output" input_dir.mkdir() @@ -95,12 +112,14 @@ def test_empty(self, tmp_path: pathlib.Path) -> None: organize(search_dir=input_dir, output_dir=output_dir) assert len(list(output_dir.rglob("*"))) == 0 - def test_same_input( - self, setup_files, monkeypatch: MonkeyPatch - ) -> None: - # organize but with (prepopulated) bios_dir as input and output - # verifies if non-additive + def test_same_input(self, setup_files, monkeypatch: MonkeyPatch) -> None: + """main.organize with shared input for search_dir and output_dir (verifies if non-additive) + + :param setup_files: A pytest fixture that generates fake BIOS files and reference dataframe + :param monkeypatch: A pytest fixture that allows us to set certain testing conditions + """ + # organize but with (prepopulated) bios_dir as input and output bios_dir, bios_lut = setup_files assert bios_dir.exists() @@ -131,10 +150,18 @@ def test_same_input( assert np.all(np.isin(bios_lut["name"].values, output_names)) -class Test_main: +class TestMain: + """Bundle of pytest asserts for main.main""" + def test_main(self, tmp_path: pathlib.Path, mocker: MockerFixture): + """main.main with valid input + + :param tmp_path: A pytest fixture that creates a temporary directory unique to this test + :param mocker: A pytest fixture that mocks specific objects for testing purposes + """ + # Mocking the organize function to prevent actual file operations - mock_organize = mocker.patch('libretro_finder.main.organize') + mock_organize = mocker.patch("libretro_finder.main.organize") search_dir = tmp_path / "search" output_dir = tmp_path / "output" @@ -143,17 +170,29 @@ def test_main(self, tmp_path: pathlib.Path, mocker: MockerFixture): argv = [str(search_dir), str(output_dir)] main(argv) - mock_organize.assert_called_once_with(search_dir=search_dir, output_dir=output_dir) + mock_organize.assert_called_once_with( + search_dir=search_dir, output_dir=output_dir + ) def test_main_search_directory_not_exists(self, tmp_path: pathlib.Path): + """main.main with non-existent search_dir + + :param tmp_path: A pytest fixture that creates a temporary directory unique to this test + """ + output_dir = tmp_path / "output" output_dir.mkdir() - argv = ['/path/to/nonexistent/search', str(output_dir)] + argv = ["/path/to/nonexistent/search", str(output_dir)] with pytest.raises(FileNotFoundError): main(argv) def test_main_search_directory_not_directory(self, tmp_path: pathlib.Path): + """main.main with file as search_dir + + :param tmp_path: A pytest fixture that creates a temporary directory unique to this test + """ + file_path = tmp_path / "search.txt" output_dir = tmp_path / "output" file_path.touch() @@ -161,4 +200,4 @@ def test_main_search_directory_not_directory(self, tmp_path: pathlib.Path): argv = [str(file_path), str(output_dir)] with pytest.raises(NotADirectoryError): - main(argv) \ No newline at end of file + main(argv) diff --git a/tests/test_utils.py b/tests/test_utils.py index bc47c44..939910a 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,3 +1,4 @@ +# pylint: disable=redefined-outer-name import hashlib import os import pathlib @@ -6,19 +7,26 @@ import numpy as np import pandas as pd import pytest -from pytest import TempdirFactory, TempPathFactory from libretro_finder.utils import hash_file, match_arrays, recursive_hash from tests import TEST_BYTES, TEST_SAMPLE_SIZE -from tests.fixtures import setup_files +from tests.fixtures import setup_files # pylint: disable=unused-import def test_libretro_meta_import(): - from config import SYSTEMS + """verifies that (remote) libretro-database can pulled, parsed and dumped to pandas DataFrame""" + from config import SYSTEMS # pylint: disable=W0611,C0415 -class Test_hash_file: +class TestHashFile: + """Bundle of pytest asserts for utils.hash_file""" + def test_existing(self, tmp_path: pathlib.Path) -> None: + """utils.hash_file with existing input + + :param tmp_path: A pytest fixture that creates a temporary directory unique to this test + """ + file_path = tmp_path / "some_file" random_bytes = os.urandom(TEST_BYTES) @@ -27,14 +35,25 @@ def test_existing(self, tmp_path: pathlib.Path) -> None: _ = hash_file(file_path) def test_nonexistent(self, tmp_path: pathlib.Path) -> None: + """utils.hash_file with non-existing input + + :param tmp_path: A pytest fixture that creates a temporary directory unique to this test + """ + file_path = tmp_path / "some_file" with pytest.raises(FileNotFoundError): hash_file(file_path) -class Test_recursive_hash: +class TestRecursiveHash: + """Bundle of pytest asserts for utils.recursive_hash""" + def test_with_fixture(self, setup_files: Tuple[pathlib.Path, pd.DataFrame]) -> None: - """populate tempdir exclusievly with matching files""" + """utils.recursive_hash with (exclusively) matching files + + :param setup_files: A pytest fixture that generates fake BIOS files and reference dataframe + """ + bios_dir, bios_lut = setup_files file_paths, file_hashes = recursive_hash(directory=bios_dir, glob="*") @@ -47,12 +66,17 @@ def test_with_fixture(self, setup_files: Tuple[pathlib.Path, pd.DataFrame]) -> N assert np.all(np.isin(file_names, bios_lut["name"].values)) # type: ignore index_a, index_b = np.where( - file_names.reshape(1, -1) == bios_lut["name"].values.reshape(-1, 1) + file_names.reshape(1, -1) == bios_lut["name"].values.reshape(-1, 1) # type: ignore ) assert np.array_equal(file_names[index_b], bios_lut["name"].values[index_a]) assert np.array_equal(file_hashes[index_b], bios_lut["md5"].values[index_a]) def test_without_fixture(self, tmp_path: pathlib.Path) -> None: + """utils.recursive_hash without matching files + + :param tmp_path: A pytest fixture that creates a temporary directory unique to this test + """ + temp_output_dir = tmp_path / "rhash" temp_output_dir.mkdir() @@ -61,8 +85,9 @@ def test_without_fixture(self, tmp_path: pathlib.Path) -> None: input_paths = [] input_hashes = [] - for i in range(len(file_names)): - temp_path = temp_output_dir / file_names[i] + + for i, file_name in enumerate(file_names): + temp_path = temp_output_dir / file_name md5 = hashlib.md5(file_bytes[i]).hexdigest() input_hashes.append(md5) input_paths.append(temp_path) @@ -77,12 +102,16 @@ def test_without_fixture(self, tmp_path: pathlib.Path) -> None: assert np.unique(output_hashes).size == output_hashes.size assert len(input_paths) == len(output_paths) - assert np.all(np.isin(input_paths, output_paths)) + assert np.all(np.isin(input_paths, output_paths)) # type: ignore assert np.all(np.isin(input_hashes, output_hashes)) -class Test_match_arrays: +class TestMatchArrays: + """Bundle of pytest asserts for utils.match_arrays""" + def test_overlap(self): + """utils.match_arrays with partial overlap""" + array_a = np.array([0, 1, 2, 3, 4, 5]) array_b = np.array([5, 2, 5, 16]) @@ -95,6 +124,8 @@ def test_overlap(self): assert np.array_equal(array_a[indices_a], array_b[indices_b]) def test_no_overlap(self): + """utils.match_arrays with no overlap""" + array_a = np.array([0, 1, 2, 3, 4, 5]) array_b = np.array([9, 9, 7, 16]) @@ -107,24 +138,26 @@ def test_no_overlap(self): assert not np.any(indices_b) def test_type(self): + """utils.match_arrays with invalid input type""" + array_a = [0, 1, 2, 3, 4, 5] array_b = np.array([5, 2, 5, 16]) with pytest.raises(AttributeError): - matching_values, indices_a, indices_b = match_arrays( - array_a=array_a, array_b=array_b - ) + _ = match_arrays(array_a=array_a, array_b=array_b) def test_shape(self): + """utils.match_arrays with input arrays of invalid shape""" + array_a = np.array([[0, 1, 2, 3, 4, 5]]) array_b = np.array([[5, 2, 5, 16]]) with pytest.raises(ValueError): - matching_values, indices_a, indices_b = match_arrays( - array_a=array_a, array_b=array_b - ) + _ = match_arrays(array_a=array_a, array_b=array_b) def test_nptype(self): + """utils.match_arrays with valid non-numerical arrays as input""" + array_a = np.array(["a", "b"]) array_b = np.array(["c", "a", "a", "x", "b"])