Skip to content

Commit d5d5fc3

Browse files
sacca97copernico
authored andcommitted
remove commented code, small fixes
1 parent c6368b9 commit d5d5fc3

File tree

2 files changed

+41
-81
lines changed

2 files changed

+41
-81
lines changed

prospector/client/cli/main.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,21 @@
33
import os
44
import signal
55
import sys
6-
from pathlib import Path
76
from typing import Any, Dict
87

98
from dotenv import load_dotenv
10-
from omegaconf import OmegaConf
119

1210
path_root = os.getcwd()
1311
if path_root not in sys.path:
1412
sys.path.append(path_root)
1513

1614

15+
import client.cli.report as report # noqa: E402
1716
from client.cli.console import ConsoleWriter, MessageStatus # noqa: E402
1817
from client.cli.prospector_client import DEFAULT_BACKEND # noqa: E402
1918
from client.cli.prospector_client import TIME_LIMIT_AFTER # noqa: E402
2019
from client.cli.prospector_client import TIME_LIMIT_BEFORE # noqa: E402
2120
from client.cli.prospector_client import prospector # noqa: E402; noqa: E402
22-
from client.cli.report import as_html, as_json, report_on_console # noqa: E402
2321

2422
# Load logger before doing anything else
2523
from log.logger import get_level, logger, pretty_log # noqa: E402
@@ -91,30 +89,31 @@ def main(argv): # noqa: C901
9189
if config.preprocess_only:
9290
return
9391

94-
with ConsoleWriter("Generating report") as console:
92+
with ConsoleWriter("Generating report\n") as console:
9593
match config.report:
9694
case "console":
97-
report_on_console(results, advisory_record, get_level() < logging.INFO)
95+
report.console(results, advisory_record, get_level() < logging.INFO)
9896
case "json":
99-
as_json(results, advisory_record, config.report_filename)
97+
report.json(results, advisory_record, config.report_filename)
10098
case "html":
101-
as_html(results, advisory_record, config.report_filename)
99+
report.html(results, advisory_record, config.report_filename)
102100
case "all":
103-
as_json(results, advisory_record, config.report_filename)
104-
as_html(results, advisory_record, config.report_filename)
101+
report.json(results, advisory_record, config.report_filename)
102+
report.html(results, advisory_record, config.report_filename)
105103
case _:
106104
logger.warning("Invalid report type specified, using 'console'")
107105
console.set_status(MessageStatus.WARNING)
108106
console.print(
109107
f"{config.report} is not a valid report type, 'console' will be used instead",
110108
)
111-
report_on_console(results, advisory_record, get_level() < logging.INFO)
109+
report.console(results, advisory_record, get_level() < logging.INFO)
112110

113111
logger.info("\n" + execution_statistics.generate_console_tree())
114112
execution_time = execution_statistics["core"]["execution time"][0]
115-
console.print(f"Execution time: {execution_time:.4f} sec")
116113
console.print(f"Report saved in {config.report_filename}")
117-
return
114+
ConsoleWriter.print(f"Execution time: {execution_time:.3f}s")
115+
116+
return
118117

119118

120119
def signal_handler(signal, frame):

prospector/client/cli/prospector_client.py

Lines changed: 30 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from filtering.filter import filter_commits
1313
from git.git import Git
1414
from git.raw_commit import RawCommit
15-
from git.version_to_tag import get_possible_tags, get_tag_for_version
15+
from git.version_to_tag import get_possible_tags
1616
from log.logger import get_level, logger, pretty_log
1717
from rules.rules import apply_rules
1818
from stats.execution import (
@@ -83,20 +83,16 @@ def prospector( # noqa: C901
8383
logger.debug(f"Found tags: {tags}")
8484
logger.info(f"Done retrieving {repository.url}")
8585

86-
prev_tag = None
87-
next_tag = None
88-
8986
if tag_interval is not None:
9087
prev_tag, next_tag = tag_interval.split(":")
9188
elif version_interval is not None:
9289
prev_tag, next_tag = get_possible_tags(tags, version_interval)
93-
# vuln_version, fixed_version = version_interval.split(":")
94-
# prev_tag = get_possible_tags(tags, vuln_version)
95-
# next_tag = get_possible_tags(tags, fixed_version)
96-
# prev_tag = get_tag_for_version(tags, vuln_version)[0]
97-
# next_tag = get_tag_for_version(tags, fixed_version)[0]
90+
else:
91+
logger.error("No version/tag interval provided")
92+
sys.exit(1)
9893

99-
print(f"Found tags: {prev_tag}, {next_tag}") if prev_tag and next_tag else None
94+
ConsoleWriter.print(f"Found tags: {prev_tag} - {next_tag}")
95+
ConsoleWriter.print_(MessageStatus.OK)
10096

10197
# retrieve of commit candidates
10298
candidates = get_candidates(
@@ -112,7 +108,7 @@ def prospector( # noqa: C901
112108
with ExecutionTimer(
113109
core_statistics.sub_collection("commit preprocessing")
114110
) as timer:
115-
with ConsoleWriter("Preprocessing commits") as writer:
111+
with ConsoleWriter("\nPreprocessing commits") as writer:
116112
try:
117113
if use_backend != "never":
118114
missing, preprocessed_commits = retrieve_preprocessed_commits(
@@ -134,17 +130,19 @@ def prospector( # noqa: C901
134130
missing = list(candidates.values())
135131
preprocessed_commits: List[Commit] = list()
136132

137-
pbar = tqdm(missing, desc="Preprocessing commits", unit="commit")
138-
with Counter(
139-
timer.collection.sub_collection("commit preprocessing")
140-
) as counter:
141-
counter.initialize("preprocessed commits", unit="commit")
142-
# Now pbar has Raw commits inside so we can skip the "get_commit" call
143-
for raw_commit in pbar:
144-
counter.increment("preprocessed commits")
145-
raw_commit.set_tags(next_tag)
146-
# TODO: here we need to check twins with the commit not already in the backend and update everything
147-
preprocessed_commits.append(make_from_raw_commit(raw_commit))
133+
if len(missing) > 0:
134+
pbar = tqdm(missing, desc="Preprocessing commits", unit="commit")
135+
with Counter(
136+
timer.collection.sub_collection("commit preprocessing")
137+
) as counter:
138+
counter.initialize("preprocessed commits", unit="commit")
139+
for raw_commit in pbar:
140+
counter.increment("preprocessed commits")
141+
raw_commit.set_tags(next_tag)
142+
# TODO: here we need to check twins with the commit not already in the backend and update everything
143+
preprocessed_commits.append(make_from_raw_commit(raw_commit))
144+
else:
145+
writer.print("\nAll commits found in the backend")
148146

149147
pretty_log(logger, advisory_record)
150148
logger.debug(
@@ -166,30 +164,17 @@ def prospector( # noqa: C901
166164
twin_branches_map = {
167165
commit.commit_id: commit.get_tag() for commit in ranked_candidates
168166
}
167+
ConsoleWriter.print("Commit ranking and aggregation...")
169168
ranked_candidates = tag_and_aggregate_commits(
170169
ranked_candidates, twin_branches_map, next_tag
171170
)
172-
# TODO: aggregate twins and check tags
173-
# ranked_candidates = aggregate_twins(ranked_candidates)
174-
# if next_tag is not None:
175-
# tagged_candidates = [
176-
# commit
177-
# for commit in ranked_candidates
178-
# if commit.has_tag()
179-
# and (commit.get_tag() in next_tag or next_tag in commit.get_tag())
180-
# ]
181-
# # This is horrible and slow
182-
# for commit in tagged_candidates:
183-
# for twin in commit.twins:
184-
# twin[0] = twin_branches[twin[1]]
185-
186-
# return tagged_candidates, advisory_record
171+
ConsoleWriter.print_(MessageStatus.OK)
187172

188173
return ranked_candidates, advisory_record
189174

190175

191176
def filter(commits: List[Commit]) -> List[Commit]:
192-
with ConsoleWriter("Candidate filtering") as console:
177+
with ConsoleWriter("Candidate filtering\n") as console:
193178
commits, rejected = filter_commits(commits)
194179
if rejected > 0:
195180
console.print(f"Dropped {rejected} candidates")
@@ -198,7 +183,7 @@ def filter(commits: List[Commit]) -> List[Commit]:
198183

199184
def evaluate_commits(commits: List[Commit], advisory: AdvisoryRecord, rules: List[str]):
200185
with ExecutionTimer(core_statistics.sub_collection("candidates analysis")):
201-
with ConsoleWriter("Applying rules"):
186+
with ConsoleWriter("Candidate analysis") as _:
202187
ranked_commits = apply_ranking(apply_rules(commits, advisory, rules=rules))
203188

204189
return ranked_commits
@@ -210,39 +195,15 @@ def tag_and_aggregate_commits(
210195
if next_tag is None:
211196
return commits
212197
tagged_commits = list()
198+
# if a twin has higher relevance than the one shown, then the relevance should be inherited
213199
for commit in commits:
214-
if commit.has_tag():
215-
# if (
216-
# commit.commit_id == "ac7ed9580331b8fd65eccd732f651b14f95f71f0"
217-
# or commit.commit_id == "8eb68266167d8f8b3fa3a00ca9f6b7889e8ec101"
218-
# or commit.commit_id == "1ba43047641e1db238ee15a6fa1a1d8dcd371bc5"
219-
# or "(#7359)" in commit.message
220-
# ):
221-
# print(commit.commit_id, commit.get_tag())
222-
if next_tag == commit.get_tag():
223-
# cleaned_tag in cleaned_next_tag or cleaned_next_tag in cleaned_tag:
224-
for twin in commit.twins:
225-
twin[0] = mapping_dict[twin[1]]
226-
227-
tagged_commits.append(commit)
200+
if commit.has_tag() and next_tag == commit.get_tag():
201+
for twin in commit.twins:
202+
twin[0] = mapping_dict[twin[1]]
228203

229-
return tagged_commits
204+
tagged_commits.append(commit)
230205

231-
# if next_tag is not None:
232-
# tagged_candidates = [
233-
# commit
234-
# for commit in ranked_candidates
235-
# if commit.has_tag()
236-
# and (commit.get_tag() in next_tag or next_tag in commit.get_tag())
237-
# ]
238-
# # This is horrible and slow
239-
# for commit in tagged_candidates:
240-
# for twin in commit.twins:
241-
# twin[0] = twin_branches[twin[1]]
242-
243-
# return tagged_candidates, advisory_record
244-
245-
# return aggregated_commits
206+
return tagged_commits
246207

247208

248209
def retrieve_preprocessed_commits(

0 commit comments

Comments
 (0)