modules.py 3.14 KB
Newer Older
Easy Build's avatar
Easy Build committed
1
#!/usr/bin/env python
David Hrbáč's avatar
David Hrbáč committed
2
"""Script to generate module lists"""
Easy Build's avatar
Easy Build committed
3 4 5 6

import os
import re

David Hrbáč's avatar
David Hrbáč committed
7
def get_software_list(path):
Easy Build's avatar
Easy Build committed
8
    """List software from given module class"""
David Hrbáč's avatar
David Hrbáč committed
9
    os.chdir(path)
Easy Build's avatar
Easy Build committed
10 11 12 13 14 15
    software_list = next(os.walk('.'))[1]
    # In case that there is 'all' module
    if 'all' in software_list:
        software_list.remove('all')
    return software_list

David Hrbáč's avatar
David Hrbáč committed
16
def get_software_versions(path):
Easy Build's avatar
Easy Build committed
17
    """List available versions of software"""
David Hrbáč's avatar
David Hrbáč committed
18
    os.chdir(path)
Easy Build's avatar
Easy Build committed
19 20 21 22 23
    return next(os.walk('.'))[2]

def get_module_description(module_path):
    """Return software homepage URL and description"""
    url = ""
easybuild's avatar
easybuild committed
24
    description = "Old module, description not available."
David Hrbáč's avatar
David Hrbáč committed
25 26
    with open(module_path) as _file:
        content = _file.read()
27
    if module_path.endswith('lua'):
Easy Build's avatar
Easy Build committed
28
        data = re.search(r"whatis\((\[\[|\[==\[)Description: ?([\s\S]+)(?:\]\]|\]==\])\)\s+whatis\((?:\[\[|\[==\[)Homepage:\s(\S+)(?:\]\]|\]==\])\)",
David Hrbáč's avatar
David Hrbáč committed
29
                         content, re.DOTALL)
30 31
    else:
        data = re.search(r"module-whatis\s{(Description: )?(.+)\s-\sHomepage:\s(\S+)}",
David Hrbáč's avatar
David Hrbáč committed
32
                         content, re.DOTALL)
33

Easy Build's avatar
Easy Build committed
34 35
    try:
        description = data.group(2)
David Hrbáč's avatar
David Hrbáč committed
36
        url = data.group(3)
Easy Build's avatar
Easy Build committed
37 38 39 40 41 42
    except AttributeError:
        pass
    return url, description

if __name__ == "__main__":
    HTML_SOURCE_DATA = {}
David Hrbáč's avatar
David Hrbáč committed
43
    SUBDIRS = os.environ['MODULEPATH'].split(':')
Easy Build's avatar
Easy Build committed
44

David Hrbáč's avatar
David Hrbáč committed
45 46 47 48 49 50 51
    if os.getenv('CLUSTER') == "ANSELM":
        SUBDIRS.remove("/apps/modules/init")
        SUBDIRS.remove("/apps/modules/environments")
        SUBDIRS.remove("/apps/modules/engineering")
        SUBDIRS.remove("/apps/modules/libraries")
        SUBDIRS.remove("/apps/modules/omics")
        SUBDIRS.remove("/apps/modules/prace")
easybuild's avatar
easybuild committed
52

David Hrbáč's avatar
David Hrbáč committed
53 54
    for subdir in sorted(SUBDIRS, key=lambda s: s.lower()):
        available_software = get_software_list(subdir)
David Hrbáč's avatar
David Hrbáč committed
55
        print "\n## %s\n" % os.path.basename(subdir).title()
Easy Build's avatar
Easy Build committed
56 57
        print "| Module | Description |"
        print "| ------ | ----------- |"
David Hrbáč's avatar
David Hrbáč committed
58
        HTML_SOURCE_DATA[subdir] = {}
Easy Build's avatar
Easy Build committed
59
        for soft in sorted(available_software, key=str.lower):
David Hrbáč's avatar
David Hrbáč committed
60 61 62
            software_path = os.path.join(subdir, soft)
            if os.getenv('CLUSTER') != "ANSELM":
                software_path = software_path.replace(os.path.basename(subdir)+'/', 'all/')
Easy Build's avatar
Easy Build committed
63
            software_versions = get_software_versions(software_path)
David Hrbáč's avatar
David Hrbáč committed
64
            software_versions = [ver for ver in software_versions if not ver[0] == '.']
Easy Build's avatar
Easy Build committed
65 66 67 68 69

            if not software_versions:
                continue
            first_module_path = os.path.join(software_path, software_versions[0])
            software_url, software_description = get_module_description(first_module_path)
David Hrbáč's avatar
David Hrbáč committed
70
            HTML_SOURCE_DATA[subdir][soft] = {'versions': [],
David Hrbáč's avatar
David Hrbáč committed
71 72
                                              'url': software_url,
                                              'description': software_description}
Easy Build's avatar
Easy Build committed
73
            for version in software_versions:
David Hrbáč's avatar
David Hrbáč committed
74 75 76
                HTML_SOURCE_DATA[subdir][soft]['versions'].append(version)
            if software_url in ["", "(none)"]:
                print "| %s | %s |" % (soft, " ".join(software_description.split()))
Easy Build's avatar
Easy Build committed
77
            else:
David Hrbáč's avatar
David Hrbáč committed
78
                print "| [%s](%s) | %s |" % (soft, software_url, " ".join(software_description.split()))