File indexing completed on 2024-12-01 23:40:21
0001
0002
0003
0004 """
0005 File : cms.py
0006 Author : Valentin Kuznetsov <vkuznet@gmail.com>
0007 Description: CMS-related utils
0008 """
0009
0010
0011 from builtins import range
0012 import os
0013 import sys
0014
0015
0016 from FWCore.Skeletons.utils import code_generator
0017
0018 def config(tmpl, pkg_help):
0019 "Parse input arguments to mk-script"
0020 kwds = {'author': '', 'tmpl': tmpl,
0021 'args': {}, 'debug': False,
0022 'working_dir': ''}
0023 etags = []
0024 if len(sys.argv) >= 2:
0025 if sys.argv[1] in ['-h', '--help', '-help']:
0026 print(pkg_help)
0027 sys.exit(0)
0028 kwds['pname'] = sys.argv[1]
0029 for idx in range(2, len(sys.argv)):
0030 opt = sys.argv[idx]
0031 if opt == '-author':
0032 kwds['author'] = sys.argv[idx+1]
0033 continue
0034 if opt.find('example') != -1:
0035 etags.append('@%s' % opt)
0036 continue
0037 if opt in ['-h', '--help', '-help']:
0038 print(pkg_help)
0039 sys.exit(0)
0040 if opt == '-debug':
0041 kwds['debug'] = True
0042 continue
0043 elif len(sys.argv) == 1:
0044
0045 msg = 'Please enter %s name: ' % tmpl.lower()
0046 kwds['pname'] = input(msg)
0047 else:
0048 print(pkg_help)
0049 sys.exit(0)
0050 kwds['tmpl_etags'] = etags
0051 return kwds
0052
0053 def config_with_parser(tmpl, args):
0054 """
0055 Inject arguments parsed upstream into mk-scripts.
0056 The arguments are parsed by the different front-ends(binaries)
0057 and passed here via the args object.
0058 """
0059
0060 kwds = {'author': '', 'tmpl': tmpl,
0061 'args': {}, 'debug': False}
0062 etags = []
0063 kwds['pname'] = args.subpackage_name
0064 if args.author: kwds['author'] = args.author
0065 if args.debug: kwds['debug'] = True
0066 if args.example: etags.append('@%s' % args.example)
0067 kwds['tmpl_etags'] = etags
0068 return kwds
0069
0070 def cms_error():
0071 "Standard CMS error message"
0072 msg = "\nPackages must be created in a 'subsystem'."
0073 msg += "\nPlease set your CMSSW environment and go to $CMSSW_BASE/src"
0074 msg += "\nCreate or choose directory from there and then "
0075 msg += "\nrun the script from that directory"
0076 return msg
0077
0078 def test_cms_environment(tmpl):
0079 """
0080 Test CMS environment and requirements to run within CMSSW_BASE.
0081 Return True if we fullfill requirements and False otherwise.
0082 """
0083 base = os.environ.get('CMSSW_BASE', None)
0084 if not base:
0085 return False, []
0086 cdir = os.getcwd()
0087 ldir = cdir.replace(os.path.join(base, 'src'), '')
0088 dirs = ldir.split('/')
0089
0090 if ldir and ldir[0] == '/' and len(dirs) == 2:
0091 return 'subsystem', ldir
0092
0093 if ldir and ldir[0] == '/' and len(dirs) == 3:
0094 return 'package', ldir
0095
0096
0097
0098
0099
0100
0101
0102 if ldir and ldir[0] == '/' and len(dirs) == 4:
0103 return dirs[-1], ldir
0104 return False, ldir
0105
0106 def generate(kwds):
0107 "Run generator code based on provided set of arguments"
0108 config = dict(kwds)
0109 tmpl = kwds.get('tmpl')
0110 stand_alone_group = ['Record', 'Skeleton']
0111 config.update({'not_in_dir': stand_alone_group})
0112 if tmpl in stand_alone_group:
0113 whereami, ldir = test_cms_environment(tmpl)
0114 dirs = ldir.split('/')
0115 config.update({'pkgname': kwds.get('pname')})
0116 config.update({'subsystem': 'Subsystem'})
0117 config.update({'pkgname': 'Package'})
0118 if whereami:
0119 if len(dirs) >= 3:
0120 config.update({'subsystem': dirs[1]})
0121 config.update({'pkgname': dirs[2]})
0122 elif len(dirs) >= 2:
0123 config.update({'subsystem': dirs[1]})
0124 config.update({'pkgname': dirs[1]})
0125 else:
0126 whereami, ldir = test_cms_environment(tmpl)
0127 dirs = ldir.split('/')
0128 if not dirs or not whereami:
0129 print(cms_error())
0130 sys.exit(1)
0131 config.update({'subsystem': dirs[1]})
0132 config.update({'pkgname': kwds.get('pname')})
0133 if whereami in ['src', 'plugins']:
0134 config.update({'working_dir': whereami})
0135 config.update({'tmpl_files': '.cc'})
0136 config.update({'pkgname': dirs[2]})
0137 elif whereami == 'test':
0138 config.update({'working_dir': whereami})
0139 config.update({'tmpl_files':'.cc'})
0140 config.update({'pkgname': dirs[2]})
0141 elif whereami == 'subsystem':
0142 config.update({'tmpl_files': 'all'})
0143 else:
0144 print(cms_error())
0145 sys.exit(1)
0146 obj = code_generator(config)
0147 obj.generate()