Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 11:03:46

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