CMS 3D CMS Logo

main.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #-*- coding: utf-8 -*-
3 #pylint: disable-msg=
4 """
5 File : Skeleton.py
6 Author : Valentin Kuznetsov <vkuznet@gmail.com>
7 Description:
8 """
9 from __future__ import print_function
10 
11 # system modules
12 import os
13 import sys
14 import pprint
15 from optparse import OptionParser
16 
17 # package modules
18 from FWCore.Skeletons.utils import code_generator, test_env, template_directory
19 
20 if sys.version_info < (2, 6):
21  raise Exception("This script requires python 2.6 or greater")
22 
24  "Skeleton option parser"
25  def __init__(self):
26  cname = os.environ.get('MKTMPL_CMD', 'main.py')
27  usage = "Usage: %s [options]\n" % cname
28  self.parser = OptionParser(usage=usage)
29  msg = "debug output"
30  self.parser.add_option("--debug", action="store_true",
31  default=False, dest="debug", help=msg)
32  msg = "specify template, e.g. EDProducer"
33  self.parser.add_option("--tmpl", action="store", type="string",
34  default='', dest="tmpl", help=msg)
35  msg = "specify package name, e.g. MyProducer"
36  self.parser.add_option("--name", action="store", type="string",
37  default="TestPkg", dest="pname", help=msg)
38  msg = "specify author name"
39  self.parser.add_option("--author", action="store", type="string",
40  default="", dest="author", help=msg)
41  msg = "specify file type to generate, "
42  msg += "e.g. --ftype=header, default is all files"
43  self.parser.add_option("--ftype", action="store", type="string",
44  default="all", dest="ftype", help=msg)
45  msg = "list examples tags which should be kept in "
46  msg += "generate code, e.g. "
47  msg += "--keep-etags='@example_trac,@example_hist'"
48  self.parser.add_option("--keep-etags", action="store", type="string",
49  default=None, dest="ketags", help=msg)
50  msg = "list template tags"
51  self.parser.add_option("--tags", action="store_true",
52  default=False, dest="tags", help=msg)
53  msg = "list template example tags"
54  self.parser.add_option("--etags", action="store_true",
55  default=False, dest="etags", help=msg)
56  msg = "list supported templates"
57  self.parser.add_option("--templates", action="store_true",
58  default=False, dest="templates", help=msg)
59  def get_opt(self):
60  "Returns parse list of options"
61  return self.parser.parse_args()
62 
63 def parse_args(args):
64  "Parse input arguments"
65  output = {}
66  for arg in args:
67  key, val = arg.split('=')
68  key = key.strip()
69  val = val.strip()
70  if val[0] == '[' and val[-1] == ']':
71  val = eval(val, { "__builtins__": None }, {})
72  output[key] = val
73  return output
74 
75 def generator():
76  """
77  Code generator function, parse user arguments and load appropriate
78  template module. Once loaded, run its data method depending on
79  user requested input parameter, e.g. print_etags, print_tags or
80  generate template code.
81  """
82  optmgr = SkeletonOptionParser()
83  opts, args = optmgr.get_opt()
84  test_env(os.path.join(opts.tdir, opts.tmpl), opts.tmpl)
85  config = {'pname': opts.pname, 'tmpl': opts.tmpl, 'author': opts.author,
86  'args': parse_args(args), 'debug': opts.debug,
87  'ftype': opts.ftype}
88  if opts.ketags:
89  etags = opts.ketags.split(',')
90  config.update({'tmpl_etags': etags})
91  else:
92  config.update({'tmpl_etags': []})
93  obj = code_generator(config)
94  if opts.etags:
95  obj.print_etags()
96  sys.exit(0)
97  elif opts.tags:
98  obj.print_tags()
99  sys.exit(0)
100  elif opts.templates:
101  for name in os.listdir(template_directory()):
102  print(name)
103  sys.exit(0)
104  obj.generate()
105 
106 if __name__ == '__main__':
107  generator()
utils.template_directory
def template_directory()
Definition: utils.py:23
main.SkeletonOptionParser
Definition: main.py:23
main.SkeletonOptionParser.get_opt
def get_opt(self)
Definition: main.py:59
main.generator
def generator()
Definition: main.py:75
main.SkeletonOptionParser.parser
parser
Definition: main.py:28
Exception
utils.test_env
def test_env(tdir, tmpl)
Definition: utils.py:53
main.SkeletonOptionParser.__init__
def __init__(self)
Definition: main.py:25
main.parse_args
def parse_args(args)
Definition: main.py:63
edm::print
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:66
utils.code_generator
def code_generator(kwds)
Definition: utils.py:122