Coverage for additional_code_checks.py: 0%

1 import ast

2 import sys

3 from glob import glob

4

5

6 def check_ast(node: ast.AST, path: str):

7 ok = True

8 if hasattr(node, "body"):

9 for child in node.body:

10 child_ok = check_ast(child, path)

11 ok = ok and child_ok

12 else:

13 if isinstance(node, ast.Assert):

14 print(f"assert detected in line {node.lineno} of {path}")

15 ok = False

16 if isinstance(node, ast.Expr) and isinstance(node.value, ast.Call):

17 value = node.value

18 if (

19 hasattr(value, "func")

20 and hasattr(value.func, "id")

21 and value.func.id == "print"

22 ):

23 print(f"print detected in line {node.lineno} of {path}")

24 ok = False

25 return ok

26

27

28 def check_modules(globpath: str):

29 paths = glob(globpath)

30 ok = True

31 for path in paths:

32 print("checking", path)

33 with open(path) as f:

34 tree = ast.parse(f.read())

35 module_ok = check_ast(tree, path)

36 ok = ok and module_ok

37 if ok:

38 sys.exit(0)

39 else:

40 sys.exit(1)

41

42

43 if __name__ == "__main__":

44 globpath = sys.argv[1]

45 print("checking", globpath)

46 check_modules(globpath)