-
Notifications
You must be signed in to change notification settings - Fork 0
/
mrtrix3.rb
364 lines (307 loc) · 11.5 KB
/
mrtrix3.rb
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
class Qt5Requirement < Requirement
fatal true
satisfy :build_env => false do
if File.file?("#{HOMEBREW_PREFIX}/opt/qt5/bin/qmake")
@qmake = which("#{HOMEBREW_PREFIX}/opt/qt5/bin/qmake")
else
@qmake = which('qmake')
end
@qmake
end
env do
ENV.append_path 'PATH', File.dirname(@qmake)
end
def message
message = <<-EOS
Homebrew was unable to find an installation of Qt 5. You can install it for instance via homebrew:
brew update
brew install qt5
EOS
end
end
class Mrtrix3 < Formula
desc "MRtrix3: tools to perform various types of diffusion MRI analyses."
homepage "http://mrtrix.org"
url "https://github.com/MRtrix3/mrtrix3.git"
version '3.0.2-48-g63d94827'
revision 0
# devel do
# url 'https://github.com/MRtrix3/mrtrix3.git', :branch => 'master', :revision => 'bogus474279845b7e79fc2b5ffad'
# version '0.3_dev'
# end
option "with-stable", "Install latest tagged stable version. Default is last commit on master branch."
option "test", "Run tests after installation."
option "with-assert", "Build with assert statements (executables are slower)."
option "with-debug", "Build with debug statements (executables are slower)."
option "with-mrconvert", "Build mrconvert, no other binaries unless stated"
option "with-mrinfo", "Build mrinfo, no other binaries unless stated"
option "with-mrview", "Build mrview, no other binaries unless stated"
option "without-multithreaded_build", "This is useful if your computer has many cores but not enough RAM to build MRtrix using multiple threads. Alternatively, set the environment variable HOMEBREW_NUMBER_OF_PROCESSORS=1"
option "without-matlab", "Do not add MRtrix scripts to matlab path."
option "with-copy_src_from_home", "Use MRtrix3 source code from ~/mrtrix3. This settting is for developers and testing purposes!"
depends_on "python" => :recommended
depends_on "eigen" => :build
depends_on "fftw" => :recommended
depends_on "libtiff" => :recommended
depends_on "pkg-config"
# depends_on "qt5" # not used as users might want to use an existing qt or install mrtrix without a GUI
depends_on Qt5Requirement => :recommended
conflicts_with "mrtrix3_0rc3", :because => "tagged version (mrtrix3@0rc3) conflicts with non-tagged version of mrtrix3 (this)."
def execute (cmd)
# verbose alternative to: system cmd
require 'pty'
raw = ''
PTY.spawn(cmd) do |stdout_err, stdin, pid|
begin
while (char = stdout_err.getc)
raw << char
print char
end
rescue Errno::EIO # always raised when PTY runs out of input
ensure
Process.waitpid pid # Wait for PTY to complete before continuing
end
end
return raw
end
def set_matlab_path ()
matlab_add = <<-EOS
import os, glob, sys, subprocess
# Trick out homebrew's overwrite of USER
p = subprocess.Popen('whoami', shell = True, stdout=subprocess.PIPE)
me = p.stdout.readline().rstrip()
p.wait()
comment='% MRtrix3 PATH automatically generated by homebrew installation formula - do NOT modify:'
set_path = "addpath('#{prefix}/matlab')" + os.linesep
def path_has_been_set(startup):
if not os.path.isfile(startup):
return False
with open (startup, "r") as inp:
for iline, line in enumerate(inp):
if comment in line:
nl = next(inp)
if not 'mrtrix' in nl.lower():
raise Exception ('matlab startup file ' + startup + ' has been modified. aborting. ' + nl)
return iline + 1
return 0
matlab_bins = glob.glob("/Applications/MATLAB_R20*/bin/matlab")
if not len(matlab_bins):
print ("WARNING: no Matlab binary found. Not adding MRtrix to Matlab startup path")
sys.exit(0)
startup_locations = []
for bin in matlab_bins:
matlab_root = os.path.split(os.path.split(bin)[0])[0]
startup_locations.append(os.path.join(matlab_root, "toolbox", "local", "startup.m"))
userdir = os.path.join('/Users',me,'Documents','MATLAB')
if os.path.isdir(userdir):
startup_locations.append(os.path.join(userdir,"startup.m"))
else:
print userdir + " not found"
is_set = 0
for startup in startup_locations:
try:
pline = path_has_been_set(startup)
if pline:
with open (startup, "r") as inp:
sf = inp.readlines()
sf[pline] = set_path + os.linesep
with open (startup, "w") as inp:
inp.write(''.join(sf))
print ("updated mrtrix path in " + startup)
else:
with open (startup, "a") as inp:
inp.write(comment + os.linesep + set_path + os.linesep)
print ("added mrtrix path to " + startup)
is_set += 1
except:
print "WARNING: could not set mrtrix path in Matlab startup file: " + startup
if not (is_set):
raise Exception('could not set mrtrix path in any Matlab startup file')
EOS
open('matlab_add.py', 'w') do |f|
f.puts matlab_add
end
system "python", "matlab_add.py"
rm 'matlab_add.py'
end
def install
puts "PATH:"
puts ENV["PATH"]
if ENV["PATH"].downcase.include? "anaconda"
puts "warning: anaconda found in PATH. You might want to exclude those directories from you PATH."
end
xcodeerror=`xcodebuild 2>&1`
# puts xcodeerror
if xcodeerror.include? "tool 'xcodebuild' requires Xcode"
puts "\nxcodebuild failed with the error message:"
puts xcodeerror
puts "If the command line tools were installed before Xcode, you can fix this with:"
puts "sudo xcode-select -s /Applications/Xcode.app/Contents/Developer"
raise 'command line tools failure'
end
if build.with? "stable"
system "git", "reset", "--hard", "origin/master"
system "git", "fetch", "-t"
latesttag = `git describe --tags --abbrev=0`.strip
system "git", "checkout", "#{latesttag}"
end
if build.without? "matlab"
print "ignoring Matlab"
else
begin
set_matlab_path()
rescue BuildError => bang
print "Unable to set Matlab path: " + bang.to_s + "\n"
end
end
if !ENV['HOMEBREW_NUMBER_OF_PROCESSORS'].nil? # ENV option: HOMEBREW_NUMBER_OF_PROCESSORS
ENV['NUMBER_OF_PROCESSORS'] = ENV['HOMEBREW_NUMBER_OF_PROCESSORS']
end
if build.without? "multithreaded_build"
ENV["NUMBER_OF_PROCESSORS"] = "1"
end
conf = [ "./configure"]
if build.without? "qt5"
conf.push("-nogui")
end
if build.with? "assert"
conf.push("-assert")
end
if build.with? "debug"
conf.push("-debug")
end
if build.with? "copy_src_from_home"
me = `whoami`.strip
external_mrtrix_src_dir = "/Users/"+me+"/mrtrix3"
if not File.directory?("#{external_mrtrix_src_dir}")
raise "not found: "+external_mrtrix_src_dir+". --with-copy_src_from_home is intended for developers only"
end
execute("rm -r *")
execute("cp -r "+external_mrtrix_src_dir+"/* ./")
end
execute (conf.join(" "))
bin.mkpath()
pkgshare.mkpath()
cp "LICENCE.txt", "#{prefix}/"
cp "LICENCE.txt", "#{pkgshare}/"
system "git", "reset", "head"
system "git", "log", "-1"
system "git", "describe", "--always", "--dirty"
env = %x[env]
puts "shared path: #{pkgshare}"
File.open("#{pkgshare}/env", 'w') { |file| file.write(env) }
if File.directory?("release") # pre tag 0.3.16
system "mkdir", "#{prefix}/lib"
system "mkdir", "#{prefix}/release"
system "ln", "-s", "#{prefix}/bin", "#{prefix}/release/bin"
system "mkdir", "#{prefix}/matlab"
cp_r 'matlab/.', "#{prefix}/matlab/"
system "mkdir", "#{prefix}/icons"
cp_r 'icons/.', "#{prefix}/icons/"
# copy and link scripts
system "mkdir", "#{prefix}/scripts"
cp_r 'scripts/.', "#{prefix}/scripts/"
# find scripts that have lib.app.initParser and add others manually
scripts = `find "#{prefix}/scripts" -type f ! -name "*.*" -maxdepth 1`
scripts = scripts.split(" ")
scripts = scripts.uniq.sort
for scrpt in scripts
print "linking "+Pathname(scrpt).each_filename.to_a[-1]+"\n"
# bin.install_symlink prefix/"scripts/"Pathname(scrpt).each_filename.to_a[-1]
system "ln", "-s", scrpt, "#{prefix}/bin/"+Pathname(scrpt).each_filename.to_a[-1]
end
bld = [ "./build"]
if build.with? "mrconvert"
bld.push("release/bin/mrconvert")
end
if build.with? "mrinfo"
bld.push("release/bin/mrinfo")
end
if build.with? "mrview"
bld.push("release/bin/mrview")
end
execute (bld.join(" "))
# This has to be before bin.install or else binaries are not in place.
if build.with? "test"
tst = [ "./run_tests"]
if build.with? "mrconvert"
tst.push("mrconvert")
end
execute (tst.join(" "))
cp "testing.log", pkgshare
print "Testing done. Testlog is in #{pkgshare}\n"
end
execute("git rev-parse HEAD > #{pkgshare}/git_hash")
bin.install Dir["release/bin/*"]
cp_r 'release/lib/.', "#{prefix}/lib/"
cp "release/config", pkgshare
else # >= tag_0.3.16
cp "config", pkgshare
cp "configure.log", pkgshare
system "mkdir", "#{prefix}/matlab"
cp_r 'matlab/.', "#{prefix}/matlab/"
system "mkdir", "#{prefix}/icons"
cp_r 'icons/.', "#{prefix}/icons/"
bld = [ "./build"]
if build.with? "mrconvert"
bld.push("bin/mrconvert")
end
if build.with? "mrinfo"
bld.push("bin/mrinfo")
end
if build.with? "mrview"
bld.push("bin/mrview")
end
execute (bld.join(" "))
# This has to be before bin.install or else binaries are not in place.
if build.with? "test"
tst = [ "./run_tests"]
if build.with? "mrconvert"
tst.push("mrconvert")
end
execute (tst.join(" "))
cp "testing.log", pkgshare
print "Testing done. Testlog is in #{pkgshare}\n"
end
execute("git rev-parse HEAD > #{pkgshare}/git_hash")
bin.install Dir["bin/*"]
system "mkdir", "#{prefix}/lib"
cp_r 'lib/.', "#{prefix}/lib/"
cp "config", pkgshare
end
# TODO: mrtrix_bash_completion
print "Installation done. MRtrix3 lives in #{prefix}\n"
print "For more information go to http://mrtrix.readthedocs.io\n"
end
# test do
# if build.with? "copy_src_from_home"
# me = `whoami`.strip
# external_mrtrix_src_dir = "/Users/"+me+"/mrtrix3"
# if not File.directory?("#{external_mrtrix_src_dir}")
# raise "not found: "+external_mrtrix_src_dir+". --with-copy_src_from_home is intended for developers only"
# end
# execute("rm -r *")
# execute("cp -r "+external_mrtrix_src_dir+" #{testpath}/mrtrix3")
# else
# execute("git clone https://github.com/MRtrix3/mrtrix3.git #{testpath}/mrtrix3")
# end
# cd "mrtrix3"
# githash = File.open("#{pkgshare}/git_hash") {|f| f.readline}
# execute("git checkout #{githash}")
# system "git", "log", "-1"
# # TODO: use config also for tests
# # cp "#{pkgshare}/config","config"
# # cp_r "#{prefix}/lib/", "lib"
# # mkdir "release"
# # cp "#{pkgshare}/config","release/config"
# begin
# execute("./configure -nogui")
# execute("./run_tests")
# rescue
# execute("echo 'Unable to run tests.")
# end
# execute("if tests failed: rerun with debug flag `brew test -d mrtrix3`, go to #{testpath} and inspect testing.log")
# system false # `brew test -d mrtrix3` will stop here
# # execute("cat testing.log")
# end
end