oa.py 5.07 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#!/usr/local/bin/python2.7
'''
orgasm -- shortdesc

orgasm is a description

It defines classes_and_methods

@author:     user_name

@copyright:  2014 organization_name. All rights reserved.

@license:    license

@contact:    user_email
@deffield    updated: Updated
'''

import sys
import pkgutil
import argparse
22 23 24 25 26 27 28 29 30
import logging
import json

default_config = {

    'orgasm' :     { 'log'            : True,
                     'loglevel'       : 'INFO',
                     'outputfilename' : None,
                     'back'           : None,
Eric Coissac committed
31
                     'seeds'          : None,
32 33
                     'seedmincov'     : 1,
                     'kup'            : None,
Eric Coissac committed
34
                     'progress'       : True
35 36 37
                   }
                 
}
38 39 40 41 42 43 44 45 46 47 48 49 50 51



from orgasm import command

__all__ = []
__version__ = 0.1
__date__ = '2014-09-28'
__updated__ = '2014-09-28'

DEBUG = 1
TESTRUN = 0
PROFILE = 0

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 78 79 80 81 82 83 84 85
def loadCommand(name,loader):
    '''
    Load a command module from its name and an ImpLoader
    
    This function is for internal use
    
    @param name:   name of the module
    @type name: str 
    @param loader: the module loader
    @type loader: ImpLoader
    
    @return the loaded module
    @rtype: module 
    '''
    
    module = loader.find_module(name).load_module(name)
    return module

def getCommandsList():
    '''
    Returns the list of sub-commands available to the main orgasm command
    
    @return: a dict instance with key corresponding to each command and
             value corresponding to the module
             
    @rtype: dict
    '''
    cmds = dict((x[1],loadCommand(x[1],x[0])) 
                for x in pkgutil.iter_modules(command.__path__) 
                if not x[2])
    return cmds

86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
def getLogger(config):
    '''
    Returns the logger as defined by the command line option
    or by the config file
    :param config:
    '''
 
    output = config['orgasm']['outputfilename'] 
    level  = config['orgasm']['loglevel'] 
    logfile= config['orgasm']['log'] 
    
    rootlogger   = logging.getLogger()
    logFormatter = logging.Formatter("%(asctime)s [%(levelname)-5.5s]  %(message)s")

    stderrHandler = logging.StreamHandler(sys.stderr)
    stderrHandler.setFormatter(logFormatter)

    rootlogger.addHandler(stderrHandler)
    
    if logfile:
        fileHandler = logging.FileHandler("%s.log" % output)
        fileHandler.setFormatter(logFormatter)
        rootlogger.addHandler(fileHandler)
    
    try:
        loglevel = getattr(logging, level) 
    except:
        loglevel = logging.INFO
        
    rootlogger.setLevel(loglevel)
    
    config['orgasm']['logger']=rootlogger
    
    return rootlogger


122 123
def buildArgumentParser():
    parser = argparse.ArgumentParser()
124
    
Eric Coissac committed
125 126 127
    parser.add_argument('--no-log',    dest='orgasm:log', 
                                       action='store_false', 
                                       default=None, 
Eric Coissac committed
128 129 130 131 132 133
                        help='Do not create a logfile for the assembling')

    parser.add_argument('--no-progress', dest='orgasm:progress', 
                                       action='store_false', 
                                       default=None, 
                        help='Do not print the progress bar during assembling')
134

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
    subparsers = parser.add_subparsers(title='subcommands',
                                       description='valid subcommands',
                                       help='additional help')
    
    commands = getCommandsList()
    
    for c in commands:
        module = commands[c]
        
        if hasattr(module, "run"):
            if hasattr(module, "__title__"):
                sub = subparsers.add_parser(c,help=module.__title__)
            else:
                sub = subparsers.add_parser(c)
    
            if hasattr(module, "addOptions"):
                module.addOptions(sub)
152 153 154
            
            sub.set_defaults(**{'orgasm:module' : module})
                        
155 156
    return parser

157 158
def buildDefaultConfiguration():
    global default_config
159
    
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
    commands = getCommandsList()
    
    for c in commands:
        module = commands[c]
    
        assert hasattr(module, "run")
        
        if hasattr(module, 'default_config'):
            default_config[c]=module.default_config
        else:
            default_config[c]={}
                        
    return default_config
        

def getConfiguration():
Eric Coissac committed
176 177 178 179 180
    global default_config

    if '__done__' in default_config:
        return default_config
    
181
    parser = buildArgumentParser()
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
    options = vars(parser.parse_args())
    
    config =  buildDefaultConfiguration()
    
    
    for k in options:
        section,key = k.split(':')
        s = config[section]
        if options[k] is not None:
            s[key]=options[k]
            
    if config['orgasm']['outputfilename'] is None:
        config['orgasm']['outputfilename']=config['orgasm']['indexfilename']
        
    getLogger(config)
Eric Coissac committed
197 198
    
    config['__done__']=True
199 200 201 202 203 204
            
    return config
    

if __name__ =="__main__":
    
Eric Coissac committed
205
    config = getConfiguration()        
206 207
    config['orgasm']['module'].run(config)
    
208 209