Skip to content
Open
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
6 changes: 6 additions & 0 deletions inspector/distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ def __init__(self, f):
def namelist(self):
return [i.filename for i in self.zipfile.infolist() if not i.is_dir()]

def sizelist(self):
return [i.file_size for i in self.zipfile.infolist() if not i.is_dir()]

def contents(self, filepath) -> bytes:
try:
return self.zipfile.read(filepath)
Expand All @@ -53,6 +56,9 @@ def __init__(self, f):
def namelist(self):
return [i.name for i in self.tarfile.getmembers() if not i.isdir()]

def sizelist(self):
return [i.size for i in self.tarfile.getmembers() if not i.isdir()]

def contents(self, filepath):
try:
file_ = self.tarfile.extractfile(filepath)
Expand Down
16 changes: 16 additions & 0 deletions inspector/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,15 @@ def decode_with_fallback(content_bytes):
# If we get here, all encodings failed sanity checks (truly binary data)
return None

def _humanbytes(n):
"""
Convert number to byte format, used for formatting file size
"""
for unit in ("B", "KB", "MB", "GB"):
if n < 1024:
return f"{n} {unit}" if unit == "B" else f"{n:.1f} {unit}"
n /= 1024
return f"{n:.1f} TB"

def traces_sampler(sampling_context):
"""
Expand All @@ -164,6 +173,7 @@ def traces_sampler(sampling_context):
app = Flask(__name__)

app.jinja_env.filters["unquote"] = lambda u: urllib.parse.unquote(u)
app.jinja_env.filters["humanbytes"] = _humanbytes
app.jinja_env.trim_blocks = True
app.jinja_env.lstrip_blocks = True

Expand Down Expand Up @@ -235,9 +245,13 @@ def distributions(project_name, version):
"." + urllib.parse.urlparse(url["url"]).path + "/"
for url in resp.json()["urls"]
]
dist_sizes = [
url["size"] for url in resp.json()["urls"]
]
return render_template(
"links.html",
links=dist_urls,
sizes=dist_sizes,
h2=f"{project_name}",
h2_link=f"/project/{project_name}",
h2_paren="View this project on PyPI",
Expand Down Expand Up @@ -288,9 +302,11 @@ def distribution(project_name, version, first, second, rest, distname):
file_urls = [
"./" + urllib.parse.quote(filename) for filename in dist.namelist()
]
file_sizes = dist.sizelist()
return render_template(
"links.html",
links=file_urls,
sizes=file_sizes,
h2=f"{project_name}",
h2_link=f"/project/{project_name}",
h2_paren=h2_paren,
Expand Down
7 changes: 6 additions & 1 deletion inspector/templates/links.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
{% block body %}
<ul>
{% for link in links %}
<li><a href="{{ link }}">{{ link|unquote }}</a></li>
<li>
<a href="{{ link }}">{{ link|unquote }}</a>
{% if sizes is defined and sizes[loop.index0] is defined %}
<span>({{ sizes[loop.index0]|humanbytes }})</span>
{% endif %}
</li>
{% endfor %}
</ul>
{% endblock %}