major logic changes. improve performance. simplify code.

This commit is contained in:
Enzo Nunes
2025-02-26 02:10:09 +00:00
parent 2731dbf995
commit e5ff919685
2 changed files with 53 additions and 25 deletions

View File

@@ -10,6 +10,7 @@ LIMIT_STARS = 200
LIMIT_NLOC = 100000
MARGIN = 0.1
CLONE_DIR = "~/temp/PICable"
EXCLUDED_LANGUAGES = ["CSV", "diff", "INI", "JSON", "Markdown", "reStructuredText", "SVG", "Text", "YAML"]
def get_stars_info(owner, repository, request_headers):
@@ -47,28 +48,29 @@ def get_commits_last_30_days(owner, repository, request_headers):
def get_lines_of_code(owner, repository):
repo_url = f"https://github.com/{owner}/{repository}.git"
repository_clone_dir = os.path.expanduser(f"{CLONE_DIR}/{repository}")
repository_url = f"https://github.com/{owner}/{repository}.git"
parent_dir = os.path.expanduser(f"{CLONE_DIR}/{owner}")
repository_clone_dir = os.path.expanduser(f"{parent_dir}/{repository}")
try:
subprocess.run(
["git", "clone", repo_url, repository_clone_dir],
["git", "clone", "--depth", "1", repository_url, repository_clone_dir],
check=True,
stdout=sys.stdout,
stderr=sys.stderr,
)
result = subprocess.run(
["cloc", f"--exclude-lang={','.join(EXCLUDED_LANGUAGES)}", repository_clone_dir],
capture_output=True,
text=True,
check=True,
)
number_of_lines_of_code = int(result.stdout.split("\n")[-3].split()[-1])
except Exception:
return -1
result = subprocess.run(["sloccount", repository_clone_dir], capture_output=True, text=True, check=True)
for line in result.stdout.splitlines():
if "Total Physical Source Lines of Code (SLOC)" in line:
loc = int(line.split()[8].replace(",", ""))
break
else:
loc = 0
subprocess.run(["rm", "-rf", repository_clone_dir], check=True)
return loc
finally:
subprocess.run(["rm", "-rf", parent_dir], check=True)
return number_of_lines_of_code
def is_valid_repository(owner, repository, request_headers):
@@ -80,7 +82,7 @@ def is_valid_repository(owner, repository, request_headers):
def PICable(owner, repository, token):
margin_percentage = round(MARGIN * 100)
request_headers = {"Authorization": f"token {token}"}
picable_report = ""
picable_report = f"Analysis complete for the repository {owner}/{repository}.\n\n"
repository_url = f"https://www.github.com/{owner}/{repository}"
if not is_valid_repository(owner, repository, request_headers):
@@ -134,4 +136,5 @@ def PICable(owner, repository, token):
else:
picable_report += f"The repository {repository_url} is almost PICable. :warning:\nThere is at least one requirement which is below the requirement, but by less than {margin_percentage}%. Consult with a professor before proceeding.\n"
print("Analysis complete.")
return picable_report