Debian Codegraph @

.github/build/scripts/compare-signatures.py

raw symbols SCIP
1 import re
2 from bs4 import BeautifulSoup # it is a python library to parse HTML
3 import sys
4 import pandas as pd
5 from io import StringIO
6
7 DIFF_FOUND=False
8
9 """
10 Extracts the signatures from the html file of the manually written documentation
11 """
12 def extract_signatures(doc_file):
13 with open(doc_file, 'r', encoding='utf-8') as file:
14 html_content = file.read()
15
16 soup = BeautifulSoup(html_content, 'html.parser')
17 tables = soup.find_all('table')
18
19 signatures = {}
20 for table in tables:
21 header = table.find('tr')
22 if header and len(header.find_all('td')) == 4:
23 headers = [td.text.strip() for td in header.find_all('td')]
24 if headers == ['Method', 'Returns', 'Signature', 'Description'] or headers == ['Method', 'Return Type', 'Signature(s)', 'Description']:
25 df = pd.read_html(StringIO(str(table)))[0] # using pandas to make sure there are no skipped cells
26 for index, row in df.iterrows():
27 method = str(row[0]) if not pd.isna(row[0]) else ""
28 returns = normalize(str(row[1])) if not pd.isna(row[1]) else ""
29 signature = normalize(str(row[2])) if not pd.isna(row[2]) else ""
30 if method not in signatures:
31 signatures[method] = {'returns': [], 'signatures': []}
32 signatures[method]['returns'].append(returns)
33 signatures[method]['signatures'].append(f"{method} ({signature})")
34 return signatures
35 return {}
36
37 """
38 This function checks if the signatures in the doxygen file match the signatures collected from the html file
39 """
40 def check_signatures(doxygen_file, doc_signatures):
41 with open(doxygen_file, 'r', encoding='utf-8') as file:
42 doxygen_content = file.read()
43
44 soup = BeautifulSoup(doxygen_content, 'html.parser')
45
46 correct = True
47 for table in soup.find_all('table', class_='memberdecls'):
48 rows = table.find_all('tr')
49 for row in rows:
50 cols = row.find_all('td')
51 if len(cols) == 2:
52 return_type = normalize(cols[0].text.strip())
53 method_signature = normalize(cols[1].text.strip())
54 match = re.match(r'([^)]+\))', method_signature) # making sure we don't have any extra information after the ) like const
55 if match:
56 method_signature = match.group(1)
57 method_name_match = re.match(r'(\w+)\s*\(', method_signature) # extract method name
58 if method_name_match:
59 method_name = method_name_match.group(1)
60
61 # check if signature is in html file
62 if method_name in doc_signatures.keys():
63 if return_type not in doc_signatures[method_name]['returns']:
64 print(f"- Return type mismatch for method \"{method_name}\"")
65 print(f"Doxygen return type : {return_type}")
66 print(f"Documentation returns : {doc_signatures[method_name]['returns']} \n")
67 correct = False
68 if method_signature not in doc_signatures[method_name]['signatures']:
69 print(f"- Signature mismatch for method \"{method_name}\" ")
70 print(f"Doxygen signature : {method_signature}")
71 print(f"Documentation signatures : {doc_signatures[method_name]['signatures']} \n")
72 correct = False
73 return correct
74
75 """
76 Used to normalize the text by removing extra spaces, new lines, tabs, etc.
77 """
78 def normalize(text):
79 ' '.join(text.replace("\n", "").replace("\t", "").replace("\xa0", " ").split())
80 text = re.sub(r'\s*\*\s*', '* ', text) # make sure * is followed by a space and doesn't have space before it
81 text = re.sub(r'\s*&\s*', '& ', text)
82 text = re.sub(r'\s*,\s*', ', ', text)
83 text = re.sub(r'::', '', text) # remove ::
84 text = re.sub(r'\s+', ' ', text)
85 return text
86
87 def main(argv):
88 if len(argv) != 2:
89 print("Usage: python3 compare_signatures.py <doc_file> <doxygen_file>")
90 sys.exit(1)
91
92 doc_file = argv[0]
93 doxygen_file = argv[1]
94 print("Checking function signatures for \n Doxygen file: ", doxygen_file, "\n Documentation file: ", doc_file, "\n")
95
96 signatures = extract_signatures(doc_file)
97 if not signatures:
98 print("No matching table found in the documentation")
99 sys.exit(1)
100
101 if not check_signatures(doxygen_file, signatures):
102 sys.exit(1)
103
104 if __name__ == "__main__":
105 main(sys.argv[1:])