Skip to content
Snippets Groups Projects
modules.py 2.68 KiB
Newer Older
  • Learn to ignore specific revisions
  • Easy Build's avatar
    Easy Build committed
    #!/usr/bin/env python
    
    David Hrbáč's avatar
    David Hrbáč committed
    """Script to generate module lists"""
    
    Easy Build's avatar
    Easy Build committed
    
    import os
    import re
    
    
    David Hrbáč's avatar
    David Hrbáč committed
    def get_software_list(path):
    
    Easy Build's avatar
    Easy Build committed
        """List software from given module class"""
    
    David Hrbáč's avatar
    David Hrbáč committed
        os.chdir(path)
    
    Easy Build's avatar
    Easy Build committed
        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
    def get_software_versions(path):
    
    Easy Build's avatar
    Easy Build committed
        """List available versions of software"""
    
    David Hrbáč's avatar
    David Hrbáč committed
        os.chdir(path)
    
    Easy Build's avatar
    Easy Build committed
        return next(os.walk('.'))[2]
    
    def get_module_description(module_path):
        """Return software homepage URL and description"""
        url = ""
    
        description = "Old module, description not available."
    
    David Hrbáč's avatar
    David Hrbáč committed
        with open(module_path) as _file:
            content = _file.read()
    
        if module_path.endswith('lua'):
    
            data = re.search(r"whatis\((\[\[|\[==\[)Description: ?([\s\S]+)(?:\]\]|\]==\])\)\s+whatis\((?:\[\[|\[==\[)Homepage:\s(\S+)(?:\]\]|\]==\])\)",
    
    David Hrbáč's avatar
    David Hrbáč committed
                             content, re.DOTALL)
    
        else:
            data = re.search(r"module-whatis\s{(Description: )?(.+)\s-\sHomepage:\s(\S+)}",
    
    David Hrbáč's avatar
    David Hrbáč committed
                             content, re.DOTALL)
    
    Easy Build's avatar
    Easy Build committed
        try:
            description = data.group(2)
    
    David Hrbáč's avatar
    David Hrbáč committed
            url = data.group(3)
    
    Easy Build's avatar
    Easy Build committed
        except AttributeError:
            pass
        return url, description
    
    if __name__ == "__main__":
        HTML_SOURCE_DATA = {}
    
    David Hrbáč's avatar
    David Hrbáč committed
        SUBDIRS = os.environ['MODULEPATH'].split(':')
    
    Easy Build's avatar
    Easy Build committed
    
    
    David Hrbáč's avatar
    David Hrbáč committed
        for subdir in sorted(SUBDIRS, key=lambda s: s.lower()):
            available_software = get_software_list(subdir)
    
    David Hrbáč's avatar
    David Hrbáč committed
            print "\n## %s\n" % os.path.basename(subdir).title()
    
    Easy Build's avatar
    Easy Build committed
            print "| Module | Description |"
            print "| ------ | ----------- |"
    
    David Hrbáč's avatar
    David Hrbáč committed
            HTML_SOURCE_DATA[subdir] = {}
    
    Easy Build's avatar
    Easy Build committed
            for soft in sorted(available_software, key=str.lower):
    
    David Hrbáč's avatar
    David Hrbáč committed
                software_path = os.path.join(subdir, soft)
    
    Easy Build's avatar
    Easy Build committed
                software_versions = get_software_versions(software_path)
    
    David Hrbáč's avatar
    David Hrbáč committed
                software_versions = [ver for ver in software_versions if not ver[0] == '.']
    
    Easy Build's avatar
    Easy Build committed
    
                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
                HTML_SOURCE_DATA[subdir][soft] = {'versions': [],
    
    David Hrbáč's avatar
    David Hrbáč committed
                                                  'url': software_url,
                                                  'description': software_description}
    
    Easy Build's avatar
    Easy Build committed
                for version in software_versions:
    
    David Hrbáč's avatar
    David Hrbáč committed
                    HTML_SOURCE_DATA[subdir][soft]['versions'].append(version)
    
                if software_url in ["", "(none)", "N/A"]:
    
    David Hrbáč's avatar
    David Hrbáč committed
                    print "| %s | %s |" % (soft, " ".join(software_description.split()))
    
    Easy Build's avatar
    Easy Build committed
                else:
    
    David Hrbáč's avatar
    David Hrbáč committed
                    print "| [%s](%s) | %s |" % (soft, software_url, " ".join(software_description.split()))