From bab2b0fb46203a9491dc18dc19975ee5afaf387f Mon Sep 17 00:00:00 2001 From: snaksartrate Date: Fri, 27 Jun 2025 12:49:51 +0530 Subject: [PATCH 1/8] adding analyze,yml --- .github/workflows/analyze.yml | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 .github/workflows/analyze.yml diff --git a/.github/workflows/analyze.yml b/.github/workflows/analyze.yml new file mode 100644 index 0000000..8df2667 --- /dev/null +++ b/.github/workflows/analyze.yml @@ -0,0 +1,15 @@ +name: analyze.py + +on: + push: + branches: + - main + +jobs: + analysis: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: analyze + run: echo "analyzing" + shell: bash \ No newline at end of file From 140044df46d96ee0d8ece97399a5756be4fc355d Mon Sep 17 00:00:00 2001 From: snaksartrate Date: Fri, 27 Jun 2025 13:07:02 +0530 Subject: [PATCH 2/8] adding analyze.py --- analyze.py | 119 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) diff --git a/analyze.py b/analyze.py index e69de29..2a8d825 100644 --- a/analyze.py +++ b/analyze.py @@ -0,0 +1,119 @@ +def remove_whitespaces_from_end(clone): + if clone[len(clone) - 1] == ' ' and len(clone) >= 80: + return remove_whitespaces_from_end(clone[:len(clone) - 1]) + return clone[:len(clone)] + +def line_length_violation(clone): + if len(clone) < 80: + return False + remove_whitespaces_from_end(clone) + if len(clone) < 80: + return False + return True + +def remove_comment(clone): + if clone.find('#') >= 0: + return clone[:clone.find('#')] + else: + return clone + +def check_unclosed_string(clone): + no_of_double = 0 + no_of_single = 0 + for char in clone: + if char == '\'': + no_of_single += 1 + elif char == '\"': + no_of_double += 1 + return no_of_double % 2 != 0 or no_of_single % 2 != 0 + +def check_print(word): + if "print" in word: + return True + return False + +def check_eval(word): + if "eval" in word: + return True + return False + +def check_exec(word): + if "exec" in word: + return True + return False + +def check_violations(line): + no_of_violations_for_this_line = 0 + clone = line + if line_length_violation(clone): + no_of_violations_for_this_line += 1 + clone = remove_comment(clone) + if check_unclosed_string(clone): + no_of_violations_for_this_line += 1 + wordlist = clone.split() + forbidden_keyword_was_used_in_this_line = False + for word in wordlist: + if check_eval(word): + no_of_violations_for_this_line += 1 + forbidden_keyword_was_used_in_this_line = True + if check_exec(word): + no_of_violations_for_this_line += 1 + forbidden_keyword_was_used_in_this_line = True + if check_print(word): + no_of_violations_for_this_line += 1 + forbidden_keyword_was_used_in_this_line = True + return no_of_violations_for_this_line, forbidden_keyword_was_used_in_this_line + +no_of_files = int(input("enter the total number of files to be analyzed: ")) +file_path_list = [] +no_of_violations_list_filewise = [] +forbidden_keyword_used_list_filewise = [] + +for _ in range(no_of_files): + no_of_violations_in_this_file = 0 + forbidden_keyword_was_used_in_this_file = False + file_path = input("enter file path: ") + with open(file_path, 'r') as f: + for line in f: + no_of_violations_in_this_line, forbidden_keyword_was_used_in_this_line = check_violations(line) + no_of_violations_in_this_file += no_of_violations_in_this_line + forbidden_keyword_was_used_in_this_file = forbidden_keyword_was_used_in_this_file or forbidden_keyword_was_used_in_this_line + file_path_list.append(file_path) + no_of_violations_list_filewise.append(no_of_violations_in_this_file) + forbidden_keyword_used_list_filewise.append(forbidden_keyword_was_used_in_this_file) + +max_path_length = 9 +for i in range(no_of_files): + if len(file_path_list[i]) > max_path_length: + max_path_length = len(file_path_list[i]) + +s = '| Sr. No. | File Path ' +for _ in range(9, max_path_length): + s += ' ' +s += '| Status | No of violations |' +print(s) + +for i in range(no_of_files): + + s = '| ' + str(i + 1) + ' | ' + file_path_list[i] + + for i in range(max_path_length - len(file_path_list[i])): + s += " " + s += " | " + + if no_of_violations_list_filewise[i] == 0: + s += "CLEAN | 0 |" + elif no_of_violations_list_filewise[i] > 5 or forbidden_keyword_used_list_filewise[i]: + s += "HIGH RISK | " + s += str(no_of_violations_list_filewise[i]) + if no_of_violations_list_filewise[i] <= 9: + s += " |" + elif no_of_violations_list_filewise[i] <= 99: + s += " |" + else: + s += " |" + else: + s += "LOW RISK | " + s += str(no_of_violations_list_filewise[i]) + s += " |" + print(s) \ No newline at end of file From 67cdb0eaa9d32d937ca456795702fe0cdb381019 Mon Sep 17 00:00:00 2001 From: snaksartrate Date: Fri, 27 Jun 2025 13:11:42 +0530 Subject: [PATCH 3/8] updated workflow to run analyze.py --- .github/workflows/analyze.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/analyze.yml b/.github/workflows/analyze.yml index 8df2667..4cb6cb9 100644 --- a/.github/workflows/analyze.yml +++ b/.github/workflows/analyze.yml @@ -11,5 +11,5 @@ jobs: steps: - uses: actions/checkout@v4 - name: analyze - run: echo "analyzing" + run: python analyze.py shell: bash \ No newline at end of file From 0171e033c13953131fa5ed78f26357346b023eec Mon Sep 17 00:00:00 2001 From: snaksartrate Date: Fri, 27 Jun 2025 13:23:41 +0530 Subject: [PATCH 4/8] changed branch name --- .github/workflows/analyze.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/analyze.yml b/.github/workflows/analyze.yml index 4cb6cb9..1bcb386 100644 --- a/.github/workflows/analyze.yml +++ b/.github/workflows/analyze.yml @@ -3,7 +3,7 @@ name: analyze.py on: push: branches: - - main + - task3 jobs: analysis: From 77a98a5cc60dcbaa7fc9d414d1066a04ac22edc5 Mon Sep 17 00:00:00 2001 From: snaksartrate Date: Fri, 27 Jun 2025 14:15:13 +0530 Subject: [PATCH 5/8] changed a noob mistake, manual input to input automated by os --- analyze.py | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/analyze.py b/analyze.py index 2a8d825..ecf1620 100644 --- a/analyze.py +++ b/analyze.py @@ -1,3 +1,5 @@ +import os + def remove_whitespaces_from_end(clone): if clone[len(clone) - 1] == ' ' and len(clone) >= 80: return remove_whitespaces_from_end(clone[:len(clone) - 1]) @@ -64,26 +66,27 @@ def check_violations(line): forbidden_keyword_was_used_in_this_line = True return no_of_violations_for_this_line, forbidden_keyword_was_used_in_this_line -no_of_files = int(input("enter the total number of files to be analyzed: ")) +src_folder = 'src' file_path_list = [] no_of_violations_list_filewise = [] forbidden_keyword_used_list_filewise = [] -for _ in range(no_of_files): - no_of_violations_in_this_file = 0 - forbidden_keyword_was_used_in_this_file = False - file_path = input("enter file path: ") - with open(file_path, 'r') as f: - for line in f: - no_of_violations_in_this_line, forbidden_keyword_was_used_in_this_line = check_violations(line) - no_of_violations_in_this_file += no_of_violations_in_this_line - forbidden_keyword_was_used_in_this_file = forbidden_keyword_was_used_in_this_file or forbidden_keyword_was_used_in_this_line - file_path_list.append(file_path) - no_of_violations_list_filewise.append(no_of_violations_in_this_file) - forbidden_keyword_used_list_filewise.append(forbidden_keyword_was_used_in_this_file) +for root, dirs, files in os.walk(src_folder): + for file in files: + no_of_violations_in_this_file = 0 + forbidden_keyword_was_used_in_this_file = False + if file.endswith('.py'): + with open(file, 'r') as f: + for line in f: + no_of_violations_in_this_line, forbidden_keyword_was_used_in_this_line = check_violations(line) + no_of_violations_in_this_file += no_of_violations_in_this_line + forbidden_keyword_was_used_in_this_file = forbidden_keyword_was_used_in_this_file or forbidden_keyword_was_used_in_this_line + file_path_list.append(file) + no_of_violations_list_filewise.append(no_of_violations_in_this_file) + forbidden_keyword_used_list_filewise.append(forbidden_keyword_was_used_in_this_file) max_path_length = 9 -for i in range(no_of_files): +for i in range(len(file_path_list)): if len(file_path_list[i]) > max_path_length: max_path_length = len(file_path_list[i]) @@ -93,7 +96,7 @@ def check_violations(line): s += '| Status | No of violations |' print(s) -for i in range(no_of_files): +for i in range(len(file_path_list)): s = '| ' + str(i + 1) + ' | ' + file_path_list[i] From a6e9d60c89da05e5e901e5df376b7a8d24a22e56 Mon Sep 17 00:00:00 2001 From: snaksartrate Date: Fri, 27 Jun 2025 14:21:39 +0530 Subject: [PATCH 6/8] updated analyze.py to be able to handle nested folders --- analyze.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/analyze.py b/analyze.py index ecf1620..4d1b44f 100644 --- a/analyze.py +++ b/analyze.py @@ -73,15 +73,16 @@ def check_violations(line): for root, dirs, files in os.walk(src_folder): for file in files: - no_of_violations_in_this_file = 0 - forbidden_keyword_was_used_in_this_file = False if file.endswith('.py'): - with open(file, 'r') as f: + no_of_violations_in_this_file = 0 + forbidden_keyword_was_used_in_this_file = False + file_path = os.path.join(root, file) + with open(file_path, 'r') as f: for line in f: no_of_violations_in_this_line, forbidden_keyword_was_used_in_this_line = check_violations(line) no_of_violations_in_this_file += no_of_violations_in_this_line forbidden_keyword_was_used_in_this_file = forbidden_keyword_was_used_in_this_file or forbidden_keyword_was_used_in_this_line - file_path_list.append(file) + file_path_list.append(file_path) no_of_violations_list_filewise.append(no_of_violations_in_this_file) forbidden_keyword_used_list_filewise.append(forbidden_keyword_was_used_in_this_file) @@ -100,7 +101,7 @@ def check_violations(line): s = '| ' + str(i + 1) + ' | ' + file_path_list[i] - for i in range(max_path_length - len(file_path_list[i])): + for _ in range(max_path_length - len(file_path_list[i])): s += " " s += " | " From b8cc3e611093fdc4fb047dfeebb92982990b1e00 Mon Sep 17 00:00:00 2001 From: snaksartrate Date: Fri, 27 Jun 2025 15:43:48 +0530 Subject: [PATCH 7/8] removing an unneeded check from line_length_violation() definition --- analyze.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/analyze.py b/analyze.py index 4d1b44f..99e5275 100644 --- a/analyze.py +++ b/analyze.py @@ -6,8 +6,6 @@ def remove_whitespaces_from_end(clone): return clone[:len(clone)] def line_length_violation(clone): - if len(clone) < 80: - return False remove_whitespaces_from_end(clone) if len(clone) < 80: return False From bd2459e30223c063bd61f25e5b2c8facedccf313 Mon Sep 17 00:00:00 2001 From: snaksartrate Date: Fri, 27 Jun 2025 15:46:46 +0530 Subject: [PATCH 8/8] made line length = 80 a not violation --- analyze.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/analyze.py b/analyze.py index 99e5275..0899966 100644 --- a/analyze.py +++ b/analyze.py @@ -7,7 +7,7 @@ def remove_whitespaces_from_end(clone): def line_length_violation(clone): remove_whitespaces_from_end(clone) - if len(clone) < 80: + if len(clone) <= 80: return False return True