mirror of
https://github.com/jorenchik/mdemory.git
synced 2026-03-21 16:16:19 +00:00
114 lines
3.2 KiB
Python
114 lines
3.2 KiB
Python
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|new|delete)\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 vai .h: {total_lloc}.")
|