forked from HelikarLab/candis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-candis
executable file
·259 lines (200 loc) · 8.08 KB
/
get-candis
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#!/usr/bin/python
# imports - compatibility imports
from __future__ import print_function
# imports - standard imports
import sys, os, os.path as osp
import shutil
import argparse
import subprocess
import logging
from distutils.spawn import find_executable
logging.basicConfig()
log = logging.getLogger('get-candis')
log.setLevel(logging.DEBUG)
class const(object):
LOGO_ASCII = \
"""
d8b d8,
88P `8P
d88
d888b d888b8b 88bd88b d888888 88b .d888b,
d8P' `Pd8P' ?88 88P' ?8bd8P' ?88 88P ?8b,
88b 88b ,88b d88 88P88b ,88b d88 `?8b
`?888P'`?88P'`88bd88' 88b`?88P'`88bd88' `?888P'
"""
LOGO_ASCII_COLOR = '\x1b[1;35;40m'
URL_HOMEBREW_INSTALL = 'https://raw.githubusercontent.com/Homebrew/install/master/install'
HOMEBREW_TAPS = ['homebrew/science', 'caskroom/cask']
HOMEBREW_DEPENDENCIES = ['git', 'python3', 'gcc', 'r', 'graphviz']
HOMEBREW_DEPENDENCIES_CASK = ['xquartz', 'weka']
APTGET_DEPENDENCIES = ['build-essential', 'git', 'python3-dev', 'python3-pip', 'graphviz-dev',
'default-jre', 'default-jre-headless', 'default-jdk',
'software-properties-common', 'python-software-properties', 'apt-transport-https',
'dialog', 'apt-utils',
'python3-tk'
]
URL_CANDIS = 'https://github.com/HelikarLab/candis'
def which(executable, raise_err = False):
path = find_executable(executable)
if not path and raise_err:
raise ValueError('{executable} not found.'.format(
executable = executable
))
return path
def popen(*params, **kwargs):
output = kwargs.get('output', False)
directory = kwargs.get('dir')
environment = kwargs.get('env')
shell = kwargs.get('shell', True)
raise_err = kwargs.get('raise_err', True)
environ = os.environ.copy()
if environment:
environ.update(environment)
command = " ".join(params)
proc = subprocess.Popen(command,
stdin = subprocess.PIPE if output else None,
stdout = subprocess.PIPE if output else None,
stderr = subprocess.PIPE if output else None,
env = environ,
cwd = directory,
shell = shell
)
code = proc.wait()
if code and raise_err:
raise subprocess.CalledProcessError(code, command)
if output:
output, error = proc.communicate()
# Rule of 3; currently 2.
if output:
output = output.decode('utf-8')
if output.count('\n') == 1:
output = output.strip('\n')
if error:
error = error.decode('utf-8')
if error.count('\n') == 1:
error = error.strip('\n')
return code, output, error
else:
return code
def check_git_dir(dirpath, raise_err = True):
git = which('git', raise_err = True)
if osp.isdir(dirpath) \
and osp.isdir(osp.join(dirpath, '.git')) \
and not popen(git, 'rev-parse', '--is-inside-work-tree', dir = dirpath, raise_err = False):
return True
else:
if raise_err:
raise ValueError('{path} not a valid git repository.'.format(
path = dirpath
))
else:
return False
def get_aptget():
try:
aptget = which('apt-get', raise_err = True)
return aptget
except ValueError as e:
raise ValueError('{error} Please have it installed manually.'.format(error = str(e)))
def get_homebrew(doctor = False):
brew = which('brew')
if not brew:
try:
curl = which('curl', raise_err = True)
ruby = which('ruby', raise_err = True)
pout = popen(curl, const.URL_HOMEBREW_INSTALL, output = False)
popen(ruby, '-e', pout)
brew = which('brew', raise_err = True)
except ValueError as e:
raise ValueError('{error} Please have it installed manually.'.format(error = str(e)))
if doctor:
popen(brew, 'doctor', raise_err = False)
return brew
def get_candis():
appdir = osp.join(osp.expanduser('~'), 'candis')
if not osp.exists(appdir):
git = which('git', raise_err = True)
popen(git, 'clone', '--recursive', const.URL_CANDIS, appdir)
else:
check_git_dir(appdir)
Rscript = which('Rscript', raise_err = True)
popen(Rscript, 'setup.R', dir = osp.join(appdir, 'R'))
python3 = which('python3', raise_err = True)
pip3 = which('pip3', raise_err = True)
popen(pip3, 'install', '--upgrade', 'pip')
popen(pip3, 'install', 'numpy') # Thanks, javabridge.
popen(pip3, 'install', '--ignore-installed', '-r', 'requirements.txt', dir = appdir)
# popen(pip3, 'install', '-r', 'requirements-dev.txt', dir = appdir, raise_err = False)
# Force matplotlib backend for macOS
if sys.platform == 'darwin':
with open(osp.join(osp.expanduser('~'), '.matplotlib', 'matplotlibrc'), mode = 'a') as f:
config = 'backend: TkAgg'
if config not in f.readlines():
f.write('\nbackend: TkAgg')
popen(python3, 'setup.py', 'install', dir = appdir)
candis = which('candis', raise_err = True)
return candis
def setup_candis(args = None):
code = os.EX_OK
if sys.platform == 'darwin':
brew = get_homebrew()
popen(brew, 'tap', *const.HOMEBREW_TAPS, raise_err = False)
popen(brew, 'cask', 'install', *const.HOMEBREW_DEPENDENCIES_CASK, raise_err = False)
popen(brew, 'install', *const.HOMEBREW_DEPENDENCIES, raise_err = False)
elif sys.platform.startswith('linux'):
try:
aptget = which('apt-get', raise_err = True)
popen(aptget, 'update')
popen(aptget, 'install', '-y', *const.APTGET_DEPENDENCIES)
# Install R
addapt = which('add-apt-repository', raise_err = True)
popen(which('apt-key', raise_err = True), 'adv', '--keyserver', 'keyserver.ubuntu.com', '--recv-keys', 'E084DAB9')
_, out, _ = popen('lsb_release -sc', output = True)
popen(addapt, '"deb [arch=amd64,i386] https://cran.rstudio.com/bin/linux/ubuntu {version}/"'.format(version = out))
popen(aptget, 'update')
popen(aptget, 'install', '-y', 'r-base')
# Install Oracle JRE/JDK
popen(addapt, '-y', 'ppa:webupd8team/java')
popen(aptget, 'update')
# Auto Agree License.
popen('echo debconf shared/accepted-oracle-license-v1-1 select true | sudo debconf-set-selections')
popen('echo debconf shared/accepted-oracle-license-v1-1 seen true | sudo debconf-set-selections')
popen(aptget, 'install', '-y', 'oracle-java8-installer')
except ValueError:
log.error('Sorry. This script is not supported for this Linux platform.')
code = 1
else:
log.error('Sorry. This script is not supported for this platform.')
code = 1
if code == os.EX_OK:
candis = get_candis()
return code
def get_argument_parser():
descr = '{color}{logo}\x1b[0m'.format(
color = const.LOGO_ASCII_COLOR,
logo = const.LOGO_ASCII
)
parser = argparse.ArgumentParser(
description = descr,
formatter_class = argparse.RawDescriptionHelpFormatter
)
parser.add_argument('-v', '--version',
default = 'develop',
help = 'version to install'
)
parser.add_argument('-d', '--data',
default = False,
help = 'fetch sample data'
)
parser.add_argument('--verbose',
default = True,
help = 'display log information'
)
return parser
def main(args = None):
parser = get_argument_parser()
args = parser.parse_args(args)
code = setup_candis(args)
sys.exit(code)
if __name__ == '__main__':
args = sys.argv[1:]
main(args)