Debian Codegraph @

doc/source/cookbook/expose-procs-vars-to-modulefiles.rst

raw
1 .. _expose-procs-to-modulefiles:
2
3 Expose procedures and variables to modulefiles
4 ==============================================
5
6 Same piece of code may be relevant for multiple modulefiles in your setup.
7 Sharing code (procedures and variables) is preferred to redefining it in each
8 modulefile. The following recipe describes an efficient way to define Tcl
9 procedures and variables that will be then available from the modulefile
10 evaluation context.
11
12 Implementation
13 --------------
14
15 To expose site-specific procedures and variables across all modulefiles during
16 their evaluation a site-specific configuration script is proposed.
17
18 This script exposes registered procedures to the Tcl interpreters that
19 evaluate either modulerc or modulefile scripts.
20
21 By using the ``env`` global array, which holds environment variables,
22 variables could also be exposed to those Tcl interpreters. Variables exposed
23 this way, will be found set within modulefile or modulerc evaluation context
24 but will not be exported to the environment changes the ``modulecmd.tcl``
25 script produces.
26
27 .. literalinclude:: ../../example/expose-procs-vars-to-modulefiles/siteconfig.tcl
28 :caption: siteconfig.tcl
29 :lines: 13-48
30
31 **Compatible with Modules v4.2+**
32
33 Installation
34 ------------
35
36 Create site-specific configuration directory if it does not exist yet:
37
38 .. parsed-literal::
39
40 $ mkdir \ |etcdir|
41
42 Then copy there the site-specific configuration script of this recipe:
43
44 .. parsed-literal::
45
46 $ cp example/expose-procs-vars-to-modulefiles/siteconfig.tcl \ |etcdir|\ /
47
48 .. note::
49
50 Defined location for the site-specific configuration script may vary from
51 one installation to another. To determine the expected location for this
52 file on your setup, check the value of the ``siteconfig`` option on Modules
53 version 4.3 or above::
54
55 $ module config siteconfig
56
57 On older version of Modules, check the ``modulecmd.tcl`` script::
58
59 $ grep '^set g_siteconfig ' $MODULES_CMD
60
61 The configuration script proposed should then be adapted to your needs:
62
63 * define your own procedures
64 * register them into the ``g_siteProcsToExposeToItrp`` list variable to expose
65 them to the modulefile and modulerc evaluation contexts
66 * define your own variables using the ``env`` array variable
67
68
69 Usage example
70 -------------
71
72 Enable the modulepath where the example modulefiles are located::
73
74 $ module use example/expose-procs-vars-to-modulefiles/modulefiles
75
76 Display one of the example modulefiles that makes use of the site-specific
77 procedure and variable defined in the ``siteconfig.tcl`` script::
78
79 $ module show foo
80 -------------------------------------------------------------------
81 .../modulefiles/foo/1:
82
83 setenv FOO1 sitevalue
84 setenv FOO2 sitevarvalue
85 -------------------------------------------------------------------
86
87 Load one example modulefile and check that the environment variable it
88 defines, which rely on the site-specific procedure and variable, are well
89 set::
90
91 $ module load bar
92 $ echo $BAR1
93 sitevalue
94 $ echo $BAR2
95 sitevarvalue
96
97 .. vim:set tabstop=2 shiftwidth=2 expandtab autoindent: