about summary refs log tree commit diff
path: root/pkgs/applications/emulators/retroarch/update_cores.py
diff options
context:
space:
mode:
Diffstat (limited to 'pkgs/applications/emulators/retroarch/update_cores.py')
-rwxr-xr-xpkgs/applications/emulators/retroarch/update_cores.py48
1 files changed, 44 insertions, 4 deletions
diff --git a/pkgs/applications/emulators/retroarch/update_cores.py b/pkgs/applications/emulators/retroarch/update_cores.py
index a6dbd35e4fe6..981fe5377c5d 100755
--- a/pkgs/applications/emulators/retroarch/update_cores.py
+++ b/pkgs/applications/emulators/retroarch/update_cores.py
@@ -1,16 +1,28 @@
 #!/usr/bin/env nix-shell
-#!nix-shell -I nixpkgs=../../../../ -i python3 -p "python3.withPackages (ps: with ps; [ nix-prefetch-github ])" -p "git"
+#!nix-shell -I nixpkgs=./ -i python3 -p "python3.withPackages (ps: with ps; [ requests ])" -p git -p nix-prefetch-github
 
 import json
 import os
 import subprocess
 import sys
-from pathlib import Path
 from concurrent.futures import ThreadPoolExecutor
+from pathlib import Path
+
+import requests
 
 SCRIPT_PATH = Path(__file__).absolute().parent
 HASHES_PATH = SCRIPT_PATH / "hashes.json"
 GET_REPO_THREADS = int(os.environ.get("GET_REPO_THREADS", 8))
+# To add a new core, add it to the dictionary below. You need to set at least
+# `repo`, that is the repository name if the owner of the repository is
+# `libretro` itself, otherwise also set `owner`.
+# You may set `deep_clone`, `fetch_submodules` or `leave_dot_git` options to
+# `True` and they're similar to `fetchgit` options. Also if for some reason you
+# need to pin a specific revision, set `rev` to a commit.
+# To generate the hash file for your new core, you can run `update_cores.py
+# <core>`. The script needs to be run from the root of your `nixpkgs` clone.
+# Do not forget to add your core to `cores.nix` file with the proper overrides
+# so the core can be build.
 CORES = {
     "2048": {"repo": "libretro-2048"},
     "atari800": {"repo": "libretro-atari800"},
@@ -73,8 +85,9 @@ CORES = {
     "opera": {"repo": "opera-libretro"},
     "parallel-n64": {"repo": "parallel-n64"},
     # libretro/lrps2 is a hard-fork of pcsx2 with simplified code to target
-    # only libretro, while libretro/pcsx2 is supposedly closer to upstream.
-    # TODO: switch to libretro/pcsx2 since this is more up-to-date
+    # only libretro, while libretro/pcsx2 is supposedly closer to upstream but
+    # it is a WIP.
+    # TODO: switch to libretro/pcsx2 when upstream switches to it.
     "pcsx2": {"repo": "lrps2"},
     "pcsx_rearmed": {"repo": "pcsx_rearmed"},
     "picodrive": {"repo": "picodrive", "fetch_submodules": True},
@@ -115,6 +128,30 @@ def info(*msg):
     print(*msg, file=sys.stderr)
 
 
+def get_rev_date_fetchFromGitHub(repo, owner, rev):
+    # https://docs.github.com/en/rest/commits/commits?apiVersion=2022-11-28#get-a-commit
+    url = f"https://api.github.com/repos/{owner}/{repo}/commits/{rev}"
+    headers = {
+        "Accept": "application/vnd.github+json",
+        "X-GitHub-Api-Version": "2022-11-28",
+    }
+    if token := os.environ.get("GITHUB_TOKEN"):
+        headers["Authorization"] = f"Bearer {token}"
+    r = requests.get(url, headers=headers)
+
+    try:
+        j = r.json()
+    except requests.exceptions.JSONDecodeError:
+        return None
+
+    date = j.get("commit", {}).get("committer", {}).get("date")
+    if date:
+        # Date format returned by API: 2023-01-30T06:29:13Z
+        return f"unstable-{date[:10]}"
+    else:
+        return None
+
+
 def get_repo_hash_fetchFromGitHub(
     repo,
     owner="libretro",
@@ -146,6 +183,9 @@ def get_repo_hash_fetchFromGitHub(
         text=True,
     )
     j = json.loads(result.stdout)
+    date = get_rev_date_fetchFromGitHub(repo, owner, j["rev"])
+    if date:
+        j["date"] = date
     # Remove False values
     return {k: v for k, v in j.items() if v}