You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
3.1KB

  1. import sys
  2. import os
  3. import glob
  4. import json
  5. import time
  6. import common
  7. PACKAGES_DIR = "../packages"
  8. # Get the timestamp of the earliest commit touching the manifest file
  9. def get_plugin_build(plugin):
  10. slug = plugin['slug']
  11. version = plugin['version']
  12. arch = 'lin'
  13. package_filename = f"{slug}-{version}-{arch}.zip"
  14. package_path = PACKAGES_DIR + "/" + package_filename
  15. return os.path.getmtime(package_path)
  16. # Get the timestamp of the earliest commit touching the manifest file
  17. def get_plugin_creation(manifest_filename):
  18. stdout = common.run(f"git log --format=%ct -- {manifest_filename} | tail -1")
  19. stdout = stdout.strip()
  20. # Use current time as a fallback because if there's no commit with the module, it was added just now.
  21. if not stdout:
  22. return time.time()
  23. return float(stdout)
  24. # Get the timestamp of the earliest commit having the module slug
  25. def get_module_creation(manifest_filename, module_slug):
  26. stdout = common.run(f"git log --format='%H %ct' -- {manifest_filename}")
  27. # Use current time as a fallback because if there's no commit with the module, it was added just now.
  28. earliestTime = time.time()
  29. for line in stdout.strip().split("\n"):
  30. # "".split("\n") == [""] which is dumb
  31. if not line:
  32. continue
  33. hash, timestamp = line.split(" ")
  34. try:
  35. stdout = common.run(f"git show {hash}:{manifest_filename}")
  36. manifest = json.loads(stdout)
  37. # If the module exists in this commit, keep iterating
  38. if not common.find(manifest.get('modules', []), lambda module: module['slug'] == module_slug):
  39. break
  40. except:
  41. # If git fails, then the commit didn't actually touch the manifest file, so we can skip it.
  42. continue
  43. earliestTime = float(timestamp)
  44. return earliestTime
  45. def update():
  46. # Load existing dataset
  47. cache_filename = "manifests-cache.json"
  48. try:
  49. with open(cache_filename) as f:
  50. cache = json.load(f)
  51. except:
  52. cache = {}
  53. # Iterate plugins
  54. for manifest_filename in glob.glob('manifests/*.json'):
  55. with open(manifest_filename) as f:
  56. plugin = json.load(f)
  57. plugin_slug = plugin['slug']
  58. cache_plugin = cache.get(plugin_slug, {})
  59. # Get plugin build
  60. print(f"Getting buildTimestamp for plugin {plugin_slug}")
  61. try:
  62. cache_plugin['buildTimestamp'] = get_plugin_build(plugin)
  63. except:
  64. pass
  65. # Get plugin creation
  66. if 'creationTimestamp' not in cache_plugin:
  67. print(f"Getting creationTimestamp for plugin {plugin_slug}")
  68. cache_plugin['creationTimestamp'] = get_plugin_creation(manifest_filename)
  69. # Iterate modules in plugin
  70. cache_modules = cache_plugin.get('modules', {})
  71. for module in plugin.get('modules', []):
  72. module_slug = module['slug']
  73. cache_module = cache_modules.get(module_slug, {})
  74. # Get module creation
  75. if 'creationTimestamp' not in cache_module:
  76. print(f"Getting creationTimestamp for plugin {plugin_slug} module {module_slug}")
  77. cache_module['creationTimestamp'] = get_module_creation(manifest_filename, module_slug)
  78. cache_modules[module_slug] = cache_module
  79. cache_plugin['modules'] = cache_modules
  80. cache[plugin_slug] = cache_plugin
  81. with open(cache_filename, 'w') as f:
  82. json.dump(cache, f, indent=2)
  83. if __name__ == "__main__":
  84. update()