From 9869fdf4456544ee23286406da5244ac34cdddc4 Mon Sep 17 00:00:00 2001 From: jorenchik Date: Sat, 4 Jan 2025 17:48:19 +0200 Subject: [PATCH] lloc counting utility --- count_lloc_cpp_h.py | 113 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 count_lloc_cpp_h.py diff --git a/count_lloc_cpp_h.py b/count_lloc_cpp_h.py new file mode 100644 index 0000000..d07c255 --- /dev/null +++ b/count_lloc_cpp_h.py @@ -0,0 +1,113 @@ +import os +import re +import argparse + +def is_logical_line(line, output_lines): + original_line = line + stripped = line.strip() + + # Izņem rindas, kas nevar būt loģiskā koda rindiņa. + if not stripped or stripped in ['{', '}']: + return False + if stripped.startswith("//"): + return False + if re.match(r"/\*.*\*/", stripped): + return False + if stripped.startswith("#"): + return False + + # Ja rindiņa satur kādu atslēgas vārdu vai simbolu ';', tad to uzskata par + # loģisku koda rindiņu. + logical_patterns = [ + r";", + r"\b(if|else|for|while|do|switch|case|return)\b", # Vadības kontroles konstrukcijas. + r"\b(class|struct|template|enum)\b", # Datu struktūras. + r"\b(namespace|typedef|using)\b", # Papildus konstrukcijas. + ] + is_logical = any(re.search(pattern, stripped) for pattern in logical_patterns) + + if output_lines and is_logical: + print(original_line) + + return is_logical + +def count_lloc_in_file(file_path, output_lines): + lloc_count = 0 + with open(file_path, 'r', encoding='utf-8') as file: + inside_multiline_comment = False + for line in file: + stripped = line.strip() + + # Izlaiž rindas, kas ir vairāku rindu komentārā. + if inside_multiline_comment: + if "*/" in stripped: + inside_multiline_comment = False + continue + elif "/*" in stripped: + inside_multiline_comment = True + continue + + if is_logical_line(stripped, output_lines): + lloc_count += 1 + return lloc_count + +def count_lloc_in_directory(directory, exclude_dirs, output_lines): + total_lloc = 0 + file_lloc = {} + + for root, _, files in os.walk(directory): + if any(excluded in root for excluded in exclude_dirs): + continue + + for file in files: + if file.endswith(('.cpp', '.h')): + file_path = os.path.join(root, file) + lloc = count_lloc_in_file(file_path, output_lines) + file_lloc[file_path] = lloc + total_lloc += lloc + + return file_lloc, total_lloc + +parser = argparse.ArgumentParser( + description="Loģisko koda rindiņu skaitu C++ un galveņu failos.", +) +parser.add_argument( + "directory", + type=str, + help="Direktorijs, kurš tiek analizēts." +) +parser.add_argument( + "--exclude", + nargs="*", + default=[], + help="Saraksts ar direktorijiem, kas tiek izslēgti no skaita." +) +parser.add_argument( + "--file-info", + action="store_true", + help="Vai rādīt rindas katrā failā.", +) +parser.add_argument( + "--output-lines", + action="store_true", + help="Vai izdrukāt loģiskās rindiņas.", +) +args = parser.parse_args() + +if not os.path.isdir(args.directory): + print("Nekorekts direktorijs analīzei.") + exit(0) + +file_lloc, total_lloc = count_lloc_in_directory( + args.directory, + args.exclude, + args.output_lines, +) + +if args.file_info: + print("Loģiskās koda rindiņas failos:") + for file, lloc in file_lloc.items(): + print(f"{file}: {lloc} loģiskās koda rindiņas") + print("") + +print(f"Kopīgs loģisko koda rindiņu skaits failos ar paplašinājumu .cpp un .h: {total_lloc}.")