| #!/usr/bin/env python3 |
| |
| # Copyright (C) 2024 Igalia S.L. |
| # |
| # Redistribution and use in source and binary forms, with or without |
| # modification, are permitted provided that the following conditions |
| # are met: |
| # |
| # 1. Redistributions of source code must retain the above copyright |
| # notice, this list of conditions and the following disclaimer. |
| # 2. Redistributions in binary form must reproduce the above copyright |
| # notice, this list of conditions and the following disclaimer in the |
| # documentation and/or other materials provided with the distribution. |
| # 3. Neither the name of Apple Inc. ("Apple") nor the names of |
| # its contributors may be used to endorse or promote products derived |
| # from this software without specific prior written permission. |
| # |
| # THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
| # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| # DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
| # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| |
| from __future__ import annotations |
| import re |
| import argparse |
| from pathlib import Path |
| |
| |
| # This warning is better placed in the code than in the template file, as |
| # otherwise it could be very confusing to people reading the template. |
| autogenerated_warning = b""" |
| # !!! Autogenerated by update-compile-commands-symlink-conf !!! |
| # ^ Removing this line will prevent this file from updating |
| # |
| # If you want to commit changes to the repository-committed template, edit |
| # Tools/clangd/update-compile-commands-symlink.conf.example |
| # ---------------------------------------------------------------------------- |
| |
| """.lstrip() |
| |
| re_autogenerated = re.compile(rb"^# !!! Autogenerated .*", flags=re.MULTILINE) |
| assert re_autogenerated.match(autogenerated_warning) |
| |
| |
| def update_commands_conf(path_example: Path, path_dest: Path): |
| current_bytes = None |
| try: |
| with open(path_dest, "rb") as f: |
| current_bytes = f.read() |
| is_autogenerated = re_autogenerated.match(current_bytes) is not None |
| if not is_autogenerated and current_bytes != b"": |
| return |
| except FileNotFoundError: |
| pass |
| # Read the example and write it to `path_dest`. |
| with open(path_example, "rb") as f: |
| example_bytes = f.read() |
| new_bytes = autogenerated_warning + example_bytes |
| if new_bytes != current_bytes: |
| with open(path_dest, "wb") as f: |
| f.write(new_bytes) |
| |
| |
| def main(): |
| parser = argparse.ArgumentParser("update-compile-commands-symlink-conf", |
| description="Creates or updates the update-compile-commands-symlink.conf file.") |
| parser.add_argument("compile_commands_conf_example", type=Path) |
| parser.add_argument("compile_commands_conf_dest", type=Path) |
| args = parser.parse_args() |
| update_commands_conf(path_example=args.compile_commands_conf_example, |
| path_dest=args.compile_commands_conf_dest) |
| |
| |
| if __name__ == "__main__": |
| main() |