Skip to content
Snippets Groups Projects
modules.py 3.35 KiB
Newer Older
  • Learn to ignore specific revisions
  • ##!/usr/bin/env python
    
    Lukáš Krupčík's avatar
    Lukáš Krupčík committed
    #
    #  Copyright (C) 2016 IT4Innovations
    #
    
    Lukáš Krupčík's avatar
    Lukáš Krupčík committed
    
    import json
    import os
    import re
    import xmlrpclib
    
    
    def software_list(class_path):
        """List software from given module class"""
        os.chdir(class_path)
        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
    
    
    def get_software_versions(software_path):
        """List available versions of software"""
        os.chdir(software_path)
        return next(os.walk('.'))[2]
    
    
    def get_module_description(module_path):
        """Return software homepage URL and description"""
        url = ""
        description = " "
        with open(module_path) as f:
            content = f.read()
        data = re.search(r"module-whatis\s{(Description: )?(.+)\s-\sHomepage:\s(\S+)}",
                         content, re.DOTALL)
        try:
            description = data.group(2)
            url = data.group(3)
        except AttributeError:
            pass
        return url, description
    
    def push_to_portal(page):
        """Push HTML page to the user portal"""
        remote = 'https://admin_write_modules_table:IcifomHantyi@docs.it4i.cz/'
        server = xmlrpclib.ServerProxy(remote)
        xmlout = server.xmlrpc_salomon_list_of_modules(page)
    
    
    if __name__ == "__main__":
        BASE_MODULES_DIR = '/apps/modules/'
        SKIP_DIRS = ['all']
        HTML_SOURCE_DATA = {}
        os.chdir(BASE_MODULES_DIR)
        subdirs = next(os.walk('.'))[1]
        # Delete unwanted directories
        for dir in SKIP_DIRS:
            subdirs.remove(dir)
    
        #print sorted(subdirs)
        #subdirs = ['base']
        # Create module class structure
    
        print "# List of Available Modules"
    
    Lukáš Krupčík's avatar
    Lukáš Krupčík committed
        for dir in sorted(subdirs):
            class_path = os.path.join(BASE_MODULES_DIR, dir)
            available_software = software_list(class_path)
            #print available_software
    
            print "\n###%s\n" % dir.title()
    
    Lukáš Krupčík's avatar
    Lukáš Krupčík committed
            print "|Module|Description|Available versions|"
            print "|--|--|--|"
            HTML_SOURCE_DATA[dir] = {}
            for soft in sorted(available_software, key=str.lower):
                software_path = os.path.join(class_path, soft)
                software_versions = get_software_versions(software_path)
                # In case that there's not any available software version
                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)
                #print software_description
                HTML_SOURCE_DATA[dir][soft] = {'versions': [],
                                               'url': software_url,
                                               'description': software_description}
                for version in software_versions:
                    HTML_SOURCE_DATA[dir][soft]['versions'].append(version)
                #for version in software_versions:
                #    HTML_SOURCE_DATA[dir][soft]['versions'].append(version)
                if software_url=="":
                  print "|**%s**|%s|<nobr>%s</nobr>|" % (soft," ".join(software_description.split()),'</br>'.join(software_versions))
                else:
                  print "|**[%s](%s)**|%s|<nobr>%s</nobr>|" % (soft,software_url," ".join(software_description.split()),'</br>'.join(software_versions))
        #json_obj = json.dumps(HTML_SOURCE_DATA, sort_keys=True, indent=4)
        #push_to_portal(json_obj)
        #print json_obj