test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 cms_error():
52  "Standard CMS error message"
53  msg = "\nPackages must be created in a 'subsystem'."
54  msg += "\nPlease set your CMSSW environment and go to $CMSSW_BASE/src"
55  msg += "\nCreate or choose directory from there and then "
56  msg += "\nrun the script from that directory"
57  return msg
58 
60  """
61  Test CMS environment and requirements to run within CMSSW_BASE.
62  Return True if we fullfill requirements and False otherwise.
63  """
64  base = os.environ.get('CMSSW_BASE', None)
65  if not base:
66  return False, []
67  cdir = os.getcwd()
68  ldir = cdir.replace(os.path.join(base, 'src'), '')
69  dirs = ldir.split('/')
70  # test if we're within CMSSW_BASE/src/SubSystem area
71  if ldir and ldir[0] == '/' and len(dirs) == 2:
72  return 'subsystem', ldir
73  # test if we're within CMSSW_BASE/src/SubSystem/Pkg area
74  if ldir and ldir[0] == '/' and len(dirs) == 3:
75  return 'package', ldir
76  # test if we're within CMSSW_BASE/src/SubSystem/Pkg/src area
77 # if ldir and ldir[0] == '/' and len(dirs) == 4 and dirs[-1] == 'src':
78 # return 'src', ldir
79  # test if we're within CMSSW_BASE/src/SubSystem/Pkg/plugin area
80 # if ldir and ldir[0] == '/' and len(dirs) == 4 and dirs[-1] == 'plugins':
81 # return 'plugins', ldir
82  # test if we're within CMSSW_BASE/src/SubSystem/Pkg/dir area
83  if ldir and ldir[0] == '/' and len(dirs) == 4:
84  return dirs[-1], ldir
85  return False, ldir
86 
87 def generate(kwds):
88  "Run generator code based on provided set of arguments"
89  config = dict(kwds)
90  tmpl = kwds.get('tmpl')
91  stand_alone_group = ['Record', 'Skeleton']
92  config.update({'not_in_dir': stand_alone_group})
93  if tmpl in stand_alone_group:
94  whereami, ldir = test_cms_environment(tmpl)
95  dirs = ldir.split('/')
96  config.update({'pkgname': kwds.get('pname')})
97  config.update({'subsystem': 'Subsystem'})
98  config.update({'pkgname': 'Package'})
99  if whereami:
100  if len(dirs) >= 3:
101  config.update({'subsystem': dirs[1]})
102  config.update({'pkgname': dirs[2]})
103  elif len(dirs) >= 2:
104  config.update({'subsystem': dirs[1]})
105  config.update({'pkgname': dirs[1]})
106  else:
107  whereami, ldir = test_cms_environment(tmpl)
108  dirs = ldir.split('/')
109  if not dirs or not whereami:
110  print cms_error()
111  sys.exit(1)
112  config.update({'subsystem': dirs[1]})
113  config.update({'pkgname': kwds.get('pname')})
114  if whereami in ['src', 'plugins']:
115  config.update({'tmpl_files': '.cc'})
116  config.update({'pkgname': dirs[2]})
117  elif whereami == 'subsystem':
118  config.update({'tmpl_files': 'all'})
119  else:
120  print cms_error()
121  sys.exit(1)
122  obj = code_generator(config)
123  obj.generate()
def generate
Definition: cms.py:87
def cms_error
Definition: cms.py:51
def test_cms_environment
Definition: cms.py:59
def code_generator
Definition: utils.py:106
def config
Definition: cms.py:17