CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_7/src/FWCore/Skeletons/python/cms.py

Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #-*- coding: utf-8 -*-
00003 #pylint: disable-msg=
00004 """
00005 File       : cms.py
00006 Author     : Valentin Kuznetsov <vkuznet@gmail.com>
00007 Description: CMS-related utils
00008 """
00009 
00010 # system modules
00011 import os
00012 import sys
00013 
00014 # package modules
00015 from FWCore.Skeletons.utils import code_generator
00016 
00017 def config(tmpl, pkg_help, tmpl_dir):
00018     "Parse input arguments to mk-script"
00019     kwds  = {'author': '', 'tmpl': tmpl,
00020              'args': {}, 'debug': False, 'tmpl_dir': tmpl_dir}
00021     etags = []
00022     if  len(sys.argv) >= 2: # user give us arguments
00023         if  sys.argv[1] in ['-h', '--help', '-help']:
00024             print pkg_help
00025             sys.exit(0)
00026         kwds['pname'] = sys.argv[1]
00027         for idx in xrange(2, len(sys.argv)):
00028             opt = sys.argv[idx]
00029             if  opt == '-author':
00030                 kwds['author'] = sys.argv[idx+1]
00031                 continue
00032             if  opt.find('example') != -1:
00033                 etags.append('@%s' % opt)
00034                 continue
00035             if  opt in ['-h', '--help', '-help']:
00036                 print pkg_help
00037                 sys.exit(0)
00038             if  opt == '-debug':
00039                 kwds['debug'] = True
00040                 continue
00041     elif len(sys.argv) == 1:
00042         # need to walk
00043         msg = 'Please enter %s name: ' % tmpl.lower()
00044         kwds['pname'] = raw_input(msg)
00045     else:
00046         print pkg_help
00047         sys.exit(0)
00048     kwds['tmpl_etags'] = etags
00049     return kwds
00050 
00051 def cms_error():
00052     "Standard CMS error message"
00053     msg  = "\nPackages must be created in a 'subsystem'."
00054     msg += "\nPlease set your CMSSW environment and go to $CMSSW_BASE/src"
00055     msg += "\nCreate or choose directory from there and then "
00056     msg += "\nrun the script from that directory"
00057     return msg
00058 
00059 def test_cms_environment(tmpl):
00060     """
00061     Test CMS environment and requirements to run within CMSSW_BASE.
00062     Return True if we fullfill requirements and False otherwise.
00063     """
00064     base = os.environ.get('CMSSW_BASE', None)
00065     if  not base:
00066         return False, []
00067     cdir = os.getcwd()
00068     ldir = cdir.replace(os.path.join(base, 'src'), '')
00069     dirs = ldir.split('/')
00070     # test if we're within CMSSW_BASE/src/SubSystem area
00071     if  ldir and ldir[0] == '/' and len(dirs) == 2:
00072         return 'subsystem', ldir
00073     # test if we're within CMSSW_BASE/src/SubSystem/Pkg area
00074     if  ldir and ldir[0] == '/' and len(dirs) == 3:
00075         return 'package', ldir
00076     # test if we're within CMSSW_BASE/src/SubSystem/Pkg/src area
00077 #    if  ldir and ldir[0] == '/' and len(dirs) == 4 and dirs[-1] == 'src':
00078 #        return 'src', ldir
00079     # test if we're within CMSSW_BASE/src/SubSystem/Pkg/plugin area
00080 #    if  ldir and ldir[0] == '/' and len(dirs) == 4 and dirs[-1] == 'plugins':
00081 #        return 'plugins', ldir
00082     # test if we're within CMSSW_BASE/src/SubSystem/Pkg/dir area
00083     if  ldir and ldir[0] == '/' and len(dirs) == 4:
00084         return dirs[-1], ldir
00085     return False, ldir
00086 
00087 def generate(kwds):
00088     "Run generator code based on provided set of arguments"
00089     config = dict(kwds)
00090     tmpl   = kwds.get('tmpl')
00091     stand_alone_group = ['Record', 'Skeleton']
00092     config.update({'not_in_dir': stand_alone_group})
00093     if  tmpl in stand_alone_group:
00094         whereami, ldir = test_cms_environment(tmpl)
00095         dirs = ldir.split('/')
00096         config.update({'pkgname': kwds.get('pname')})
00097         config.update({'subsystem': 'Subsystem'})
00098         config.update({'pkgname': 'Package'})
00099         if  whereami:
00100             if  len(dirs) >= 3:
00101                 config.update({'subsystem': dirs[1]})
00102                 config.update({'pkgname': dirs[2]})
00103             elif len(dirs) >= 2:
00104                 config.update({'subsystem': dirs[1]})
00105                 config.update({'pkgname': dirs[1]})
00106     else:
00107         whereami, ldir = test_cms_environment(tmpl)
00108         dirs = ldir.split('/')
00109         if  not dirs or not whereami:
00110             print cms_error()
00111             sys.exit(1)
00112         config.update({'subsystem': dirs[1]})
00113         config.update({'pkgname': kwds.get('pname')})
00114         if  whereami in ['src', 'plugins']:
00115             config.update({'tmpl_files': '.cc'})
00116             config.update({'pkgname': dirs[2]})
00117         elif whereami == 'subsystem':
00118             config.update({'tmpl_files': 'all'})
00119         else:
00120             print cms_error()
00121             sys.exit(1)
00122     obj = code_generator(config)
00123     obj.generate()