-
Notifications
You must be signed in to change notification settings - Fork 2
/
macros.py
77 lines (61 loc) · 2.52 KB
/
macros.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import os
import pathlib
import re
import yaml
def parse_spack_index(modulefiles_path):
with open(os.path.join(modulefiles_path, 'module-index.yaml')) as fp:
module_index = yaml.load(fp, yaml.SafeLoader)['module_index']
return module_index
def build_module_data(module_index, modulefiles_path):
modules = {}
for pkg_hash in module_index:
name = module_index[pkg_hash]['use_name'].split('/')
path = pathlib.PurePath(module_index[pkg_hash]['path'])
relpath = pathlib.PurePath(os.path.join(*path.parts[5:]))
path = modulefiles_path / relpath
fullname = os.path.join(*name)
software = name[0]
version, _, arch = name[-1].partition('+')
build = None if len(name) == 2 else name[1]
if software not in modules:
modules[software] = {}
modules[software]['variants'] = {}
modules[software]['versions'] = set()
modules[software]['builds'] = set()
modules[software]['arches'] = set()
entry = modules[software]
entry['versions'].add(version)
entry['variants'][fullname] = path
if build:
entry['builds'].add(build)
if arch:
entry['arches'].add(arch)
return modules
def render_spack_modules(modules):
entries = []
for software, entry in sorted(modules.items()):
path = list(entry['variants'].values())[-1]
versions = ', '.join(entry['versions'])
variants = ', '.join(entry['builds'])
modulenames = ', '.join((f'`{name}`' for name in entry['variants'].keys()))
arches = entry['arches']
if not arches:
arches.add('generic')
arches = ', '.join(arches)
with open(path) as fp:
contents = fp.read()
about = re.search(r'help\(\[\[([^\]]*)\]\]\)', contents, flags=re.MULTILINE).group(1)
entries.append(( f'### {software}\n\n'
f'{about}\n\n'
f'**Versions**: {versions}\n\n'
f'{"**Variants**: " if variants else ""}{variants}\n\n'
f'**Arches**: {arches}\n\n'
f'**Modules**: {modulenames}'))
return '\n'.join(entries)
def define_env(env):
@env.macro
def spack_modules_list():
path = pathlib.PurePath(env.variables['spack_modulefiles_path'])
modules = build_module_data(parse_spack_index(path), path)
rendered = render_spack_modules(modules)
return rendered