Skip to content

Commit b5b1c40

Browse files
committed
Add module-info modulename and moduleversion modulefile commands
module-info name returns the full name of the evaluating modulefile, like app/1.2.3, whereas modulefile authors frequently need only its version part, to build an installation path for instance. Getting it required Tcl string manipulation like [file tail [module-info name]], an idiom that newcomers find hard to read and that gets copied around with small variations. module-info moduleversion returns the part of the module name after its last / character and module-info modulename the part before it. Both work on the name resolved for the evaluation, so symbolic versions and aliases are handled like module-info name does. A name holding no / character gives an empty version and is returned whole as name part. Fixes #713 Assisted-by: Claude:claude-fable-5-1 Signed-off-by: Xavier Delaruelle <xavier.delaruelle@cea.fr>
1 parent c490376 commit b5b1c40

16 files changed

Lines changed: 299 additions & 11 deletions

File tree

‎.hunspell.en.dic‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1634,3 +1634,7 @@ defineModStartNbProc
16341634
isIcase
16351635
CVE
16361636
backport
1637+
moduleversion
1638+
myModuleFullName
1639+
myModuleName
1640+
myModuleVersion

‎NEWS.rst‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ Modules 5.8.0 (not yet released)
1515
designated by :instopt:`--with-tcl` when :instopt:`--with-tclsh` is not set,
1616
so that installed scripts run with the same Tcl the extension library is
1717
built against. (fix issue #717)
18+
* Add :mfcmd:`module-info modulename<module-info>` and :mfcmd:`module-info
19+
moduleversion<module-info>` modulefile commands to get the name part and
20+
the version part of currently evaluating modulefile. (fix issue #713)
1821

1922

2023
.. _5.7 release notes:

‎doc/source/changes.rst‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,9 @@ The following modulefile Tcl commands appeared on Modules 5.
11481148
+------------+-----------------------------------------------------------------+
11491149
| 5.7 | :mfcmd:`modulepath-ignore` |
11501150
+------------+-----------------------------------------------------------------+
1151+
| 5.8 | :mfcmd:`module-info modulename<module-info>`, |
1152+
| | :mfcmd:`module-info moduleversion<module-info>` |
1153+
+------------+-----------------------------------------------------------------+
11511154

11521155
:mfcmd:`system`, :mfcmd:`is-used`
11531156

‎doc/source/cookbook/module-info-name-return-basename.rst‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ as once loaded this module is identified by its full pathname. This recipe
1111
describes a way to get back the behavior of Modules compatibility version for
1212
the ``module-info name`` modulefile command.
1313

14+
.. note::
15+
16+
Starting Modules 5.8, the :mfcmd:`module-info moduleversion<module-info>`
17+
modulefile command returns the part of the module name after its last
18+
``/`` character, which is the file basename when module is specified as a
19+
full path. It can be used instead of this recipe when the goal is to get
20+
this basename rather than to change the result of :mfcmd:`module-info
21+
name<module-info>`.
22+
1423
Implementation
1524
--------------
1625

‎doc/source/modulefile.rst‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,45 @@ the *modulefile* is being loaded.
875875
``refresh``, ``nonpersist`` (alias of ``refresh``), ``display``, ``help``,
876876
``test``, ``whatis`` or ``scan``.
877877

878+
**module-info modulename**
879+
880+
Return the name of currently evaluating *modulefile* without its version
881+
part, which is the string returned by :mfcmd:`module-info
882+
name<module-info>` without the last ``/`` character and what follows it.
883+
Returns ``app`` for a *modulefile* named ``app/1.2.3`` and ``compiler/gcc``
884+
for ``compiler/gcc/12.1``. Returns the full name if it contains no ``/``
885+
character.
886+
887+
If currently evaluating *modulefile* has been specified as a full path,
888+
:mfcmd:`module-info name<module-info>` returns this full path, so
889+
:mfcmd:`module-info modulename<module-info>` returns the directory part of
890+
this path.
891+
892+
.. only:: html or latex
893+
894+
.. versionadded:: 5.8
895+
896+
**module-info moduleversion**
897+
898+
Return the version of currently evaluating *modulefile*, which is the part
899+
after the last ``/`` character of the string returned by
900+
:mfcmd:`module-info name<module-info>`. Returns ``1.2.3`` for a
901+
*modulefile* named ``app/1.2.3`` and ``12.1`` for ``compiler/gcc/12.1``.
902+
Returns an empty string if the name contains no ``/`` character.
903+
904+
Symbolic versions and aliases are resolved as for :mfcmd:`module-info
905+
name<module-info>`, so the version of the resolved *modulefile* is
906+
returned. Variants are not included in the returned string.
907+
908+
If currently evaluating *modulefile* has been specified as a full path,
909+
:mfcmd:`module-info name<module-info>` returns this full path, so
910+
:mfcmd:`module-info moduleversion<module-info>` returns the file basename of
911+
this path.
912+
913+
.. only:: html or latex
914+
915+
.. versionadded:: 5.8
916+
878917
**module-info name**
879918

880919
Return the name of the *modulefile*. This is not the full pathname for
@@ -886,6 +925,10 @@ the *modulefile* is being loaded.
886925
See :mfcmd:`getvariant` command or :mfvar:`ModuleVariant` array variable to
887926
get defined variant values for currently evaluating *modulefile*.
888927

928+
See :mfcmd:`module-info modulename<module-info>` and :mfcmd:`module-info
929+
moduleversion<module-info>` to get separately the name and version parts of
930+
the returned string.
931+
889932
**module-info shell** [shellname]
890933

891934
Return the current shell under which :file:`modulecmd.tcl` was invoked if

‎doc/source/other-implementations.rst‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,9 @@ table highlights features that are unique to each implementation.
5656
* Handle modulefile outside modulepath
5757
* :ref:`Case insensitive module load<Insensitive case>`
5858
* Automatic ``latest`` and ``loaded`` symbols
59-
* ``alias``, ``command``, ``loaded``, ``tags``, ``usergroups`` and
60-
``username`` sub-commands of :mfcmd:`module-info`
59+
* ``alias``, ``command``, ``loaded``, ``modulename``,
60+
``moduleversion``, ``tags``, ``usergroups`` and ``username``
61+
sub-commands of :mfcmd:`module-info`
6162
* :ref:`Super-sticky modules<Sticky modules>`
6263
* :ref:`Fine-tuned output configuration<--output>`
6364
* :ref:`Editing modulefiles`
@@ -217,6 +218,12 @@ implementation.
217218
- |foo@5.0:7.0|_
218219
* - ``latest("foo")``
219220
- |foo@latest|_
221+
* - ``myModuleFullName()``
222+
- :mfcmd:`module-info name<module-info>`
223+
* - ``myModuleName()``
224+
- :mfcmd:`module-info modulename<module-info>`
225+
* - ``myModuleVersion()``
226+
- :mfcmd:`module-info moduleversion<module-info>`
220227

221228
.. _Irreversible module actions: https://lmod.readthedocs.io/en/latest/370_irreversible.html
222229
.. _NAG file: https://lmod.readthedocs.io/en/latest/140_deprecating_modules.html

‎doc/source/user-guide.rst‎

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,42 @@ This allows users to write:
770770
771771
:ps:`$` module load openmpi %gcc
772772
773+
``module-info``
774+
^^^^^^^^^^^^^^^
775+
776+
The :mfcmd:`module-info` modulefile command provides information about the
777+
current evaluation context, like the name of the module being evaluated or
778+
the evaluation mode.
779+
780+
The :mfcmd:`module-info name<module-info>` sub-command returns the full name
781+
of the evaluating module, like ``app/1.2.3``. Modulefiles often need only
782+
the version part of this name, to build an installation path for instance.
783+
The :mfcmd:`module-info moduleversion<module-info>` sub-command returns this
784+
part, what follows the last ``/`` character of the module name, and the
785+
:mfcmd:`module-info modulename<module-info>` sub-command returns what comes
786+
before this character.
787+
788+
For example:
789+
790+
.. code-block:: tcl
791+
792+
#%Module
793+
794+
set prefix /opt/[module-info modulename]/[module-info moduleversion]
795+
prepend-path PATH $prefix/bin
796+
797+
For a module named ``compiler/gcc/12.1``, :mfcmd:`module-info
798+
modulename<module-info>` returns ``compiler/gcc`` and :mfcmd:`module-info
799+
moduleversion<module-info>` returns ``12.1``. If the module name contains no
800+
``/`` character, the full name is returned as name part and the version part
801+
is an empty string.
802+
803+
.. tip::
804+
805+
Symbolic versions and aliases are resolved before the modulefile is
806+
evaluated, so these sub-commands always report the name of the resolved
807+
modulefile.
808+
773809
Advanced manipulations
774810
----------------------
775811

@@ -1085,9 +1121,7 @@ name being evaluated.
10851121
10861122
#%Module
10871123
1088-
set version [lindex [split [module-info name] /] end]
1089-
1090-
setenv APP_VERSION $version
1124+
setenv APP_VERSION [module-info moduleversion]
10911125
10921126
Module logger
10931127
^^^^^^^^^^^^^

‎share/nagelfar/syntaxdb_modulefile.tcl‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ set {::syntax(module-info alias)} 1
164164
set {::syntax(module-info command)} x?
165165
set {::syntax(module-info loaded)} 1
166166
set {::syntax(module-info mode)} x?
167+
set {::syntax(module-info modulename)} 0
168+
set {::syntax(module-info moduleversion)} 0
167169
set {::syntax(module-info name)} 0
168170
set {::syntax(module-info shell)} x?
169171
set {::syntax(module-info shelltype)} x?
@@ -240,9 +242,9 @@ set ::subCmd(module) {add load add-any load-any swap switch try-add try-load\
240242
initadd initload initclear initlist initprepend initunload initrm\
241243
initswap initswitch list purge reload restore save savelist saverm disable\
242244
saveshow describe apropos keyword search test update whatis}
243-
set ::subCmd(module-info) {alias command loaded mode name shell shelltype\
244-
specified symbols tags type usergroups username version user trace\
245-
tracepat flags}
245+
set ::subCmd(module-info) {alias command loaded mode modulename moduleversion\
246+
name shell shelltype specified symbols tags type usergroups username\
247+
version user trace tracepat flags}
246248
set ::subCmd(uname) {sysname nodename domain release version machine}
247249

248250
# option

‎share/nagelfar/syntaxdb_modulerc.tcl‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ set {::syntax(module-info alias)} 1
6060
set {::syntax(module-info command)} x?
6161
set {::syntax(module-info loaded)} 1
6262
set {::syntax(module-info mode)} x?
63+
set {::syntax(module-info modulename)} 0
64+
set {::syntax(module-info moduleversion)} 0
6365
set {::syntax(module-info name)} 0
6466
set {::syntax(module-info shell)} x?
6567
set {::syntax(module-info shelltype)} x?
@@ -96,9 +98,9 @@ set {::return(module-info flags)} int
9698

9799
# subCmd
98100
set ::subCmd(lsb-release) {id release codename}
99-
set ::subCmd(module-info) {alias command loaded mode name shell shelltype\
100-
specified symbols tags type usergroups username version user trace\
101-
tracepat flags}
101+
set ::subCmd(module-info) {alias command loaded mode modulename moduleversion\
102+
name shell shelltype specified symbols tags type usergroups username\
103+
version user trace tracepat flags}
102104
set ::subCmd(uname) {sysname nodename domain release version machine}
103105

104106
# option

‎tcl/mfcmd.tcl‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,25 @@ proc module-info {what {more {}}} {
6060
name {
6161
return [currentState modulename]
6262
}
63+
modulename {
64+
set modname [currentState modulename]
65+
set idx [string last / $modname]
66+
# whole name is the name part if it has no version separator
67+
if {$idx == -1} {
68+
return $modname
69+
} else {
70+
return [string range $modname 0 $idx-1]
71+
}
72+
}
73+
moduleversion {
74+
set modname [currentState modulename]
75+
set idx [string last / $modname]
76+
if {$idx == -1} {
77+
return {}
78+
} else {
79+
return [string range $modname $idx+1 end]
80+
}
81+
}
6382
specified {
6483
return [currentState specifiedname]
6584
}

0 commit comments

Comments
 (0)