|
1 | 1 | import os |
2 | 2 | import sys |
3 | | -import subprocess |
| 3 | +import glob |
4 | 4 |
|
5 | 5 | def check_author_file(project_path: str): |
| 6 | + """ |
| 7 | + :param project_path: The path of the project where you want to check the author file |
| 8 | +
|
| 9 | + :return: This function will return 0 if everything is ok, |
| 10 | + 1 if file not found, |
| 11 | + 2 if there's too many lines in the file, |
| 12 | + 3 if the newline char is missing in the end of line |
| 13 | + """ |
6 | 14 | # @todo: Add a skip if author file set as optional |
7 | 15 | author_fr = project_path + "/auteur" |
8 | 16 | author_us = project_path + "/author" |
9 | 17 | if os.path.exists(author_fr): |
10 | | - output = subprocess.check_output("awk 'END {printf NR}' " + author_fr, shell=True) |
| 18 | + count = len(open(author_fr).readlines()) |
| 19 | + author = "fr" |
11 | 20 | elif os.path.exists(author_us): |
12 | | - output = subprocess.check_output("awk 'END {printf NR}' " + author_us, shell=True) |
| 21 | + count = len(open(author_us).readlines()) |
| 22 | + author = "us" |
13 | 23 | else: |
14 | 24 | print("Author file not found") |
15 | | - sys.exit() # @todo Add message if author file is set as optional |
16 | | - if output != 1: |
| 25 | + return 1 # @todo Add message if author file is set as optional |
| 26 | + if count != 1: |
17 | 27 | print("Too many lines in author file (Or the file is empty)") |
18 | | - sys.exit() # @todo Add message if author file is set as optional or if project isn't solo |
| 28 | + return 2 # @todo Add message if author file is set as optional or if project isn't solo |
| 29 | + if author == "fr": |
| 30 | + with open(author_fr, 'r') as file: |
| 31 | + content = file.read() |
| 32 | + if "\n" not in content: |
| 33 | + print("Missing <newline> character at the end of line") |
| 34 | + return 3 # @todo: Add message if author file is set as optional and handle multiple authors |
| 35 | + elif author == "us": |
| 36 | + with open(author_us, 'r') as file: |
| 37 | + content = file.read() |
| 38 | + if "\n" not in content: |
| 39 | + print("Missing <newline> character at the end of line") |
| 40 | + return 3 # @todo: Add message if author file is set as optional and handle multiple authors |
| 41 | + return 0 |
| 42 | + |
| 43 | + |
| 44 | +def check_norme(project_path: str): |
| 45 | + """ |
| 46 | + :param project_path: The path of the project where you want to check the author file |
| 47 | +
|
| 48 | + :return: Returns 0 if everything is ok, |
| 49 | + 1 if there isn't any file to check |
| 50 | + """ |
| 51 | + # @todo: Add a skip if norme is set as optional |
| 52 | + files = "" |
| 53 | + with open(os.path.dirname(os.path.realpath(__file__)) + "/.mynorme", 'w+') as file: |
| 54 | + for filename in glob.iglob(project_path + '/**/*.c', recursive=True): |
| 55 | + files = files + ' ' + filename |
| 56 | + for filename in glob.iglob(project_path + '/**/*.h', recursive=True): |
| 57 | + files = files + ' ' + filename |
| 58 | + if file == "": |
| 59 | + print("No source file (.c) or header (.h) to check") |
| 60 | + return 1 |
| 61 | + |
19 | 62 |
|
20 | | -check_author_file("C:/Users/Jules/PycharmProjects/42PyChecker") |
| 63 | +sys.exit(check_norme("C:/Users/Jules/Share/ft_printf")) |
0 commit comments