CMS 3D CMS Logo

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