Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env python
"""
Guide users through installing the installation dependencies
"""
# Unfortunately, we can't rely on setuptools' install_requires
# keyword, because matplotlib doesn't properly install its dependencies
from subprocess import check_call, CalledProcessError
import sys
import os
from imp import find_module
import shlex
import argparse
class Dependency(object):
def __init__(self, module, info, package=None, min_version=None):
self.module = module
self.info = info
self.package = package or module
self.min_version = min_version
self.failed = False
self.env = None
@property
def installed(self):
try:
find_module(self.module)
return True
except ImportError:
return False
def install(self):
if self.installed:
return
try:
if self.env:
cmd = 'conda install -p %s --yes %s' % (self.env, self.package)
else:
cmd = 'conda install --yes %s' % self.package
check_call(shlex.split(cmd))
except CalledProcessError:
self.failed = True
def __str__(self):
if self.installed:
status = 'INSTALLED'
elif self.failed:
status = 'FAILED (%s)' % self.info
else:
status = 'MISSING (%s)' % self.info
return "%20s:\t%s" % (self.module, status)
# Add any dependencies here
# Make sure to add new categories to the categories tuple
required = (
Dependency('numpy', 'Required', min_version='1.4'),
Dependency('matplotlib', 'Required for plotting', min_version='1.1'),
Dependency('pandas', 'Adds support for Excel files and DataFrames', min_version='0.13.1'),
Dependency('scipy', 'Used for some image processing calculation'),
Dependency('IPython', 'Needed for interactive IPython terminal'),
Dependency('pygments', 'Needed for interactive IPython terminal'),
Dependency('zmq', 'Needed for interactive IPython terminal', 'pyzmq'))
testing = (
Dependency('mock', 'Used in test code'),
Dependency('pytest', 'Used in test code'))
doc = (
Dependency('numpydoc', 'Used to generate docstrings'),)
categories = (('required', required),
('testing', testing),
('documentation', doc))
dependencies = {d.module: d for c in categories for d in c[1]}
def show_status():
for category, deps in categories:
print "%21s" % category.upper()
for dep in deps:
print dep
print '\n'
def install_all(env):
req_str = ' '.join(req.package for _, reqs in categories for req in reqs)
if env:
cmd = 'conda install --yes -p %s %s' % (env, req_str)
else:
cmd = 'conda install --yes %s' % req_str
def check_conda_env(env):
return os.path.exists(env)
def make_conda_env(env):
req_str = ' '.join(req.package for _, reqs in categories for req in reqs)
if env:
cmd = 'conda create --yes -p %s %s' % (env, req_str)
print cmd
raise KeyboardInterrupt
else:
cmd = 'conda create --yes %s' % req_str
check_call(shlex.split(cmd))
def main(parsed_args):
if not parsed_args.list_deps:
env = parsed_args.conda_env
if not check_conda_env(env):
make_conda_env(env)
install_all(env)
show_status()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Tool to install conda dependancies!")
parser.add_argument('packages', nargs='+')
parser.add_argument('--list',
dest='list_deps',
default=False,
action='store_true',
help='List the status of the requirements')
parser.add_argument('--conda-env',
dest='conda_env',
action='store',
default=None,
help='Path to conda environment if installing into a restricted environment.')
main(parser.parse_args())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.