3 from optparse import OptionParser
7 WSCRIPT_TEMPLATE = '''# -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
12 # def configure(conf):
13 # conf.check_nonfatal(header_name='stdint.h', define_name='HAVE_STDINT_H')
16 module = bld.create_ns3_module(%(MODULE)r, ['core'])
18 'model/%(MODULE)s.cc',
19 'helper/%(MODULE)s-helper.cc',
22 headers = bld.new_task_gen(features=['ns3header'])
23 headers.module = %(MODULE)r
26 'helper/%(MODULE)s-helper.h',
29 if bld.env.ENABLE_EXAMPLES:
30 bld.add_subdirs('examples')
32 # bld.ns3_python_bindings()
38 MODEL_CC_TEMPLATE = '''/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
40 #include "%(MODULE)s.h"
53 MODEL_H_TEMPLATE = '''/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
54 #ifndef %(INCLUDE_GUARD)s
55 #define %(INCLUDE_GUARD)s
63 #endif /* %(INCLUDE_GUARD)s */
69 HELPER_CC_TEMPLATE = '''/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
71 #include "%(MODULE)s-helper.h"
84 HELPER_H_TEMPLATE = '''/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
85 #ifndef %(INCLUDE_GUARD)s
86 #define %(INCLUDE_GUARD)s
88 #include "ns3/%(MODULE)s.h"
96 #endif /* %(INCLUDE_GUARD)s */
101 EXAMPLES_WSCRIPT_TEMPLATE = '''# -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
104 obj = bld.create_ns3_program('%(MODULE)s-example', [%(MODULE)r])
105 obj.source = '%(MODULE)s-example.cc'
109 EXAMPLE_CC_TEMPLATE = '''/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
111 #include "ns3/core-module.h"
112 #include "ns3/%(MODULE)s-helper.h"
118 main (int argc, char *argv[])
123 cmd.AddValue ("verbose", "Tell application to log if true", verbose);
125 cmd.Parse (argc,argv);
130 Simulator::Destroy ();
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()
147 assert os.path.sep not in modname
149 moduledir = os.path.join(os.path.dirname(__file__), modname)
151 if os.path.exists(moduledir):
152 print >> sys.stderr, "Module %r already exists" % (modname,)
156 wscript = file(os.path.join(moduledir, "wscript"), "wt")
157 wscript.write(WSCRIPT_TEMPLATE % dict(MODULE=modname))
164 modeldir = os.path.join(moduledir, "model")
167 model_cc = file(os.path.join(moduledir, "model", "%s.cc" % modname), "wt")
168 model_cc.write(MODEL_CC_TEMPLATE % dict(MODULE=modname))
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()),))
180 helperdir = os.path.join(moduledir, "helper")
183 helper_cc = file(os.path.join(moduledir, "helper", "%s-helper.cc" % modname), "wt")
184 helper_cc.write(HELPER_CC_TEMPLATE % dict(MODULE=modname))
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()),))
192 examplesdir = os.path.join(moduledir, "examples")
193 os.mkdir(examplesdir)
195 examples_wscript = file(os.path.join(examplesdir, "wscript"), "wt")
196 examples_wscript.write(EXAMPLES_WSCRIPT_TEMPLATE % dict(MODULE=modname))
197 examples_wscript.close()
199 example_cc = file(os.path.join(moduledir, "examples", "%s-example.cc" % modname), "wt")
200 example_cc.write(EXAMPLE_CC_TEMPLATE % dict(MODULE=modname))
206 if __name__ == '__main__':
207 sys.exit(main(sys.argv))