lmod-toolkit 0.0.5__tar.gz → 0.0.7__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. {lmod_toolkit-0.0.5/src/lmod_toolkit.egg-info → lmod_toolkit-0.0.7}/PKG-INFO +4 -2
  2. lmod_toolkit-0.0.7/README.md +35 -0
  3. {lmod_toolkit-0.0.5 → lmod_toolkit-0.0.7}/pyproject.toml +2 -1
  4. {lmod_toolkit-0.0.5 → lmod_toolkit-0.0.7}/setup.sh +68 -24
  5. lmod_toolkit-0.0.7/src/lmod_toolkit/__init__.py +5 -0
  6. lmod_toolkit-0.0.7/src/lmod_toolkit/__main__.py +6 -0
  7. {lmod_toolkit-0.0.5 → lmod_toolkit-0.0.7}/src/lmod_toolkit/_version.py +3 -3
  8. lmod_toolkit-0.0.7/src/lmod_toolkit/bin/lmtk.sh +13 -0
  9. lmod_toolkit-0.0.7/src/lmod_toolkit/cli.py +86 -0
  10. lmod_toolkit-0.0.7/src/lmod_toolkit/config.py +73 -0
  11. lmod_toolkit-0.0.7/src/lmod_toolkit/examples/common.toml.sample +33 -0
  12. lmod_toolkit-0.0.7/src/lmod_toolkit/http.py +62 -0
  13. {lmod_toolkit-0.0.5 → lmod_toolkit-0.0.7}/src/lmod_toolkit/lmod_config.py +17 -1
  14. lmod_toolkit-0.0.7/src/lmod_toolkit/lmod_ext_attrs.py +95 -0
  15. lmod_toolkit-0.0.7/src/lmod_toolkit/lmod_spider.py +75 -0
  16. lmod_toolkit-0.0.7/src/lmod_toolkit/logging_config.py +47 -0
  17. lmod_toolkit-0.0.7/src/lmod_toolkit/metadata.py +61 -0
  18. lmod_toolkit-0.0.7/src/lmod_toolkit/publish.py +95 -0
  19. lmod_toolkit-0.0.7/src/lmod_toolkit/test.py +54 -0
  20. {lmod_toolkit-0.0.5 → lmod_toolkit-0.0.7/src/lmod_toolkit.egg-info}/PKG-INFO +4 -2
  21. {lmod_toolkit-0.0.5 → lmod_toolkit-0.0.7}/src/lmod_toolkit.egg-info/SOURCES.txt +11 -1
  22. lmod_toolkit-0.0.7/src/lmod_toolkit.egg-info/requires.txt +2 -0
  23. {lmod_toolkit-0.0.5 → lmod_toolkit-0.0.7}/src/lmod_toolkit.egg-info/scm_file_list.json +10 -1
  24. lmod_toolkit-0.0.7/src/lmod_toolkit.egg-info/scm_version.json +8 -0
  25. lmod_toolkit-0.0.7/utils/README.md +18 -0
  26. {lmod_toolkit-0.0.5 → lmod_toolkit-0.0.7}/utils/quickstart.sh +10 -9
  27. {lmod_toolkit-0.0.5 → lmod_toolkit-0.0.7}/utils/uv_installer.sh +25 -25
  28. lmod_toolkit-0.0.5/README.md +0 -7
  29. lmod_toolkit-0.0.5/src/lmod_toolkit/__init__.py +0 -1
  30. lmod_toolkit-0.0.5/src/lmod_toolkit/bin/lmtk-test.sh +0 -10
  31. lmod_toolkit-0.0.5/src/lmod_toolkit/lmod_ext_attrs.py +0 -79
  32. lmod_toolkit-0.0.5/src/lmod_toolkit/lmod_spider.py +0 -54
  33. lmod_toolkit-0.0.5/src/lmod_toolkit/test.py +0 -14
  34. lmod_toolkit-0.0.5/src/lmod_toolkit.egg-info/scm_version.json +0 -8
  35. {lmod_toolkit-0.0.5 → lmod_toolkit-0.0.7}/.github/workflows/publish-to-test-pypi.yml +0 -0
  36. {lmod_toolkit-0.0.5 → lmod_toolkit-0.0.7}/.gitignore +0 -0
  37. {lmod_toolkit-0.0.5 → lmod_toolkit-0.0.7}/README.pypi +0 -0
  38. {lmod_toolkit-0.0.5 → lmod_toolkit-0.0.7}/setup.cfg +0 -0
  39. {lmod_toolkit-0.0.5 → lmod_toolkit-0.0.7}/src/lmod_toolkit/build_helper.py +0 -0
  40. {lmod_toolkit-0.0.5 → lmod_toolkit-0.0.7}/src/lmod_toolkit.egg-info/dependency_links.txt +0 -0
  41. {lmod_toolkit-0.0.5 → lmod_toolkit-0.0.7}/src/lmod_toolkit.egg-info/top_level.txt +0 -0
@@ -1,8 +1,8 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: lmod-toolkit
3
- Version: 0.0.5
3
+ Version: 0.0.7
4
4
  Summary: ACCESS Lmod Toolkit
5
- Author-email: Andy Loftus <aloftus@illinois.edu>, Eric Blau <blau@globus.org>
5
+ Author-email: Andy Loftus <aloftus@illinois.edu>
6
6
  Project-URL: Repository, https://github.com/access-ci-org/lmod-toolkit
7
7
  Keywords: monitoring,information,gathering,publishing
8
8
  Classifier: Development Status :: 1 - Planning
@@ -10,6 +10,8 @@ Classifier: Programming Language :: Python :: 3
10
10
  Classifier: Topic :: System :: Monitoring
11
11
  Requires-Python: >=3.14
12
12
  Description-Content-Type: text/markdown
13
+ Requires-Dist: platformdirs
14
+ Requires-Dist: requests
13
15
 
14
16
  # lmod toolkit (LMTK)
15
17
  Tools for working with lmod
@@ -0,0 +1,35 @@
1
+ # lmod toolkit
2
+ Tools for working with lmod
3
+
4
+ ## Install
5
+ * `curl -o lmtk-setup.sh
6
+ https://raw.githubusercontent.com/access-ci-org/lmod-toolkit/refs/heads/main/setup.sh
7
+ `
8
+ * `./lmtk-setup.sh`
9
+ * `~/lmod-toolkit/bin/mkcfg.sh`
10
+ * Edit config file (from previous step)
11
+ * `~/lmod-toolkit/bin/test.sh`
12
+ * Add api token to .netrc
13
+ * machine opsapi3.access-ci.org
14
+ * account API_ACCESS_TOKEN
15
+ * `~/lmod-toolkit/bin/publish.sh`
16
+
17
+ ## Alternate install methods
18
+ * [Quickstart](utils/README.md)
19
+
20
+ # Design
21
+ Config holds a list of Resource Definitions to be reported on.
22
+
23
+ Publish loops over each Resource Definition and for each,
24
+ * gathers & publishes:
25
+ * lmod_spider (available software modules)
26
+ * lmod_extended_attributes
27
+
28
+ It does this via Config.iter_resources(), a Python generator function.
29
+
30
+ Each iteration of iter_resources moves to a new Resource Definition and saves
31
+ the current Resource Definition.
32
+
33
+ lmod_spider and lmod_ext_attrs will ask Config for
34
+ "current_resource_definition" and do their work relative to any settings given
35
+ therein.
@@ -8,6 +8,8 @@ build-backend = "setuptools.build_meta:__legacy__"
8
8
  name = "lmod-toolkit"
9
9
  requires-python = ">=3.14"
10
10
  dependencies = [
11
+ "platformdirs",
12
+ "requests",
11
13
  ]
12
14
  dynamic = [
13
15
  "version",
@@ -28,7 +30,6 @@ keywords = [
28
30
  ]
29
31
  authors = [
30
32
  {name = "Andy Loftus", email = "aloftus@illinois.edu"},
31
- {name = "Eric Blau", email = "blau@globus.org"},
32
33
  ]
33
34
 
34
35
  [project.urls]
@@ -31,6 +31,9 @@ GREEN='\033[0;32m'
31
31
  RED='\033[0;31m'
32
32
  NC='\033[0m' # No Color
33
33
 
34
+ export UV_NO_CACHE=1
35
+ export UV_MANAGED_PYTHON=1
36
+
34
37
 
35
38
  err() {
36
39
  echo -e "${RED}✗ ERROR: $*${NC}"
@@ -83,8 +86,6 @@ mk_venv() {
83
86
  [[ $DEBUG -eq $YES ]] && set -x
84
87
  [[ -d "$VENV" ]] || {
85
88
  export UV_PYTHON_INSTALL_DIR="${UV_DIR}"/python
86
- export UV_NO_CACHE=1
87
- export UV_MANAGED_PYTHON=1
88
89
  # allow override python version
89
90
  [[ -n "${LMTK_UV_PYTHON}" ]] && UV_PYTHON="${LMTK_UV_PYTHON}"
90
91
  export UV_PYTHON
@@ -134,17 +135,29 @@ is_upgrade_allowed() {
134
135
  }
135
136
 
136
137
 
138
+ install_lmod_toolkit_prerelease() {
139
+ local -a _cli_opts
140
+ _cli_opts+=(
141
+ '--index' 'https://test.pypi.org/simple'
142
+ '--prerelease-package' 'lmod-toolkit=allow'
143
+ '--no-deps'
144
+ )
145
+ is_upgrade_allowed && _cli_opts+=( '--upgrade' )
146
+ pushd "${INSTALL_DIR}"
147
+ # Install from test.pypi
148
+ "${UV}" pip install "${_cli_opts[@]}" \
149
+ lmod-toolkit"${LMTK_INSTALL_VERSION:+==$LMTK_INSTALL_VERSION}"
150
+ popd
151
+ }
152
+
153
+
137
154
  install_lmod_toolkit() {
138
155
  [[ $DEBUG -eq $YES ]] && set -x
139
156
  local -a _cli_opts
140
- is_upgrade_allowed && {
141
- _cli_opts+=( '--upgrade' )
142
- }
143
157
  is_pre_release_allowed && {
144
- export UV_INDEX=https://test.pypi.org/simple
145
- _cli_opts+=( '--prerelease-package' 'lmod-toolkit=allow' )
158
+ install_lmod_toolkit_prerelease
146
159
  }
147
- export UV_DEFAULT_INDEX=https://pypi.org/simple
160
+ is_upgrade_allowed && _cli_opts+=( '--upgrade' )
148
161
  pushd "${INSTALL_DIR}"
149
162
  "${UV}" pip install \
150
163
  "${_cli_opts[@]}" \
@@ -160,28 +173,57 @@ update_files() {
160
173
  local _replacement="$INSTALL_DIR"
161
174
  grep -r --files-with-matches -F "$_pattern" "$LMTK_PATH" \
162
175
  | while read; do
163
- sed -i -e "s?$_pattern?$_replacement?" "$REPLY"
176
+ # update only regular files while ignoring symlinks
177
+ if [[ -f "$REPLY" ]] && [[ ! -L "$REPLY" ]]; then
178
+ sed -i -e "s?$_pattern?$_replacement?" "$REPLY"
179
+ fi
164
180
  done
165
181
  }
166
182
 
167
183
 
168
- mk_symlinks() {
184
+ mk_support_dirs() {
169
185
  [[ $DEBUG -eq $YES ]] && set -x
170
- pushd "$INSTALL_DIR"
171
- local _symlink_dirs=( bin etc configure lib var )
172
- # for d in "${_symlink_dirs[@]}"; do
173
- # [[ -L $d ]] || ln -s "$LMTK_PATH"/$d
174
- # done
175
- popd
176
- pushd "$INSTALL_DIR"/bin
177
- ln -s "${V_PYTHON}"
178
- popd
186
+ local _dirs
187
+ _dirs=( bin data logs )
188
+ for d in "${_dirs[@]}"; do
189
+ mkdir -p "$INSTALL_DIR"/"${d}"
190
+ done
191
+ }
192
+
193
+
194
+ mk_source_symlinks() {
195
+ # For each target dir, look for dir by same name in source,
196
+ # for each file in source dir,
197
+ # create a symlink in target dir to source file
198
+ [[ $DEBUG -eq $YES ]] && set -x
199
+ local -a _symlink_dirs
200
+ _symlink_dirs=( 'bin' )
201
+ for target_dir in "${_symlink_dirs[@]}"; do
202
+ pushd "${INSTALL_DIR}"/"${target_dir}"
203
+ find "${LMTK_PATH}"/"${target_dir}" -mindepth 1 -maxdepth 1 -type f -print \
204
+ | while read; do
205
+ tgt_fn=$( basename "${REPLY}" )
206
+ [[ -L "${tgt_fn}" ]] || ln -s "${REPLY}"
207
+ done
208
+ popd
209
+ done
179
210
  }
180
211
 
181
212
 
182
- restore_config_links() {
213
+ mk_custom_symlinks() {
183
214
  [[ $DEBUG -eq $YES ]] && set -x
184
- /bin/bash "$INSTALL_DIR"/bin/save_configs.sh
215
+ pushd "$INSTALL_DIR"/bin
216
+ local -A _links_targets
217
+ _links_targets=(
218
+ ['mkcfg.sh']='lmtk.sh'
219
+ ['publish.sh']='lmtk.sh'
220
+ ['test.sh']='lmtk.sh'
221
+ )
222
+ for linkname in "${!_links_targets[@]}"; do
223
+ linktarget="${_links_targets[$linkname]}"
224
+ [[ -L "${linkname}" ]] || ln -s "${linktarget}" "${linkname}"
225
+ done
226
+ popd
185
227
  }
186
228
 
187
229
 
@@ -193,8 +235,10 @@ mk_venv
193
235
 
194
236
  install_lmod_toolkit
195
237
 
196
- # update_files
238
+ update_files
239
+
240
+ mk_support_dirs
197
241
 
198
- # mk_symlinks
242
+ mk_source_symlinks
199
243
 
200
- # restore_config_links
244
+ mk_custom_symlinks
@@ -0,0 +1,5 @@
1
+ from . import lmod_config, lmod_spider, lmod_ext_attrs, _version, metadata, test, config, publish, http
2
+
3
+ import logging
4
+
5
+ logging.getLogger(__name__).addHandler(logging.NullHandler())
@@ -0,0 +1,6 @@
1
+ import sys
2
+
3
+ from .cli import main
4
+
5
+ if __name__ == "__main__":
6
+ sys.exit( main() )
@@ -18,7 +18,7 @@ version_tuple: tuple[int | str, ...]
18
18
  commit_id: str | None
19
19
  __commit_id__: str | None
20
20
 
21
- __version__ = version = '0.0.5'
22
- __version_tuple__ = version_tuple = (0, 0, 5)
21
+ __version__ = version = '0.0.7'
22
+ __version_tuple__ = version_tuple = (0, 0, 7)
23
23
 
24
- __commit_id__ = commit_id = 'g772328371'
24
+ __commit_id__ = commit_id = 'g8c7a0fca7'
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/bash
2
+
3
+ INSTALL_DIR=___INSTALL_DIR___
4
+
5
+ set -x
6
+
7
+ CMD=$( basename $0 '.sh' )
8
+
9
+ OPT="--$CMD"
10
+
11
+ PYTHON="${INSTALL_DIR}"/.venv/bin/python
12
+
13
+ $PYTHON -m lmod_toolkit "$OPT" "$@"
@@ -0,0 +1,86 @@
1
+ import argparse
2
+ import functools
3
+ import logging
4
+ from logging.handlers import RotatingFileHandler
5
+ from textwrap import dedent
6
+
7
+ from .logging_config import configure_logging
8
+ import lmod_toolkit as lmtk
9
+
10
+ logr = logging.getLogger( __name__ )
11
+
12
+
13
+ @functools.cache
14
+ def get_args( params=None ):
15
+ constructor_args = {
16
+ 'formatter_class': argparse.RawDescriptionHelpFormatter,
17
+ 'description': 'lmod_toolkit command line interface.',
18
+ 'epilog': '''NETRC:
19
+ ACCESS lmod_toolkit publishing credentials should be stored in ~/.netrc.
20
+ ''',
21
+ }
22
+ parser = argparse.ArgumentParser( **constructor_args )
23
+ parser.add_argument( '-d', '--debug', action='store_true' )
24
+ parser.add_argument( '-v', '--verbose', action='store_true' )
25
+ action = parser.add_mutually_exclusive_group( required=True )
26
+ action.add_argument( '--test',
27
+ const='test',
28
+ dest='action',
29
+ action='store_const',
30
+ help='Test run of gathering publishing data' )
31
+ action.add_argument( '--mkcfg',
32
+ const='mkcfg',
33
+ dest='action',
34
+ action='store_const',
35
+ help='Copy a sample config into the default location' )
36
+ action.add_argument( '--publish',
37
+ const='publish',
38
+ dest='action',
39
+ action='store_const',
40
+ help='Collect and publish data (default)' )
41
+ # parser.set_defaults(
42
+ # action = 'publish',
43
+ # )
44
+ args = parser.parse_args( params )
45
+ return args
46
+
47
+
48
+ def mkcfg():
49
+ try:
50
+ lmtk.config.mk_config()
51
+ except UserWarning as e:
52
+ msg = str( e )
53
+ else:
54
+ msg = dedent( f"""
55
+ New config file: '{lmtk.config.get_config_path()}'
56
+ You MUST add at least one valid resource to the config before publishing.
57
+ Next Steps:
58
+ 1. Edit the config file '{lmtk.config.get_config_path()}'
59
+ 2. Test with: '~/lmod-toolkit/bin/test.sh'
60
+ """).strip( "\n" )
61
+ return( msg )
62
+
63
+
64
+ def setup_logging():
65
+ args = get_args()
66
+ loglvl = logging.WARNING
67
+ if args.debug:
68
+ loglvl = logging.DEBUG
69
+ elif args.verbose:
70
+ loglvl = logging.INFO
71
+ logfile = lmtk.metadata.log_dir / f'{__package__}.log'
72
+ configure_logging( log_level=loglvl, log_file=logfile )
73
+
74
+
75
+ def main():
76
+ setup_logging()
77
+ args = get_args()
78
+ if args.action == 'test':
79
+ lmtk.test.all_tests()
80
+ elif args.action == 'publish':
81
+ lmtk.publish.go()
82
+ elif args.action == 'mkcfg':
83
+ status = mkcfg()
84
+ print( status )
85
+ else:
86
+ raise SystemExit( f"Invalid action: '{args.action}'" )
@@ -0,0 +1,73 @@
1
+ import functools
2
+ import os
3
+ import tomllib
4
+
5
+ from pathlib import Path
6
+ from platformdirs import PlatformDirs
7
+
8
+ import lmod_toolkit as lmtk
9
+
10
+
11
+ __all__ = [
12
+ 'get_config_path',
13
+ 'get_config',
14
+ 'mk_config',
15
+ ]
16
+
17
+ module_resources = {
18
+ 'current_resource_definition': None,
19
+ }
20
+
21
+
22
+ @functools.cache
23
+ def get_config_path():
24
+ # allow LMTK_CONFIG_PATH override
25
+ from_env = os.getenv( 'LMTK_CONFIG_PATH', None ) #default to None
26
+ if from_env:
27
+ cfg_path = Path( from_env )
28
+ else:
29
+ dirs = PlatformDirs( appname=__package__, ensure_exists=True )
30
+ cfg_path = dirs.place_config_file( 'common.toml' )
31
+ return cfg_path
32
+
33
+
34
+ @functools.cache
35
+ def get_config():
36
+ cfg_path = get_config_path()
37
+ with cfg_path.open( mode='rb' ) as fh:
38
+ cfg_data = tomllib.load( fh )
39
+ return cfg_data
40
+
41
+
42
+ def mk_config():
43
+ cfg_path = get_config_path()
44
+ if cfg_path.exists():
45
+ raise UserWarning( f"ConfigFile '{cfg_path}' exists, refusing to overwrite" )
46
+ # get module install dir
47
+ pkg_dir = lmtk.metadata.pkg_install_dir
48
+ cfg_sample = pkg_dir / 'examples' / 'common.toml.sample'
49
+ encoding='utf-8'
50
+ sample_text = cfg_sample.read_text( encoding=encoding )
51
+ cfg_path.write_text( sample_text, encoding=encoding )
52
+
53
+
54
+ def iter_resources():
55
+ ''' Loop through all the resource definitions in the config
56
+ Yield one at a time as a generator function.
57
+ Additionally, and more importantly, set "current_resource_definition"
58
+ so other functions can operate accordingly
59
+ for whatever is the current_resource_definition.
60
+ This function effectively controls all the activity in this package ...
61
+ ... it is the "main loop", if you will.
62
+ '''
63
+ for resource_definition in get_config()['resources']:
64
+ module_resources['current_resource_definition'] = resource_definition
65
+ yield resource_definition
66
+
67
+
68
+ def get_current_resource_definition():
69
+ return module_resources['current_resource_definition']
70
+
71
+
72
+ def __getattr__( name ):
73
+ return( get_config()[name] )
@@ -0,0 +1,33 @@
1
+ [[resources]]
2
+ # MUST SPECIFY EXACTLY ONE OF info_resourceid OR info_groupid
3
+
4
+ # Resource ID is the name defined in cider.access-ci.org
5
+ # Specify info_resourceid to either override the defaults published for info_groupid
6
+ # of if there is no data published for info_groupid
7
+ # [REQUIRED] string: ( set ONLY ONE of info_resourceid" or "info_groupid" )
8
+ #info_resourceid = "my.resource.access-ci.org"
9
+
10
+ # Group ID is the name defined at:
11
+ # https://operations-api.access-ci.org/wh2/cider/v1/access-active-groups/
12
+ # The info_groupid can be used to publish a set of default software modules
13
+ # to be used for all the resources under said group
14
+ # [REQUIRED] string: ( set ONLY ONE of "info_groupid" or "info_resourceid")
15
+ #info_groupid = "my.site.access-ci.org"
16
+
17
+ # Specify a custom module path to use instead of MODULEPATH environment variable
18
+ # [OPTIONAL] string: same format and purpose of MODULEPATH
19
+ # module_path =
20
+
21
+
22
+ # Can specify multiple resources.
23
+ # Data for each unique resourceid OR groupid will be published in turn.
24
+ #[[resources]]
25
+ #info_resourceid = "my-other-resource.access-ci.org"
26
+ #info_groupid =
27
+ #module_path =
28
+
29
+
30
+ # Details for the OPS API server
31
+ [server]
32
+ host = "opsapi3.access-ci.org"
33
+ path = "wh2/publish/v1/jsondocument/"
@@ -0,0 +1,62 @@
1
+ # Interact with the ACCESS OPS API
2
+ import functools
3
+ import logging
4
+ import netrc
5
+ import pprint
6
+ import requests
7
+
8
+ import lmod_toolkit as lmtk
9
+
10
+ logr = logging.getLogger(__name__)
11
+
12
+
13
+ @functools.cache
14
+ def get_session():
15
+ s = requests.Session()
16
+ s.trust_env = False #disable netrc lookup in requests
17
+ return s
18
+
19
+ @functools.cache
20
+ def get_netrc():
21
+ n = netrc.netrc()
22
+ host = lmtk.config.server['host']
23
+ (login, account, password) = n.authenticators( host )
24
+ return {
25
+ 'login': login,
26
+ 'account': account,
27
+ 'password': password,
28
+ }
29
+
30
+
31
+
32
+ def get_account():
33
+ return get_netrc()['account']
34
+
35
+ get_api_token = get_account
36
+
37
+
38
+
39
+ def api_go( method: str, **kw ):
40
+ host = lmtk.config.server['host']
41
+ path = lmtk.config.server['path']
42
+ url = f'https://{host}/{path}'
43
+ headers = {
44
+ 'Authorization': f'Basic {get_api_token()}'
45
+ }
46
+ if 'headers' in kw:
47
+ kw['headers'].update( headers )
48
+ else:
49
+ kw['headers'] = headers
50
+ logr.debug( f'{method}, {url}, {pprint.pformat(kw)}' )
51
+ r = get_session().request( method, url, **kw )
52
+ logr.debug(f'RETURN CODE .. {r}' )
53
+ r.raise_for_status()
54
+ return r
55
+
56
+
57
+ def api_post_gzip( data, **kw):
58
+ headers = {
59
+ "Content-Type": "application/json",
60
+ "Content-Encoding": "gzip",
61
+ }
62
+ return api_go( method='POST', data=data, headers=headers, **kw )
@@ -2,10 +2,14 @@ import functools
2
2
  import json
3
3
  import pprint
4
4
  import subprocess
5
+ import sys
6
+
7
+ from pathlib import Path
5
8
 
6
9
  __all__ = [
7
10
  'as_json',
8
11
  'get_config',
12
+ 'get_spider_path',
9
13
  ]
10
14
 
11
15
  @functools.cache
@@ -19,8 +23,20 @@ def get_config():
19
23
  return json.loads( result.stderr )
20
24
 
21
25
 
26
+ @functools.cache
27
+ def get_spider_path():
28
+ this_py_module = sys.modules[ __name__ ]
29
+ prefix = this_py_module.prefix
30
+ libexec = Path( prefix , 'lmod' , 'lmod' , 'libexec' )
31
+ return libexec / 'spider'
32
+
33
+
34
+ def as_dict():
35
+ return get_config()
36
+
37
+
22
38
  def as_json():
23
- return json.dumps( get_config() )
39
+ return json.dumps( as_dict() )
24
40
 
25
41
 
26
42
  def __getattr__( name ):
@@ -0,0 +1,95 @@
1
+ import functools
2
+ import json
3
+ import logging
4
+ import pprint
5
+ import re
6
+
7
+ from pathlib import Path
8
+
9
+ import lmod_toolkit as lmtk
10
+
11
+ logr = logging.getLogger( __name__ )
12
+
13
+ __all__ = [
14
+ 'as_json',
15
+ 'get_ext_attrs',
16
+ ]
17
+
18
+ # Adapted from:
19
+ # https://github.com/access-ci-org/ipf/blob/3e419ebb2d0f404454ec7a8c3630f5cf24c2d756/ipf/glue2/modules.py#L444
20
+ EXTATTR_KEYS = [
21
+ 'Name',
22
+ 'Version',
23
+ '.*escription',
24
+ 'URL',
25
+ 'Category',
26
+ 'Keywords',
27
+ 'SupportStatus',
28
+ 'SupportContact',
29
+ 'Default',
30
+ 'IPF_FLAGS',
31
+ ]
32
+ LONGEST_FIRST = sorted(EXTATTR_KEYS, key=len, reverse=True)
33
+ WHATIS_RE = re.compile(r'whatis\(\[\[((?:{}))\s*:\s*(.*)\]\]\)'.format('|'.join(LONGEST_FIRST)))
34
+ COMMENT_RE = re.compile(r'\"((?:{})):([^\"]+)\"'.format('|'.join(LONGEST_FIRST)))
35
+
36
+
37
+ def get_ext_attrs():
38
+ ''' Search through all modules' lua files for any extended attributes.
39
+ Extended attributes are non-lmod supported data added to a
40
+ lua module definition.
41
+ '''
42
+ ext_attrs = {}
43
+ for module in lmtk.lmod_spider.get_module_data():
44
+ for version in module[ 'versions' ]:
45
+ # path = version[ 'path' ]
46
+ ## skip non-lua files
47
+ #if not path.endswith(".lua"):
48
+ # #logr.debug( f"skipping non-lua file {path}" )
49
+ # print( f"skipping non-lua file {path}" )
50
+ # continue
51
+ filepath = Path( version[ 'path' ] )
52
+ # try:
53
+ # file_contents = filepath.read_text()
54
+ # except ( FileNotFoundError, PermissionError, IOError ) as e:
55
+ # logr.warning( f"error opening file '{filepath}' {e}" )
56
+ # continue
57
+ # ext_data = get_ext_attrs_from_lua_file( file_contents )
58
+ ext_data = get_ext_attrs_from_lua_file( filepath )
59
+ if ext_data:
60
+ full_name = version[ 'full' ]
61
+ ext_attrs[ full_name ] = ext_data
62
+ return ext_attrs
63
+
64
+
65
+ # wrapping this with functools.cache ensures we open a file only once
66
+ # @functools.cache
67
+ def get_ext_attrs_from_lua_file( filepath: Path ) -> dict:
68
+ ''' text parsing taken from
69
+ https://github.com/access-ci-org/ipf/blob/3e419ebb/ipf/glue2/modules.py#L444
70
+ '''
71
+ try:
72
+ text = filepath.read_text()
73
+ except ( FileNotFoundError, PermissionError, IOError ) as e:
74
+ logr.warning( f"error opening file '{filepath}' {e}" )
75
+ return None
76
+ whatis_res = WHATIS_RE.findall(text)
77
+ comment_res = COMMENT_RE.findall(text)
78
+ # Return all tuples found, allow "comment" to override "whatis"
79
+ ext_attrs = {
80
+ k: v
81
+ for k, v in whatis_res+comment_res
82
+ }
83
+ return ext_attrs
84
+
85
+
86
+ def as_dict():
87
+ return get_ext_attrs()
88
+
89
+
90
+ def as_json():
91
+ return json.dumps( as_dict() )
92
+
93
+
94
+ if __name__ == '__main__':
95
+ pprint.pprint( get_ext_attrs() )