updateFabric.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import json
  2. import os
  3. import zipfile
  4. from datetime import datetime
  5. import requests
  6. from cachecontrol import CacheControl
  7. from cachecontrol.caches import FileCache
  8. from meta.common import upstream_path, ensure_upstream_dir, transform_maven_key
  9. from meta.common.fabric import JARS_DIR, INSTALLER_INFO_DIR, META_DIR, DATETIME_FORMAT_HTTP
  10. from meta.model.fabric import FabricJarInfo
  11. UPSTREAM_DIR = upstream_path()
  12. ensure_upstream_dir(JARS_DIR)
  13. ensure_upstream_dir(INSTALLER_INFO_DIR)
  14. ensure_upstream_dir(META_DIR)
  15. forever_cache = FileCache('caches/http_cache', forever=True)
  16. sess = CacheControl(requests.Session(), forever_cache)
  17. def filehash(filename, hashtype, blocksize=65536):
  18. h = hashtype()
  19. with open(filename, "rb") as f:
  20. for block in iter(lambda: f.read(blocksize), b""):
  21. h.update(block)
  22. return h.hexdigest()
  23. def get_maven_url(maven_key, server, ext):
  24. parts = maven_key.split(":", 3)
  25. maven_ver_url = server + parts[0].replace(".", "/") + "/" + parts[1] + "/" + parts[2] + "/"
  26. maven_url = maven_ver_url + parts[1] + "-" + parts[2] + ext
  27. return maven_url
  28. def get_json_file(path, url):
  29. with open(path, 'w', encoding='utf-8') as f:
  30. r = sess.get(url)
  31. r.raise_for_status()
  32. version_json = r.json()
  33. json.dump(version_json, f, sort_keys=True, indent=4)
  34. return version_json
  35. def head_file(url):
  36. r = sess.head(url)
  37. r.raise_for_status()
  38. return r.headers
  39. def get_binary_file(path, url):
  40. with open(path, 'wb') as f:
  41. r = sess.get(url)
  42. r.raise_for_status()
  43. for chunk in r.iter_content(chunk_size=128):
  44. f.write(chunk)
  45. def compute_jar_file(path, url):
  46. # These two approaches should result in the same metadata, except for the timestamp which might be a few minutes
  47. # off for the fallback method
  48. try:
  49. # Let's not download a Jar file if we don't need to.
  50. headers = head_file(url)
  51. tstamp = datetime.strptime(headers["Last-Modified"], DATETIME_FORMAT_HTTP)
  52. except requests.HTTPError:
  53. # Just in case something changes in the future
  54. print(f"Falling back to downloading jar for {url}")
  55. jar_path = path + ".jar"
  56. get_binary_file(jar_path, url)
  57. tstamp = datetime.fromtimestamp(0)
  58. with zipfile.ZipFile(jar_path) as jar:
  59. allinfo = jar.infolist()
  60. for info in allinfo:
  61. tstamp_new = datetime(*info.date_time)
  62. if tstamp_new > tstamp:
  63. tstamp = tstamp_new
  64. data = FabricJarInfo(release_time=tstamp)
  65. data.write(path + ".json")
  66. def main():
  67. # get the version list for each component we are interested in
  68. for component in ["intermediary", "loader"]:
  69. index = get_json_file(os.path.join(UPSTREAM_DIR, META_DIR, f"{component}.json"),
  70. "https://meta.fabricmc.net/v2/versions/" + component)
  71. for it in index:
  72. print(f"Processing {component} {it['version']} ")
  73. jar_maven_url = get_maven_url(it["maven"], "https://maven.fabricmc.net/", ".jar")
  74. compute_jar_file(os.path.join(UPSTREAM_DIR, JARS_DIR, transform_maven_key(it["maven"])), jar_maven_url)
  75. # for each loader, download installer JSON file from maven
  76. with open(os.path.join(UPSTREAM_DIR, META_DIR, "loader.json"), 'r', encoding='utf-8') as loaderVersionIndexFile:
  77. loader_version_index = json.load(loaderVersionIndexFile)
  78. for it in loader_version_index:
  79. print(f"Downloading JAR info for loader {it['version']} ")
  80. maven_url = get_maven_url(it["maven"], "https://maven.fabricmc.net/", ".json")
  81. get_json_file(os.path.join(UPSTREAM_DIR, INSTALLER_INFO_DIR, f"{it['version']}.json"), maven_url)
  82. if __name__ == '__main__':
  83. main()