Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

85 linhas
2.5 KiB

  1. # Copyright 2022 The Matrix.org Foundation C.I.C.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import os
  15. import sys
  16. from hashlib import blake2b
  17. import synapse
  18. from synapse.synapse_rust import get_rust_file_digest
  19. def check_rust_lib_up_to_date() -> None:
  20. """For editable installs check if the rust library is outdated and needs to
  21. be rebuilt.
  22. """
  23. if not _dist_is_editable():
  24. return
  25. synapse_dir = os.path.dirname(synapse.__file__)
  26. synapse_root = os.path.abspath(os.path.join(synapse_dir, ".."))
  27. # Double check we've not gone into site-packages...
  28. if os.path.basename(synapse_root) == "site-packages":
  29. return
  30. # ... and it looks like the root of a python project.
  31. if not os.path.exists("pyproject.toml"):
  32. return
  33. # Get the hash of all Rust source files
  34. hash = _hash_rust_files_in_directory(os.path.join(synapse_root, "rust", "src"))
  35. if hash != get_rust_file_digest():
  36. raise Exception("Rust module outdated. Please rebuild using `poetry install`")
  37. def _hash_rust_files_in_directory(directory: str) -> str:
  38. """Get the hash of all files in a directory (recursively)"""
  39. directory = os.path.abspath(directory)
  40. paths = []
  41. dirs = [directory]
  42. while dirs:
  43. dir = dirs.pop()
  44. with os.scandir(dir) as d:
  45. for entry in d:
  46. if entry.is_dir():
  47. dirs.append(entry.path)
  48. else:
  49. paths.append(entry.path)
  50. # We sort to make sure that we get a consistent and well-defined ordering.
  51. paths.sort()
  52. hasher = blake2b()
  53. for path in paths:
  54. with open(os.path.join(directory, path), "rb") as f:
  55. hasher.update(f.read())
  56. return hasher.hexdigest()
  57. def _dist_is_editable() -> bool:
  58. """Is distribution an editable install?"""
  59. for path_item in sys.path:
  60. egg_link = os.path.join(path_item, "matrix-synapse.egg-link")
  61. if os.path.isfile(egg_link):
  62. return True
  63. return False