Skip to content

Commit

Permalink
pull platform - amy, grods, stopwatch, archem
Browse files Browse the repository at this point in the history
  • Loading branch information
sisby-folk committed Nov 30, 2024
1 parent 5ad5567 commit 3fcdb9a
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 100 deletions.
170 changes: 88 additions & 82 deletions scripts/pull_platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,87 +15,93 @@


def main():
modrinth_api = "https://api.modrinth.com/v2"
repo_root = common.get_repo_root()
constants_file = repo_root / "constants.jsonc"
submissions_file = repo_root / "submissions.json"
submission_lock_file = repo_root / "submissions-lock.json"
packwiz_pack_toml = repo_root / "pack" / "pack.toml"
packwiz = common.check_packwiz()

common.fix_packwiz_pack(packwiz_pack_toml)

constants = common.jsonc_at_home(common.read_file(constants_file))

# Download the json
event_name = constants["event"]
if event_name == None:
print(f"{Ansi.WARN}No event name defined. Treating it as if there were zero submissions{Ansi.RESET}")
print(f"Was this unintentional? Check {constants_file.relative_to(repo_root)} and make sure it defines \"event\"")
submission_data = []
else:
submissions_url = f"https://platform.modfest.net/event/{event_name}/submissions"
with urllib.request.urlopen(submissions_url) as submissions:
submission_data = json.load(submissions)

# Update the lock file
# Read the needed files and transform the submission data into a dict where the ids are keys
lock_data: SubmissionLockfileFormat = json.loads(common.read_file(submission_lock_file)) if submission_lock_file.exists() else {}
submissions_by_id = {s["id"]:s for s in submission_data}

# Remove stale data
lock_data = {k:v for k,v in lock_data.items() if (k in submissions_by_id)}

# Loop through all submissions
rate_limit = common.Ratelimiter(1)
for mod_id in submissions_by_id:
platform_info = submissions_by_id[mod_id]
lock_info = lock_data.get(mod_id) # Might be None
# If the url changes we need to update the lock data. This is the only use of 'url' in the lock file
if lock_info is None or lock_info["url"] != platform_info["download"]:
print(f"Updating lock data for {mod_id}")
lock_info = {} # Reset the lock info for this mod
assert lock_info is not None # mypy is quite stupid
lock_info["url"] = platform_info["download"]

# We steal packwiz's dependency resolution by making a quick packwiz dir
with tempfile.TemporaryDirectory() as tmpdir_name:
tmpdir = Path(tmpdir_name)
# Run commands in the temporary directory
os.chdir(tmpdir)

# This is the minimum for packwiz to consider this a pack dir
shutil.copyfile(packwiz_pack_toml, tmpdir / "pack.toml")
(tmpdir / "index.toml").touch()

# Install the mod into the temporary packwiz pack
rate_limit.limit()
mod_type = platform_info.get("platform")
if mod_type != None and mod_type.get("type") == "modrinth":
subprocess.run([packwiz, "modrinth", "install", "--project-id", mod_type["project_id"], "--version-id", mod_type["version_id"], "-y"])
else:
subprocess.run([packwiz, "url", "add", platform_info["download"]])

# Now lets see which files packwiz thought we should download
files = {}
mod_dir = tmpdir / "mods"
if not mod_dir.exists():
print(f"{Ansi.WARN}Packwiz didn't generate any files for {mod_id}{Ansi.RESET}")
continue
for packwiz_meta in os.listdir(mod_dir):
packwiz_data = tomllib.loads(common.read_file(mod_dir / packwiz_meta))
del packwiz_data["update"]
files[packwiz_meta] = packwiz_data
lock_info["files"] = files
lock_data[mod_id] = lock_info

# Write the update lock data back
with open(submission_lock_file, "w") as f:
f.write(json.dumps(lock_data, indent=2, sort_keys=True))

# Make it clear that this script didn't really do anything if event_name is null
if event_name == None:
sys.exit(1)
modrinth_api = "https://api.modrinth.com/v2"
repo_root = common.get_repo_root()
constants_file = repo_root / "constants.jsonc"
submissions_file = repo_root / "submissions.json"
submission_lock_file = repo_root / "submissions-lock.json"
packwiz_pack_toml = repo_root / "pack" / "pack.toml"
packwiz = common.check_packwiz()

common.fix_packwiz_pack(packwiz_pack_toml)

constants = common.jsonc_at_home(common.read_file(constants_file))

# Download the json
event_name = constants["event"]
if event_name == None:
print(f"{Ansi.WARN}No event name defined. Treating it as if there were zero submissions{Ansi.RESET}")
print(f"Was this unintentional? Check {constants_file.relative_to(repo_root)} and make sure it defines \"event\"")
submission_data = []
else:
submissions_url = f"https://platform.modfest.net/event/{event_name}/submissions"
with urllib.request.urlopen(submissions_url) as submissions:
submission_data = json.load(submissions)

# Update the lock file
# Read the needed files and transform the submission data into a dict where the ids are keys
lock_data: SubmissionLockfileFormat = json.loads(common.read_file(submission_lock_file)) if submission_lock_file.exists() else {}
submissions_by_id = {s["id"]: s for s in submission_data}

# Remove stale data
lock_data = {k: v for k, v in lock_data.items() if (k in submissions_by_id)}

# Loop through all submissions
rate_limit = common.Ratelimiter(1)
for mod_id in submissions_by_id:
platform_info = submissions_by_id[mod_id]
lock_info = lock_data.get(mod_id) # Might be None
# If the url changes we need to update the lock data. This is the only use of 'url' in the lock file
if lock_info is None or lock_info["url"] != platform_info["download"]:
print(f"Updating lock data for {mod_id}")
lock_info = {} # Reset the lock info for this mod
assert lock_info is not None # mypy is quite stupid
lock_info["url"] = platform_info["download"]

old_dir = os.getcwd()

# We steal packwiz's dependency resolution by making a quick packwiz dir
with tempfile.TemporaryDirectory() as tmpdir_name:
tmpdir = Path(tmpdir_name)
# Run commands in the temporary directory

os.chdir(tmpdir)

# This is the minimum for packwiz to consider this a pack dir
shutil.copyfile(packwiz_pack_toml, tmpdir / "pack.toml")
(tmpdir / "index.toml").touch()

# Install the mod into the temporary packwiz pack
rate_limit.limit()
mod_type = platform_info.get("platform")
if mod_type != None and mod_type.get("type") == "modrinth":
subprocess.run([packwiz, "modrinth", "install", "--project-id", mod_type["project_id"], "--version-id", mod_type["version_id"], "-y"])
else:
subprocess.run([packwiz, "url", "add", platform_info["download"]])

# Now lets see which files packwiz thought we should download
files = {}
mod_dir = tmpdir / "mods"
if not mod_dir.exists():
print(f"{Ansi.WARN}Packwiz didn't generate any files for {mod_id}{Ansi.RESET}")
continue
for packwiz_meta in os.listdir(mod_dir):
packwiz_data = tomllib.loads(common.read_file(mod_dir / packwiz_meta))
del packwiz_data["update"]
files[packwiz_meta] = packwiz_data
lock_info["files"] = files

os.chdir(old_dir)
lock_data[mod_id] = lock_info

# Write the update lock data back
with open(submission_lock_file, "w") as f:
f.write(json.dumps(lock_data, indent=2, sort_keys=True))

# Make it clear that this script didn't really do anything if event_name is null
if event_name == None:
sys.exit(1)


if __name__ == "__main__":
main()
main()
36 changes: 18 additions & 18 deletions submissions-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@
"files": {
"amys-industrious-mod.pw.toml": {
"download": {
"hash": "a883bfb421f8422074ad827bf817e26cb7f6c7c3",
"hash": "ae749283df34b67d14e68bf864792c3f0a6e08b4",
"hash-format": "sha1",
"url": "https://cdn.modrinth.com/data/qDPkkwh3/versions/eAUKHCke/industrious-1.0.1%2B1.21.jar"
"url": "https://cdn.modrinth.com/data/qDPkkwh3/versions/kfj8mdlT/industrious-1.0.2%2B1.21.jar"
},
"filename": "industrious-1.0.1+1.21.jar",
"filename": "industrious-1.0.2+1.21.jar",
"name": "Amy's Industrious Mod",
"side": "both"
}
},
"url": "https://cdn.modrinth.com/data/qDPkkwh3/versions/eAUKHCke/industrious-1.0.1%2B1.21.jar"
"url": "https://cdn.modrinth.com/data/qDPkkwh3/versions/kfj8mdlT/industrious-1.0.2%2B1.21.jar"
},
"armistice": {
"files": {
Expand Down Expand Up @@ -843,16 +843,16 @@
"files": {
"rods-from-god.pw.toml": {
"download": {
"hash": "80ed5ddb3c0cd33d6f94b1d4cdf8be96935987ef",
"hash": "b05f700ca5495c6c2c01450b67f06d3a3c333fc3",
"hash-format": "sha1",
"url": "https://cdn.modrinth.com/data/lfJQNrlz/versions/NbJERsNj/rods_from_god-1.1.3%2B1.21.1.jar"
"url": "https://cdn.modrinth.com/data/lfJQNrlz/versions/jzxVJxwM/rods_from_god-1.1.4%2B1.21.1.jar"
},
"filename": "rods_from_god-1.1.3+1.21.1.jar",
"filename": "rods_from_god-1.1.4+1.21.1.jar",
"name": "Rods from God",
"side": "both"
}
},
"url": "https://cdn.modrinth.com/data/lfJQNrlz/versions/NbJERsNj/rods_from_god-1.1.3%2B1.21.1.jar"
"url": "https://cdn.modrinth.com/data/lfJQNrlz/versions/jzxVJxwM/rods_from_god-1.1.4%2B1.21.1.jar"
},
"roehrchen": {
"files": {
Expand Down Expand Up @@ -888,16 +888,16 @@
"files": {
"shattered-stopwatch.pw.toml": {
"download": {
"hash": "637710855914c9e496c985fc971bb4061e3a5d64c9e9ecd28c545f11931c1186dceb7b8fe15c075cf5d6981579e8db353743caa1232478af2f94b696f0e9df1e",
"hash-format": "sha512",
"url": "https://cdn.modrinth.com/data/So5Pd4qV/versions/rbTUBrDA/shattered-stopwatch-0.2.0%2B1.21.jar"
"hash": "e979f9f6912b5155d85e1ab05cb3fef5e42fcc20",
"hash-format": "sha1",
"url": "https://cdn.modrinth.com/data/So5Pd4qV/versions/CtXsgp9C/shattered-stopwatch-0.2.1%2B1.21.jar"
},
"filename": "shattered-stopwatch-0.2.0+1.21.jar",
"filename": "shattered-stopwatch-0.2.1+1.21.jar",
"name": "Shattered Stopwatch",
"side": "both"
}
},
"url": "https://cdn.modrinth.com/data/So5Pd4qV/versions/rbTUBrDA/shattered-stopwatch-0.2.0%2B1.21.jar"
"url": "https://cdn.modrinth.com/data/So5Pd4qV/versions/CtXsgp9C/shattered-stopwatch-0.2.1%2B1.21.jar"
},
"shrink_ray": {
"files": {
Expand Down Expand Up @@ -1003,16 +1003,16 @@
"files": {
"the-arcane-path-of-chemistry.pw.toml": {
"download": {
"hash": "57d8d7cf274ad404b502aade5c57aeb702c584b4815d8b3cd1f3316114e91725fff090dc6508e317689950cc11e5cb1d47cf3c49d71d12bbeaa38a8b563cb4b3",
"hash-format": "sha512",
"url": "https://cdn.modrinth.com/data/d2pc786h/versions/XENdt5xq/The-Arcane-path-of-Chemistry-1.0.3-1.21.1-1.21.2.jar"
"hash": "c17739b79e2c1541c7a9502a2753ce4bc92d2da8",
"hash-format": "sha1",
"url": "https://cdn.modrinth.com/data/d2pc786h/versions/BUmmIW4X/The-Arcane-path-of-Chemistry-1.0.4-1.21.1-1.21.2.jar"
},
"filename": "The-Arcane-path-of-Chemistry-1.0.3-1.21.1-1.21.2.jar",
"filename": "The-Arcane-path-of-Chemistry-1.0.4-1.21.1-1.21.2.jar",
"name": "The Arcane path of Chemistry",
"side": "both"
}
},
"url": "https://cdn.modrinth.com/data/d2pc786h/versions/XENdt5xq/The-Arcane-path-of-Chemistry-1.0.3-1.21.1-1.21.2.jar"
"url": "https://cdn.modrinth.com/data/d2pc786h/versions/BUmmIW4X/The-Arcane-path-of-Chemistry-1.0.4-1.21.1-1.21.2.jar"
},
"timewarp": {
"files": {
Expand Down

0 comments on commit 3fcdb9a

Please sign in to comment.