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