Skip to content

Commit 4298c24

Browse files
committed
[FEATURE] Added logging to the script. This fixes #90
1 parent dc8d68a commit 4298c24

File tree

17 files changed

+303
-21
lines changed

17 files changed

+303
-21
lines changed

42PyChecker.py

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import platform
88
from PyChecker.projects import libft, ft_commandements, other, fillit
99
import sys
10+
import logging
1011

1112

1213
def check_args_rules(parser, args):
@@ -34,77 +35,103 @@ def check_args_rules(parser, args):
3435
3536
:param args: the parsed arguments passed to the program
3637
"""
37-
38+
logging.info("Starting argument checking.")
3839
# If no project is given the parser sends an error.
3940
if args.project is None:
41+
logging.critical('You need to specify a project.')
4042
parser.error("You need to specify a project.")
4143
# If the path of the selected project is empty, the parser prints an error.
4244
if args.path == "":
45+
logging.critical("`--path' needs to be specified in order for 42PyChecker"
46+
" to know where your project is.")
4347
parser.error("`--path' needs to be specified in order for 42PyChecker"
4448
" to know where your project is.")
4549
if args.path[0] != '/':
50+
logging.critical("`--path' needs to have an absolute path")
4651
parser.error("`--path' needs to have an absolute path")
4752
# If a test is disabled and the libft project is selected, the parser will
4853
# return an error.
4954

5055
# Here, if the `--no-tests` option is set, all the testing suites will be
5156
# disabled, no matter the project.
5257
if args.project == "other" and args.no_tests:
58+
logging.critical("`--no-tests' Can only be applied on projects, not when 'other' is selected.")
5359
parser.error("`--no-tests' Can only be applied on projects, not when 'other' is selected.")
5460

5561
if args.no_author and args.project == "42commandements":
62+
logging.critical("`--no-author' Can only be applied on project, but not on 42commandements.")
5663
parser.error("`--no-author' Can only be applied on project, but not on 42commandements.")
5764

5865
forbidden_functions_projects = ['fdf', 'fillit', 'ft_ls', 'ft_p', 'ft_printf', 'gnl', 'get_next_line', 'libft', 'libftasm', 'libft_asm', 'minishell', 'pushswap', 'push_swap']
5966
if args.no_forbidden_functions and args.project not in forbidden_functions_projects:
67+
logging.critical("`--no-forbidden-functions' Cannot be set if project isn't one of " + str(forbidden_functions_projects))
6068
parser.error("`--no-forbidden-functions' Cannot be set if project isn't one of " + str(forbidden_functions_projects))
6169

6270
if args.no_makefile and args.project == "42commandements":
71+
logging.critical("`--no-makefile' Can only be applied on project, but not on 42commandements.")
6372
parser.error("`--no-makefile' Can only be applied on project, but not on 42commandements.")
6473

6574
if args.no_norm and args.project == "42commandements":
75+
logging.critical("`--no-norm' Can only be applied on project, but not on 42commandements.")
6676
parser.error("`--no-norm' Can only be applied on project, but not on 42commandements.")
6777

6878
if args.no_static and args.project != "libft":
79+
logging.critical("`--no-static' Can only be applied project `libft'")
6980
parser.error("`--no-static' Can only be applied project `libft'")
7081

7182
if args.no_extra and args.project != "libft":
83+
logging.critical("`--no-extra' Can only be applied project `libft'")
7284
parser.error("`--no-extra' Can only be applied project `libft'")
7385

7486
if args.no_required and args.project != "libft":
87+
logging.critical("`--no-required' Can only be applied project `libft'")
7588
parser.error("`--no-required' Can only be applied project `libft'")
7689

7790
if args.no_bonus and args.project != "libft":
91+
logging.critical("`--no-bonus' Can only be applied project `libft'")
7892
parser.error("`--no-bonus' Can only be applied project `libft'")
7993

8094
if args.do_benchmark and args.project != "libft":
95+
logging.critical("`--do-benchmark' Can only be applied project `libft'")
8196
parser.error("`--do-benchmark' Can only be applied project `libft'")
8297

8398
if args.no_libftest and args.project != "libft":
99+
logging.critical("`--no-libftest' can only be applied if libft is selected "
100+
"with `--project'")
84101
parser.error("`--no-libftest' can only be applied if libft is selected "
85102
"with `--project'")
86103

87104
if args.no_maintest and args.project != "libft":
105+
logging.critical("`--no-maintest' can only be applied if libft is selected "
106+
"with `--project'")
88107
parser.error("`--no-maintest' can only be applied if libft is selected "
89108
"with `--project'")
90109

91110
if args.no_moulitest and args.project != "libft":
111+
logging.critical("`--no-moulitest' can only be applied if libft is selected"
112+
" with `--project'")
92113
parser.error("`--no-moulitest' can only be applied if libft is selected"
93114
" with `--project'")
94115

95116
if args.no_libft_unit_test and args.project != "libft":
117+
logging.critical("`--no-libft-unit-test' can only be applied if libft is selected"
118+
" with `--project'")
96119
parser.error("`--no-libft-unit-test' can only be applied if libft is selected"
97120
" with `--project'")
98121

99122
if args.no_fillit_checker and args.project != "fillit":
123+
logging.critical("`--no-fillit-checker' can only be applied if fillit is selected"
124+
" with `--project'")
100125
parser.error("`--no-fillit-checker' can only be applied if fillit is selected"
101126
" with `--project'")
102127
if args.no_tests:
128+
logging.debug("Option `--no-tests` selected. Setting all tests options to 'True' to disable them all")
103129
args.no_libftest = True
104130
args.no_maintest = True
105131
args.no_moulitest = True
106132
args.no_libft_unit_test = True
107133
args.no_fillit_checker = True
134+
logging.info("Argument checking done.")
108135

109136

110137
def print_header():
@@ -130,8 +157,8 @@ def main():
130157

131158
# @todo: Add verbose output
132159
# Adds all the arguments one by one.
133-
parser.add_argument("-v", "--verbose", help="Increases output verbosity",
134-
action="store_true")
160+
parser.add_argument("--log", help="Sets up the log output.",
161+
choices=['debug', 'DEBUG', 'info', 'INFO', 'warning', 'WARNING', 'error', 'ERROR', 'critical', 'CRITICAL'])
135162
parser.add_argument("--no-gui", help="disables the Graphical User Interface",
136163
action="store_true")
137164
parser.add_argument("--project", help="Specifies the type of project you want to check", choices=['libft', '42commandements', 'other', 'fillit'], default=None)
@@ -176,22 +203,33 @@ def main():
176203
with open(root_path + '/.github/LICENSE.lesser', 'r') as file:
177204
print(file.read())
178205
sys.exit()
179-
206+
# @todo: Check for log file size and delete it if needed.
207+
logging.basicConfig(filename='42PyChecker.log', level=args.log.upper(), format='%(asctime)s,%(msecs)d %(levelname)-8s [%(filename)s:%(lineno)d] %(message)s', datefmt='%d-%m-%Y:%H:%M:%S')
208+
logging.info("************************************************************")
209+
logging.info("***********Starting new instance of 42PyChecker*************")
210+
logging.info("************************************************************")
211+
# @todo: Format the args to be printed in log
212+
logging.debug("Arguments passed : {}".format(args))
180213
check_args_rules(parser, args)
181214

182215
# Here we create the directory where the testing suites will be cloned
183216
if not os.path.exists(root_path + '/testing_suites'):
217+
logging.debug("The directory `{}/testing_suites` doesn't exist. Creating it".format(root_path))
184218
os.makedirs(root_path + '/testing_suites')
185219

186220
# Here we select the project and start the check based on the argument `--project`
187221
if args.project == "libft":
222+
logging.info("Starting {} project check".format(args.project))
188223
libft.check(root_path, args)
189224
if args.project == "42commandements":
225+
logging.info("Starting {} project check".format(args.project))
190226
ft_commandements.check(args)
191-
if args.project == "other":
192-
other.check(root_path, args)
193227
if args.project == "fillit":
228+
logging.info("Starting {} project check".format(args.project))
194229
fillit.check(root_path, args)
230+
if args.project == "other":
231+
logging.info("Starting {} project check".format(args.project))
232+
other.check(root_path, args)
195233

196234

197235
if __name__ == '__main__':

PyChecker/projects/fillit.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55

66
from PyChecker.utils import author, norme, makefile, forbidden_functions
77
from PyChecker.testing_suite import fillit_checker
8+
import logging
89

910

1011
def check(root_path: str, args):
11-
12+
logging.info("Started checks for project `fillit`")
1213
authorized_functions = ['exit', 'open', 'close', 'write', 'read', 'malloc',
1314
'free', 'main']
1415

@@ -35,4 +36,5 @@ def check(root_path: str, args):
3536
print("Forbidden Functions: \n" + forbidden_functions_results)
3637
if not args.no_fillit_checker:
3738
print("Fillit Checker: \n" + fillit_checker_results)
39+
logging.info("Finished checks for project `fillit`")
3840
return 0

PyChecker/projects/ft_commandements.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"""
55

66
import os
7+
import logging
78

89

910
def check(args):
@@ -15,12 +16,16 @@ def check(args):
1516
1 if the file doesn't exists,
1617
2 if the file content is different.
1718
"""
19+
logging.info("Started checks for project `42Commandements`")
1820
if not os.path.exists(args.path + '/rendu'):
21+
logging.critical("File rendu not found")
1922
print("file `rendu' not found.")
2023
return 1
2124
with open(args.path + '/rendu', 'r') as file:
2225
content = file.read()
2326
if content != "En choisissant d'effectuer ma scolarite a 42, je declare adherer a ces regles ainsi qu'aux valeurs morales qu'elles vehiculent.\n":
27+
logging.critical("Error: The `rendu' file content is different from what is expected.")
2428
print("Error: The `rendu' file content is different from what is expected.")
2529
return 2
30+
logging.info("Finished checks for project `42Commandements`")
2631
return 0

PyChecker/projects/libft.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,42 @@
77
from PyChecker.utils import author, forbidden_functions, makefile, norme, static
88
from PyChecker.testing_suite import maintest, moulitest, libftest
99
from PyChecker.testing_suite import libft_unit_test
10+
import logging
11+
1012

1113
def check_required(project_path: str, required_functions):
14+
logging.info("LIBFT: Started required function check.")
1215
while True:
1316
if all([os.path.isfile(project_path + '/' + function) for function in required_functions]):
1417
break
1518
else:
19+
logging.debug("LIBFT: ERROR: not all required files are here")
1620
print("--> ERROR: not all required files are here")
21+
logging.info("LIBFT: Finished required function check.")
1722
return "--> ERROR: not all required files are here"
23+
logging.debug("LIBFT: All required functions are present.")
24+
logging.info("LIBFT: Finished required function check.")
1825
return "-- All required functions are present."
1926

2027
def check_bonuses(project_path: str, bonus_functions):
28+
logging.info("LIBFT: Started bonus function check")
2129
has_libft_bonuses = True
2230
while True:
2331
if all([os.path.isfile(project_path + '/' + function) for function in bonus_functions]):
32+
logging.debug("LIBFT: Found all bonuses files")
2433
break
2534
else:
2635
has_libft_bonuses = False
36+
logging.warning("LIBFT: Warning: Warning: not all bonus files are here")
2737
print("--> Warning: not all bonus files are here")
38+
logging.info("LIBFT: Finished bonus function check")
2839
return has_libft_bonuses, "--> Warning: not all bonus files are here"
40+
logging.info("LIBFT: Finished bonus function check")
2941
return has_libft_bonuses, "-- All bonuses files were found."
3042

3143

3244
def count_extras(project_path: str, required_functions, bonus_functions):
45+
logging.info("LIBFT: Started extra function count.")
3346
file_list = []
3447
for file in glob.glob(project_path + '/*.c'):
3548
file_list.append(file.replace(project_path + '/', ''))
@@ -38,12 +51,17 @@ def count_extras(project_path: str, required_functions, bonus_functions):
3851
print("*------------------------Extra functions:-----------------------*")
3952
print("*---------------------------------------------------------------*")
4053
print("You have {} extra functions.".format(len(extra_functions)))
54+
if extra_functions:
55+
logging.debug("Found extra functions:")
4156
for function in extra_functions:
57+
logging.debug(function)
4258
print(function)
59+
logging.info("LIBFT: Finished extra function count.")
4360
return extra_functions
4461

4562

4663
def check(root_path: str, args):
64+
logging.info("Started check for project `libft`")
4765
required_functions = ['libft.h', 'ft_strcat.c', 'ft_strncat.c',
4866
'ft_strlcat.c', 'ft_strchr.c', 'ft_strnstr.c',
4967
'ft_strrchr.c', 'ft_strclr.c', 'ft_strcmp.c',
@@ -125,4 +143,5 @@ def check(root_path: str, args):
125143
print('libft-unit-test: \n' + libft_unit_test_results + '\n\n' + benchmark_results + '\n')
126144
else:
127145
print('libft-unit-test: \n' + libft_unit_test_results + '\n')
146+
logging.info("Finished check for project `libft`")
128147
return 0

PyChecker/projects/other.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
See full notice in `LICENSE'
44
"""
55
from PyChecker.utils import author, norme, makefile
6+
import logging
67

78

89
def check(root_path: str, args):
10+
logging.info("Starting check for project `other`")
911
if not args.no_author:
1012
author.check(args.path)
1113
if not args.no_norm:

PyChecker/testing_suite/fillit_checker.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@
77
from PyChecker.utils import git
88
import re
99
import os
10+
import logging
1011

1112

1213
def clean_log(root_path: str):
14+
logging.info("FILLIT_CHECKER: Cleaning log file.")
15+
logging.debug("FILLIT_CHECKER: Opening file {}.".format(root_path + '/.myfillitchecker-clean'))
1316
with open(root_path + '/.myfillitchecker-clean', 'w+') as file2:
17+
logging.debug("FILLIT_CHECKER: Opening file {}.".format(root_path + '/.myfillitchecker'))
1418
with open(root_path + '/.myfillitchecker', 'r') as file:
1519
for line in file:
1620
line = re.sub(r"\033\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]/", "", line)
@@ -26,17 +30,20 @@ def run(root_path: str, project_path: str):
2630
2731
:param project_path: The path of the project you want to test.
2832
"""
33+
logging.info("Starting fillit checker tests.")
2934
print("*---------------------------------------------------------------*")
3035
print("*-------------------------fillit_checker------------------------*")
3136
print("*---------------------------------------------------------------*")
32-
3337
if "fatal: Not a git repository" in git.status(root_path + '/testing_suites/fillit_checker'):
3438
git.clone("https://github.com/anisg/fillit_checker", root_path + '/testing_suites/fillit_checker')
3539
else:
3640
git.reset(root_path + '/testing_suites/fillit_checker')
3741

3842
# @todo: Find a way to supress colors from output
43+
logging.debug("FILLIT_CHECKER: Running `bash {}`".format(root_path + "/testing_suites/fillit_checker/test.sh"))
3944
result = subprocess.run(['bash', root_path + "/testing_suites/fillit_checker/test.sh", project_path], stdout=subprocess.PIPE, stderr=subprocess.STDOUT).stdout.decode('utf-8')
45+
logging.debug("FILLIT_CHECKER: Opening file {}.".format(
46+
root_path + '/.myfillitchecker'))
4047
with open(root_path + '/.myfillitchecker', 'w+') as file:
4148
file.write(result)
4249
#clean_log(root_path)
@@ -45,4 +52,5 @@ def run(root_path: str, project_path: str):
4552
# for line in file:
4653
# if "NOTE" in line:
4754
# return line
55+
logging.info("Finished fillit checker tests.")
4856
return "WIP"

PyChecker/testing_suite/libft_unit_test.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,47 +8,58 @@
88
import os
99
import io
1010
from PyChecker.utils import git
11+
import logging
1112

1213

1314
def run(root_path: str, args):
1415

15-
16+
logging.info("Started runing `libft-unit-test` tests.")
1617
print("*---------------------------------------------------------------*")
1718
print("*------------------------libft-unit-test------------------------*")
1819
print("*---------------------------------------------------------------*")
19-
2020
if platform.system() != 'Darwin':
2121
print("Sorry, this testing suite can only be ran on Darwin computers (MacOS)")
2222
with open(root_path + '/.mylibftunittest', 'w+') as file:
2323
file.write("Sorry, this testing suite can only be ran on Darwin computers (MacOS)\n")
24+
logging.critical("Sorry, this testing suite can only be ran on Darwin computers (MacOS)")
2425
return "Sorry, this testing suite can only be ran on Darwin computers (MacOS)"
2526

2627
if "fatal: Not a git repository" in git.status(root_path + '/testing_suites/libft-unit-test'):
2728
git.clone("https://github.com/alelievr/libft-unit-test.git", root_path + '/testing_suites/libft-unit-test')
2829
else:
2930
git.reset(root_path + '/testing_suites/libft-unit-test')
3031

32+
logging.debug("LUB: Opening file {}.".format(root_path + '/.mylibftunittest'))
3133
with open(root_path + '/.mylibftunittest', 'w+') as file:
34+
logging.debug("LUB: Running `make re -C {} LIBFTDIR={}`".format(root_path + '/testing_suites/libft-unit-test', args.path))
3235
result = subprocess.run(['make', 're', '-C', root_path + '/testing_suites/libft-unit-test', 'LIBFTDIR=' + args.path], stdout=subprocess.PIPE, stderr=subprocess.STDOUT).stdout.decode('utf-8')
3336
file.write(result)
3437
print(result)
38+
logging.debug("LUB: Running `make f {} LIBFTDIR={}`".format(root_path + '/testing_suites/libft-unit-test', args.path))
3539
result = subprocess.run(['make', 'f', '-C', root_path + '/testing_suites/libft-unit-test', 'LIBFTDIR=' + args.path], stdout=subprocess.PIPE, stderr=subprocess.STDOUT).stdout.decode('utf-8')
3640
print(result)
3741
file.write(result)
42+
logging.debug("LUB: Renaming result.log and moving it into {}".format(root_path + '/.mylibftunittest-results'))
3843
os.rename(root_path + '/testing_suites/libft-unit-test/result.log', root_path + '/.mylibftunittest-results')
3944
# Starts benchmark if asked
4045
if args.do_benchmark:
46+
logging.debug("LUB: Starting benchmark")
4147
oldpwd = os.getcwd()
48+
logging.debug("Changing directory to {}".format(root_path + '/testing_suites/libft-unit-test/'))
4249
os.chdir(root_path + '/testing_suites/libft-unit-test/')
50+
logging.debug("LUB: Running `./run_test -b`")
4351
result = subprocess.run(['./run_test', '-b'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT).stdout.decode('utf-8')
52+
logging.debug("LUB: Changing back directory to {}".format(oldpwd))
4453
os.chdir(oldpwd)
4554
print(result)
4655
file.write(result)
56+
logging.debug("LUB: Opening file {}".format(root_path + '/.mylibftunittest'))
4757
with open(root_path + '/.mylibftunittest', 'r') as file2:
4858
for line in file2:
4959
if "WINNER:" in line:
5060
benchmark_results = line
5161
# Open result file with correct encoding
62+
logging.debug("LUB: Opening file {} with encoding ISO-8859-1".format(root_path + '/.mylibftunittest-results'))
5263
with io.open(root_path + '/.mylibftunittest-results', "r", encoding="ISO-8859-1") as file:
5364
data = file.read()
5465
results = 'OKs: ' + str(data.count('OK')) + '\n'
@@ -59,6 +70,7 @@ def run(root_path: str, args):
5970
results += 'not protected:' + str(data.count('not protected'))
6071
print(results)
6172
# Send results back
73+
logging.info("Finished runing `libft-unit-test` tests.")
6274
if args.do_benchmark:
6375
return results, benchmark_results
6476
else:

0 commit comments

Comments
 (0)