-
Notifications
You must be signed in to change notification settings - Fork 259
Expand file tree
/
Copy pathdocstrings_checksum.py
More file actions
47 lines (38 loc) · 1.58 KB
/
docstrings_checksum.py
File metadata and controls
47 lines (38 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import ast
import hashlib
from pathlib import Path
from typing import Iterator
def docstrings_checksum(python_files: Iterator[Path]):
files_content = (f.read_text() for f in python_files)
trees = (ast.parse(c) for c in files_content)
# Get all docstrings from async functions, functions,
# classes and modules definitions
docstrings = []
for tree in trees:
for node in ast.walk(tree):
if not isinstance(
node, (ast.AsyncFunctionDef, ast.FunctionDef, ast.ClassDef, ast.Module)
):
# Skip all node types that can't have docstrings to prevent failures
continue
docstring = ast.get_docstring(node)
if docstring:
docstrings.append(docstring)
# Sort them to be safe, since ast.walk() returns
# nodes in no specified order.
# See https://docs.python.org/3/library/ast.html#ast.walk
docstrings.sort()
return hashlib.md5(str(docstrings).encode("utf-8")).hexdigest()
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--root", help="Project root folder", required=True, type=Path)
parser.add_argument("--integration", help="Integration folder relative path", required=False, type=str)
args = parser.parse_args()
# Get all Python files
root: Path = args.root.absolute()
python_files = root.glob("integrations/**/*.py")
if args.integration:
python_files = root.glob(f"integrations/{args.integration}/**/*.py")
md5 = docstrings_checksum(python_files)
print(md5)