Skip to content

Instantly share code, notes, and snippets.

@nfaggian
Last active November 15, 2017 09:25
Show Gist options
  • Save nfaggian/8644080 to your computer and use it in GitHub Desktop.
Save nfaggian/8644080 to your computer and use it in GitHub Desktop.
Fabric script to setup a Python 3.3 environment, with some patched libraries.
"""
Fabric configuration for a Python 3.3 environment using Anaconda.
"""
from fabric.api import run, cd
from fabric.context_managers import prefix
CONDA_REPO = 'http://repo.continuum.io/miniconda/'
CONDA_VERS = 'Miniconda3-3.0.0-Linux-x86_64.sh'
DEPENDENCIES = [
'pip',
'numpy',
'scipy',
'matplotlib',
'ipython',
'scikit-learn',
'scikit-image',
'pandas',
'geos',
'requests']
def setup(install_prefix='~'):
"""
Install an anaconda environment.
"""
_base_environment(install_prefix)
_dependencies(install_prefix)
def _dependencies(install_prefix):
"""
Install a set of binary packages and then some patched packages to support
Python > 3.
"""
with prefix('source {}/anaconda/bin/activate python-devel'.format(install_prefix)):
for module in DEPENDENCIES:
run('{}/anaconda/bin/conda install --yes {}'.format(install_prefix, module))
# PyProj (requires proj4 on the remote system)
with cd('{}/downloads'.format(install_prefix)):
run('svn checkout http://pyproj.googlecode.com/svn/trunk/ pyproj')
with cd('pyproj'):
run('python setup.py build install')
run('rm -rf pyproj')
# CartoPy (like basemap)
run('pip install git+https://github.com/esc24/cartopy.git@three_osx_new')
# Fabric
run('pip install git+https://github.com/scottkmaxwell/paramiko.git@py3-support-without-py25')
run('pip install contextlib2')
run('pip install git+https://github.com/pashinin/fabric.git@p33')
def _base_environment(install_prefix):
"""
Creates a "standard" python working environment.
"""
run('mkdir -p {}/downloads'.format(install_prefix))
with cd('{}/downloads'.format(install_prefix)):
run('wget -nv -N {}{}'.format(CONDA_REPO, CONDA_VERS))
run('sh {} -b -p {}/anaconda/'.format(CONDA_VERS, install_prefix))
with cd('{}/anaconda/bin'.format(install_prefix)):
run('./conda create --yes -n python-devel python=3')
def clean(install_prefix='~'):
"""
Removes a standard working environment.
"""
run('rm -rf {}/anaconda'.format(install_prefix))
@nfaggian
Copy link
Author

Note:

You need to install fabric first, which can be installed with "pip" or "easy_install".

Install:

fab -h "remote_host" -u "username" setup

Uninstall:

fab -h "remote_host" -u "username" clean

@guysoft
Copy link

guysoft commented Nov 8, 2014

I get after using this:

Traceback (most recent call last):
File "convert.py", line 22, in <module>
    hello()
File "convert.py", line 18, in hello
    run("uname -a")
File "/usr/local/lib/python3.4/dist-packages/fabric/network.py", line 642, in host_prompting_wrapper
    host_string = input("No hosts found. Please specify (single)"
UnboundLocalError: local variable 'input' referenced before assignment

Example code:

#!/usr/bin/python3
import fabric
from fabric.api import env
from fabric.api import run
from fabric.network import ssh

env.hosts = []
env.hosts.append("localhost")

def hello():
    run("uname -a")

if __name__ == "__main__":
    hello()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment