Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions compare50/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import tempfile
import requests
import packaging.version
import names

import attr
import lib50
Expand Down Expand Up @@ -299,10 +300,10 @@ def expand_patterns(patterns):


def check_version(package_name=__package__, timeout=5):
"""Check for newer version of the package on PyPI"""
"""Check for newer version of the package on PyPI"""
if not __version__:
return

try:
current = packaging.version.parse(__version__)
latest = max(requests.get(f"https://pypi.org/pypi/{package_name}/json", timeout=timeout).json()["releases"], key=packaging.version.parse)
Expand Down Expand Up @@ -468,9 +469,23 @@ def main():
object.__setattr__(sub, "preprocessor", preprocessor)
pass_to_results[pass_] = _api.compare(scores, ignored_files, pass_)

all_subs = list(subs) + list(archive_subs) + list(ignored_subs)

# Generate a unique first name for each submission
fictional_names = []
used_names = set()
while len(fictional_names) < len(all_subs):
name = names.get_first_name()
if name not in used_names:
fictional_names.append(name)
used_names.add(name)

# Map each Submission to a fictional name
sub_to_machine_name = {sub: name for sub, name in zip(all_subs, fictional_names)}

# Render results
with _api.progress_bar("Rendering", disable=args.debug):
index = _renderer.render(pass_to_results, dest=args.output)
index = _renderer.render(pass_to_results, dest=args.output, sub_to_machine_name=sub_to_machine_name)

termcolor.cprint(
f"Done! Visit file://{index.absolute()} in a web browser to see the results.", "green")
Expand Down
21 changes: 12 additions & 9 deletions compare50/_renderer/_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def percentage(self):

@attr.s(slots=True)
class HTMLSubmission:
path = attr.ib(converter=str)
name = attr.ib(converter=str)
files = attr.ib()
num_chars_matched = attr.ib()
Expand All @@ -81,7 +82,7 @@ def percentage(self):
return 0


def render(pass_to_results, dest):
def render(pass_to_results, dest, sub_to_machine_name):
bar = _api.get_progress_bar()
dest = pathlib.Path(dest)

Expand All @@ -102,7 +103,7 @@ def render(pass_to_results, dest):
with _api.Executor() as executor:
# Load static files
max_id = len(results_per_sub_pair)
for id, html in executor.map(_RenderTask(dest, max_id, match_js, match_css), enumerate(results_per_sub_pair, 1)):
for id, html in executor.map(_RenderTask(dest, max_id, match_js, match_css, sub_to_machine_name), enumerate(results_per_sub_pair, 1)):
with open(dest / f"match_{id}.html", "w") as f:
f.write(html)
bar.update()
Expand All @@ -125,13 +126,13 @@ def render(pass_to_results, dest):
subs = set()
graph_info = {"nodes": [], "links": [], "data": {}}
for i, result in enumerate(ranking_results):
graph_info["links"].append({"index":i, "source": str(result.sub_a.path), "target": str(result.sub_b.path), "value": 10 * result.score.score/max_score})
graph_info["links"].append({"index":i, "source": sub_to_machine_name[result.sub_a], "target": sub_to_machine_name[result.sub_b], "value": 10 * result.score.score/max_score})
subs.add(result.sub_a)
subs.add(result.sub_b)

for sub in subs:
graph_info["nodes"].append({"id": str(sub.path)})
graph_info["data"][str(sub.path)] = {"is_archive": sub.is_archive}
graph_info["nodes"].append({"id": sub_to_machine_name[sub]})
graph_info["data"][sub_to_machine_name[sub]] = {"is_archive": sub.is_archive, "path": str(sub.path)}

index_css = common_css + [read_file(STATIC / "index.css")]
index_js = [read_file(STATIC / f) for f in ("d3.v4.min.js", "d3-scale-chromatic.v1.min.js", "d3-simple-slider.js", "index.js")]
Expand Down Expand Up @@ -160,20 +161,21 @@ def read_file(fname):


class _RenderTask:
def __init__(self, dest, max_id, js, css):
def __init__(self, dest, max_id, js, css, sub_to_machine_name):
dest.mkdir(exist_ok=True)
self.dest = dest
self.max_id = max_id
self.js = js
self.css = css
self.sub_to_machine_name = sub_to_machine_name

def __call__(self, arg):
id, results = arg
data = []
match_htmls = []

for result in results:
renderer = _Renderer(result.name)
renderer = _Renderer(result.name, self.sub_to_machine_name)
score = result.score
groups = result.groups
ignored_spans = result.ignored_spans
Expand Down Expand Up @@ -236,8 +238,9 @@ def _prepare_dest(dest):


class _Renderer:
def __init__(self, name):
def __init__(self, name, sub_to_machine_name):
self.name = name
self.sub_to_machine_name = sub_to_machine_name
self._frag_id_counter = -1
self._span_id_store = IdStore()
self._group_id_store = IdStore()
Expand Down Expand Up @@ -285,7 +288,7 @@ def html_submission(self, submission, file_to_spans, ignored_spans):
html_files = self.html_files(submission, file_to_spans, ignored_spans)
num_chars_matched = sum(f.num_chars_matched for f in html_files)
num_chars = sum(f.num_chars for f in html_files)
return HTMLSubmission(submission.path, html_files, num_chars_matched, num_chars)
return HTMLSubmission(submission.path, self.sub_to_machine_name[submission], html_files, num_chars_matched, num_chars)

def data(self, result, html_fragments, ignored_spans):
fragment_to_spans = {}
Expand Down
6 changes: 5 additions & 1 deletion compare50/_renderer/static/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,11 @@ function update_index() {
new_trs.append("td")
.attr("class", "sub_name")
.datum(d => d[field])
.html(d => GRAPH.data[d.id].is_archive ? `${ARCHIVE_IMG} ${d.id}` : d.id);
.html(d => {
const fic_name = GRAPH.data[d.id].is_archive ? `${ARCHIVE_IMG} ${d.id}` : d.id;
const path = GRAPH.data[d.id].path || '';
return `<span title="${path}">${fic_name}</span>`;
});
}

new_trs.append("td")
Expand Down
2 changes: 1 addition & 1 deletion compare50/_renderer/templates/match.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<div class="row sticky-top bg-dark text-light" id="{{name}}sub_names">
{% for sub in sub_a, sub_b %}
<div class="col-sm-6 col-2 sub_name">
<h5>{{sub.name}} ({{sub.percentage}}%)</h5>
<h5 title="{{sub.path}}">{{sub.name}} ({{sub.percentage}}%)</h5>
</div>
{% endfor %}
</div>
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
license="GPLv3",
description="This is compare50, with which you can compare files for similarities.",
long_description="This is compare50, with which you can compare files for similarities.",
install_requires=["attrs>=18,<21", "intervaltree>=3,<4", "lib50>=2,<4", "numpy>=1.15,<2", "pygments>=2.2,<3", "jinja2>=3,<4", "termcolor>=1.1.0,<2", "tqdm>=4.32,<5", "importlib"],
install_requires=["attrs>=18,<21", "intervaltree>=3,<4", "lib50>=2,<4", "numpy>=1.15,<2", "pygments>=2.2,<3", "jinja2>=3,<4", "termcolor>=1.1.0,<2", "tqdm>=4.32,<5", "importlib", "names"],
extras_require = {
"develop": ["sphinx", "sphinx_rtd_theme", "sphinx-autobuild", "line_profiler"]
},
Expand All @@ -22,6 +22,6 @@
packages=find_packages(exclude=["tests"]),
scripts=["bin/compare50"],
url="https://github.com/cs50/compare50",
version="1.2.13",
version="1.2.14",
include_package_data=True,
)