|
| 1 | +# Copyright 2022 Google LLC |
| 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 | +""" |
| 16 | +Modify the version in leveldb.cmake from the Firebase iOS SDK to match the |
| 17 | +version from this C++ SDK. |
| 18 | +""" |
| 19 | + |
| 20 | +import argparse |
| 21 | +import os |
| 22 | +import pathlib |
| 23 | +import re |
| 24 | +from typing import List, Tuple |
| 25 | + |
| 26 | + |
| 27 | +VERSION_PATTERN = r"\s*set\s*\(\s*version\s+([^)\s]+)\s*\)\s*" |
| 28 | +VERSION_EXPR = re.compile(VERSION_PATTERN, re.IGNORECASE) |
| 29 | +URL_HASH_PATTERN = r"\s*URL_HASH\s+([0-9a-zA-Z=]+)\s*" |
| 30 | +URL_HASH_EXPR = re.compile(URL_HASH_PATTERN, re.IGNORECASE) |
| 31 | + |
| 32 | + |
| 33 | +def main() -> None: |
| 34 | + (src_file, dest_file) = parse_args() |
| 35 | + |
| 36 | + leveldb_version = load_value(src_file, VERSION_EXPR) |
| 37 | + url_hash = load_value(src_file, URL_HASH_EXPR) |
| 38 | + |
| 39 | + set_value(dest_file, VERSION_EXPR, leveldb_version) |
| 40 | + set_value(dest_file, URL_HASH_EXPR, url_hash) |
| 41 | + |
| 42 | + |
| 43 | +def parse_args() -> Tuple[pathlib.Path, pathlib.Path]: |
| 44 | + arg_parser = argparse.ArgumentParser() |
| 45 | + arg_parser.add_argument("--leveldb-version-from", required=True) |
| 46 | + arg_parser.add_argument("--leveldb-version-to") |
| 47 | + |
| 48 | + parsed_args = arg_parser.parse_args() |
| 49 | + |
| 50 | + leveldb_cmake_src_file = pathlib.Path(parsed_args.leveldb_version_from) |
| 51 | + |
| 52 | + if parsed_args.leveldb_version_to: |
| 53 | + leveldb_cmake_dest_file = pathlib.Path(parsed_args.leveldb_version_to) |
| 54 | + else: |
| 55 | + leveldb_cmake_dest_file = pathlib.Path.cwd() \ |
| 56 | + / "cmake" / "external" / "leveldb.cmake" |
| 57 | + |
| 58 | + return (leveldb_cmake_src_file, leveldb_cmake_dest_file) |
| 59 | + |
| 60 | + |
| 61 | +def load_value(file_: pathlib.Path, expr: re.Pattern) -> str: |
| 62 | + value = None |
| 63 | + value_line = None |
| 64 | + value_line_number = None |
| 65 | + |
| 66 | + with file_.open("rt", encoding="utf8") as f: |
| 67 | + for (line_number, line) in enumerate(f, start=1): |
| 68 | + match = expr.fullmatch(line) |
| 69 | + if not match: |
| 70 | + continue |
| 71 | + elif value is not None: |
| 72 | + raise RegexMatchError( |
| 73 | + f"Multiple lines matching regex {expr.pattern} found in " |
| 74 | + f"{file_}: line {value_line_number}, {value_line.strip()} " |
| 75 | + f"and line {line_number}, {line.strip()}") |
| 76 | + |
| 77 | + value = match.group(1).strip() |
| 78 | + value_line = line |
| 79 | + value_line_number = line_number |
| 80 | + |
| 81 | + if value is None: |
| 82 | + raise RegexMatchError( |
| 83 | + f"No line matching regex {expr.pattern} found in {file_}") |
| 84 | + |
| 85 | + return value |
| 86 | + |
| 87 | + |
| 88 | +def set_value(file_: pathlib.Path, expr: re.Pattern, version: str) -> None: |
| 89 | + with file_.open("rt", encoding="utf8") as f: |
| 90 | + lines = list(f) |
| 91 | + |
| 92 | + matching_line = None |
| 93 | + matching_line_number = None |
| 94 | + |
| 95 | + for (line_number, line) in enumerate(lines, start=1): |
| 96 | + match = expr.fullmatch(line) |
| 97 | + if not match: |
| 98 | + continue |
| 99 | + elif matching_line is not None: |
| 100 | + raise RegexMatchError( |
| 101 | + f"Multiple lines matching regex {expr.pattern} found in " |
| 102 | + f"{file_}: line {matching_line_number}, {matching_line.strip()} " |
| 103 | + f"and line {line_number}, {line.strip()}") |
| 104 | + |
| 105 | + lines[line_number - 1] = line[:match.start(1)] + version + line[match.end(1):] |
| 106 | + matching_line = line |
| 107 | + matching_line_number = line_number |
| 108 | + |
| 109 | + if matching_line is None: |
| 110 | + raise RegexMatchError( |
| 111 | + f"No line matching regex {expr.pattern} found in {file_}") |
| 112 | + |
| 113 | + with file_.open("wt", encoding="utf8") as f: |
| 114 | + f.writelines(lines) |
| 115 | + |
| 116 | + |
| 117 | +class RegexMatchError(Exception): |
| 118 | + pass |
| 119 | + |
| 120 | + |
| 121 | +if __name__ == "__main__": |
| 122 | + main() |
0 commit comments