Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-07-07 22:33:13

0001 #!/usr/bin/env python3
0002 #-*- coding: utf-8 -*-
0003 #pylint: disable-msg=
0004 """
0005 File       : Skeleton.py
0006 Author     : Valentin Kuznetsov <vkuznet@gmail.com>
0007 Description:
0008 """
0009 from __future__ import print_function
0010 
0011 # system modules
0012 import os
0013 import sys
0014 import pprint
0015 from optparse import OptionParser
0016 
0017 # package modules
0018 from FWCore.Skeletons.utils import code_generator, test_env, template_directory
0019 
0020 if  sys.version_info < (2, 6):
0021     raise Exception("This script requires python 2.6 or greater")
0022 
0023 class SkeletonOptionParser:
0024     "Skeleton option parser"
0025     def __init__(self):
0026         cname  = os.environ.get('MKTMPL_CMD', 'main.py')
0027         usage  = "Usage: %s [options]\n" % cname
0028         self.parser = OptionParser(usage=usage)
0029         msg  = "debug output"
0030         self.parser.add_option("--debug", action="store_true",
0031                 default=False, dest="debug", help=msg)
0032         msg  = "specify template, e.g. EDProducer"
0033         self.parser.add_option("--tmpl", action="store", type="string",
0034                 default='', dest="tmpl", help=msg)
0035         msg  = "specify package name, e.g. MyProducer"
0036         self.parser.add_option("--name", action="store", type="string",
0037                 default="TestPkg", dest="pname", help=msg)
0038         msg  = "specify author name"
0039         self.parser.add_option("--author", action="store", type="string",
0040                 default="", dest="author", help=msg)
0041         msg  = "specify file type to generate, "
0042         msg += "e.g. --ftype=header, default is all files"
0043         self.parser.add_option("--ftype", action="store", type="string",
0044                 default="all", dest="ftype", help=msg)
0045         msg  = "list examples tags which should be kept in "
0046         msg += "generate code, e.g. "
0047         msg += "--keep-etags='@example_trac,@example_hist'"
0048         self.parser.add_option("--keep-etags", action="store", type="string",
0049                 default=None, dest="ketags", help=msg)
0050         msg  = "list template tags"
0051         self.parser.add_option("--tags", action="store_true",
0052                 default=False, dest="tags", help=msg)
0053         msg  = "list template example tags"
0054         self.parser.add_option("--etags", action="store_true",
0055                 default=False, dest="etags", help=msg)
0056         msg  = "list supported templates"
0057         self.parser.add_option("--templates", action="store_true",
0058                 default=False, dest="templates", help=msg)
0059     def get_opt(self):
0060         "Returns parse list of options"
0061         return self.parser.parse_args()
0062 
0063 def parse_args(args):
0064     "Parse input arguments"
0065     output = {}
0066     for arg in args:
0067         key, val = arg.split('=')
0068         key = key.strip()
0069         val = val.strip()
0070         if  val[0] == '[' and val[-1] == ']':
0071             val = eval(val, { "__builtins__": None }, {})
0072         output[key] = val
0073     return output
0074 
0075 def generator():
0076     """
0077     Code generator function, parse user arguments and load appropriate
0078     template module. Once loaded, run its data method depending on
0079     user requested input parameter, e.g. print_etags, print_tags or
0080     generate template code.
0081     """
0082     optmgr = SkeletonOptionParser()
0083     opts, args = optmgr.get_opt()
0084     test_env(os.path.join(opts.tdir, opts.tmpl), opts.tmpl)
0085     config = {'pname': opts.pname, 'tmpl': opts.tmpl, 'author': opts.author,
0086               'args': parse_args(args), 'debug': opts.debug,
0087               'ftype': opts.ftype}
0088     if  opts.ketags:
0089         etags = opts.ketags.split(',')
0090         config.update({'tmpl_etags': etags})
0091     else:
0092         config.update({'tmpl_etags': []})
0093     obj = code_generator(config)
0094     if  opts.etags:
0095         obj.print_etags()
0096         sys.exit(0)
0097     elif opts.tags:
0098         obj.print_tags()
0099         sys.exit(0)
0100     elif opts.templates:
0101         for name in os.listdir(template_directory()):
0102             print(name)
0103         sys.exit(0)
0104     obj.generate()
0105 
0106 if __name__ == '__main__':
0107     generator()