Robert E. Minsk wrote:
> I find that I have several programs that rely on at base set
> of programs. Lets say I have some modules called Common, Maya,
> Composer, Paint. Maya, Composer, and Paint all have the base module
> Common as a prereq. What I would like to do in Common is implement
> some kind of reference counting scheme.
>
> When module Common is loaded the first time it executes all the
> "load" commands and sets it's reference count to 1. Each additional
> time the module Common is loaded only the reference count is
> incremented. When the module Common is unloaded the reference count
> is decremented. When the reference count reaches zero then the
> "unload" commands are executed.
> [snip]
Hello, Robert.
I think I have some code that will work for you, but it won't work
perfectly on the newest version of modules (modules-3.0.6-rko). I'll
get to the gotcha in a minute.
First, create a .version file for your applications as so:
#%Module1.0
##
## The default version of Maya
##
set ModulesVersion "1.0"
if { [info exists env(MAYA_VER)] } {
set ModulesVersion $env(MAYA_VER)
}
Do this for "Paint", "Composer" and "Common" as well, giving them
their own unique environment variables in this file.
Next, have code like the following in Maya:
=======================================================
# Prequisites and conflicts
prereq Common
# Determine the operating system (os) and version (rev)
set os [uname sysname]
set rev [uname release]
# Platform independent stuff
if { ![module-info mode remove] && ![module-info mode switch] && [info
exists env(MAYA_VER)] } {
# If we're doing a "load" and there's already a version of us loaded...
puts stderr "Unloading version $env(MAYA_VER) first...\n"
module unload Maya
}
# Here's where we track which apps are loaded that are using
# the "Common" module
append-path COMCNT maya
# Now see if we're the last to unload; if so, unload Common
if [module-info mode remove] {
if { ![info exists env(COMCNT)] || ![expr [string length $env(COMCNT)] >
0] }
{
module load Common
}
}
# Use MAYA_VER to 1) declare that a version of Maya is loaded,
# and 2) which version is loaded. Must match name in .version!
setenv MAYA_VER 1.0
=======================================================
Add similar sections to "Paint" and "Composer" (all versions). Viola!
You're all set.
The above example appears to works fine even if you "module unload Common"
before "module unload Maya|Paint|Composer". A "module switch Common" will
also not hurt you *unless* you are using a newer version of modules. There's
a bug in how "setenv" statements are handled in switched modules in at least
modules-3.0.6-rko; if you do a "module switch Common Common/<other version>"
then you won't have any env variables defined by "setenv" statements in the
module. In modules-2.2b, this bug is not present and the above examples
work fine. If you're using a version with the "setenv bug", just avoid
switching out "Common" and you'll be fine (or define $COM_VER yourself on
the shell command line after the switch... bleah!).
Also note that I'm checking for a zero-length environment variable in the
code above; strangely enough, while still inside the modulefile the
"append-path" statement gets undone during an unload operation but this
leaves a zero-length env var... Once the module is "fully unloaded", the
env var is gone. So it's a workaround, but appears to work okay.
Lastly, you can just avoid using "module switch" with the version mechanism
above. Just do a "module load" of whatever version you want and the old
version will automatically be removed.
If something doesn't work correctly for you, let me know and I'll either
send you my example modulefiles or remove any undocumented feature. ;-)
Cheers,
- Leo
|