CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 
10 # system modules
11 import os
12 import sys
13 import pprint
14 from optparse import OptionParser
15 
16 # package modules
17 from FWCore.Skeletons.utils import code_generator, test_env
18 
19 if sys.version_info < (2, 6):
20  raise Exception("This script requires python 2.6 or greater")
21 
22 def tmpl_dir():
23  "Retturn default location of template directory"
24  return '%s/templates' % '/'.join(__file__.split('/')[:-1])
25 
27  "Skeleton option parser"
28  def __init__(self):
29  cname = os.environ.get('MKTMPL_CMD', 'main.py')
30  usage = "Usage: %s [options]\n" % cname
31  self.parser = OptionParser(usage=usage)
32  msg = "debug output"
33  self.parser.add_option("--debug", action="store_true",
34  default=False, dest="debug", help=msg)
35  msg = "specify template, e.g. EDProducer"
36  self.parser.add_option("--tmpl", action="store", type="string",
37  default='', dest="tmpl", help=msg)
38  msg = "specify package name, e.g. MyProducer"
39  self.parser.add_option("--name", action="store", type="string",
40  default="TestPkg", dest="pname", help=msg)
41  msg = "specify author name"
42  self.parser.add_option("--author", action="store", type="string",
43  default="", dest="author", help=msg)
44  msg = "specify file type to generate, "
45  msg += "e.g. --ftype=header, default is all files"
46  self.parser.add_option("--ftype", action="store", type="string",
47  default="all", dest="ftype", help=msg)
48  msg = "list examples tags which should be kept in "
49  msg += "generate code, e.g. "
50  msg += "--keep-etags='@example_trac,@example_hist'"
51  self.parser.add_option("--keep-etags", action="store", type="string",
52  default=None, dest="ketags", help=msg)
53  msg = "specify template directory, "
54  self.parser.add_option("--tdir", action="store", type="string",
55  default=tmpl_dir(), dest="tdir", help=msg)
56  msg = "list template tags"
57  self.parser.add_option("--tags", action="store_true",
58  default=False, dest="tags", help=msg)
59  msg = "list template example tags"
60  self.parser.add_option("--etags", action="store_true",
61  default=False, dest="etags", help=msg)
62  msg = "list supported templates"
63  self.parser.add_option("--templates", action="store_true",
64  default=False, dest="templates", help=msg)
65  def get_opt(self):
66  "Returns parse list of options"
67  return self.parser.parse_args()
68 
69 def parse_args(args):
70  "Parse input arguments"
71  output = {}
72  for arg in args:
73  key, val = arg.split('=')
74  key = key.strip()
75  val = val.strip()
76  if val[0] == '[' and val[-1] == ']':
77  val = eval(val, { "__builtins__": None }, {})
78  output[key] = val
79  return output
80 
81 def generator():
82  """
83  Code generator function, parse user arguments and load appropriate
84  template module. Once loaded, run its data method depending on
85  user requested input parameter, e.g. print_etags, print_tags or
86  generate template code.
87  """
88  optmgr = SkeletonOptionParser()
89  opts, args = optmgr.get_opt()
90  test_env(os.path.join(opts.tdir, opts.tmpl), opts.tmpl)
91  config = {'pname': opts.pname, 'tmpl': opts.tmpl, 'author': opts.author,
92  'args': parse_args(args), 'debug': opts.debug,
93  'ftype': opts.ftype, 'tmpl_dir': opts.tdir}
94  if opts.ketags:
95  etags = opts.ketags.split(',')
96  config.update({'tmpl_etags': etags})
97  else:
98  config.update({'tmpl_etags': []})
99  obj = code_generator(config)
100  if opts.etags:
101  obj.print_etags()
102  sys.exit(0)
103  elif opts.tags:
104  obj.print_tags()
105  sys.exit(0)
106  elif opts.templates:
107  for name in os.listdir(opts.tdir):
108  print name
109  sys.exit(0)
110  obj.generate()
111 
112 if __name__ == '__main__':
113  generator()
def generator
Definition: main.py:81
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
def tmpl_dir
Definition: main.py:22
def code_generator
Definition: utils.py:106
def parse_args
Definition: main.py:69
def test_env
Definition: utils.py:37