-
Notifications
You must be signed in to change notification settings - Fork 0
/
mrtrix3_0rc3.rb
359 lines (304 loc) · 11.5 KB
/
mrtrix3_0rc3.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
# typed: false
# frozen_string_literal: true
run_test_patch = <<~EOS
diff --git a/run_tests b/run_tests
index a9dd80320..bd5fb9133 100755
--- a/run_tests
+++ b/run_tests
@@ -10,6 +10,10 @@ cat > $LOGFILE <<EOD
EOD
+no_fetch=${1:-do_fetch}
+no_build=${2:-do_build}
+
+if [ $no_fetch != no_fetch ]; then
echo -n "fetching test data... "
git submodule update --init >> $LOGFILE 2>&1
@@ -19,9 +23,12 @@ if [ $? != 0 ]; then
else
echo OK
fi
+else
+ shift
+fi
-
+if [ $no_build != no_build ]; then
echo -n "building testing commands... "
cat >> $LOGFILE <<EOD
@@ -30,9 +37,10 @@ cat >> $LOGFILE <<EOD
## building test commands...
EOD
+
(
cd testing
- ../build
+ ../build -nopaginate
) >> $LOGFILE 2>&1
if [ $? != 0 ]; then
echo ERROR!
@@ -40,6 +48,9 @@ if [ $? != 0 ]; then
else
echo OK
fi
+else
+ shift
+fi
# generate list of tests to run:
EOS
class Mrtrix30rc3 < 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_RC3-0-g57e351eb"
revision 1
bottle do
root_url "https://github.com/MRtrix3/mrtrix3/releases/download/3.0_RC3"
rebuild 0
sha256 cellar: :any, high_sierra: "5483cefd60aa12fe2039997588b055e811863c65b75577e96da06a8b0ea96edd"
end
option "test", "Run tests after installation."
option "assert", "Build with assert statements (executables are slower)."
option "debug", "Build with debug statements (executables are slower)."
option "mrconvert", "Build mrconvert, no other binaries unless stated"
option "mrinfo", "Build mrinfo, no other binaries unless stated"
option "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."
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 "eigen" => :build
depends_on "fftw"
depends_on "libtiff"
depends_on "pkg-config"
depends_on "qt5"
depends_on "python" => :recommended
conflicts_with "mrtrix3", because: "tagged version (this) conflicts with non-tagged version of mrtrix3"
def execute(cmd)
# verbose alternative to: system cmd
require "pty"
raw = ""
PTY.spawn(cmd) do |stdout_err, _stdin, pid|
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
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("#{pkgshare}/matlab_add.py", "w") do |f|
f.puts matlab_add
end
system "python", "#{pkgshare}/matlab_add.py"
rm "#{pkgshare}/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
system "git", "fetch", "--unshallow"
system "git", "reset", "--hard", "origin/master"
system "git", "checkout", version.to_s
if build.without? "multithreaded_build"
ENV["NUMBER_OF_PROCESSORS"] = "1"
print "NUMBER_OF_PROCESSORS = 1"
end
conf = ["./configure"]
# if build.without? "qt5"
# conf.push("-nogui")
# end
conf.push("-assert") if build.include? "assert"
conf.push("-debug") if build.include? "debug"
if build.with? "copy_src_from_home"
me = `whoami`.strip
external_mrtrix_src_dir = "/Users/#{me}/mrtrix3"
unless File.directory?(external_mrtrix_src_dir.to_s)
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 = `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
scripts.each do |scrpt|
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"]
bld.push("release/bin/mrconvert") if build.include? "mrconvert"
bld.push("release/bin/mrinfo") if build.include? "mrinfo"
bld.push("release/bin/mrview") if build.include? "mrview"
execute(bld.join(" "))
# This has to be before bin.install or else binaries are not in place.
if build.include? "test"
tst = ["./run_tests"]
tst.push("mrconvert") if build.include? "mrconvert"
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"]
bld.push("bin/mrconvert") if build.include? "mrconvert"
bld.push("bin/mrinfo") if build.include? "mrinfo"
bld.push("bin/mrview") if build.include? "mrview"
execute(bld.join(" "))
# This has to be before bin.install or else binaries are not in place.
if build.include? "test"
tst = ["./run_tests"]
tst.push("mrconvert") if build.include? "mrconvert"
execute(tst.join(" "))
system "mkdir", "#{prefix}/testing"
cp_r "testing/.", "#{prefix}/testing/"
# patch run_tests tests to not fetch the data and not build
open("run_test_patch.diff", "w") do |f|
f.puts un_test_patch
end
system "python", "matlab_add.py"
execute("git apply run_test_patch.diff")
rm "run_test_patch.diff"
cp_r "run_tests", prefix.to_s
cp "testing.log", prefix.to_s
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
end
def post_install
if build.without? "matlab"
print "ignoring Matlab"
else
begin
set_matlab_path
rescue Exception => e
print "Warning: unable to set Matlab path: #{e}\n"
print "You can add it manually to your startup.m: addpath('#{prefix}/matlab')\n"
end
end
print "Installation done. The MRtrix3 binaries are in #{prefix}/bin\n"
print "\n"
print "⚠️: If you have an existing version of MRtrix3 in your PATH\n"
print "and want to use this version without prepending '$(brew --prefix)/bin' to your commands,\n"
print "make sure to remove the previous version from your PATH.\n"
print "You can find out with 'ls -l $(which mrinfo)'\n"
print "\n"
# print "Alternatively, rename its folder or prepend your PATH with #{prefix}/bin\n"
print "For more information go to http://mrtrix.readthedocs.io\n"
end
test do
if build.include? "test"
puts prefix.to_s
puts testpath
cp_r "#{prefix}/.", testpath # mrtrix directory is not writable, testpath is
cd testpath
system("./run_tests", "no_fetch", "no_build")
end
end
end