/
collections-management.in
executable file
·1033 lines (973 loc) · 32.9 KB
/
collections-management.in
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/sh
#
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the Common Development
# and Distribution License Version 1.0 (the "License").
#
# You can obtain a copy of the license at
# http://www.opensource.org/licenses/CDDL-1.0. See the License for the
# specific language governing permissions and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each file and
# include the License file in a prominent location with the name LICENSE.CDDL.
# If applicable, add the following below this CDDL HEADER, with the fields
# enclosed by brackets "[]" replaced with your own identifying information:
#
# Portions Copyright (c) [yyyy] [name of copyright owner]. All rights reserved.
#
# CDDL HEADER END
#
#
# Copyright (c) 2017--2018, Regents of the University of Minnesota.
# All rights reserved.
#
# Contributors:
# Ryan S. Elliott
# John S. Spear
#
#
# Release: This file is part of the kim-api.git repository.
#
collections_info=@CMAKE_INSTALL_FULL_LIBEXECDIR@/@PROJECT_NAME@/@PROJECT_NAME@-collections-info
major_version=@PROJECT_MAJOR_VERSION@
make_command="make"
cmake_command="cmake"
# define usage function
usage () {
local command="`printf $0 | sed 's|.*/\([^/][^/]*\)/*|\1|'`"
# Follows docopt.org format
printf "Usage:\n"
printf " ${command} list [--with-version] [--log]\n"
printf " ${command} set-user-models-dir <directory>\n"
printf " ${command} set-user-drivers-dir <directory>\n"
printf " ${command} install [--force]\n"
printf " (CWD | environment | user | system [--sudo])\n"
printf " (<openkim-item-id>... | <local-item-path>... | OpenKIM |\n"
printf " OpenKIM_with_history)\n"
printf " ${command} reinstall [--interacitive] [--sudo]\n"
printf " (<openkim-item-id> | <local-item-path>)...\n"
printf " ${command} remove [--interactive] [--sudo] <item-id>...\n"
printf " ${command} remove-all [--interactive] [--sudo]\n"
printf "\n\n"
printf "list:\n"
printf " List installed kim-api models and model drivers\n"
printf " * '--with-version' print the kim-api version used to compiled the item.\n"
printf " * '--log' generate a kim.log file. This will show any errors that\n"
printf " occur as the utility searches for installed items.\n"
printf "\n"
printf "set-user-models-dir:\n"
printf " Rewrite configuration file with provided directory\n"
printf "\n"
printf "set-user-drivers-dir:\n"
printf " Rewrite configuration file with provided directory\n"
printf "\n"
printf "install:\n"
printf " Install model and/or model driver from openkim.org or from a local path\n"
printf " * Installing to the environment collection places items in the first\n"
printf " directory of the list.)\n"
printf " * 'install --force' is essentially equivalent to 'reinstall', but works\n"
printf " even when the item is not currently installed.\n"
printf " * 'OpenKIM' installs all 'current' items found at openkim.org,\n"
printf " whereas 'OpenKIM_with_history' installs all 'current'\n"
printf " and all 'old' items.\n"
printf "\n"
printf "reinstall:\n"
printf " Remove and reinstall items.\n"
printf " WARNING: This will remove and reinstall the entire directory of files\n"
printf " associated with the item.\n"
printf "\n"
printf "remove:\n"
printf " Remove model or model driver.\n"
printf " WARNING: This will remove the entire directory of files associated\n"
printf " with the item.\n"
printf "\n"
printf "remove-all:\n"
printf " Remove all items from all collections.\n"
printf " WARNING: This will remove the entire directory of files associated\n"
printf " with all items.\n"
}
check_version_compatibility () {
local version="$1"
local major="`printf -- "${version}" | sed -e 's/\([^.}]*\).*/\1/'`"
local minor="`printf -- "${version}" | sed -e 's/[^.]*\.\([^.:]*\).*/\1/'`"
if test \( ${major} -eq ${major_version} \) ; then
return 0
else
return 1
fi
}
check_item_compatibility () {
local item_name="$1"
local query="query={\"kimcode\":\"${item_name}\"}"
query="${query}"'&fields={"kim-api-version":1}'
query="${query}"'&database=obj&history=on'
local version
version="`wget -q -O - --post-data="${query}" https://query.openkim.org/api`"
if test $? -ne 0; then
printf "*** ERROR *** Unable to successfully contact openkim.org to check ${item_name} compatibility.\n"
return 1
fi
version="`printf -- "${version}" | \
sed -e 's/\[//g' -e 's/\]//g' \
-e 's/{"kim-api-version": "\([0-9.]*\)"}/\1/g'`"
if test x"" = x"${version}"; then
printf "*** ERROR *** ${item_name} not found at openkim.org.\n"
return 1
else
if check_version_compatibility "${version}"; then
return 0
else
printf "*** ERROR *** ${item_name} found at openkim.org is not compatible with this version of the KIM API.\n"
return 1
fi
fi
}
check_for_local_build () {
local name="$1"
if test -d "${name}"; then
local_build_path=`cd "${name}" && pwd`
return 0
else
local_build_path=""
return 1
fi
# output is: local_build_path
}
get_local_build_item_name () {
local build_path="$1"
if test \! -d "${build_path}"; then
printf "Item path '${build_path}' does not exist.\n"
return 1
fi
# create private temporary directory
if test x"" = x"${TMPDIR}"; then TMPDIR="/tmp"; fi
local build_dir="`mktemp -d "${TMPDIR}/@PROJECT_NAME@-build-XXXXXXXXXX"`"
if test $? -ne 0; then
printf "Unable to create temporary directory.\n"
return 1;
fi
( # subshell
cd "${build_dir}" || return 1
cp -r "${build_path}" "./item-source"
cd item-source && mkdir build && cd build
if ! (${cmake_command} ../ > ./cmake-log.txt 2>&1); then
cat ./cmake-log.txt
return 1
fi
local item_type="`grep -- "item-type:" item-info.txt | sed -e 's/^item-type://'`"
case "${item_type}" in
STAND_ALONE_MODEL|PARAMETERIZED_MODEL|SIMULATOR_MODEL)
printf -- "`grep -- "item-name:" item-info.txt | sed -e 's/^item-name:"\(.*\)"/\1/'`" \
> "${build_dir}/item_name"
;;
MODEL_DRIVER)
printf -- "`grep -- "item-name:" item-info.txt | sed -e 's/^item-name:"\(.*\)"/\1/'`" \
> "${build_dir}/item_name"
;;
*)
printf -- "" > "${build_dir}/item_name"
printf "Item type '${item_type}' unknown.\n"
error="true"
;;
esac
if test x"true" = x"${error}"; then
return 1
else
return 0
fi
) || return 1 # exit subshell
item_name=`cat ${build_dir}/item_name`
rm -rf "${build_dir}" || return 1
return 0
# output is: item_name
}
check_config_file () {
local config_file_name drivers_dir models_dir
config_file_name="`${collections_info} config_file name`"; if test $? -ne 0; then return 1; fi
drivers_dir="`${collections_info} config_file model_drivers`"; if test $? -ne 0; then return 1; fi
models_dir="`${collections_info} config_file models`"; if test $? -ne 0; then return 1; fi
if test \! -f "${config_file_name}" -o x"" = x"${drivers_dir}" -o x"" = x"${models_dir}"; then
printf "Invalid kim-api configuration file.\n"
return 1
fi
}
rewrite_config_file_models_dir () {
if test -d "$1"; then
local config_file_name drivers_dir models_dir
config_file_name="`${collections_info} config_file name`"; if test $? -ne 0; then return 1; fi
drivers_dir="`${collections_info} config_file model_drivers`"; if test $? -ne 0; then return 1; fi
models_dir="`cd "$1" && pwd`"; if test $? -ne 0; then return 1; fi
printf "model_drivers_dir = %s\n" "${drivers_dir}" > "${config_file_name}" || return 1
printf "models_dir = %s\n" "${models_dir}" >> "${config_file_name}"
else
printf "Directory '%s' does not exist.\n" "$1"
return 1
fi
}
rewrite_config_file_drivers_dir () {
if test -d "$1"; then
local config_file_name drivers_dir models_dir
config_file_name="`${collections_info} config_file name`"; if test $? -ne 0; then return 1; fi
drivers_dir="`cd "$1" && pwd`"; if test $? -ne 0; then return 1; fi
models_dir="`${collections_info} config_file models`"; if test $? -ne 0; then return 1; fi
printf "model_drivers_dir = %s\n" "${drivers_dir}" > "${config_file_name}" || return 1
printf "models_dir = %s\n" "${models_dir}" >> "${config_file_name}"
else
printf "Directory '%s' does not exist." "$1"
return 1
fi
}
get_build_install_item () {
local install_collection="$1"
local item_name="$2"
local force="$3"
local use_sudo="$4"
local PASSWORD="$5"
local found_item=""
local item_type=""
local local_build_path=""
# check for non-empty item_name
if test x"" = x"${item_name}"; then
printf "Empty item id.\n"
return 1
fi
# make changes for installing to CWD collection, if necessary
if test x"${install_collection}" = x"CWD"; then
export KIM_API_MODEL_DRIVERS_DIR=${PWD}
export KIM_API_MODELS_DIR=${PWD}
install_collection="environment"
fi
# check for local build and get item name
if check_for_local_build "${item_name}"; then # sets local_build_path
if ! get_local_build_item_name "${local_build_path}"; then # sets item_name
# error message already printed
return 1
else
printf "Found local item named: ${item_name}.\n"
printf "In source directory: ${local_build_path}.\n"
printf " (If you are trying to install an item from openkim.org\n"
printf " rerun this command from a different working directory,\n"
printf " or rename the source directory mentioned above.)\n"
printf "\n"
fi
fi
# check for existing item
if test x"OpenKIM" = x"${item_name}"; then
found_item=""
item_type="OpenKIM"
elif test x"OpenKIM_with_history" = x"${item_name}"; then
found_item=""
item_type="OpenKIM_with_history"
else
found_item="`${collections_info} model_drivers find "${item_name}"`"; if test $? -ne 0; then return 1; fi
found_item="${found_item}""`${collections_info} models find "${item_name}"`"; if test $? -ne 0; then return 1; fi
fi
if test x"" != x"${found_item}"; then
local item_collection="`printf -- "${found_item}" | sed -e 's/ .*//'`"
printf "Item '${item_name}' already installed in collection '${item_collection}'.\n"
if test x"${force}" = x"yes"; then
if ! remove_item "${item_name}" "${use_sudo}" "${PASSWORD}" "interactive-no"; then
return 1
fi
else
if test x"${item_collection}" = x"${install_collection}"; then
return 0
else
return 1
fi
fi
fi
# create private temporary directory
if test x"" = x"${TMPDIR}"; then TMPDIR="/tmp"; fi
local build_dir="`mktemp -d "${TMPDIR}/@PROJECT_NAME@-build-XXXXXXXXXX"`"
if test $? -ne 0; then
printf "Unable to create temporary directory.\n"
return 1;
fi
( # subshell
cd "${build_dir}" || return 1
# download item (and possibly its driver)
if test x"OpenKIM" = x"${item_type}" -o x"OpenKIM_with_history" = x"${item_type}"; then
local query='query={"type":"mo","kim-api-version":{"$regex":"^'"${major_version}"'\\."}}'
query="${query}"'&fields={"kimcode":1, "kim-api-version":1}'
query="${query}"'&database=obj'
if test x"OpenKIM_with_history" = x"${item_type}"; then
query="${query}"'&history=on'
fi
local list
list="`wget -q -O - --post-data="${query}" https://query.openkim.org/api`"
if test $? -ne 0; then # wget failed
printf "*** ERROR *** Unable to successfully contact openkim.org to download models.\n"
return 1
fi
list="`printf -- "${list}" | \
sed -e 's/\[//g' -e 's/\]//g' \
-e 's/{"kim-api-version": "\([0-9.]*\)", "kimcode": "\([^"]*\)"},*/\1:\2/g'`"
for version in ${list}; do \
if check_version_compatibility "${version}"; then
local modname="`printf -- ${version} | sed -e s/[^:]*://`"
get_build_install_item "$install_collection" "${modname}" "${force}" "${use_sudo}" "${PASSWORD}" || return 1
fi
done
else
if test x"" = x"${local_build_path}"; then
if check_item_compatibility "${item_name}"; then
printf "Downloading..............#%s\n" "${item_name}" | sed -e 's/ /./g' -e 's/#/ /g'
if wget -q --content-disposition "https://openkim.org/download/${item_name}.txz"; then
tar Jxf "${item_name}.txz" 2>&1 | sed -e 's/^/ /' &&
rm -f "${item_name}.txz"
else
printf " Unable to download ${item_name} from https://openkim.org. Check the KIM Item ID for errors.\n"
return 1
fi
else
return 1
fi
else
cp -r "${local_build_path}" "./${item_name}"
fi
cd "./${item_name}" && mkdir build && cd build
if ! ${cmake_command} ../ -DKIM_API_INSTALL_COLLECTION="`printf ${install_collection} | tr "[:lower:]" "[:upper:]"`" > ./cmake-log.txt 2>&1; then
cat ./cmake-log.txt
return 1
fi
item_type="`grep "item-type:" item-info.txt | sed -e 's/item-type://'`"
if test x"PARAMETERIZED_MODEL" = x"${item_type}"; then
dvr="`grep "item-driver-name:" item-info.txt | sed -e 's/item-driver-name:"\(.*\)"/\1/'`"
if test x"" != x"`${collections_info} model_drivers find "${dvr}"`"; then
printf "Found#installed#driver...#%s\n" "${dvr}" | sed -e 's/ /./g' -e 's/#/ /g' || return 1
else
printf "This item needs its driver '${dvr}', but it is not currently installed.\n"
printf "Trying to find it at openkim.org.\n"
# try openkim.org
get_build_install_item "${install_collection}" "${dvr}" "${force}" "${use_sudo}" "${PASSWORD}" || \
printf "*** WARNING *** Model Driver for this item not currently installed in any of the KIM API collections.\n%s\n\n" \
" Continuing with installation of the item anyway."
fi
if ! ((${make_command} clean && ${make_command} && ${make_command} CTEST_OUTPUT_ON_FAILURE=1 test) > ./make-log.txt 2>&1 && \
if test x"sudo-yes" = x"${use_sudo}"; then
printf -- "${PASSWORD}\n" | sudo -k -S ${make_command} install 2> /dev/null
else
${make_command} install
fi); then
cat ./make-log.txt
return 1
fi
elif test x"STAND_ALONE_MODEL" = x"${item_type}" -o \
x"MODEL_DRIVER" = x"${item_type}" -o \
x"SIMULATOR_MODEL" = x"${item_type}"; then
if ! ((${make_command} clean && ${make_command} && ${make_command} CTEST_OUTPUT_ON_FAILURE=1 test) > ./make-log.txt 2>&1 && \
if test x"sudo-yes" = x"${use_sudo}"; then
printf -- "${PASSWORD}\n" | sudo -k -S ${make_command} install 2> /dev/null
else
${make_command} install
fi); then
cat ./make-log.txt
return 1
fi
else
printf "Item '${item_name}' of unknown type '${item_type}'.\n"
return 1
fi
fi
) || return 1 # exit subshell
rm -rf "${build_dir}" || return 1
}
remove_item () {
local item_name="$1"
local use_sudo="$2"
local PASSWORD="$3"
local interactive="$4"
local found_item=""
local item_type=""
local confirmed=1
# check for existing item
found_item="`${collections_info} model_drivers find "${item_name}"`"
if test x"" = x"${found_item}"; then
found_item="`${collections_info} models find "${item_name}"`"
if test x"" = x"${found_item}"; then
printf "Item '${item_name}' not installed.\n"
return 1
else
item_type="models"
fi
else
item_type="model_drivers"
fi
local item_dir
item_dir="`${collections_info} "${item_type}" find "${item_name}" | sed -e 's/^[^ ]* [^ ]* \([^ ]*\).*/\1/'`""/${item_name}"; if test $? -ne 0; then return 1; fi
printf "Removing '%s'. " "${item_dir}"
if test x"${interactive}" = x"interactive"; then
printf "Continue? "
if get_confirmation; then
confirmed=0
fi
else
confirmed=0
fi
if test 0 -eq ${confirmed}; then
if test x"sudo-yes" = x"${use_sudo}"; then
printf -- "${PASSWORD}\n" | sudo -k -S rm -rf "${item_dir}" 2> /dev/null || return 1
else
rm -rf "${item_dir}" || return 1
fi
printf "[done]\n"
else
printf "[skipped]\n"
fi
}
split_drivers_list_into_collections () {
local with_version=$2
drivers_cwd_collection=""; number_drivers_cwd=0
drivers_env_collection=""; number_drivers_env=0
drivers_usr_collection=""; number_drivers_usr=0
drivers_sys_collection=""; number_drivers_sys=0
while read line; do
local collection="`printf -- "$line" | sed -e 's/\([^ ]*\) .*/\1/'`"
local name="`printf -- "$line" | sed -e 's/[^ ]* \([^ ]*\) .*/\1/'`"
local directory="`printf -- "$line" | sed -e 's/[^ ]* [^ ]* \([^ ]*\) .*/\1/'`"
local version="`printf -- "$line" | sed -e 's/[^ ]* [^ ]* [^ ]* \([^ ]*\).*/\1/'`"
case $collection in
"")
# empty do nothing
;;
CWD)
number_drivers_cwd="`expr $number_drivers_cwd \+ 1`"
drivers_cwd_collection="${drivers_cwd_collection}\t${name}"
if test x"${with_version}" = x"yes"; then
drivers_cwd_collection="${drivers_cwd_collection}\n\t\t${version}"
fi
drivers_cwd_collection="${drivers_cwd_collection}\n"
;;
environment)
number_drivers_env="`expr $number_drivers_env \+ 1`"
drivers_env_collection="${drivers_env_collection}\t${name}"
if test x"${with_version}" = x"yes"; then
drivers_env_collection="${drivers_env_collection}\n\t\t${version}"
fi
drivers_env_collection="${drivers_env_collection}\n"
;;
user)
number_drivers_usr="`expr $number_drivers_usr \+ 1`"
drivers_usr_collection="${drivers_usr_collection}\t${name}"
if test x"${with_version}" = x"yes"; then
drivers_usr_collection="${drivers_usr_collection}\n\t\t${version}"
fi
drivers_usr_collection="${drivers_usr_collection}\n"
;;
system)
number_drivers_sys="`expr $number_drivers_sys \+ 1`"
drivers_sys_collection="${drivers_sys_collection}\t${name}"
if test x"${with_version}" = x"yes"; then
drivers_sys_collection="${drivers_sys_collection}\n\t\t${version}"
fi
drivers_sys_collection="${drivers_sys_collection}\n"
;;
*)
printf "Error unknown collection!\n"
exit 1
;;
esac
done <<EOF
$1
EOF
}
split_models_list_into_collections () {
local with_version=$2
models_cwd_collection=""; number_models_cwd=0
models_env_collection=""; number_models_env=0
models_usr_collection=""; number_models_usr=0
models_sys_collection=""; number_models_sys=0
while read line; do
local collection="`printf -- "$line" | sed -e 's/\([^ ]*\) .*/\1/'`"
local name="`printf -- "$line" | sed -e 's/[^ ]* \([^ ]*\) .*/\1/'`"
local directory="`printf -- "$line" | sed -e 's/[^ ]* [^ ]* \([^ ]*\) .*/\1/'`"
local version="`printf -- "$line" | sed -e 's/[^ ]* [^ ]* [^ ]* \([^ ]*\).*/\1/'`"
case $collection in
"")
# empty do nothing
;;
CWD)
number_models_cwd="`expr $number_models_cwd \+ 1`"
models_cwd_collection="${models_cwd_collection}\t${name}"
if test x"${with_version}" = x"yes"; then
models_cwd_collection="${models_cwd_collection}\n\t\t${version}"
fi
models_cwd_collection="${models_cwd_collection}\n"
;;
environment)
number_models_env="`expr $number_models_env \+ 1`"
models_env_collection="${models_env_collection}\t${name}"
if test x"${with_version}" = x"yes"; then
models_env_collection="${models_env_collection}\n\t\t${version}"
fi
models_env_collection="${models_env_collection}\n"
;;
user)
number_models_usr="`expr $number_models_usr \+ 1`"
models_usr_collection="${models_usr_collection}\t${name}"
if test x"${with_version}" = x"yes"; then
models_usr_collection="${models_usr_collection}\n\t\t${version}"
fi
models_usr_collection="${models_usr_collection}\n"
;;
system)
number_models_sys="`expr $number_models_sys \+ 1`"
models_sys_collection="${models_sys_collection}\t${name}"
if test x"${with_version}" = x"yes"; then
models_sys_collection="${models_sys_collection}\n\t\t${version}"
fi
models_sys_collection="${models_sys_collection}\n"
;;
*)
printf "Error unknown collection!\n"
exit 1
;;
esac
done <<EOF
$1
EOF
}
print_separator_line () {
printf "%79s\n" " " | sed -e "s/ /$1/g"
}
print_list_of_drivers_and_models () {
local with_version=$1
local with_log
if test x"$2" = x"yes"; then
with_log="--log"
else
with_log=""
fi
config_env_name="`${collections_info} config_file env | sed -e 's/ .*//'`"; if test $? -ne 0; then return 1; fi
config_env="`${collections_info} config_file env | sed -e 's/^[^ ]* //'`"; if test $? -ne 0; then return 1; fi
if test x"" = x"${config_env}"; then config_env="--empty--"; fi
drivers_env_name="`${collections_info} env env | sed -e 's/^[^ ]* //'`"; if test $? -ne 0; then return 1; fi
drivers_env="`${collections_info} env model_drivers`"; if test $? -ne 0; then return 1; fi
if test x"" = x"${drivers_env}"; then drivers_env="--empty--"; fi
models_env_name="`${collections_info} env env | sed -e 's/ .*//'`"; if test $? -ne 0; then return 1; fi
models_env="`${collections_info} env models`"; if test $? -ne 0; then return 1; fi
if test x"" = x"${models_env}"; then models_env="--empty--"; fi
printf "\n\n"
printf "Knowledgebase of Interatomic Models (KIM)"
printf -- " --- Model Collections Listing\n"
print_separator_line "="
printf "\n"
printf "kim-api library: \n\t%s\n" "`${collections_info} system library`"
printf "\n"
printf "kim-api configuration file:\n\t%s\n" \
"`${collections_info} config_file name`"
printf "\n\n"
printf "Environment Variables:\n"
print_separator_line "-"
printf -- "${config_env_name}:\n"
printf -- "\t${config_env}\n"
printf "\n"
printf -- "${drivers_env_name}:\n"
printf -- "%s\n" "`printf -- "${drivers_env}" | sed -e 's/^/ /g'`"
printf "\n"
printf -- "${models_env_name}:\n"
printf -- "%s\n" "`printf -- "${models_env}" | sed -e 's/^/ /g'`"
printf "\n"
print_separator_line "="
model_drivers_list="`${collections_info} model_drivers ${with_log}`"; if test $? -ne 0; then return 1; fi
split_drivers_list_into_collections "${model_drivers_list}" "${with_version}"
models_list="`${collections_info} models ${with_log}`"; if test $? -ne 0; then return 1; fi
split_models_list_into_collections "${models_list}" "${with_version}"
printf "\n\n\n"
printf "Current Working Directory Collection\n"
print_separator_line "-"
printf "Drivers: %s\n" "${PWD}"
if test $number_drivers_cwd -gt 0; then
printf "${drivers_cwd_collection}"
else
printf "\t--empty--\n"
fi
printf "\n"
printf "Models: %s\n" "${PWD}"
if test $number_models_cwd -gt 0; then
printf -- "${models_cwd_collection}"
else
printf "\t--empty--\n"
fi
printf "\n\n"
printf "Environment Variable Collection\n"
print_separator_line "-"
printf "Drivers: "
if test x"--empty--" = x"${drivers_env}"; then
printf "%s" ${drivers_env}
else
printf "'%s' " ${drivers_env}
fi
printf "\n"
if test $number_drivers_env -gt 0; then
printf -- "${drivers_env_collection}"
else
printf "\t--empty--\n"
fi
printf "\n"
printf "Models: "
if test x"--empty--" = x"${models_env}"; then
printf "%s" ${models_env}
else
printf "'%s' " ${models_env}
fi
printf "\n"
if test $number_models_env -gt 0; then
printf -- "${models_env_collection}"
else
printf "\t--empty--\n"
fi
printf "\n\n"
drivers_usr="`${collections_info} config_file model_drivers`"; if test $? -ne 0; then return 1; fi
if test x"" = x"${drivers_usr}"; then drivers_usr="--empty--"; fi
models_usr="`${collections_info} config_file models`"; if test $? -ne 0; then return 1; fi
if test x"" = x"${models_usr}"; then models_usr="--empty--"; fi
printf "User Collection\n"
print_separator_line "-"
printf "Drivers: %s\n" "${drivers_usr}"
if test $number_drivers_usr -gt 0; then
printf -- "${drivers_usr_collection}"
else
printf "\t--empty--\n"
fi
printf "\n"
printf "Models: %s\n" "${models_usr}"
if test $number_models_usr -gt 0; then
printf -- "${models_usr_collection}"
else
printf "\t--empty--\n"
fi
printf "\n\n"
printf "System Collection\n"
print_separator_line "-"
printf "Drivers: %s\n" "`${collections_info} system model_drivers`"
if test $number_drivers_sys -gt 0; then
printf -- "${drivers_sys_collection}"
else
printf "\t--empty--\n"
fi
printf "\n\n"
printf "Models: %s\n" "`${collections_info} system models`"
if test $number_models_sys -gt 0; then
printf -- "${models_sys_collection}"
else
printf "\t--empty--\n"
fi
printf "\n"
}
get_password () {
printf "Enter Password : "
stty -echo
trap 'stty echo' EXIT
read PASSWORD
stty echo
trap - EXIT
printf "\n"
if ! (printf -- "${PASSWORD}\n" | \
sudo -k -S printf "" > /dev/null 2>&1); then
printf "Bad password.\n"
return 1
fi
}
get_confirmation () {
local ANSWER
printf "[y/n] : "
read ANSWER
if test x"${ANSWER}" = x"y"; then
return 0;
else
return 1;
fi
}
######## main script ########
# check that command is given
if test $# -lt 1; then
usage
exit 1
else
command=$1
case $command in
list|set-user-drivers-dir|set-user-models-dir|install|reinstall|remove|remove-all)
;;
*)
printf "unknown command: %s\n\n" $command
usage
exit 1
esac
fi
if ! check_config_file; then
printf "Aborting!\n"
exit 1
fi
case $command in
list)
if test $# -gt 3; then
usage
exit 1
fi
shift
list_with_version="no"
list_with_log="no"
for arg in "$@"; do
case "$arg" in
--with-version)
list_with_version="yes"
;;
--log)
list_with_log="yes"
;;
*)
printf "unknown option %s\n\n" "$arg"
usage
exit 1
;;
esac
done
print_list_of_drivers_and_models "${list_with_version}" "${list_with_log}"
;;
set-user-models-dir)
if test $# -lt 2; then
usage
exit 1
else
subcommand=$2
if ! rewrite_config_file_models_dir "$subcommand"; then
printf "\nAborting!\n"
exit 1
else
printf "\nSuccess!\n."
fi
fi
;;
set-user-drivers-dir)
if test $# -lt 2; then
usage
exit 1
else
subcommand=$2
if ! rewrite_config_file_drivers_dir "$subcommand"; then
printf "\nAborting!\n"
exit 1
else
printf "\nSuccess!\n"
fi
fi
;;
install)
if test $# -lt 3; then
usage
exit 1
else
force="no"
if test x"$2" = x"--force"; then
force="yes"
shift
fi
subcommand=$2
shift
shift
case $subcommand in
CWD|environment|user)
for item_id in $@; do
if ! get_build_install_item "${subcommand}" "${item_id}" "${force}" "sudo-no" ""; then
printf "\nAborting!\n"
exit 1
else
printf "\nSuccess!\n"
fi
done
;;
system)
PASSWORD=""
if test x"--sudo" = x"$1"; then
shift
use_sudo="sudo-yes"
if ! get_password; then
printf "\nAborting!\n"
exit 1
fi
else
use_sudo="sudo-no"
fi
for item_id in $@; do
if ! get_build_install_item "system" "${item_id}" "${force}" "${use_sudo}" "${PASSWORD}"; then
printf "\nAborting!\n"
exit 1
else
printf "\nSuccess!\n"
fi
done
;;
*)
printf "unknown subcommand: %s\n\n" $subcommand
usage
exit 1
;;
esac
fi
;;
reinstall)
if test $# -lt 2; then
usage
exit 1
else
shift
use_sudo="sudo-no"
use_interactive="interactive-no"
while test x"--" = x"`printf -- "$1" | sed -e 's/^\(..\).*/\1/'`"; do
case $1 in
--interactive)
use_interactive="interactive"
;;
--sudo)
use_sudo="sudo-yes"
if ! get_password; then
printf "\nAborting!\n"
exit 1
fi
;;
*)
printf "unknown option: %s\n\n" $1
usage
exit 1
;;
esac
shift
done
fi
printf "This will remove and reinstall all files associated with these items.\n"
printf " Use the '--interactive' option to request confirmation\n"
printf " before attempting to remove each item.\n"
printf "\n"
printf "Are you sure you want to proceed? "
if get_confirmation; then
for item_id in $@; do
if check_for_local_build "${item_id}"; then # sets local_build_path
if ! get_local_build_item_name "${local_build_path}"; then # sets item_name
printf "\nAborting!\n"
exit 1
fi
else
if ! check_item_compatibility "${item_id}"; then
printf "\nAborting!\n"
exit 1
else
item_name="${item_id}"
fi
fi
found_item="`${collections_info} model_drivers find "${item_name}"` `${collections_info} models find "${item_name}"`"
if test \! x"" = x"${found_item}"; then
item_collection="`printf "${found_item}" | sed -e 's/^[[:space:]]*\([^[:space:]]*\) .*/\1/'`"
if ! (remove_item "${item_name}" "${use_sudo}" "${PASSWORD}" "${use_interactive}" && \
get_build_install_item "${item_collection}" "${item_id}" "${use_sudo}" "${PASSWORD}" "no"); then
printf "\nAborting!\n"
exit 1
fi
else
printf "\nAborting!\n"
exit 1
fi
done
printf "\nSuccess!\n"
fi
;;
remove)
if test $# -lt 2; then
usage
exit 1
else
shift
use_sudo="sudo-no"
use_interactive="interactive-no"
while test x"--" = x"`printf -- "$1" | sed -e 's/^\(..\).*/\1/'`"; do
case $1 in
--interactive)
use_interactive="interactive"
;;
--sudo)
use_sudo="sudo-yes"
if ! get_password; then
printf "\nAborting!\n"
exit 1
fi
;;
*)
printf "unknown option: %s\n\n" $1
usage
exit 1
;;
esac
shift
done
fi
printf "This will remove all files associated with these items.\n"
printf " Use the '--interactive' option to request confirmation\n"
printf " before attempting to remove each item.\n"
printf "\n"
printf "Are you sure you want to proceed? "
if get_confirmation; then
for item_id in $@; do
if ! remove_item "${item_id}" "${use_sudo}" "${PASSWORD}" "${use_interactive}"; then
printf "\nAborting!\n"
exit 1
fi
done
printf "\nSuccess!\n"
else
printf "\nAborting!\n"
fi
;;
remove-all)
shift
use_sudo="sudo-no"
use_interactive="interactive-no"
while test x"--" = x"`printf -- "$1" | sed -e 's/^\(..\).*/\1/'`"; do
case $1 in
--interactive)
use_interactive="interactive"
;;
--sudo)
use_sudo="sudo-yes"
if ! get_password; then
printf "\nAborting!\n"
exit 1
fi
;;
*)
printf "unknown option: %s\n\n" $1