Skip to content

Commit

Permalink
windows: compare Ghostscript versions properly
Browse files Browse the repository at this point in the history
  • Loading branch information
jbarlow83 committed Aug 1, 2022
1 parent 52e829d commit 9f3a52f
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/ocrmypdf/subprocess/_windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@

import logging
import os
import re
import shutil
import sys
from itertools import chain
from pathlib import Path
from typing import Any, Callable, Iterable, Iterator, TypeVar

from packaging.version import InvalidVersion, Version

if sys.version_info >= (3, 10):
from typing import TypeAlias
else:
Expand Down Expand Up @@ -100,6 +103,31 @@ def registry_path_tesseract(env=None) -> Iterator[Path]:
log.warning(e)


def _program_version_in_path_key(path: Path) -> tuple[str, Version | None]:
"""Key function for comparing Ghostscript and Tesseract paths.
Ghostscript installs on Windows:
%PROGRAMFILES%/gs/gs9.56.1 -> ('gs', Version('9.56.1'))
%PROGRAMFILES%/gs/gs9.24 -> ('gs', Version('9.24'))
Tesseract looks like:
%PROGRAMFILES%/Tesseract-OCR -> ('Tesseract-OCR', None)
Thus ensuring the resulting tuple will order the alternatives correctly,
e.g. gs10.0 > gs9.99.
"""
match = re.match(r'([^0-9]+)(.*)', str(path.name))
if match:
try:
program = match.group(1)
version_str = match.group(2)
version = Version(version_str)
return program, version
except InvalidVersion:
pass
return path.name, None


def program_files_paths(env=None) -> Iterator[Path]:
if not env:
env = os.environ
Expand All @@ -117,7 +145,7 @@ def path_walker() -> Iterator[Path]:
return iter(
sorted(
(p for p in path_walker()),
key=lambda p: (p.name, p.parent.name),
key=_program_version_in_path_key,
reverse=True,
)
)
Expand Down

0 comments on commit 9f3a52f

Please sign in to comment.