|
2 | 2 | import sys |
3 | 3 | import glob |
4 | 4 | import subprocess |
5 | | - |
| 5 | +import re |
6 | 6 |
|
7 | 7 | def check_author_file(project_path: str): |
8 | 8 | """ |
@@ -203,6 +203,24 @@ def check_42_commandements(project_path:str): |
203 | 203 | return 2 |
204 | 204 | return 0 |
205 | 205 |
|
| 206 | + |
| 207 | +def check_forbidden_functions(project_path: str, binary: str): |
| 208 | + # @todo: Refactor check_forbidden_functions to have a modular authorized_functions (to be used on multiple projects) |
| 209 | + authorized_functions = ['free', 'malloc', 'write', 'main'] |
| 210 | + functions_called = [] |
| 211 | + result = subprocess.run(['nm', project_path + '/' + binary], stdout=subprocess.PIPE).stdout.decode('utf-8') |
| 212 | + for line in result.splitlines(): |
| 213 | + if "U _" in line: |
| 214 | + functions_called.append(re.sub(' +', ' ', line)) |
| 215 | + sys_calls = {function.replace(' U _', '') for function in functions_called} |
| 216 | + sys_calls = [item for item in sys_calls if not item.startswith("ft_")] |
| 217 | + extra_function_call = [item for item in sys_calls if item not in authorized_functions] |
| 218 | + with open(os.path.dirname(os.path.realpath(__file__)) + "/.myforbiddenfunctions", 'w+') as file: |
| 219 | + for item in extra_function_call: |
| 220 | + file.write("You should justify the use of this function: `{}'\n".format(item)) |
| 221 | + return 0 |
| 222 | + |
| 223 | + |
206 | 224 | def check_libft(project_path: str): |
207 | 225 | required_functions = ['libft.h', 'ft_strcat.c', 'ft_strncat.c', |
208 | 226 | 'ft_strlcat.c', 'ft_strchr.c', 'ft_strnstr.c', |
@@ -246,11 +264,11 @@ def check_libft(project_path: str): |
246 | 264 | result = subprocess.run(['sh', 'check_static.sh', project_path], stdout=subprocess.PIPE).stdout.decode('utf-8') |
247 | 265 | file.write(result) |
248 | 266 | check_makefile(project_path, "libft.a") # @todo: Have number of errors printed for makefile check |
249 | | - #forbidden @todo: Add forbidden function check utility method |
| 267 | + check_forbidden_functions(project_path, "libft.a") |
250 | 268 | #moulitest |
251 | 269 | #libft-unit-test |
252 | 270 | #maintest |
253 | 271 | return 0 |
254 | 272 |
|
255 | 273 |
|
256 | | -sys.exit(check_libft("/tmp/libft")) |
| 274 | +sys.exit(check_forbidden_functions("/tmp/libft", "libft.a")) |
0 commit comments