|
| 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 | + |
| 15 | +import os |
| 16 | +import sys |
| 17 | +from hashlib import blake2b |
| 18 | + |
| 19 | +import synapse |
| 20 | +from synapse.synapse_rust import get_rust_file_digest |
| 21 | + |
| 22 | + |
| 23 | +def check_rust_lib_up_to_date() -> None: |
| 24 | + """For editable installs check if the rust library is outdated and needs to |
| 25 | + be rebuilt. |
| 26 | + """ |
| 27 | + |
| 28 | + if not _dist_is_editable(): |
| 29 | + return |
| 30 | + |
| 31 | + synapse_dir = os.path.dirname(synapse.__file__) |
| 32 | + synapse_root = os.path.abspath(os.path.join(synapse_dir, "..")) |
| 33 | + |
| 34 | + # Double check we've not gone into site-packages... |
| 35 | + if os.path.basename(synapse_root) == "site-packages": |
| 36 | + return |
| 37 | + |
| 38 | + # ... and it looks like the root of a python project. |
| 39 | + if not os.path.exists("pyproject.toml"): |
| 40 | + return |
| 41 | + |
| 42 | + # Get the hash of all Rust source files |
| 43 | + hash = _hash_rust_files_in_directory(os.path.join(synapse_root, "rust", "src")) |
| 44 | + |
| 45 | + if hash != get_rust_file_digest(): |
| 46 | + raise Exception("Rust module outdated. Please rebuild using `poetry install`") |
| 47 | + |
| 48 | + |
| 49 | +def _hash_rust_files_in_directory(directory: str) -> str: |
| 50 | + """Get the hash of all files in a directory (recursively)""" |
| 51 | + |
| 52 | + directory = os.path.abspath(directory) |
| 53 | + |
| 54 | + paths = [] |
| 55 | + |
| 56 | + dirs = [directory] |
| 57 | + while dirs: |
| 58 | + dir = dirs.pop() |
| 59 | + with os.scandir(dir) as d: |
| 60 | + for entry in d: |
| 61 | + if entry.is_dir(): |
| 62 | + dirs.append(entry.path) |
| 63 | + else: |
| 64 | + paths.append(entry.path) |
| 65 | + |
| 66 | + # We sort to make sure that we get a consistent and well-defined ordering. |
| 67 | + paths.sort() |
| 68 | + |
| 69 | + hasher = blake2b() |
| 70 | + |
| 71 | + for path in paths: |
| 72 | + with open(os.path.join(directory, path), "rb") as f: |
| 73 | + hasher.update(f.read()) |
| 74 | + |
| 75 | + return hasher.hexdigest() |
| 76 | + |
| 77 | + |
| 78 | +def _dist_is_editable() -> bool: |
| 79 | + """Is distribution an editable install?""" |
| 80 | + for path_item in sys.path: |
| 81 | + egg_link = os.path.join(path_item, "matrix-synapse.egg-link") |
| 82 | + if os.path.isfile(egg_link): |
| 83 | + return True |
| 84 | + return False |
0 commit comments