Skip to content

Commit

Permalink
Merge pull request #141 from cyrraz/font-bugfix-pytest-dep
Browse files Browse the repository at this point in the history
Fonts bugfix
  • Loading branch information
0ctagon authored Mar 28, 2024
2 parents e026e9a + c020231 commit 9d2a49e
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 24 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ plothist
:target: https://badge.fury.io/py/plothist
.. |Code style: black| image:: https://img.shields.io/badge/code%20style-black-000000.svg
:target: https://github.com/psf/black
.. |Docs from latest| image:: https://img.shields.io/badge/docs-v1.2.0-blue.svg
.. |Docs from latest| image:: https://img.shields.io/badge/docs-v1.2.1-blue.svg
:target: https://plothist.readthedocs.io/en/latest/
.. |Docs from main| image:: https://img.shields.io/badge/docs-main-blue.svg
:target: https://plothist.readthedocs.io/en/main/
4 changes: 2 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
author = "Cyrille Praz, Tristan Fillinger"

# The short X.Y version
version = "1.2.0"
version = "1.2.1"
# The full version, including alpha/beta/rc tags
release = "1.2.0"
release = "1.2.1"


# -- General configuration ---------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion src/plothist/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Plot histograms in a scalable way and a beautiful style."""

__version__ = "1.2.0"
__version__ = "1.2.1"

from .plotters import (
create_comparison_figure,
Expand Down
85 changes: 67 additions & 18 deletions src/plothist/scripts/install_latin_modern_fonts.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,36 @@
import platform
from pathlib import PosixPath
import time
import re


def _get_wget_version():
"""
Get the version of wget.
Returns
-------
tuple or str
The version of wget as a tuple of integers.
Raises
------
RuntimeError
If the version of wget could not be determined.
"""
version_string = subprocess.check_output(
["wget", "--version"], universal_newlines=True
)
# Try to find the version number in the format "XX.XX.XX"
version_match = re.search(r"(\d+\.\d+\.\d+)", version_string)
if not version_match:
# Try to find the version number in the format "XX.XX"
version_match = re.search(r"(\d+\.\d+)", version_string)
if version_match:
version = version_match.group(1)
return tuple(map(int, version.split(".")))
else:
raise RuntimeError("Could not determine wget version.")


def _get_install_command(url, font_directory):
Expand All @@ -24,7 +54,9 @@ def _get_install_command(url, font_directory):
return [
"wget",
"--retry-connrefused", # retry refused connections and similar fatal errors
"--retry-on-host-error", # retry on host errors such as 404 "Not Found"
*(
["--retry-on-host-error"] if _get_wget_version() >= (1, 20, 0) else []
), # retry on host errors such as 404 "Not Found"
"--waitretry=1", # wait 1 second before next retry
"--read-timeout=20", # wait a maximum of 20 seconds in case no data is received and then try again
"--timeout=15", # wait max 15 seconds before the initial connection times out
Expand Down Expand Up @@ -121,25 +153,42 @@ def install_latin_modern_fonts():

# Install Latin Modern Roman and Latin Modern Sans
for lm in ["roman", "sans"]:
_download_font(
f"https://www.1001fonts.com/download/latin-modern-{lm}.zip",
font_directory,
f"Latin Modern {lm}",
)
print(f"Unzipping Latin Modern {lm}...")

subprocess.run(
[
"unzip",
"-o",
(font_directory / f"latin-modern-{lm}.zip"),
"-d",
(font_directory / f"latin-modern-{lm}"),
]
)
subprocess.run(["rm", "-f", (font_directory / f"latin-modern-{lm}.zip")])
attempt = 0
max_attempt = 10
success = False

while not success and attempt < max_attempt:
_download_font(
f"https://www.1001fonts.com/download/latin-modern-{lm}.zip",
font_directory,
f"Latin Modern {lm}",
)
print(f"Unzipping Latin Modern {lm}...")

result = subprocess.run(
[
"unzip",
"-o",
(font_directory / f"latin-modern-{lm}.zip"),
"-d",
(font_directory / f"latin-modern-{lm}"),
],
capture_output=True,
text=True,
)
success = result.returncode == 0
if not success:
# Print the output to the terminal
print("Try", attempt + 1, "of", max_attempt)
print("STDOUT:", result.stdout)
print("STDERR:", result.stderr)
# Increment attempt counter and wait before the next attempt
attempt += 1
time.sleep(1)
subprocess.run(["rm", "-f", (font_directory / f"latin-modern-{lm}.zip")])

print(f"Latin Modern {lm} installed successfully.\n")
subprocess.run(["rm", "-f", (font_directory / f"latin-modern-{lm}.zip")])

# Remove font cache
try:
Expand Down
4 changes: 2 additions & 2 deletions src/plothist/scripts/make_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import yaml
import subprocess
import plothist
from pytest import fail
import hashlib
import warnings
import sys
Expand Down Expand Up @@ -112,6 +111,7 @@ def make_examples(no_input=False, check_svg=False, print_code=False):
svg_metadata = "metadata=" + str(svg_metadata)

if check_svg:
from pytest import fail
img_hashes = {}
for file in os.listdir(img_folder):
if file.endswith(".svg"):
Expand Down Expand Up @@ -150,7 +150,7 @@ def make_examples(no_input=False, check_svg=False, print_code=False):
capture_output=True,
text=True,
)
if result.returncode != 0:
if result.returncode != 0 and check_svg:
fail(f"Error while redoing {file}:\n{result.stderr}\n{result.stdout}")

# Move the svg files to the img folder
Expand Down

0 comments on commit 9d2a49e

Please sign in to comment.