1818"""
1919
2020import argparse
21- import dataclasses
2221import os
2322import pathlib
2423import re
25- from typing import Iterable , Sequence
24+ from typing import List , Tuple
2625
2726
2827VERSION_PATTERN = r"\s*set\s*\(\s*version\s+([^)\s]+)\s*\)\s*"
2928VERSION_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 )
3031
3132
3233def main () -> None :
33- args = parse_args ()
34- leveldb_version = load_leveldb_version (args .leveldb_cmake_src_file )
35- set_leveldb_version (args .leveldb_cmake_dest_file , leveldb_version )
34+ (src_file , dest_file ) = parse_args ()
3635
36+ leveldb_version = load_value (src_file , VERSION_EXPR )
37+ url_hash = load_value (src_file , URL_HASH_EXPR )
3738
38- @dataclasses .dataclass (frozen = True )
39- class ParsedArgs :
40- leveldb_cmake_src_file : pathlib .Path
41- leveldb_cmake_dest_file : pathlib .Path
39+ set_value (dest_file , VERSION_EXPR , leveldb_version )
40+ set_value (dest_file , URL_HASH_EXPR , url_hash )
4241
4342
44- def parse_args () -> ParsedArgs :
43+ def parse_args () -> Tuple [ pathlib . Path , pathlib . Path ] :
4544 arg_parser = argparse .ArgumentParser ()
4645 arg_parser .add_argument ("--leveldb-version-from" , required = True )
4746 arg_parser .add_argument ("--leveldb-version-to" )
@@ -56,64 +55,68 @@ def parse_args() -> ParsedArgs:
5655 leveldb_cmake_dest_file = pathlib .Path .cwd () \
5756 / "cmake" / "external" / "leveldb.cmake"
5857
59- return ParsedArgs (
60- leveldb_cmake_src_file = leveldb_cmake_src_file ,
61- leveldb_cmake_dest_file = leveldb_cmake_dest_file ,
62- )
58+ return (leveldb_cmake_src_file , leveldb_cmake_dest_file )
6359
6460
65- def load_leveldb_version ( cmake_file : pathlib .Path ) -> str :
66- version = None
67- version_line = None
68- version_line_number = None
61+ def load_value ( file_ : pathlib .Path , expr : re . Pattern ) -> str :
62+ value = None
63+ value_line = None
64+ value_line_number = None
6965
70- with cmake_file .open ("rt" , encoding = "utf8" ) as f :
66+ with file_ .open ("rt" , encoding = "utf8" ) as f :
7167 for (line_number , line ) in enumerate (f , start = 1 ):
72- match = VERSION_EXPR .fullmatch (line )
73- if match :
74- if version is not None :
75- raise LevelDbVersionLineError (
76- f"Multiple lines matching regex { VERSION_EXPR .pattern } found in "
77- f"{ cmake_file } : line { version_line_number } , { version_line .strip ()} "
78- f"and line { line_number } , { line .strip ()} " )
79- version = match .group (1 ).strip ()
80- version_line = line
81- version_line_number = line_number
82-
83- if version is None :
84- raise LevelDbVersionLineError (
85- f"No line matching regex { VERSION_EXPR .pattern } found in { cmake_file } " )
86-
87- return version
88-
89-
90- def set_leveldb_version (cmake_file : pathlib .Path , version : str ) -> str :
91- with cmake_file .open ("rt" , encoding = "utf8" ) as f :
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 :
9290 lines = list (f )
9391
94- version_lines_found = []
95- for (i , line ) in enumerate (lines ):
96- match = VERSION_EXPR .fullmatch (line )
97- if match :
98- lines [i ] = line [:match .start (1 )] + version + line [match .end (1 ):]
99- version_lines_found .append (i )
100-
101- if len (version_lines_found ) == 0 :
102- raise LevelDbVersionLineError (
103- f"No line matching regex { VERSION_EXPR .pattern } found in { cmake_file } " )
104- elif len (version_lines_found ) > 1 :
105- raise LevelDbVersionLineError (
106- f"Multiple lines matching regex { VERSION_EXPR .pattern } found in "
107- f"{ cmake_file } : { ', ' .join (str (i + 1 ) for i in version_lines_found )} " )
108-
109- with cmake_file .open ("wt" , encoding = "utf8" ) as f :
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 :
110114 f .writelines (lines )
111115
112116
113- class LevelDbVersionLineError (Exception ):
117+ class RegexMatchError (Exception ):
114118 pass
115119
116120
117-
118121if __name__ == "__main__" :
119122 main ()
0 commit comments