Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
shivamshrirao
GitHub Repository: shivamshrirao/diffusers
Path: blob/main/utils/overwrite_expected_slice.py
1440 views
1
# coding=utf-8
2
# Copyright 2023 The HuggingFace Inc. team.
3
#
4
# Licensed under the Apache License, Version 2.0 (the "License");
5
# you may not use this file except in compliance with the License.
6
# You may obtain a copy of the License at
7
#
8
# http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15
import argparse
16
from collections import defaultdict
17
18
19
def overwrite_file(file, class_name, test_name, correct_line, done_test):
20
_id = f"{file}_{class_name}_{test_name}"
21
done_test[_id] += 1
22
23
with open(file, "r") as f:
24
lines = f.readlines()
25
26
class_regex = f"class {class_name}("
27
test_regex = f"{4 * ' '}def {test_name}("
28
line_begin_regex = f"{8 * ' '}{correct_line.split()[0]}"
29
another_line_begin_regex = f"{16 * ' '}{correct_line.split()[0]}"
30
in_class = False
31
in_func = False
32
in_line = False
33
insert_line = False
34
count = 0
35
spaces = 0
36
37
new_lines = []
38
for line in lines:
39
if line.startswith(class_regex):
40
in_class = True
41
elif in_class and line.startswith(test_regex):
42
in_func = True
43
elif in_class and in_func and (line.startswith(line_begin_regex) or line.startswith(another_line_begin_regex)):
44
spaces = len(line.split(correct_line.split()[0])[0])
45
count += 1
46
47
if count == done_test[_id]:
48
in_line = True
49
50
if in_class and in_func and in_line:
51
if ")" not in line:
52
continue
53
else:
54
insert_line = True
55
56
if in_class and in_func and in_line and insert_line:
57
new_lines.append(f"{spaces * ' '}{correct_line}")
58
in_class = in_func = in_line = insert_line = False
59
else:
60
new_lines.append(line)
61
62
with open(file, "w") as f:
63
for line in new_lines:
64
f.write(line)
65
66
67
def main(correct, fail=None):
68
if fail is not None:
69
with open(fail, "r") as f:
70
test_failures = set([l.strip() for l in f.readlines()])
71
else:
72
test_failures = None
73
74
with open(correct, "r") as f:
75
correct_lines = f.readlines()
76
77
done_tests = defaultdict(int)
78
for line in correct_lines:
79
file, class_name, test_name, correct_line = line.split(";")
80
if test_failures is None or "::".join([file, class_name, test_name]) in test_failures:
81
overwrite_file(file, class_name, test_name, correct_line, done_tests)
82
83
84
if __name__ == "__main__":
85
parser = argparse.ArgumentParser()
86
parser.add_argument("--correct_filename", help="filename of tests with expected result")
87
parser.add_argument("--fail_filename", help="filename of test failures", type=str, default=None)
88
args = parser.parse_args()
89
90
main(args.correct_filename, args.fail_filename)
91
92