2015-07-25 23:45:13 -07:00
|
|
|
#!/usr/bin/env python3
|
2015-07-28 04:36:58 -07:00
|
|
|
# © 2015 James R. Barlow: github.com/jbarlow83
|
2015-07-25 23:45:13 -07:00
|
|
|
|
2015-12-21 09:38:38 -08:00
|
|
|
from __future__ import print_function, unicode_literals
|
2015-07-25 23:45:13 -07:00
|
|
|
from setuptools import setup
|
2016-01-19 15:07:21 -08:00
|
|
|
from subprocess import STDOUT, check_output, CalledProcessError
|
2015-08-15 15:12:05 -07:00
|
|
|
from collections.abc import Mapping
|
2015-07-27 01:45:17 -07:00
|
|
|
import re
|
|
|
|
import sys
|
2016-01-19 15:07:21 -08:00
|
|
|
import os
|
2015-07-27 01:45:17 -07:00
|
|
|
|
|
|
|
|
2016-01-04 12:56:51 -08:00
|
|
|
if sys.version_info < (3, 4):
|
|
|
|
print("Python 3.4 or newer is required")
|
2015-12-21 09:38:38 -08:00
|
|
|
sys.exit(1)
|
|
|
|
|
2016-01-19 15:07:21 -08:00
|
|
|
version_py = os.path.join(os.path.dirname(__file__), 'ocrmypdf', 'version.py')
|
|
|
|
try:
|
|
|
|
version_git = check_output(["git", "describe"],
|
|
|
|
universal_newlines=True).rstrip()
|
|
|
|
version_git = version_git.replace('-g', '+n')
|
|
|
|
version_git = version_git.replace('-', '.dev')
|
|
|
|
except Exception:
|
|
|
|
with open(version_py, 'r') as fh:
|
|
|
|
version_git = fh.read().strip().split('=')[-1].replace('"', '')
|
|
|
|
|
|
|
|
version_msg = "# Do not edit this file"
|
|
|
|
with open(version_py, 'w') as fh:
|
|
|
|
fh.write('\n'.join([version_msg, '__version__="' + version_git + '"']))
|
|
|
|
|
|
|
|
|
2015-07-27 01:45:17 -07:00
|
|
|
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
|
2015-08-15 15:12:05 -07:00
|
|
|
installing the RPM for {program}.
|
2015-07-27 01:45:17 -07:00
|
|
|
'''
|
|
|
|
|
|
|
|
|
2015-08-15 15:12:05 -07:00
|
|
|
def get_platform():
|
|
|
|
if sys.platform.startswith('freebsd'):
|
|
|
|
return 'freebsd'
|
|
|
|
elif sys.platform.startswith('linux'):
|
|
|
|
return 'linux'
|
|
|
|
return sys.platform
|
|
|
|
|
|
|
|
|
2015-07-28 13:05:23 -07:00
|
|
|
def _error_trailer(program, package, optional, **kwargs):
|
2015-07-27 01:45:17 -07:00
|
|
|
if optional:
|
|
|
|
print(okay_its_optional.format(**locals()), file=sys.stderr)
|
|
|
|
else:
|
|
|
|
print(not_okay_its_required.format(**locals()), file=sys.stderr)
|
2015-08-15 15:12:05 -07:00
|
|
|
|
|
|
|
if isinstance(package, Mapping):
|
|
|
|
package = package[get_platform()]
|
|
|
|
|
|
|
|
if get_platform() == 'darwin':
|
2015-07-27 01:45:17 -07:00
|
|
|
print(osx_install_advice.format(**locals()), file=sys.stderr)
|
2015-08-15 15:12:05 -07:00
|
|
|
elif get_platform() == 'linux':
|
2015-07-27 01:45:17 -07:00
|
|
|
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,
|
2015-07-28 13:05:23 -07:00
|
|
|
optional,
|
|
|
|
need_version
|
2015-07-27 01:45:17 -07:00
|
|
|
):
|
|
|
|
print(unknown_version.format(**locals()), file=sys.stderr)
|
|
|
|
_error_trailer(**locals())
|
|
|
|
|
|
|
|
|
|
|
|
def error_old_version(
|
|
|
|
program,
|
|
|
|
package,
|
|
|
|
optional,
|
2015-07-28 13:05:23 -07:00
|
|
|
need_version,
|
|
|
|
found_version
|
2015-07-27 01:45:17 -07:00
|
|
|
):
|
|
|
|
print(old_version.format(**locals()), file=sys.stderr)
|
|
|
|
_error_trailer(**locals())
|
|
|
|
|
|
|
|
|
|
|
|
def check_external_program(
|
|
|
|
program,
|
2015-07-28 13:05:23 -07:00
|
|
|
need_version,
|
2015-07-27 01:45:17 -07:00
|
|
|
package,
|
|
|
|
version_check_args=['--version'],
|
|
|
|
version_scrape_regex=re.compile(r'(\d+\.\d+(?:\.\d+)?)'),
|
|
|
|
optional=False):
|
|
|
|
|
2015-07-28 13:05:23 -07:00
|
|
|
print('Checking for {program} >= {need_version}...'.format(
|
|
|
|
program=program, need_version=need_version))
|
2015-07-27 01:45:17 -07:00
|
|
|
try:
|
|
|
|
result = check_output(
|
|
|
|
[program] + version_check_args,
|
|
|
|
universal_newlines=True, stderr=STDOUT)
|
2015-07-28 12:41:24 -07:00
|
|
|
except (CalledProcessError, FileNotFoundError):
|
2015-07-27 01:45:17 -07:00
|
|
|
error_missing_program(program, package, optional)
|
|
|
|
if not optional:
|
|
|
|
sys.exit(1)
|
2015-07-28 13:05:23 -07:00
|
|
|
print('Continuing install without {program}'.format(program=program))
|
|
|
|
return
|
2015-07-27 01:45:17 -07:00
|
|
|
|
|
|
|
try:
|
2015-07-28 13:05:23 -07:00
|
|
|
found_version = version_scrape_regex.search(result).group(1)
|
|
|
|
except AttributeError:
|
|
|
|
error_unknown_version(program, package, optional, need_version)
|
|
|
|
sys.exit(1)
|
2015-07-27 01:45:17 -07:00
|
|
|
|
2015-07-28 13:05:23 -07:00
|
|
|
if found_version < need_version:
|
|
|
|
error_old_version(program, package, optional, need_version,
|
|
|
|
found_version)
|
2015-07-27 01:45:17 -07:00
|
|
|
|
2015-07-28 13:05:23 -07:00
|
|
|
print('Found {program} {found_version}'.format(
|
|
|
|
program=program, found_version=found_version))
|
2015-07-27 01:45:17 -07:00
|
|
|
|
2015-08-09 14:16:30 -07:00
|
|
|
|
2015-07-27 02:14:51 -07:00
|
|
|
command = next((arg for arg in sys.argv[1:] if not arg.startswith('-')), '')
|
|
|
|
|
2015-08-09 14:16:30 -07:00
|
|
|
|
2015-07-27 02:14:51 -07:00
|
|
|
if command.startswith('install') or \
|
2015-07-28 05:44:15 -07:00
|
|
|
command in ['check', 'test', 'nosetests', 'easy_install', 'egg_info']:
|
2015-07-27 02:14:51 -07:00
|
|
|
check_external_program(
|
|
|
|
program='tesseract',
|
2015-07-28 13:05:23 -07:00
|
|
|
need_version='3.02.02',
|
2015-08-15 15:12:05 -07:00
|
|
|
package={'darwin': 'tesseract', 'linux': 'tesseract-ocr'}
|
2015-07-27 02:14:51 -07:00
|
|
|
)
|
|
|
|
check_external_program(
|
|
|
|
program='gs',
|
2015-07-28 13:05:23 -07:00
|
|
|
need_version='9.14',
|
2015-07-27 02:14:51 -07:00
|
|
|
package='ghostscript'
|
|
|
|
)
|
|
|
|
check_external_program(
|
|
|
|
program='unpaper',
|
2015-08-22 01:51:08 -07:00
|
|
|
need_version='6.1',
|
2015-07-27 02:14:51 -07:00
|
|
|
package='unpaper',
|
|
|
|
optional=True
|
|
|
|
)
|
|
|
|
check_external_program(
|
2015-07-30 04:06:31 -07:00
|
|
|
program='qpdf',
|
|
|
|
need_version='5.0.0',
|
|
|
|
package='qpdf',
|
|
|
|
version_check_args=['--version']
|
2015-07-27 02:14:51 -07:00
|
|
|
)
|
2015-07-25 23:45:13 -07:00
|
|
|
|
2015-08-09 14:16:30 -07:00
|
|
|
|
|
|
|
if 'upload' in sys.argv[1:]:
|
|
|
|
print('Use twine to upload the package - setup.py upload is insecure')
|
|
|
|
sys.exit(1)
|
|
|
|
|
2015-08-19 13:43:32 -07:00
|
|
|
tests_require = open('test_requirements.txt').read().splitlines()
|
|
|
|
|
2015-07-25 23:45:13 -07:00
|
|
|
setup(
|
|
|
|
name='ocrmypdf',
|
2016-01-19 15:07:21 -08:00
|
|
|
version="{ver}".format(ver=version_git),
|
2015-07-25 23:45:13 -07:00
|
|
|
description='OCRmyPDF adds an OCR text layer to scanned PDF files, allowing them to be searched',
|
2015-09-05 00:53:14 -07:00
|
|
|
url='https://github.com/jbarlow83/OCRmyPDF',
|
|
|
|
author='James R. Barlow',
|
2015-07-25 23:45:13 -07:00
|
|
|
author_email='jim@purplerock.ca',
|
|
|
|
license='Public Domain',
|
|
|
|
packages=['ocrmypdf'],
|
|
|
|
keywords=['PDF', 'OCR', 'optical character recognition', 'PDF/A', 'scanning'],
|
2015-07-27 01:45:17 -07:00
|
|
|
classifiers=[
|
2015-07-25 23:45:13 -07:00
|
|
|
"Programming Language :: Python :: 3",
|
2015-09-05 00:53:14 -07:00
|
|
|
"Development Status :: 5 - Production/Stable",
|
2015-07-25 23:45:13 -07:00
|
|
|
"Environment :: Console",
|
|
|
|
"Intended Audience :: End Users/Desktop",
|
|
|
|
"Intended Audience :: Science/Research",
|
|
|
|
"Intended Audience :: System Administrators",
|
|
|
|
"License :: Public Domain",
|
|
|
|
"Operating System :: MacOS :: MacOS X",
|
|
|
|
"Operating System :: POSIX",
|
|
|
|
"Operating System :: POSIX :: BSD",
|
|
|
|
"Operating System :: POSIX :: Linux",
|
|
|
|
"Topic :: Scientific/Engineering :: Image Recognition",
|
|
|
|
"Topic :: Text Processing :: Indexing",
|
|
|
|
"Topic :: Text Processing :: Linguistic",
|
|
|
|
],
|
2016-01-19 15:07:21 -08:00
|
|
|
install_requires=[
|
|
|
|
'ruffus',
|
|
|
|
'Pillow',
|
|
|
|
'reportlab',
|
|
|
|
'PyPDF2',
|
|
|
|
'img2pdf'
|
|
|
|
],
|
2015-08-19 13:43:32 -07:00
|
|
|
tests_require=tests_require,
|
2015-07-25 23:45:13 -07:00
|
|
|
entry_points={
|
|
|
|
'console_scripts': [
|
2015-07-26 01:52:08 -07:00
|
|
|
'ocrmypdf = ocrmypdf.main:run_pipeline'
|
2015-07-25 23:45:13 -07:00
|
|
|
],
|
|
|
|
},
|
|
|
|
include_package_data=True,
|
|
|
|
zip_safe=False)
|