2
2
3
3
import logging
4
4
import pathlib
5
+ import re
5
6
import subprocess
6
7
import typing
8
+ import itertools
7
9
8
10
import inflection
9
11
@@ -93,7 +95,10 @@ def get_classes_used_by(cls: ql.Class):
93
95
return sorted (set (t for t in get_types_used_by (cls ) if t [0 ].isupper ()))
94
96
95
97
96
- def _is_generated_stub (file , check_modification = False ):
98
+ _generated_stub_re = re .compile (r"private import .*\n\nclass \w+ extends \w+ \{[ \n]\}" , re .MULTILINE )
99
+
100
+
101
+ def _is_generated_stub (file ):
97
102
with open (file ) as contents :
98
103
for line in contents :
99
104
if not line .startswith ("// generated" ):
@@ -102,12 +107,12 @@ def _is_generated_stub(file, check_modification=False):
102
107
else :
103
108
# no lines
104
109
return False
105
- if check_modification :
106
- # one line already read, if we can read 5 other we are past the normal stub generation
107
- line_threshold = 5
108
- if sum ( 1 for _ in zip ( range ( line_threshold ), contents )) == line_threshold :
109
- raise ModifiedStubMarkedAsGeneratedError (
110
- f"{ file .name } stub was modified but is still marked as generated" )
110
+ # one line already read, if we can read 5 other we are past the normal stub generation
111
+ line_threshold = 5
112
+ first_lines = list ( itertools . islice ( contents , line_threshold ))
113
+ if len ( first_lines ) == line_threshold or not _generated_stub_re . match ( "" . join ( first_lines )) :
114
+ raise ModifiedStubMarkedAsGeneratedError (
115
+ f"{ file .name } stub was modified but is still marked as generated" )
111
116
return True
112
117
113
118
@@ -157,8 +162,9 @@ def generate(opts, renderer):
157
162
stub_out = opts .ql_stub_output
158
163
test_out = opts .ql_test_output
159
164
missing_test_source_filename = "MISSING_SOURCE.txt"
165
+
160
166
existing = {q for q in out .rglob ("*.qll" )}
161
- existing |= {q for q in stub_out .rglob ("*.qll" ) if _is_generated_stub (q , check_modification = True )}
167
+ existing |= {q for q in stub_out .rglob ("*.qll" ) if _is_generated_stub (q )}
162
168
existing |= {q for q in test_out .rglob ("*.ql" )}
163
169
existing |= {q for q in test_out .rglob (missing_test_source_filename )}
164
170
0 commit comments