CMS 3D CMS Logo

cms.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #-*- coding: utf-8 -*-
3 #pylint: disable-msg=
4 """
5 File : cms.py
6 Author : Valentin Kuznetsov <vkuznet@gmail.com>
7 Description: CMS-related utils
8 """
9 
10 # system modules
11 import os
12 import sys
13 
14 # package modules
15 from FWCore.Skeletons.utils import code_generator
16 
17 def config(tmpl, pkg_help, tmpl_dir):
18  "Parse input arguments to mk-script"
19  kwds = {'author': '', 'tmpl': tmpl,
20  'args': {}, 'debug': False, 'tmpl_dir': tmpl_dir}
21  etags = []
22  if len(sys.argv) >= 2: # user give us arguments
23  if sys.argv[1] in ['-h', '--help', '-help']:
24  print pkg_help
25  sys.exit(0)
26  kwds['pname'] = sys.argv[1]
27  for idx in xrange(2, len(sys.argv)):
28  opt = sys.argv[idx]
29  if opt == '-author':
30  kwds['author'] = sys.argv[idx+1]
31  continue
32  if opt.find('example') != -1:
33  etags.append('@%s' % opt)
34  continue
35  if opt in ['-h', '--help', '-help']:
36  print pkg_help
37  sys.exit(0)
38  if opt == '-debug':
39  kwds['debug'] = True
40  continue
41  elif len(sys.argv) == 1:
42  # need to walk
43  msg = 'Please enter %s name: ' % tmpl.lower()
44  kwds['pname'] = raw_input(msg)
45  else:
46  print pkg_help
47  sys.exit(0)
48  kwds['tmpl_etags'] = etags
49  return kwds
50 
51 def config_with_parser(tmpl, args, tmpl_dir):
52  """
53  Inject arguments parsed upstream into mk-scripts.
54  The arguments are parsed by the different front-ends(binaries)
55  and passed here via the args object.
56  """
57 
58  kwds = {'author': '', 'tmpl': tmpl,
59  'args': {}, 'debug': False, 'tmpl_dir': tmpl_dir}
60  etags = []
61  kwds['pname'] = args.subpackage_name
62  if args.author: kwds['author'] = args.author
63  if args.debug: kwds['debug'] = True
64  if args.example: etags.append('@%s' % args.example)
65  kwds['tmpl_etags'] = etags
66  return kwds
67 
68 def cms_error():
69  "Standard CMS error message"
70  msg = "\nPackages must be created in a 'subsystem'."
71  msg += "\nPlease set your CMSSW environment and go to $CMSSW_BASE/src"
72  msg += "\nCreate or choose directory from there and then "
73  msg += "\nrun the script from that directory"
74  return msg
75 
77  """
78  Test CMS environment and requirements to run within CMSSW_BASE.
79  Return True if we fullfill requirements and False otherwise.
80  """
81  base = os.environ.get('CMSSW_BASE', None)
82  if not base:
83  return False, []
84  cdir = os.getcwd()
85  ldir = cdir.replace(os.path.join(base, 'src'), '')
86  dirs = ldir.split('/')
87  # test if we're within CMSSW_BASE/src/SubSystem area
88  if ldir and ldir[0] == '/' and len(dirs) == 2:
89  return 'subsystem', ldir
90  # test if we're within CMSSW_BASE/src/SubSystem/Pkg area
91  if ldir and ldir[0] == '/' and len(dirs) == 3:
92  return 'package', ldir
93  # test if we're within CMSSW_BASE/src/SubSystem/Pkg/src area
94 # if ldir and ldir[0] == '/' and len(dirs) == 4 and dirs[-1] == 'src':
95 # return 'src', ldir
96  # test if we're within CMSSW_BASE/src/SubSystem/Pkg/plugin area
97 # if ldir and ldir[0] == '/' and len(dirs) == 4 and dirs[-1] == 'plugins':
98 # return 'plugins', ldir
99  # test if we're within CMSSW_BASE/src/SubSystem/Pkg/dir area
100  if ldir and ldir[0] == '/' and len(dirs) == 4:
101  return dirs[-1], ldir
102  return False, ldir
103 
104 def generate(kwds):
105  "Run generator code based on provided set of arguments"
106  config = dict(kwds)
107  tmpl = kwds.get('tmpl')
108  stand_alone_group = ['Record', 'Skeleton']
109  config.update({'not_in_dir': stand_alone_group})
110  if tmpl in stand_alone_group:
111  whereami, ldir = test_cms_environment(tmpl)
112  dirs = ldir.split('/')
113  config.update({'pkgname': kwds.get('pname')})
114  config.update({'subsystem': 'Subsystem'})
115  config.update({'pkgname': 'Package'})
116  if whereami:
117  if len(dirs) >= 3:
118  config.update({'subsystem': dirs[1]})
119  config.update({'pkgname': dirs[2]})
120  elif len(dirs) >= 2:
121  config.update({'subsystem': dirs[1]})
122  config.update({'pkgname': dirs[1]})
123  else:
124  whereami, ldir = test_cms_environment(tmpl)
125  dirs = ldir.split('/')
126  if not dirs or not whereami:
127  print cms_error()
128  sys.exit(1)
129  config.update({'subsystem': dirs[1]})
130  config.update({'pkgname': kwds.get('pname')})
131  if whereami in ['src', 'plugins']:
132  config.update({'tmpl_files': '.cc'})
133  config.update({'pkgname': dirs[2]})
134  elif whereami == 'subsystem':
135  config.update({'tmpl_files': 'all'})
136  else:
137  print cms_error()
138  sys.exit(1)
139  obj = code_generator(config)
140  obj.generate()
def test_cms_environment(tmpl)
Definition: cms.py:76
def cms_error()
Definition: cms.py:68
def config_with_parser(tmpl, args, tmpl_dir)
Definition: cms.py:51
def config(tmpl, pkg_help, tmpl_dir)
Definition: cms.py:17
def code_generator(kwds)
Definition: utils.py:106
def generate(kwds)
Definition: cms.py:104