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