Skip to content

Commit

Permalink
setup: Revamp setup to handle deps better
Browse files Browse the repository at this point in the history
Have a class for every dependency which checks for
the required packages correctly.
  • Loading branch information
AbdealiLoKo committed Jun 28, 2016
1 parent 7db1cfe commit c85dd41
Show file tree
Hide file tree
Showing 5 changed files with 650 additions and 82 deletions.
8 changes: 4 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ install:
# Install dlib from git (with pip) as there was an issue with libjpeg
# Which was solved and not yet released.
- pip install git+https://github.com/davisking/dlib.git@02f6da285149a61bc58728d9c5e77932151118b5#egg=dlib ;
- pip install -r test-requirements.txt -r requirements.txt ;
- pip install -r test-requirements.txt ;
- pip install .
- pip uninstall -y file-metadata

script:
- flake8 setup.py file_metadata tests
- flake8 setup.py setupdeps.py file_metadata tests
- python -m pytest --cov ;
- python setup.py sdist bdist bdist_wheel
- pip install .
- pip uninstall -y file-metadata

after_script:
- bash <(curl -s https://codecov.io/bash)
Expand Down
1 change: 0 additions & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
include README.*
include LICENSE
include test-requirements.txt
include requirements.txt
include setup.cfg
21 changes: 0 additions & 21 deletions requirements.txt

This file was deleted.

148 changes: 92 additions & 56 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,86 +5,122 @@
print_function)

import os
import platform
import subprocess
import sys
from distutils import log
from distutils.errors import DistutilsSetupError

from setuptools import find_packages, setup
import setupdeps

log.set_verbosity(log.INFO)

with open(os.devnull, 'w') as nul:
# Check if exiftool or perl is installed.
try:
out = subprocess.check_call(['perl', '-v'], stdout=nul, stderr=nul)
except (OSError, subprocess.CalledProcessError):
try:
out = subprocess.check_call(['exiftool', '-ver'], stdout=nul,
stderr=nul)
except (OSError, subprocess.CalledProcessError):
print('`perl` (https://www.perl.org/) or `exiftool` '
'(http://www.sno.phy.queensu.ca/~phil/exiftool/) '
'need to be installed and made available in your PATH. '
'On Ubuntu, you can install perl with '
'`sudo apt-get install perl` or install exiftool with '
'`sudo apt-get install exiftool`.')
sys.exit(1)

# Check if java is installed.
try:
out = subprocess.check_call(['java', '-version'], stdout=nul,
stderr=nul)
except (OSError, subprocess.CalledProcessError):
print('`java` (https://java.com/) needs to be installed and needs to '
'be made available in your PATH. If using Ubuntu, you can do '
'`sudo apt-get install openjdk-7-jre`')
sys.exit(1)

# Check if avprobe of ffprobe is installed if system is not Linux.
# If it's linux, we use static builds from http://johnvansickle.com/libav/
if platform.system() != 'Linux':
setup_deps = [
setupdeps.Distro(),
setupdeps.SetupTools()
]

install_deps = [
# Core deps
setupdeps.LibMagic(),
setupdeps.PythonMagic(),
setupdeps.Six(),
setupdeps.ExifTool(),
setupdeps.AppDirs(),
# Image deps
setupdeps.PathLib(),
setupdeps.LibLZMA(),
setupdeps.LZMA(),
setupdeps.Pillow(),
setupdeps.Numpy(),
setupdeps.Dlib(),
setupdeps.ScikitImage(),
setupdeps.MagickWand(),
setupdeps.Wand(),
setupdeps.LibZBar(),
setupdeps.ZBar(),
setupdeps.JavaJRE(),
setupdeps.PyColorName(),
# Audio video deps
setupdeps.FFProbe(),
]


def check_deps(deplist):
failed_deps = []
for dep in deplist:
try:
out = subprocess.check_call(['ffprobe', '-version'], stdout=nul,
stderr=nul)
except (OSError, subprocess.CalledProcessError):
try:
out = subprocess.check_output(['avprobe', '-version'],
stdout=nul, stderr=nul)
except (OSError, subprocess.CalledProcessError):
print('`ffprobe` (https://ffmpeg.org/ffprobe.html) or '
'`avprobe` (http://libav.org/documentation/avprobe.html)'
'need to be installed and made available in your PATH. '
'If using Ubuntu, you can install avprobe with '
'`sudo apt-get install libav-tools`.')
sys.exit(1)
log_msg = dep.check()
log.info(dep.name + ' - ' + log_msg)
except setupdeps.CheckFailed as err:
log.info(dep.name + ' - ' + err.args[0])
failed_deps.append(dep)

if len(failed_deps) > 0:
msg = 'Some dependencies could not be installed automatically: '
msg += ", ".join(i.name for i in failed_deps)
for dep in failed_deps:
install_msg = dep.install_help_msg()
if install_msg:
msg += '\n* ' + install_msg
raise DistutilsSetupError(msg)
return deplist


def get_install_requires(deplist):
packages = []
for dep in check_deps(deplist):
packages += dep.get_install_requires()
return packages


def read_reqs(reqs_filename):
reqs = open(reqs_filename).read().strip().splitlines()
return list(i for i in reqs if not (i.startswith('#') or len(i) == 0))

required = read_reqs('requirements.txt')
test_required = read_reqs('test-requirements.txt')
VERSION = open(os.path.join('file_metadata', 'VERSION')).read().strip()

if sys.version_info >= (3,):
# mock is not required for python 3
test_required.remove('mock')
if __name__ == '__main__':
log.info('Check and install dependencies required for setup:')
setup_packages = []
for dep in check_deps(setup_deps):
setup_packages += dep.get_setup_requires()

if setup_packages:
log.info('Some required packages required for file-metadata\'s setup '
'were not found: {0}.\nInstalling them with `pip` ...'
.format(", ".join(setup_packages)))
out = setupdeps.setup_install(setup_packages)
if out is not True:
raise DistutilsSetupError(
'Unable to install package(s) required by the setup script '
'using pip: {0}\nReport this issue to the maintainer.\n'
'Temporarily, this could be solved by running: '
'`python -m pip install {1}`.'
.format(", ".join(setup_packages), " ".join(setup_packages)))

log.info('Check dependencies required for using file-metadata:')
install_required = get_install_requires(install_deps)
test_required = read_reqs('test-requirements.txt')
VERSION = open(os.path.join('file_metadata', 'VERSION')).read().strip()

if sys.version_info >= (3,): # mock is not required for python 3
test_required.remove('mock')

from setuptools import find_packages, setup

if __name__ == "__main__":
setup(name='file-metadata',
version=VERSION,
description='Helps to find structured metadata from a given file.',
author="DrTrigon",
author_email="dr.trigon@surfeu.ch",
maintainer="AbdealiJK",
maintainer_email='abdealikothari@gmail.com',
license="MIT",
url='https://github.com/AbdealiJK/file-metadata',
packages=find_packages(exclude=["build.*", "tests.*", "tests"]),
install_requires=required,
# Dependency management by pip
install_requires=install_required,
tests_require=test_required,
license="MIT",
# Setuptools has a bug where they use isinstance(x, str) instead
# of basestring. Because of this we convert it to str.
# of basestring. Because of this we convert it to str for Py2.
package_data={str('file_metadata'): [str("VERSION")]},
# from http://pypi.python.org/pypi?%3Aaction=list_classifiers
classifiers=[
Expand Down
Loading

0 comments on commit c85dd41

Please sign in to comment.