licensing.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import os
  2. from hashlib import sha1
  3. from mod.settings import KEYS_PATH
  4. LICENSE_INDEX = {}
  5. NEW_LICENSES = []
  6. def check_missing_licenses(licenses):
  7. # Compute a list of all licenses as revoking candidates
  8. revoked = set()
  9. for checksum in os.listdir(KEYS_PATH):
  10. revoked.add(checksum)
  11. # Check missing licenses and confirm the ones locally installed
  12. missing = []
  13. for plugin_license in licenses:
  14. uri = plugin_license['plugin_uri'].encode()
  15. LICENSE_INDEX[plugin_license['id']] = uri
  16. checksum = sha1(uri).hexdigest()
  17. if checksum in revoked:
  18. # license is still valid, let's unmark for revoking
  19. revoked.remove(checksum)
  20. key_path = os.path.join(KEYS_PATH, checksum)
  21. if not os.path.exists(key_path):
  22. missing.append(plugin_license['id'])
  23. # Delete revoked licenses
  24. for checksum in revoked:
  25. os.remove(os.path.join(KEYS_PATH, checksum))
  26. return missing
  27. def save_license(lid, data):
  28. uri = LICENSE_INDEX[lid]
  29. checksum = sha1(uri).hexdigest()
  30. key_path = os.path.join(KEYS_PATH, checksum)
  31. fh = open(key_path, 'wb')
  32. fh.write(data)
  33. fh.close()
  34. NEW_LICENSES.append(uri)
  35. def get_new_licenses_and_flush():
  36. licenses = []
  37. while len(NEW_LICENSES) > 0:
  38. licenses.append(NEW_LICENSES.pop())
  39. return licenses