bthci/bthci2/CommandsEvents/generator/generator.py
changeset 0 29b1cd4cb562
equal deleted inserted replaced
-1:000000000000 0:29b1cd4cb562
       
     1 # Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 # All rights reserved.
       
     3 # This component and the accompanying materials are made available
       
     4 # under the terms of "Eclipse Public License v1.0"
       
     5 # which accompanies this distribution, and is available
       
     6 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 #
       
     8 # Initial Contributors:
       
     9 # Nokia Corporation - initial contribution.
       
    10 #
       
    11 # Contributors:
       
    12 #
       
    13 # Description:
       
    14 #
       
    15 
       
    16 import ConfigParser
       
    17 import getopt
       
    18 import string
       
    19 import sys
       
    20 import os
       
    21 import os.path
       
    22 
       
    23 from command import writeCommand
       
    24 from mmp import writeMMPFile
       
    25 from event import writeEvent
       
    26 from completeevent import writeCompleteEvent
       
    27 from datainput import getEntries, getTypes, getFolded
       
    28 from testserver import writeTestServer
       
    29 from teststep import writeTestSteps
       
    30 
       
    31 def printHelp():
       
    32     print('Automatically generates source files for Bluetooth commands')
       
    33     print('Usage: python ' + sys.argv[0] + ' -i ini-file -t type [mmp|impl|test] [template] [destination]')
       
    34     print('Example: \"python ' + sys.argv[0] + ' -i dllmmp.ini -t mmp dllmmp.tmpl hcilib.mmp\"')
       
    35     print('Options:\n-i\t File specifing data associated with this command. It\'s layout is type specific.')
       
    36     print('-t\t Type of data to generate, either mmp for mmp file, impl for implementation or test for test code.')
       
    37 
       
    38 # Retrieves command line options
       
    39 def getOpts():
       
    40     scanned = {}
       
    41     opts, args = getopt.getopt(sys.argv[1:], 'i:t:e:')
       
    42 
       
    43     for o, a in opts:
       
    44         if o == '-i':
       
    45             scanned['INI'] = a
       
    46         elif o == '-t':
       
    47             scanned['COMMAND'] = a.lower()
       
    48         else:
       
    49             printHelp()
       
    50 
       
    51     return scanned, args
       
    52 
       
    53 opts, args = getOpts()
       
    54 
       
    55 if len(opts) < 2:
       
    56     printHelp()
       
    57 else:
       
    58     if opts['COMMAND'] == 'mmp': # Generate mmp file
       
    59         writeMMPFile(opts['INI'], string.Template(file(args[0]).read()), args[1])
       
    60     elif opts['COMMAND'] == 'impl': # Generate implementation
       
    61         cfg = ConfigParser.ConfigParser()
       
    62         cfg.readfp(file(opts['INI']))
       
    63                    
       
    64         for s in cfg.sections():
       
    65             entries = getEntries(cfg.get(s, 'Datafile'))
       
    66             types = getTypes(cfg.get(s, 'Typefile'))
       
    67             headerTemplate = string.Template(file(cfg.get(s, 'HeaderTemplate')).read())
       
    68             sourceTemplate = string.Template(file(cfg.get(s, 'SourceTemplate')).read())
       
    69 
       
    70             typ = cfg.get(s, 'Type').lower()
       
    71 
       
    72             if os.path.exists(cfg.get(s, 'SourcePath')) == False:
       
    73                 os.mkdir(cfg.get(s, 'SourcePath'))
       
    74 
       
    75             if os.path.exists(cfg.get(s, 'HeaderPath')) == False:
       
    76                 os.mkdir(cfg.get(s, 'HeaderPath'))
       
    77 
       
    78             if typ == 'command':
       
    79                 folded = getFolded(entries, types, 3)
       
    80                 print('Writing %i commands\n\theaders at %s\n\tsource at %s...'
       
    81                       % (len(folded), cfg.get(s, 'HeaderPath'), cfg.get(s, 'SourcePath')))
       
    82 
       
    83                 for f in folded:
       
    84                     writeCommand(f, folded[f][0], folded[f][1], headerTemplate, sourceTemplate,
       
    85                                  cfg.get(s, 'HeaderPath'), cfg.get(s, 'SourcePath'))
       
    86             elif typ == 'event':
       
    87                 folded = getFolded(entries, types, 2)
       
    88                 print('Writing %i events\n\theaders at %s\n\tsource at %s...'
       
    89                       % (len(folded), cfg.get(s, 'HeaderPath'), cfg.get(s, 'SourcePath')))
       
    90 
       
    91                 for f in folded:
       
    92                     writeEvent(f, folded[f][1], headerTemplate, sourceTemplate,
       
    93                                cfg.get(s, 'HeaderPath'), cfg.get(s, 'SourcePath'))
       
    94             elif typ == 'completeevent':
       
    95                 folded = getFolded(entries, types, 5)
       
    96                 print('Writing %i command complete events\n\theaders at %s\n\tsource at %s...'
       
    97                       % (len(folded), cfg.get(s, 'HeaderPath'), cfg.get(s, 'SourcePath')))
       
    98 
       
    99                 for f in folded:
       
   100                     writeCompleteEvent(f, folded[f][1], headerTemplate, sourceTemplate,
       
   101                                        cfg.get(s, 'HeaderPath'), cfg.get(s, 'SourcePath'))
       
   102     elif opts['COMMAND'] == 'test': # Generate test code
       
   103         cfg = ConfigParser.ConfigParser()
       
   104         cfg.readfp(file(opts['INI']))
       
   105 
       
   106         for s in cfg.sections():
       
   107             if os.path.exists(cfg.get(s, 'Path')) == False:
       
   108                 os.mkdir(cfg.get(s, 'Path'))
       
   109             
       
   110             writeTestServer(cfg.get(s, 'Name'),
       
   111                             string.Template(file(cfg.get(s, 'HeaderTemplate')).read()),
       
   112                             string.Template(file(cfg.get(s, 'SourceTemplate')).read()),
       
   113                             string.Template(file(cfg.get(s, 'ScriptTemplate')).read()),
       
   114                             cfg.get(s, 'TestSteps'),
       
   115                             cfg.get(s, 'Path'))
       
   116 
       
   117             writeTestSteps(cfg.get(s, 'TestSteps'), cfg.get(s, 'Path'))
       
   118 
       
   119             
       
   120