-
Notifications
You must be signed in to change notification settings - Fork 0
/
python.py
174 lines (123 loc) · 4.63 KB
/
python.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
from fabric.api import cd, env, put, run, task, sudo, prefix, require
from fabric.contrib import files
import utils
@task
def conda_install():
"""
Download and install "miniconda".
:Example:
fab --config=config.conf python.conda_install
"""
require('MINICONDA_NAME')
require('MINICONDA_FILE')
with cd(utils.home()):
if not files.exists(env.MINICONDA_NAME):
# Download the miniconda installer.
run('wget {0}'.format(env.MINICONDA_FILE))
# Give permission to execute installer.
run('chmod u+x {0}'.format(env.MINICONDA_NAME))
# Put miniconda in the "apps" directory.
apps = utils.home('apps')
run('mkdir -p {0}'.format(apps))
# OPTIONS:
#
# -b Run in silent mode.
# -p The path where miniconda will be installed.
run('bash {0} -b -p {1}/miniconda'.format(env.MINICONDA_NAME, apps))
# Add the executables to the system path.
export = 'export PATH="{0}/miniconda/bin:$PATH"'.format(apps)
files.append('.bashrc', '###########')
files.append('.bashrc', '# MINICONDA')
files.append('.bashrc', export)
# Remove the installer after installation.
run('rm {0}'.format(env.MINICONDA_NAME))
@task
def conda_list_environments():
"""
List all conda virtual environments.
:Example:
fab --config=config.conf python.conda_list_environments
"""
conda = '{0}/bin/conda'.format(utils.home('apps', 'miniconda'))
run('{conda} info --envs'.format(conda=conda))
@task
def conda_create_environment(name, python='3'):
"""
Create a new virtual environment. Installs "Python 3" by default.
:Example:
fab --config=config.conf python.conda_create_environment:name=bugtrax
"""
conda = '{0}/bin/conda'.format(utils.home('apps', 'miniconda'))
run('{conda} create --name {name} python={python} --yes'.format(
name=name,
conda=conda,
python=python))
@task
def conda_install_requirements(venv):
"""
Run "pip install -r" on the requirements file.
:Example:
fab --config=config.conf python.conda_install_requirements:venv=bugtrax
"""
# Upload the requirements file.
put(utils.files('requirements', 'base.txt'), utils.home('base.txt'))
put(utils.files('requirements', 'prod.txt'), utils.home('prod.txt'))
# Activate the virtual environment.
activate = '{0}/bin/activate'.format(utils.home('apps', 'miniconda'))
with prefix('source {activate} {venv}'.format(venv=venv, activate=activate)):
run('pip install -r {0}'.format(utils.home('prod.txt')))
# Remove the uploaded files.
with cd(utils.home()):
run('rm {0}'.format(utils.home('base.txt')))
run('rm {0}'.format(utils.home('prod.txt')))
@task
def dev():
"""
Install dependencies for building Python C extensions.
:Example:
fab --config=config.conf python.dev
"""
# Python build headers.
packages = [
'python3-setuptools',
'python3-dev',
'python3-tk',
'python-setuptools',
'python-dev',
'python-tk',
]
sudo('apt-get -y install {}'.format(' '.join(packages)))
@task
def venv(name):
"""
Create a new Python virtual environment.
:param name: The name of the new virtual environment.
:Example:
fab --config=config.conf python.venv:name=myvenv
"""
venvs_directory = '/home/{}/venvs'.format(env.user)
# Put all virtual environments in one directory.
run('mkdir -p {}'.format(venvs_directory))
# In Ubuntu 14.04, there's an issue with Python 3's built-in pip.
# So, virtual environments are created without pip.
with cd(venvs_directory):
run('python3 -m venv --without-pip {}'.format(name))
# Activate the virtual environment then manually download and install pip.
with prefix('source {}/{}/bin/activate'.format(venvs_directory, name)):
run('curl https://bootstrap.pypa.io/get-pip.py | python')
@task
def install(name):
"""
Upload and install the dependencies in the requirements file.
:param name: The name of the virtual environment to use.
:Example:
fab --config=config.conf python.install:name=myvenv
"""
base = '/home/{}/venvs/{}/base.txt'.format(env.user, name)
prod = '/home/{}/venvs/{}/prod.txt'.format(env.user, name)
# Upload requirements file.
put(utils.file_path('requirements', 'base.txt'), base)
put(utils.file_path('requirements', 'prod.txt'), prod)
# Activate the virtual environment.
with prefix('source /home/{}/venvs/{}/bin/activate'.format(env.user, name)):
run('pip install -r {}'.format(prod))