File: bornagain.rb

package info (click to toggle)
bornagain 23.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 103,948 kB
  • sloc: cpp: 423,131; python: 40,997; javascript: 11,167; awk: 630; sh: 318; ruby: 173; xml: 130; makefile: 51; ansic: 24
file content (251 lines) | stat: -rw-r--r-- 7,630 bytes parent folder | download
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
class Bornagain < Formula
  require "etc"

  desc "Open-source research software to simulate and fit " \
       "neutron and x-ray reflectometry and grazing-incidence small-angle scattering"
  homepage "https://bornagainproject.org"

  url "https://jugit.fz-juelich.de/mlz/bornagain.git", branch: "r22", depth: 1, using: :git
  version "22.0"

  license "GPL-3.0-or-later"

  depends_on "cmake" => [:build, "3.20"]
  depends_on "fftw"
  depends_on "gsl"
  depends_on "libcerf"
  depends_on "libformfactor" => "0.3.2"
  depends_on "libheinz" => "2.0.1"
  depends_on "libtiff"
  depends_on "python@3"
  depends_on "qt@6"

  def build_dir
    "#{buildpath}/build/"
  end

  def local_dir
    "#{build_dir}/var/local/"
  end

  def nproc
    [Etc.nprocessors - 2, 1].max
  end

  def cmake_exe
    "#{Formula["cmake"].opt_bin.realpath}/cmake"
  end

  def ctest_exe
    "#{Formula["cmake"].opt_bin.realpath}/ctest"
  end

  def python3_exe
    # default Python
    "python3"
  end

  def qt_cmake_dir
    Formula["qt@6"].prefix/"lib/cmake/"
  end

  def ff_cmake_dir
    Formula["libformfactor"].prefix/"cmake/"
  end

  def heinz_cmake_dir
    Formula["libheinz"].prefix/"cmake/"
  end

  def create_venv(root, prompt = "")
    puts ".: Creating a Python virtualenv in '#{root}'..."

    if prompt.empty?
      system python3_exe, "-m", "venv", "--clear", root
    else
      system python3_exe, "-m", "venv", "--clear", "--prompt", prompt, root
    end

    "#{root}/bin/python3"
  end

  def install
    odie ".: Error: This BornAgain install script is intended for MacOS only." unless OS.mac?

    if Hardware::CPU.arm?
      puts ".: ARM architecture detected."
    elsif Hardware::CPU.intel?
      puts ".: Intel architecture detected."
    else
      odie ".: Error: Hardware architecture is not supported."
    end

    if Hardware::CPU.is_64_bit?
      puts ".: 64-bit architecture detected."
    else
      odie ".: Error: Hardware architecture is not supported."
    end

    cmake = Formula["cmake"]
    cmake_dir = cmake.prefix.realpath
    cmake_ver = cmake.version
    # path of the Python executable
    py_dir = `#{python3_exe} -c "import sys; print(sys.executable)"`.strip
    # major and minor version of the Python platform
    py_ver = `#{python3_exe} --version`.split[1]
    py_v = py_ver.split(".")
    py_v_major = py_v[0].to_i
    py_v_minor = py_v[1].to_i

    puts ".: CMake #{cmake_ver} at '#{cmake_dir}'"
    puts ".: Python #{py_ver} at '#{py_dir}'"
    puts ".: BornAgain #{version} build directory: '#{build_dir}'"
    puts ".: BornAgain installation prefix: '#{prefix}'"
    puts ".: std_cmake_args = ", std_cmake_args.inspect

    odie ".: Error: BornAgain requires Python 3.10 or later." if py_v_major < 3 || py_v_minor < 10

    # Python virtual environment
    venv_root = "#{local_dir}/venv"
    venv_py = create_venv(venv_root)

    puts "   - Installing the required packages in the Python virtualenv..."
    system venv_py, "-m", "pip", "install", "numpy>=2.0.0", "setuptools", "wheel"

    py_pkgs = ["numpy", "setuptools", "wheel"]
    py_pkg_info = {}
    name_rx = /^\s*Name\s*:\s*([\w_]+)/i        # eg. 'Name: numpy'
    version_rx = /^\s*Version\s*:\s*([\d.]+)/i  # eg., 'Version: 1.2.3'

    py_pkgs.each do |pkg|
      # extract package info via `pip show`
      info = `#{venv_py} -m pip show #{pkg}`
      nm = info.match(name_rx)[1]
      ver = info.match(version_rx)[1]
      py_pkg_info[nm] = ver
    end

    puts ".: Python virtualenv:"
    py_pkg_info.each do |nm, ver|
      puts "   #{nm} : #{ver}"
    end

    puts ".: Building BornAgain..."

    ba_cmd = [cmake_exe, "-S", buildpath.to_s, "-B", build_dir.to_s,
              *std_cmake_args, "-DBA_TESTS=OFF",
              "-DCMAKE_PREFIX_PATH=#{qt_cmake_dir};#{ff_cmake_dir};#{heinz_cmake_dir};",
              "-DBORNAGAIN_PYTHON=ON", "-DBA_PY_PACK=ON",
              "-DPython3_EXECUTABLE=#{venv_py}"]

    puts "   >> " + ba_cmd.join(" ")

    # CMake configuration step
    system(*ba_cmd)

    # CMake build step
    system cmake_exe, "--build", build_dir.to_s,
           "--config", "Release", "--parallel", nproc

    puts "   - Building BornAgain Python wheel..."
    system cmake_exe, "--build", build_dir.to_s,
           "--config", "Release", "--target", "ba_wheel"

    puts ".: Building BornAgain: Done."

    # CMake install step
    puts ".: Installing BornAgain..."
    system cmake_exe, "--install", build_dir.to_s, "--config", "Release"
    puts ".: Installing BornAgain: Done."
  end

  def post_install
    # install the BornAgain Python wheel in a dedicated virtual environment
    puts ".: Building BornAgain virtual environment..."
    venv_root = (Pathname "#{prefix}/").realpath/"venv"
    venv_py = create_venv(venv_root, "BornAgain")

    wheelname = Dir.glob("#{share}/BornAgain/wheel/*.whl").first
    puts ".: Installing BornAgain wheel '#{wheelname}'..."
    system venv_py, "-m", "pip", "install", wheelname
    # install Fabio package
    system venv_py, "-m", "pip", "install", "fabio"

    # extract the installation location of the BornAgain package via `pip show`
    # eg. 'Location: /opt/homebrew/Cellar/bornagain/22.0/venv/lib/python3.12/site-packages'
    location_rx = /^\s*Location\s*:\s*(.+)\s*/i
    info = `#{venv_py} -m pip show bornagain`
    ba_loc = info.match(location_rx)[1]

    # script to run BornAgain GUI with embedded Python
    gui_script = %Q(\
     #! /bin/sh

     # run BornAgain with Python support
     source #{venv_root}/bin/activate
     export PYTHONPATH="#{ba_loc}:${PYTHONPATH}"
     #{bin}/bornagain
    )

    ba_gui_cmd = "#{bin}/bornagain_gui.sh"
    File.write(ba_gui_cmd, gui_script, mode: "w")
    # make the script executable for all users
    chmod 0755, ba_gui_cmd

    # GUI entrypoint (symlink)
    brew_bin_pth = (Pathname "#{HOMEBREW_PREFIX}/bin/").realpath
    brew_bin_pth.install_symlink ba_gui_cmd => "bornagain"

    # script to activate BornAgain's Python virtual environment
    pyenv_script = %Q(\
     #! /bin/sh

     # activate BornAgain's Python virtual environment
     source #{venv_root}/bin/activate
     export PYTHONPATH="#{ba_loc}:${PYTHONPATH}"

     # usage:
     # $ source #{brew_bin_pth}/bornagain_py"
    )

    ba_pyenv_cmd = "#{bin}/bornagain_py.sh"

    File.write(ba_pyenv_cmd, pyenv_script, mode: "w")
    chmod 0755, ba_pyenv_cmd
    brew_bin_pth.install_symlink ba_pyenv_cmd => "bornagain_py"

    puts ".: Building BornAgain virtual environment: Done."

    # script to display info about BornAgain
    ba_share = "#{share}/BornAgain"
    info_script = %Q(\
     #! /bin/sh

     echo '-------------- BornAgain ---------------'
     echo '* BornAgain #{version}: #{prefix}/'
     echo '* Python wheel: #{ba_share}/wheel/'
     echo '* Python examples: #{ba_share}/Examples/'
     echo '* Build log: #{ba_share}/bornagain_build.log'
     echo '----------------------------------------'
     echo '* Run GUI with Python support:'
     echo '$ bornagain'
     echo '----------------------------------------'
     echo "* Activate BornAgain's Python virtual environment"
     echo "  where BornAgain wheel is already installed:"
     echo '$ source #{brew_bin_pth}/bornagain_py'
     echo '----------------------------------------'
    )

    ba_info_cmd = "#{bin}/bornagain_info.sh"
    File.write(ba_info_cmd, info_script, mode: "w")
    chmod 0755, ba_info_cmd
    brew_bin_pth.install_symlink ba_info_cmd => "bornagain_info"

    puts ".: To obtain info about BornAgain, use the shell command:"
    puts "   $ bornagain_info"
  end

  test do
    puts ".: No tests for the BornAgain GUI."
  end
end