Skip to content

Commit 710bb61

Browse files
Merge pull request #180 from lnls-sirius/dev/maint/adapt-install
Fix mamba installation.
2 parents 1de677f + b1efa7e commit 710bb61

File tree

1 file changed

+95
-83
lines changed

1 file changed

+95
-83
lines changed

bin/sirius-script-mamba-env-create.bash

Lines changed: 95 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,17 @@ set -e
77
# Parse arguments.
88
# Adapted from: https://stackabuse.com/how-to-parse-command-line-arguments-in-bash/
99

10-
function help
10+
help()
1111
{
1212
echo "Usage: sirius-script-mamba-env-create.bash
13-
[ -n | --no-clone-repos ] Instead of cloning, find repos in system.
13+
[ -c | --clone-repos ] Instead of finding repos in system, clone them inside environment folder.
14+
[ -f | --clone-folder ] If repos will be cloned, clone them in this folder. Default: ~/repos.
1415
[ -d | --develop ] Install sirius packages in develop mode.
1516
[ --no-sim ] Do not install simulation packages.
1617
[ --no-ioc ] Do not install IOC related packages.
1718
[ --no-ima ] Do not install magnets simulation packages.
1819
[ --no-colleff ] Do not install collective effects packages.
19-
[ --root-lnls-fac ] Root folder for lnls-fac repos. Default: \"/\".
20-
[ --root-lnls-sirius ] Root folder for lnls-sirius repos. Default: \"/\".
21-
[ --root-lnls-ima ] Root folder for lnls-imas repos. Default: \"/\".
20+
[ -r | --root-lnls-repos ] Root folder for lnls repos. Default: \"/\".
2221
[ -b | --branches ] Branches to install. For each package you want to force a branch you must provide:
2322
<package1>:<branch1>,<package2>:<branch2>,...,<packagen>:<branchn>
2423
Please note there is no spaces in the string.
@@ -33,9 +32,9 @@ function help
3332
[ -h | --help ] Print help and exit."
3433
}
3534

36-
SHORT="ndb:e:h"
37-
LONG+="no-clone-repos,develop,no-sim,no-ioc,no-ima,no-colleff,root-lnls-fac:,"
38-
LONG+="root-lnls-sirius:,root-lnls-ima:,branches:,env-name:,help"
35+
SHORT="cf:dr:b:e:h"
36+
LONG+="clone-repos,clone-folder:,develop,no-sim,no-ioc,no-ima,no-colleff,"
37+
LONG+="root-lnls-repos:,branches:,env-name:,help"
3938
OPTS=$(getopt -a -n sirius-script-mamba-env-create.bash \
4039
--options $SHORT --longoptions $LONG -- "$@")
4140

@@ -48,24 +47,27 @@ fi
4847

4948
eval set -- "$OPTS"
5049

51-
CLONE="yes"
50+
CLONE="no"
51+
CLONE_FOL="~/repos"
5252
DEVELOP="no"
5353
INST_SIM="yes"
5454
INST_IOC="yes"
5555
INST_IMA="yes"
5656
INST_COL="yes"
57-
ROOT_SIR="/"
58-
ROOT_FAC="/"
59-
ROOT_IMA="/"
60-
BRANCHES="Radia:lnls-sirius"
57+
ROOT_REP="/"
58+
BRANCHES=""
6159
ENV_NAME="sirius"
6260
# now enjoy the options in order and nicely split until we see --
6361
while true; do
6462
case "$1" in
65-
-n|--no-clone-repos)
66-
CLONE="no"
63+
-c|--clone-repos)
64+
CLONE="yes"
6765
shift
6866
;;
67+
-f|--clone-folder)
68+
CLONE_FOL="$2"
69+
shift 2
70+
;;
6971
-d|--develop)
7072
DEVELOP="yes"
7173
shift
@@ -86,16 +88,8 @@ while true; do
8688
INST_COL="no"
8789
shift
8890
;;
89-
--root-lnls-fac)
90-
ROOT_FAC="$2"
91-
shift 2
92-
;;
93-
--root-lnls-sirius)
94-
ROOT_SIR="$2"
95-
shift 2
96-
;;
97-
--root-lnls-ima)
98-
ROOT_IMA="$2"
91+
-r|--root-lnls-repos)
92+
ROOT_REP="$2"
9993
shift 2
10094
;;
10195
-b|--branches)
@@ -127,34 +121,40 @@ done
127121

128122
##############################################################################
129123
# Define some useful functions
130-
function printf_yellow {
124+
printf_yellow()
125+
{
131126
printf "\e[1;33m$1\e[0m"
132127
}
133-
function printf_yellow_clear {
128+
printf_yellow_clear()
129+
{
134130
printf "\e[0;33m$1\e[0m"
135131
}
136-
function printf_blue {
132+
printf_blue()
133+
{
137134
printf "\e[1;34m$1\e[0m"
138135
}
139-
function printf_green {
136+
printf_green()
137+
{
140138
printf "\e[1;32m$1\e[0m"
141139
}
142-
function printf_red {
140+
printf_red()
141+
{
143142
printf "\e[1;31m$1\e[0m"
144143
}
145-
function _abort {
144+
_abort()
145+
{
146146
printf_red "SIGINT received. Aborting...\n"
147147
exit
148148
}
149149

150150
# Trap SIG INT to abort exectution:
151151
trap _abort SIGINT;
152152

153-
function get_branch
153+
get_branch()
154154
{
155155
for BRAN in "${BRANCHES[@]}"
156156
do
157-
BR=(${BRAN//:/ }) # split string in array in delimiter ":"
157+
local BR=(${BRAN//:/ }) # split string in array in delimiter ":"
158158
if [ "${BR[0]}" == "$1" ]
159159
then
160160
echo ${BR[1]}
@@ -163,14 +163,31 @@ function get_branch
163163
done
164164
}
165165

166+
# This function extracts the organization and repo name from a
167+
# remote.origin.url of a given repository.
168+
# It was done by Microsoft Windows Copilot and works for urls such as:
169+
# - git@github.com:org/repo.git
170+
# - https://gitlab.com/org/repo.git
171+
# - git@bitbucket.org:org/repo.git
172+
# - lnls-sirius/scripts.git
173+
extract_info_git()
174+
{
175+
local url="$1"
176+
local path=$(echo "$url" | sed -E 's|.*[:/]([^/]+/[^/]+)$|\1|')
177+
path=${path%.git}
178+
local org=$(echo "$path" | cut -d'/' -f1)
179+
local repo=$(echo "$path" | cut -d'/' -f2)
180+
echo "$org" "$repo"
181+
}
182+
166183
# takes three input variables: repo name, organization, and repo tag/branch
167-
function clone_or_find
184+
clone_or_find()
168185
{
169186
printf_yellow " - $1\n"
170187
if [ "$CLONE" == "yes" ]
171188
then
172-
printf_yellow_clear "Cloning repo $1 in $CONDA_PREFIX/repos: \n"
173-
cd $CONDA_PREFIX/repos
189+
printf_yellow_clear "Cloning repo $1 in $CLONE_FOL: \n"
190+
cd $CLONE_FOL
174191
if ! [ -d "$1" ]
175192
then
176193
git clone https://github.com/$2/$1.git
@@ -180,29 +197,20 @@ function clone_or_find
180197
fi
181198
cd $1
182199
else
183-
ROO=$ROOT_SIR
184-
if [ $2 == "lnls-fac" ]
185-
then
186-
ROO=$ROOT_FAC
187-
elif [ $2 == "lnls-ima" ]
188-
then
189-
ROO=$ROOT_IMA
190-
fi
191-
192-
printf_yellow_clear "Searching repo $1 in $ROO: "
193-
VAR="$(find $ROO -path */$1 2>/dev/null)"
200+
printf_yellow_clear "Searching repo $1 in $ROOT_REP: "
201+
local PTH=
202+
local VAR="$(find "$ROOT_REP" -path */$1 2>/dev/null)"
194203
VAR=($VAR)
195-
PTH=
196204
for V in "${VAR[@]}"
197205
do
198206
cd $V
199207
if git rev-parse --is-inside-work-tree 1>/dev/null 2>&1
200208
then
201-
PH=`git rev-parse --show-toplevel`
202-
NAME="$(basename $PH)"
203-
if [ "$NAME" == "$1" ]
209+
local NAME=$(git config --get remote.origin.url)
210+
NAME=$(extract_info_git "$NAME")
211+
if [ "$NAME" == "$2 $1" ]
204212
then
205-
PTH="$PH"
213+
PTH=$(git rev-parse --show-toplevel)
206214
break
207215
fi
208216
fi
@@ -217,7 +225,7 @@ function clone_or_find
217225
return 1
218226
fi
219227
fi
220-
BRAN="$(get_branch $1)"
228+
local BRAN="$(get_branch $1)"
221229
if ! [ "$BRAN" ]
222230
then
223231
BRAN="$(get_branch all)"
@@ -227,7 +235,6 @@ function clone_or_find
227235
printf_yellow_clear "Checking out to branch $BRAN.\n"
228236
git checkout $BRAN
229237
fi
230-
# wont be nice to fetch and pull the latest updates of the branch? Instead of only "checkouting" it.
231238
}
232239

233240
##############################################################################
@@ -281,9 +288,10 @@ printf_yellow_clear "Install mamba in path /opt/mamba_files/mamba: "
281288
cd /opt/mamba_files
282289
if ! [ -d mamba ]
283290
then
284-
wget https://github.com/conda-forge/miniforge/releases/latest/download/Mambaforge-Linux-x86_64.sh
285-
sh Mambaforge-Linux-x86_64.sh -b -p /opt/mamba_files/mamba
286-
rm Mambaforge-Linux-x86_64.sh
291+
fname="Miniforge3-Linux-x86_64.sh"
292+
wget https://github.com/conda-forge/miniforge/releases/latest/download/$fname
293+
sh $fname -b -p /opt/mamba_files/mamba
294+
rm $fname
287295
printf_green "done!\n"
288296
else
289297
printf_blue "there already is a mamba installation. Skipping...\n"
@@ -296,22 +304,21 @@ sudo chown -R $USER:mamba ~/.conda
296304

297305
printf_yellow_clear "Adding mamba and conda to path\n"
298306
source /opt/mamba_files/mamba/etc/profile.d/conda.sh
299-
source /opt/mamba_files/mamba/etc/profile.d/mamba.sh
307+
export MAMBA_ROOT_PREFIX='/opt/mamba_files/mamba'
308+
eval "$(mamba shell hook --shell bash)"
300309

301310
printf_yellow_clear "Adding mamba and conda paths to .bashrc: "
302311
cd ~/
303-
if ! grep -q "MAMBA_ADD" .bashrc;
312+
if ! grep -q "CONDA_ADD" .bashrc;
304313
then
305314
cat >> ~/.bashrc <<'EOM'
306315
# add conda and mamba to path
307-
MAMBA_ADD=/opt/mamba_files/mamba/etc/profile.d/mamba.sh
308-
if [ -f "$MAMBA_ADD" ] ; then
309-
source "$MAMBA_ADD"
310-
fi
311316
CONDA_ADD=/opt/mamba_files/mamba/etc/profile.d/conda.sh
312317
if [ -f "$CONDA_ADD" ] ; then
313318
source "$CONDA_ADD"
314319
fi
320+
export MAMBA_ROOT_PREFIX='/opt/mamba_files/mamba'
321+
eval "$(mamba shell hook --shell bash)"
315322
EOM
316323
printf_green "done!\n"
317324
else
@@ -347,18 +354,20 @@ printf_yellow "Install some mamba packages in $ENV_NAME environment.\n"
347354
COMM="mamba install --freeze-installed -y"
348355

349356
printf_yellow_clear "- System and generic python packages:\n"
350-
$COMM gxx make binutils swig=4.2.0 libxcrypt build gsl libblas wmctrl fftw \
357+
pip install build
358+
# No not use freeze installed here, since it's the first time it is called.
359+
mamba install -y gxx make binutils swig=4.2.0 libxcrypt gsl libblas wmctrl fftw \
351360
pyparsing bottleneck aiohttp==3.7.4 numpy=1.23 scipy matplotlib \
352361
pytest mpmath entrypoints requests pyqt=5.12.3 pandas pyqtgraph=0.11.0 \
353362
qtpy=2.3.1 QtAwesome=0.7.2 numexpr tk sh pywavelets scikit-image \
354363
scikit-learn pydocstyle pycodestyle pylama openpyxl gpy gpyopt fpdf sympy \
355-
h5py scienceplots
364+
h5py scienceplots seaborn
356365

357366
printf_yellow_clear "- Install EPICS Base:\n"
358367
$COMM -c conda-forge/label/cf202003 epics-base=3.15.6
359368

360369
printf_yellow_clear "- And some other EPICS packages:\n"
361-
$COMM pyepics=3.5.0 pcaspy==0.7.3 pydm=1.10.3 timechart=1.2.3
370+
$COMM pyepics=3.5.7 pcaspy==0.7.3 pydm=1.10.3 timechart=1.2.3
362371
# remove the activate and deactivate files created by pyepics and pydm:
363372
cd $CONDA_PREFIX/etc/conda/activate.d
364373
if [ -f "pydm.bat" ]
@@ -379,10 +388,10 @@ mamba update jupyter_client
379388
if [ "$CLONE" == "yes" ]
380389
then
381390
printf_yellow "Clone and install our applications.\n"
382-
printf_yellow_clear "Creating folder $CONDA_PREFIX/repos: "
383-
if ! [ -d $CONDA_PREFIX/repos ]
391+
printf_yellow_clear "Creating folder $CLONE_FOL: "
392+
if ! [ -d $CLONE_FOL ]
384393
then
385-
mkdir $CONDA_PREFIX/repos
394+
mkdir $CLONE_FOL
386395
printf_green "done!\n"
387396
else
388397
printf_blue "already exists. Skipping...\n"
@@ -437,15 +446,19 @@ then
437446
clone_or_find fieldmaptrack lnls-fac && make $TARGET
438447
clone_or_find Radia lnls-sirius && make install 2>/dev/null
439448
clone_or_find idanalysis lnls-fac && make $TARGET
440-
clone_or_find insertion-devices lnls-ima && make $TARGET
449+
clone_or_find insertion-devices lnls-ids && make $TARGET
441450
fi
442451

452+
printf_yellow_clear "Deactivate conda enviroment\n"
453+
CONDA_PREF=$CONDA_PREFIX
454+
mamba deactivate
455+
443456
printf_yellow "Add enviroment variables to conda environment\n"
444457

445-
#### Cria arquivo para configurar ativação do ambiente
446-
cat > $CONDA_PREFIX/etc/conda/activate.d/sirius_env.sh <<'EOM'
458+
#### Create file to configure environment activation
459+
cat > $CONDA_PREF/etc/conda/activate.d/sirius_env.sh <<'EOM'
447460
# Define function to set variable and save previous state
448-
function defvar ()
461+
defvar()
449462
{
450463
tmp="${1}"
451464
if ! [ -z "${!tmp+x}" ]
@@ -487,13 +500,12 @@ defvar "PYQTDESIGNERPATH" "$CONDA_PREFIX/repos/hla/pyqt-apps"
487500
# Aliases
488501
# ======="
489502
alias g-conda="cd $CONDA_PREFIX"
490-
alias g-conda-repos="cd $CONDA_PREFIX/repos"
491503
EOM
492504

493505
#### Cria arquivo para configurar desativação do ambiente
494-
cat > $CONDA_PREFIX/etc/conda/deactivate.d/sirius_env.sh <<'EOM'
506+
cat > $CONDA_PREF/etc/conda/deactivate.d/sirius_env.sh <<'EOM'
495507
# Define function to unset variable with previous state
496-
function undefvar ()
508+
undefvar()
497509
{
498510
tmp="${1}"
499511
old="${tmp}_OLD"
@@ -538,11 +550,9 @@ undefvar "PYQTDESIGNERPATH"
538550
# Aliases
539551
# =======
540552
unalias g-conda
541-
unalias g-conda-repos
542553
EOM
543554

544-
printf_yellow_clear "Deactivate conda enviroment\n"
545-
mamba deactivate
555+
unset CONDA_PREF
546556

547557
##############################################################################
548558
printf_yellow "Fix permissions of some files\n"
@@ -557,28 +567,30 @@ printf_yellow "Create scripts to access apps in conda environment\n"
557567

558568
cd /usr/local/bin
559569
printf_yellow_clear " - jupyter-mamba-${ENV_NAME} \n"
560-
sudo tee jupyter-mamba-${ENV_NAME} >/dev/null <<'EOM'
570+
sudo tee jupyter-mamba-${ENV_NAME} >/dev/null <<EOM
561571
#!/bin/bash
562572
bash -c "source /opt/mamba_files/mamba/etc/profile.d/conda.sh && conda activate ${ENV_NAME} && jupyter notebook"
563573
EOM
564574

565575
printf_yellow_clear " - ipython-mamba-${ENV_NAME} \n"
566-
sudo tee ipython-mamba-${ENV_NAME} >/dev/null <<'EOM'
576+
sudo tee ipython-mamba-${ENV_NAME} >/dev/null <<EOM
567577
#!/bin/bash
568578
bash -c "source /opt/mamba_files/mamba/etc/profile.d/conda.sh && conda activate ${ENV_NAME} && ipython"
569579
EOM
570580

571581
printf_yellow_clear " - designer-mamba-${ENV_NAME} \n"
572-
sudo tee designer-mamba-${ENV_NAME} >/dev/null <<'EOM'
582+
sudo tee designer-mamba-${ENV_NAME} >/dev/null <<EOM
573583
#!/bin/bash
574584
bash -c "source /opt/mamba_files/mamba/etc/profile.d/conda.sh && conda activate ${ENV_NAME} && designer"
575585
EOM
576586

577587
printf_yellow_clear " - sirius-hla-as-ap-launcher-mamba-${ENV_NAME} \n"
578-
sudo tee sirius-hla-as-ap-launcher-mamba-${ENV_NAME} >/dev/null <<'EOM'
588+
sudo tee sirius-hla-as-ap-launcher-mamba-${ENV_NAME} >/dev/null <<EOM
579589
#!/bin/bash
580590
bash -c "source /opt/mamba_files/mamba/etc/profile.d/conda.sh && conda activate ${ENV_NAME} && sirius-hla-as-ap-launcher.py"
581591
EOM
582592

583593
sudo chmod +x jupyter-mamba-${ENV_NAME} ipython-mamba-${ENV_NAME} designer-mamba-${ENV_NAME} \
584594
sirius-hla-as-ap-launcher-mamba-${ENV_NAME}
595+
596+
printf_yellow "Finished!"

0 commit comments

Comments
 (0)