src/create-module.py
author Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
Thu Oct 06 18:03:59 2011 +0100 (4 months ago)
changeset 7557 81ca9a7671bb
child 7646 0c1ed7ef4ee7
permissions -rwxr-xr-x
Rescan API of spectrum module
     1 #! /usr/bin/env python
     2 import sys
     3 from optparse import OptionParser
     4 import os
     5 
     6 
     7 WSCRIPT_TEMPLATE = '''# -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
     8 
     9 # def options(opt):
    10 #     pass
    11 
    12 # def configure(conf):
    13 #     conf.check_nonfatal(header_name='stdint.h', define_name='HAVE_STDINT_H')
    14 
    15 def build(bld):
    16     module = bld.create_ns3_module(%(MODULE)r, ['core'])
    17     module.source = [
    18         'model/%(MODULE)s.cc',
    19         'helper/%(MODULE)s-helper.cc',
    20         ]
    21 
    22     headers = bld.new_task_gen(features=['ns3header'])
    23     headers.module = %(MODULE)r
    24     headers.source = [
    25         'model/%(MODULE)s.h',
    26         'helper/%(MODULE)s-helper.h',
    27         ]
    28 
    29     if bld.env.ENABLE_EXAMPLES:
    30         bld.add_subdirs('examples')
    31 
    32     # bld.ns3_python_bindings()
    33 
    34 '''
    35 
    36 
    37 
    38 MODEL_CC_TEMPLATE = '''/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
    39 
    40 #include "%(MODULE)s.h"
    41 
    42 namespace ns3 {
    43 
    44 /* ... */
    45 
    46 
    47 }
    48 
    49 '''
    50 
    51 
    52 
    53 MODEL_H_TEMPLATE = '''/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
    54 #ifndef %(INCLUDE_GUARD)s
    55 #define %(INCLUDE_GUARD)s
    56 
    57 namespace ns3 {
    58 
    59 /* ... */
    60 
    61 }
    62 
    63 #endif /* %(INCLUDE_GUARD)s */
    64 
    65 '''
    66 
    67 
    68 
    69 HELPER_CC_TEMPLATE = '''/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
    70 
    71 #include "%(MODULE)s-helper.h"
    72 
    73 namespace ns3 {
    74 
    75 /* ... */
    76 
    77 
    78 }
    79 
    80 '''
    81 
    82 
    83 
    84 HELPER_H_TEMPLATE = '''/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
    85 #ifndef %(INCLUDE_GUARD)s
    86 #define %(INCLUDE_GUARD)s
    87 
    88 #include "ns3/%(MODULE)s.h"
    89 
    90 namespace ns3 {
    91 
    92 /* ... */
    93 
    94 }
    95 
    96 #endif /* %(INCLUDE_GUARD)s */
    97 
    98 '''
    99 
   100 
   101 EXAMPLES_WSCRIPT_TEMPLATE = '''# -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
   102 
   103 def build(bld):
   104     obj = bld.create_ns3_program('%(MODULE)s-example', [%(MODULE)r])
   105     obj.source = '%(MODULE)s-example.cc'
   106 
   107 '''
   108 
   109 EXAMPLE_CC_TEMPLATE = '''/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
   110 
   111 #include "ns3/core-module.h"
   112 #include "ns3/%(MODULE)s-helper.h"
   113 
   114 using namespace ns3;
   115 
   116 
   117 int 
   118 main (int argc, char *argv[])
   119 {
   120   bool verbose = true;
   121 
   122   CommandLine cmd;
   123   cmd.AddValue ("verbose", "Tell application to log if true", verbose);
   124 
   125   cmd.Parse (argc,argv);
   126 
   127   /* ... */
   128 
   129   Simulator::Run ();
   130   Simulator::Destroy ();
   131   return 0;
   132 }
   133 
   134 
   135 '''
   136 
   137 
   138 def main(argv):
   139     parser = OptionParser(usage=("Usage: %prog [options] modulename\n"
   140                                  "Utility script to create a basic template for a new ns-3 module"))
   141     (options, args) = parser.parse_args()
   142     if len(args) != 1:
   143         parser.print_help()
   144         return 1
   145 
   146     modname = args[0]
   147     assert os.path.sep not in modname
   148 
   149     moduledir = os.path.join(os.path.dirname(__file__), modname)
   150 
   151     if os.path.exists(moduledir):
   152         print >> sys.stderr, "Module %r already exists" % (modname,)
   153         return 2
   154 
   155     os.mkdir(moduledir)
   156     wscript = file(os.path.join(moduledir, "wscript"), "wt")
   157     wscript.write(WSCRIPT_TEMPLATE % dict(MODULE=modname))
   158     wscript.close()
   159 
   160 
   161     #
   162     # model
   163     # 
   164     modeldir = os.path.join(moduledir, "model")
   165     os.mkdir(modeldir)
   166 
   167     model_cc = file(os.path.join(moduledir, "model", "%s.cc" % modname), "wt")
   168     model_cc.write(MODEL_CC_TEMPLATE % dict(MODULE=modname))
   169     model_cc.close()
   170 
   171     model_h = file(os.path.join(moduledir, "model", "%s.h" % modname), "wt")
   172     model_h.write(MODEL_H_TEMPLATE % dict(MODULE=modname, INCLUDE_GUARD="__%s_H__" % (modname.upper()),))
   173     model_h.close()
   174 
   175 
   176 
   177     #
   178     # helper
   179     # 
   180     helperdir = os.path.join(moduledir, "helper")
   181     os.mkdir(helperdir)
   182 
   183     helper_cc = file(os.path.join(moduledir, "helper", "%s-helper.cc" % modname), "wt")
   184     helper_cc.write(HELPER_CC_TEMPLATE % dict(MODULE=modname))
   185     helper_cc.close()
   186 
   187     helper_h = file(os.path.join(moduledir, "helper", "%s-helper.h" % modname), "wt")
   188     helper_h.write(HELPER_H_TEMPLATE % dict(MODULE=modname, INCLUDE_GUARD="__%s_HELPER_H__" % (modname.upper()),))
   189     helper_h.close()
   190 
   191 
   192     examplesdir = os.path.join(moduledir, "examples")
   193     os.mkdir(examplesdir)
   194 
   195     examples_wscript = file(os.path.join(examplesdir, "wscript"), "wt")
   196     examples_wscript.write(EXAMPLES_WSCRIPT_TEMPLATE % dict(MODULE=modname))
   197     examples_wscript.close()
   198 
   199     example_cc = file(os.path.join(moduledir, "examples", "%s-example.cc" % modname), "wt")
   200     example_cc.write(EXAMPLE_CC_TEMPLATE % dict(MODULE=modname))
   201     example_cc.close()
   202 
   203 
   204     return 0
   205 
   206 if __name__ == '__main__':
   207     sys.exit(main(sys.argv))