Skip to content

Commit b29b40f

Browse files
Update RST prelude checker to use beginning and ending markers
1 parent 5c799e6 commit b29b40f

File tree

103 files changed

+1174
-863
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

103 files changed

+1174
-863
lines changed

contrib/ci/fix_prelude.py

Lines changed: 113 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,123 @@
88
import hashlib
99

1010

11+
PRELUDE_FLAG = "container:: PRELUDE "
1112
PROJECT = Path(sys.argv[0]).resolve().parents[2]
1213
CONTRIB = PROJECT / "contrib"
14+
PRELUDE_RST = PROJECT / "support_files" / "prelude.rst"
1315

16+
EXPECTED = {}
17+
ROLES = None
18+
SYMBOLS = None
1419

15-
def rst_update_prelude(files):
16-
subprocess.check_call(
17-
str(s)
18-
for s in [sys.executable, CONTRIB / "rst_update_prelude.py", "-i"] + files
19-
)
20+
VALIDATORS = {
21+
"ROLES": "validate_content",
22+
"SYMBOLS": "validate_content",
23+
"PROVIDES": "validate_provides",
24+
}
25+
26+
27+
def load_prelude():
28+
global EXPECTED
29+
30+
with open(PRELUDE_RST, "r") as file:
31+
content = file.read()
32+
pieces = content.split(PRELUDE_FLAG)
33+
for section in pieces:
34+
stripped = section.strip()
35+
try:
36+
name, content = section.split("\n", 1)
37+
EXPECTED[name] = content.strip()
38+
except:
39+
pass
40+
41+
42+
def validate_content(name, content):
43+
global EXPECTED
44+
return content == EXPECTED[name]
45+
46+
47+
def validate_provides(name, content):
48+
global EXPECTED
49+
return True
2050

2151

22-
def files_digest(files):
23-
h = hashlib.sha256()
24-
for f in sorted(files):
25-
with open(f, "rb") as frd:
26-
h.update(frd.read())
27-
return h.digest()
52+
def compare_content(title, actual_str):
53+
global EXPECTED
54+
retval = []
55+
actual = actual_str.split("\n")
56+
expected = EXPECTED[title].split("\n")
57+
l_actual = len(actual)
58+
l_expected = len(expected)
59+
length = min([l_actual, l_expected])
60+
for idx in range(0, length):
61+
if actual[idx] != expected[idx]:
62+
retval.append("In " + title)
63+
retval.append(" Line " + str(idx + 1))
64+
retval.append(" Actual : " + actual[idx])
65+
retval.append(" Expected: " + expected[idx])
66+
if len(retval) == 0 and l_actual < l_expected:
67+
retval.append("In " + title + " missing:")
68+
for idx in range(l_actual, l_expected):
69+
retval.append(" " + expected[idx])
70+
elif len(retval) == 0 and l_actual > l_expected:
71+
retval.append("In " + title + " extra:")
72+
for idx in range(l_expected, l_actual):
73+
retval.append(" " + actual[idx])
74+
return retval
75+
76+
77+
def process_one_file(filename, interactive):
78+
global VALIDATORS
79+
80+
failures = None
81+
82+
sections_needed = ["BEGIN", "ROLES", "SYMBOLS", "REQUIRES", "PROVIDES", "END"]
83+
84+
if interactive:
85+
failures = []
86+
else:
87+
failures = ""
88+
with open(filename, "r") as file:
89+
content = file.read()
90+
pieces = content.split(PRELUDE_FLAG)
91+
for section in pieces:
92+
stripped = section.strip()
93+
name, content = section.split("\n", 1)
94+
try:
95+
sections_needed.remove(name)
96+
except:
97+
pass
98+
content = content.strip()
99+
if name in VALIDATORS.keys():
100+
validator = VALIDATORS[name]
101+
if not globals()[validator](name, content):
102+
if interactive:
103+
failures.extend(compare_content(name, content))
104+
else:
105+
failures = failures + " " + name
106+
if len(sections_needed) > 0:
107+
if interactive:
108+
failures.append("Missing Section(s)")
109+
for section in sections_needed:
110+
failures.append(" " + section)
111+
else:
112+
for section in sections_needed:
113+
failures = failures + " " + section
114+
115+
return failures
28116

29117

30118
if __name__ == "__main__":
31119
ap = argparse.ArgumentParser()
32120
ap.add_argument(
33121
"--files-to-check", type=Path, default=CONTRIB / "rst_files_with_prelude.txt"
34122
)
35-
ap.add_argument("--no-digests-check", "-C", action="store_true")
123+
ap.add_argument("--interactive", action="store_true")
36124
args = ap.parse_args()
37125

38-
check_digest = not args.no_digests_check
39-
digests_have_changed = False
126+
total_failures = 0
127+
load_prelude()
40128

41129
with open(args.files_to_check, "rt") as f:
42130
files_with_prelude_glob = f.read().splitlines()
@@ -46,15 +134,15 @@ def files_digest(files):
46134
if not f_prel:
47135
continue
48136

49-
print(glob)
50-
if check_digest:
51-
before = files_digest(f_prel)
52-
53-
rst_update_prelude(f_prel)
54-
if check_digest and before != files_digest(f_prel):
55-
print(f"{glob}: files didn't have the proper prelude", file=sys.stderr)
56-
print(f"run {Path(sys.argv[0]).name} locally and commit the results to fix")
57-
digests_have_changed = True
137+
for one in f_prel:
138+
failures = process_one_file(one, args.interactive)
139+
if len(failures) > 0:
140+
total_failures = total_failures + 1
141+
if args.interactive:
142+
print("FAIL: " + str(one))
143+
for line in failures:
144+
print(" " + line)
145+
else:
146+
print("FAIL: " + str(one) + failures)
58147

59-
if check_digest:
60-
sys.exit(0 if not digests_have_changed else 1)
148+
sys.exit(0 if total_failures == 0 else 1)

courses/advanced_exception_analysis/010_advanced_exception_analysis.rst

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
Advanced Exception Analysis
33
*****************************
44

5-
..
6-
Coding language
5+
.. container:: PRELUDE BEGIN
6+
7+
.. container:: PRELUDE ROLES
78

89
.. role:: ada(code)
910
:language: Ada
@@ -14,8 +15,7 @@ Advanced Exception Analysis
1415
.. role:: cpp(code)
1516
:language: C++
1617

17-
..
18-
Math symbols
18+
.. container:: PRELUDE SYMBOLS
1919

2020
.. |rightarrow| replace:: :math:`\rightarrow`
2121
.. |forall| replace:: :math:`\forall`
@@ -25,11 +25,13 @@ Advanced Exception Analysis
2525
.. |ge| replace:: :math:`\ge`
2626
.. |lt| replace:: :math:`<`
2727
.. |gt| replace:: :math:`>`
28+
.. |checkmark| replace:: :math:`\checkmark`
2829

29-
..
30-
Miscellaneous symbols
30+
.. container:: PRELUDE REQUIRES
3131

32-
.. |checkmark| replace:: :math:`\checkmark`
32+
.. container:: PRELUDE PROVIDES
33+
34+
.. container:: PRELUDE END
3335

3436
==============
3537
Introduction

courses/fundamentals_of_ada/005_introduction.rst

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
Introduction
33
************
44

5-
..
6-
Coding language
5+
.. container:: PRELUDE BEGIN
6+
7+
.. container:: PRELUDE ROLES
78

89
.. role:: ada(code)
910
:language: Ada
@@ -14,8 +15,7 @@ Introduction
1415
.. role:: cpp(code)
1516
:language: C++
1617

17-
..
18-
Math symbols
18+
.. container:: PRELUDE SYMBOLS
1919

2020
.. |rightarrow| replace:: :math:`\rightarrow`
2121
.. |forall| replace:: :math:`\forall`
@@ -25,11 +25,13 @@ Introduction
2525
.. |ge| replace:: :math:`\ge`
2626
.. |lt| replace:: :math:`<`
2727
.. |gt| replace:: :math:`>`
28+
.. |checkmark| replace:: :math:`\checkmark`
2829

29-
..
30-
Miscellaneous symbols
30+
.. container:: PRELUDE REQUIRES
3131

32-
.. |checkmark| replace:: :math:`\checkmark`
32+
.. container:: PRELUDE PROVIDES
33+
34+
.. container:: PRELUDE END
3335

3436
=============
3537
About AdaCore

courses/fundamentals_of_ada/010_overview.rst

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
Overview
33
**********
44

5-
..
6-
Coding language
5+
.. container:: PRELUDE BEGIN
6+
7+
.. container:: PRELUDE ROLES
78

89
.. role:: ada(code)
910
:language: Ada
@@ -14,8 +15,7 @@ Overview
1415
.. role:: cpp(code)
1516
:language: C++
1617

17-
..
18-
Math symbols
18+
.. container:: PRELUDE SYMBOLS
1919

2020
.. |rightarrow| replace:: :math:`\rightarrow`
2121
.. |forall| replace:: :math:`\forall`
@@ -25,11 +25,13 @@ Overview
2525
.. |ge| replace:: :math:`\ge`
2626
.. |lt| replace:: :math:`<`
2727
.. |gt| replace:: :math:`>`
28+
.. |checkmark| replace:: :math:`\checkmark`
2829

29-
..
30-
Miscellaneous symbols
30+
.. container:: PRELUDE REQUIRES
3131

32-
.. |checkmark| replace:: :math:`\checkmark`
32+
.. container:: PRELUDE PROVIDES
33+
34+
.. container:: PRELUDE END
3335

3436
==================
3537
A Little History

courses/fundamentals_of_ada/020_declarations.rst

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
Declarations
33
**************
44

5-
..
6-
Coding language
5+
.. container:: PRELUDE BEGIN
6+
7+
.. container:: PRELUDE ROLES
78

89
.. role:: ada(code)
910
:language: Ada
@@ -14,8 +15,7 @@ Declarations
1415
.. role:: cpp(code)
1516
:language: C++
1617

17-
..
18-
Math symbols
18+
.. container:: PRELUDE SYMBOLS
1919

2020
.. |rightarrow| replace:: :math:`\rightarrow`
2121
.. |forall| replace:: :math:`\forall`
@@ -25,11 +25,13 @@ Declarations
2525
.. |ge| replace:: :math:`\ge`
2626
.. |lt| replace:: :math:`<`
2727
.. |gt| replace:: :math:`>`
28+
.. |checkmark| replace:: :math:`\checkmark`
2829

29-
..
30-
Miscellaneous symbols
30+
.. container:: PRELUDE REQUIRES
3131

32-
.. |checkmark| replace:: :math:`\checkmark`
32+
.. container:: PRELUDE PROVIDES
33+
34+
.. container:: PRELUDE END
3335

3436
==============
3537
Introduction

courses/fundamentals_of_ada/030_basic_types-extras.rst

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
Basic Types
33
*************
44

5-
..
6-
Coding language
5+
.. container:: PRELUDE BEGIN
6+
7+
.. container:: PRELUDE ROLES
78

89
.. role:: ada(code)
910
:language: Ada
@@ -14,8 +15,7 @@ Basic Types
1415
.. role:: cpp(code)
1516
:language: C++
1617

17-
..
18-
Math symbols
18+
.. container:: PRELUDE SYMBOLS
1919

2020
.. |rightarrow| replace:: :math:`\rightarrow`
2121
.. |forall| replace:: :math:`\forall`
@@ -25,11 +25,13 @@ Basic Types
2525
.. |ge| replace:: :math:`\ge`
2626
.. |lt| replace:: :math:`<`
2727
.. |gt| replace:: :math:`>`
28+
.. |checkmark| replace:: :math:`\checkmark`
2829

29-
..
30-
Miscellaneous symbols
30+
.. container:: PRELUDE REQUIRES
3131

32-
.. |checkmark| replace:: :math:`\checkmark`
32+
.. container:: PRELUDE PROVIDES
33+
34+
.. container:: PRELUDE END
3335

3436
.. include:: 030_basic_types/03-modular_types.rst
3537
.. include:: 030_basic_types/05-representation_values.rst

courses/fundamentals_of_ada/030_basic_types-in_depth.rst

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
Basic Types
33
*************
44

5-
..
6-
Coding language
5+
.. container:: PRELUDE BEGIN
6+
7+
.. container:: PRELUDE ROLES
78

89
.. role:: ada(code)
910
:language: Ada
@@ -14,8 +15,7 @@ Basic Types
1415
.. role:: cpp(code)
1516
:language: C++
1617

17-
..
18-
Math symbols
18+
.. container:: PRELUDE SYMBOLS
1919

2020
.. |rightarrow| replace:: :math:`\rightarrow`
2121
.. |forall| replace:: :math:`\forall`
@@ -25,11 +25,13 @@ Basic Types
2525
.. |ge| replace:: :math:`\ge`
2626
.. |lt| replace:: :math:`<`
2727
.. |gt| replace:: :math:`>`
28+
.. |checkmark| replace:: :math:`\checkmark`
2829

29-
..
30-
Miscellaneous symbols
30+
.. container:: PRELUDE REQUIRES
3131

32-
.. |checkmark| replace:: :math:`\checkmark`
32+
.. container:: PRELUDE PROVIDES
33+
34+
.. container:: PRELUDE END
3335

3436
.. include:: 030_basic_types/01-introduction.rst
3537
.. include:: 030_basic_types/02-discrete_numeric_types.rst

0 commit comments

Comments
 (0)