updateQuilt.py 3.9 KB

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