| 1 |
#%Module4.1 |
| 2 |
## |
| 3 |
## When loaded, use a local git repo for our Modulefiles, |
| 4 |
## instead of the system-wide location that is typically used. |
| 5 |
## |
| 6 |
## This is useful for editing and testing Modulefiles. |
| 7 |
## |
| 8 |
## Tune to your system: |
| 9 |
## $specialuser, $localmoddir, $globalmodfetch, $globalmodpush |
| 10 |
## |
| 11 |
## Author: Scott Johnson <[email protected]> |
| 12 |
## |
| 13 |
## Originally developed at: |
| 14 |
## github.com/scottj97/environment-modules-in-git |
| 15 |
|
| 16 |
|
| 17 |
eval set [array get env HOME] |
| 18 |
|
| 19 |
# Special Unix username that owns the main Modulefiles repository |
| 20 |
set specialuser modules |
| 21 |
|
| 22 |
# Where each user's local copy should go: |
| 23 |
set localmoddir $HOME/modulefiles |
| 24 |
|
| 25 |
# Where `git fetch` can find the main repository. |
| 26 |
# This must be a filesystem path, since it will also be used by Modules: |
| 27 |
set globalmodfetch /home/modules/modulefiles |
| 28 |
|
| 29 |
# Where `git push` should write to the main repository: |
| 30 |
set globalmodpush ssh://modules@localhost/home/modules/modulefiles |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
proc ModulesHelp {} { |
| 35 |
global localmoddir |
| 36 |
puts stderr "localmodules: switch to local Git repo for Modulefiles\n" |
| 37 |
puts stderr {This is useful for editing and testing Modulefiles.} |
| 38 |
puts stderr {Usage:} |
| 39 |
puts stderr { $ module load localmodules} |
| 40 |
puts stderr "Then edit and test modulefiles in $localmoddir" |
| 41 |
puts stderr {When complete, git commit and push, then} |
| 42 |
puts stderr { $ module unload localmodules} |
| 43 |
} |
| 44 |
|
| 45 |
module-whatis {switch MODULEPATH to local git repo} |
| 46 |
|
| 47 |
# Runs `system` and dies if error code is nonzero |
| 48 |
proc safeSystem cmd { |
| 49 |
set retcode [system $cmd] |
| 50 |
if {[expr {$retcode != 0}]} { |
| 51 |
error "`$cmd` returned non-zero exit code: $retcode" |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
# Make sure $localmoddir is what we expect, so we don't clobber |
| 56 |
# anybody's work if they happen to have something unexpected here |
| 57 |
proc ensureProperLocalmoddir {} { |
| 58 |
global localmoddir |
| 59 |
global globalmodfetch |
| 60 |
global globalmodpush |
| 61 |
|
| 62 |
# Make sure it's a directory |
| 63 |
if {![file isdirectory $localmoddir]} { |
| 64 |
error "a file named $localmoddir already exists, and\ |
| 65 |
I don't want to clobber it" |
| 66 |
} |
| 67 |
|
| 68 |
# Make sure it has the expected .git remote setup |
| 69 |
if {![file isdirectory [file join $localmoddir .git]]} { |
| 70 |
error "expected git repo inside $localmoddir, found none" |
| 71 |
} |
| 72 |
safeSystem "if \[ `git -C $localmoddir remote get-url origin` !=\ |
| 73 |
\"$globalmodfetch\" ]; then exit 1; fi" |
| 74 |
safeSystem "if \[ `git -C $localmoddir remote get-url --push origin` !=\ |
| 75 |
\"$globalmodpush\" ]; then exit 1; fi" |
| 76 |
} |
| 77 |
|
| 78 |
# No $localmoddir exists, so run `git clone` to create |
| 79 |
proc createLocalmoddir {} { |
| 80 |
global localmoddir |
| 81 |
global globalmodfetch |
| 82 |
global globalmodpush |
| 83 |
|
| 84 |
safeSystem "git clone $globalmodfetch $localmoddir" |
| 85 |
safeSystem "git -C $localmoddir remote set-url --push origin $globalmodpush" |
| 86 |
} |
| 87 |
|
| 88 |
proc checkRepoStatus {} { |
| 89 |
global localmoddir |
| 90 |
safeSystem "git -C $localmoddir remote update" |
| 91 |
safeSystem "git -C $localmoddir status" |
| 92 |
} |
| 93 |
|
| 94 |
# Special modules user not allowed to load or unload this. |
| 95 |
# Why? Because it would defeat the purpose of tracking changes, if the |
| 96 |
# changes were committed by this anonymous special user. |
| 97 |
eval set [array get env USER] |
| 98 |
if {$USER == $specialuser} { |
| 99 |
error "special user $specialuser should not load this module" |
| 100 |
} |
| 101 |
|
| 102 |
# create directory if necessary |
| 103 |
if [module-info mode load] { |
| 104 |
if {![file exists $localmoddir]} { |
| 105 |
createLocalmoddir |
| 106 |
} |
| 107 |
ensureProperLocalmoddir |
| 108 |
} |
| 109 |
|
| 110 |
## These two work for load, but not for unload |
| 111 |
## (the $globalmodfetch doesn't get put back in): |
| 112 |
## remove-path MODULEPATH $globalmodfetch |
| 113 |
## append-path MODULEPATH $localmoddir |
| 114 |
|
| 115 |
## These two work for load, but not for unload |
| 116 |
## (the $globalmodfetch doesn't get put back in): |
| 117 |
## module unuse $globalmodfetch |
| 118 |
## module use --append $localmoddir |
| 119 |
|
| 120 |
## This method assumes that $MODULES_REPO is part |
| 121 |
## of MODULEPATH, e.g. in your modulerc: |
| 122 |
## setenv MODULES_REPO /home/modules/modulefiles |
| 123 |
## module use /\$MODULES_REPO |
| 124 |
|
| 125 |
|
| 126 |
if [module-info mode load] { |
| 127 |
setenv MODULES_REPO $localmoddir |
| 128 |
} |
| 129 |
if [module-info mode unload] { |
| 130 |
# unsetenv gets converted to setenv since we're unloading |
| 131 |
unsetenv MODULES_REPO $globalmodfetch |
| 132 |
} |
| 133 |
|
| 134 |
if [module-info mode load] { |
| 135 |
puts stderr "\nSwitched to local modulefiles in $localmoddir" |
| 136 |
puts stderr {When editing and testing complete, git commit and push, then:} |
| 137 |
puts stderr " $ module unload localmodules\n" |
| 138 |
checkRepoStatus |
| 139 |
} |