mirror of
https://github.com/ocrmypdf/OCRmyPDF.git
synced 2025-12-30 00:31:59 +00:00
setup.py: check for third party program requirements
This commit is contained in:
parent
8aced0b6d3
commit
d5f4862749
167
setup.py
167
setup.py
@ -1,6 +1,171 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
from setuptools import setup
|
||||
from subprocess import Popen, STDOUT, check_output, CalledProcessError
|
||||
from string import Template
|
||||
import re
|
||||
import sys
|
||||
|
||||
|
||||
missing_program = '''
|
||||
The program '{program}' could not be executed or was not found on your
|
||||
system PATH.
|
||||
'''
|
||||
|
||||
unknown_version = '''
|
||||
OCRmyPDF requires '{program}' {need_version} or higher. Your system has
|
||||
'{program}' but we cannot tell what version is installed. Contact the
|
||||
package maintainer.
|
||||
'''
|
||||
|
||||
old_version = '''
|
||||
OCRmyPDF requires '{program}' {need_version} or higher. Your system appears
|
||||
to have {found_version}. Please update this program.
|
||||
'''
|
||||
|
||||
okay_its_optional = '''
|
||||
This program is OPTIONAL, so installation of OCRmyPDF can proceed, but
|
||||
some functionality may be missing.
|
||||
'''
|
||||
|
||||
not_okay_its_required = '''
|
||||
This program is REQUIRED for OCRmyPDF to work. Installation will abort.
|
||||
'''
|
||||
|
||||
osx_install_advice = '''
|
||||
If you have homebrew installed, try these command to install the missing
|
||||
packages:
|
||||
brew update
|
||||
brew upgrade
|
||||
brew install {package}
|
||||
'''
|
||||
|
||||
linux_install_advice = '''
|
||||
On systems with the aptitude package manager (Debian, Ubuntu), try these
|
||||
commands:
|
||||
sudo apt-get update
|
||||
sudo apt-get install {package}
|
||||
|
||||
On RPM-based systems (Red Hat, Fedora), search for instructions on
|
||||
installing the RPM for {package}.
|
||||
'''
|
||||
|
||||
|
||||
def _error_trailer(program, package, optional):
|
||||
if program == 'java':
|
||||
return # You're fucked
|
||||
|
||||
if optional:
|
||||
print(okay_its_optional.format(**locals()), file=sys.stderr)
|
||||
else:
|
||||
print(not_okay_its_required.format(**locals()), file=sys.stderr)
|
||||
if sys.platform.startswith('darwin'):
|
||||
print(osx_install_advice.format(**locals()), file=sys.stderr)
|
||||
elif sys.platform.startswith('linux'):
|
||||
print(linux_install_advice.format(**locals()), file=sys.stderr)
|
||||
|
||||
|
||||
def error_missing_program(
|
||||
program,
|
||||
package,
|
||||
optional
|
||||
):
|
||||
print(missing_program.format(**locals()), file=sys.stderr)
|
||||
_error_trailer(**locals())
|
||||
|
||||
|
||||
def error_unknown_version(
|
||||
program,
|
||||
package,
|
||||
optional
|
||||
):
|
||||
print(unknown_version.format(**locals()), file=sys.stderr)
|
||||
_error_trailer(**locals())
|
||||
|
||||
|
||||
def error_old_version(
|
||||
program,
|
||||
package,
|
||||
optional,
|
||||
need_version
|
||||
):
|
||||
print(old_version.format(**locals()), file=sys.stderr)
|
||||
_error_trailer(**locals())
|
||||
|
||||
|
||||
def check_external_program(
|
||||
program,
|
||||
minimum_version,
|
||||
package,
|
||||
version_check_args=['--version'],
|
||||
version_scrape_regex=re.compile(r'(\d+\.\d+(?:\.\d+)?)'),
|
||||
optional=False):
|
||||
|
||||
print('Checking for {program} >= {minimum_version}...'.format(
|
||||
program=program, minimum_version=minimum_version))
|
||||
try:
|
||||
result = check_output(
|
||||
[program] + version_check_args,
|
||||
universal_newlines=True, stderr=STDOUT)
|
||||
except CalledProcessError:
|
||||
error_missing_program(program, package, optional)
|
||||
if not optional:
|
||||
sys.exit(1)
|
||||
|
||||
try:
|
||||
version = version_scrape_regex.search(result).group(1)
|
||||
except AttributeError:
|
||||
error_unknown_version(program, package, optional, minimum_version)
|
||||
if not optional:
|
||||
sys.exit(1)
|
||||
|
||||
if version < minimum_version:
|
||||
error_old_version(program, package, optional, minimum_version)
|
||||
|
||||
print('Found {program} {version}'.format(
|
||||
program=program, version=version))
|
||||
|
||||
check_external_program(
|
||||
program='tesseract',
|
||||
minimum_version='3.02.02',
|
||||
package='tesseract'
|
||||
)
|
||||
|
||||
check_external_program(
|
||||
program='gs',
|
||||
minimum_version='9.14',
|
||||
package='ghostscript'
|
||||
)
|
||||
|
||||
check_external_program(
|
||||
program='unpaper',
|
||||
minimum_version='6.1',
|
||||
package='unpaper',
|
||||
optional=True
|
||||
)
|
||||
|
||||
# Deprecated
|
||||
check_external_program(
|
||||
program='pdfseparate',
|
||||
minimum_version='0.29.0',
|
||||
package='poppler',
|
||||
version_check_args=['-v']
|
||||
)
|
||||
|
||||
check_external_program(
|
||||
program='java',
|
||||
minimum_version='1.5.0',
|
||||
package='Java Runtime Environment',
|
||||
version_check_args=['-version']
|
||||
)
|
||||
|
||||
check_external_program(
|
||||
program='mutool',
|
||||
minimum_version='1.7a',
|
||||
version_check_args=['-v'],
|
||||
version_scrape_regex=re.compile(r'(\d+\.\d+[a-z]+)'),
|
||||
package='mupdf-tools'
|
||||
)
|
||||
|
||||
setup(
|
||||
name='ocrmypdf',
|
||||
@ -12,7 +177,7 @@ setup(
|
||||
license='Public Domain',
|
||||
packages=['ocrmypdf'],
|
||||
keywords=['PDF', 'OCR', 'optical character recognition', 'PDF/A', 'scanning'],
|
||||
classifiers = [
|
||||
classifiers=[
|
||||
"Programming Language :: Python :: 3",
|
||||
"Development Status :: 4 - Beta",
|
||||
"Environment :: Console",
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user