CMS 3D CMS Logo

List of all members | Public Member Functions | Public Attributes
pkg.AbstractPkg Class Reference
Inheritance diagram for pkg.AbstractPkg:

Public Member Functions

def __init__ (self, config=None)
 
def generate (self)
 
def get_kwds (self)
 
def parse_etags (self, line)
 
def print_etags (self)
 
def print_tags (self)
 
def tmpl_etags (self)
 
def tmpl_tags (self)
 
def write (self, fname, tmpl_name, kwds)
 

Public Attributes

 author
 
 config
 
 date
 
 debug
 
 not_in_dir
 
 pname
 
 tdir
 
 tmpl
 

Detailed Description

AbstractPkg takes care how to generate code from template/PKG
package area. The PKG can be any directory which may include
any types of files, e.g. C++ (.cc), python (.py), etc.
This class relies on specific logic which we outline here:

    - each template may use tags defined with double underscores
      enclosure, e.g. __class__, __record__, etc.
    - each template may have example tags, such tags should
      start with @example_. While processing template user may
      choose to strip them off or keep the code behind those tags
    - in addition user may specify pure python code which can
      operate with user defined tags. This code snipped should
      be enclosed with #python_begin and #python_end lines
      which declares start and end of python block

Definition at line 21 of file pkg.py.

Constructor & Destructor Documentation

def pkg.AbstractPkg.__init__ (   self,
  config = None 
)

Definition at line 38 of file pkg.py.

38  def __init__(self, config=None):
39  super(AbstractPkg, self).__init__()
40  if not config:
41  self.config = {}
42  else:
43  self.config = config
44  self.pname = self.config.get('pname', None)
45  self.tmpl = self.config.get('tmpl', None)
46  self.debug = self.config.get('debug', 0)
47  self.tdir = self.config.get('tmpl_dir')
48  self.author = user_info(self.config.get('author', None))
49  self.date = time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime())
50  self.not_in_dir = self.config.get('not_in_dir', [])
51 
def __init__(self, config=None)
Definition: pkg.py:38
def user_info(ainput=None)
Definition: utils.py:96

Member Function Documentation

def pkg.AbstractPkg.generate (   self)

Definition at line 156 of file pkg.py.

References DTDigiToRawModule.debug, CSCRPCData.debug, CSCDCCUnpacker.debug, ALIUtils.debug, CSCTMBScope.debug, crabFunctions.CrabController.debug, DTDigiToRaw.debug, EgammaProbeSelector.debug, DTTTrigCorrectionFirst.debug, DTSegmentsTask.debug, pkg.AbstractPkg.debug, CSCDCCEventData.debug, DTTPDeadWriter.debug, DTTTrigWriter.debug, DTT0CalibrationRMS.debug, SiPixelRawToDigi.debug, SiPixelDigiToRaw.debug, DTDDUUnpacker.debug, DTT0Calibration.debug, DTOccupancyPoint.debug, util.rrapi.RRApi.debug, DTNoiseComputation.debug, DTROS25Unpacker.debug, DTTTrigSyncT0Only.debug, DTTTrigCalibration.debug, DTEfficiencyTask.debug, CSCDDUEventData.debug, DTRunConditionVar.debug, HCALResponse.debug, DTChamberEfficiencyTask.debug, DTTMax.debug, DTChamberEfficiency.debug, CSCCLCTData.debug, DTVDriftCalibration.debug, PixelDataFormatter.debug, DTTTrigSyncFromDB.debug, CSCTMBData.debug, DTTTrigSyncTOFCorr.debug, BaseParticlePropagator.debug, RPCEfficiency.debug, CSCEventData.debug, CSCTMBHeader.debug, CSCALCTTrailer.debug, CSCALCTHeader.debug, crabFunctions.CrabTask.debug, spr.find(), pkg.AbstractPkg.get_kwds(), join(), pkg.AbstractPkg.not_in_dir, pkg.AbstractPkg.pname, python.rootplot.root2matplotlib.replace(), split, pkg.AbstractPkg.tdir, pkg.AbstractPkg.tmpl, edm::ThreadSafeOutputFileStream.write(), Json::Writer.write(), AlignmentCorrelationsIO.write(), pos::PixelConfig.write(), FileBlob.write(), edm::RunLumiEntryInfo.write(), HcalSiPMParametersPopConAnalyzer.write(), HcalTPChannelParametersPopConAnalyzer.write(), AlignmentCorrelationsIORoot.write(), edm::EventEntryInfo.write(), HcalSiPMCharacteristicsPopConAnalyzer.write(), HcalTPParametersPopConAnalyzer.write(), SurveyOutput.write(), edm::AsciiOutputModule.write(), ExTestEcalChannelStatusAnalyzer.write(), personalPlayback.Applet.write(), HcalFrontEndMapPopConAnalyzer.write(), HcalChannelQualityPopConAnalyzer.write(), HcalElectronicsMapPopConAnalyzer.write(), HcalQIEDataPopConAnalyzer.write(), HcalTimingParamsPopConAnalyzer.write(), HcalFlagHFDigiTimeParamsPopConAnalyzer.write(), HcalRecoParamsPopConAnalyzer.write(), HcalMCParamsPopConAnalyzer.write(), CastorElectronicsMapPopConAnalyzer.write(), CastorGainsPopConAnalyzer.write(), CastorGainWidthsPopConAnalyzer.write(), CastorPedestalsPopConAnalyzer.write(), CastorPedestalWidthsPopConAnalyzer.write(), CastorQIEDataPopConAnalyzer.write(), CastorRecoParamsPopConAnalyzer.write(), CastorSaturationCorrsPopConAnalyzer.write(), HcalDcsMapPopConAnalyzer.write(), HcalDcsValuesPopConAnalyzer.write(), CastorChannelQualityPopConAnalyzer.write(), HcalGainsPopConAnalyzer.write(), HcalGainWidthsPopConAnalyzer.write(), HcalLongRecoParamsPopConAnalyzer.write(), HcalLUTCorrsPopConAnalyzer.write(), HcalLutMetadataPopConAnalyzer.write(), HcalPedestalsPopConAnalyzer.write(), HcalPedestalWidthsPopConAnalyzer.write(), HcalPFCorrsPopConAnalyzer.write(), HcalQIETypesPopConAnalyzer.write(), HcalRespCorrsPopConAnalyzer.write(), HcalTimeCorrsPopConAnalyzer.write(), HcalL1TriggerObjectsPopConAnalyzer.write(), HcalValidationCorrsPopConAnalyzer.write(), HcalZSThresholdsPopConAnalyzer.write(), MultiFileBlob.write(), edm::RunAuxiliary.write(), HcalZDCLowGainFractionsPopConAnalyzer.write(), SiPixelConfigWriter.write(), AlignmentParametersIO.write(), edm::EventEntryDescription.write(), Json::FastWriter.write(), AlignmentUserVariablesIO.write(), SiStripConfigWriter.write(), edm::ProductProvenance.write(), edm::LuminosityBlockAuxiliary.write(), edm::GetProductCheckerOutputModule.write(), popcon::PopConAnalyzer< S >.write(), RawEventOutputModuleForBU< Consumer >.write(), edm::ProvenanceCheckerOutputModule.write(), edm::Parentage.write(), popcon::PopCon.write(), edm::ModuleDescription.write(), electronCompare.flushfile.write(), MuonAlignmentOutputXML.write(), l1t::LUT.write(), electronStore.flushfile.write(), edm::StableProvenance.write(), CSCPairResidualsConstraint.write(), edm::EventAuxiliary.write(), XMLDOMBlock.write(), edmplugin::CacheParser.write(), Benchmark.write(), edm::BranchDescription.write(), XMLDocument.write, uploads.output.write(), edm::Provenance.write(), Json::StyledWriter.write(), amc13::Packet.write(), ztee.GZipLog.write(), MuonResidualsTwoBin.write(), presentation.SubsectionBase.write(), DTWorkflow.DTWorkflow.write(), edm::FileInPath.write(), pkg.AbstractPkg.write(), Json::StyledStreamWriter.write(), edm::ProductResolverBase.write(), XMLProcessor.write(), esMonitoring.LineHistoryEnd.write(), DQMRootOutputModule.write(), esMonitoring.LineHistoryStart.write(), MuonResidualsFitter.write(), edm::OutputModule.write(), betterConfigParser.BetterConfigParser.write(), edm::one::OutputModuleBase.write(), edm::global::OutputModuleBase.write(), edm::limited::OutputModuleBase.write(), and querying.connection.write().

156  def generate(self):
157  "Generate package templates in a given directory"
158 
159  # keep current location, since generate will switch directories
160  cdir = os.getcwd()
161 
162  # read from configutation which template files to create
163  tmpl_files = self.config.get('tmpl_files', 'all')
164 
165  # setup keyword arguments which we'll pass to write method
166  kwds = self.get_kwds()
167 
168  # create template package dir and cd into it
169  if tmpl_files == 'all' and self.tmpl not in self.not_in_dir:
170  if os.path.isdir(self.pname):
171  msg = "Can't create package '%s'\n" % self.pname
172  msg += "Directory %s is already exists" % self.pname
173  print msg
174  sys.exit(1)
175  os.makedirs(self.pname)
176  os.chdir(self.pname)
177 
178  # read directory driver information and create file list to generate
179  sdir = os.path.join(self.tdir, self.tmpl)
180  sources = [s for s in os.listdir(sdir) \
181  if s != 'Driver.dir' and s.find('~') == -1]
182  driver = os.path.join(sdir, 'Driver.dir')
183  if os.path.isfile(driver):
184  sources = [s.replace('\n', '') for s in open(driver, 'r').readlines()]
185  if 'CVS' in sources:
186  sources.remove('CVS')
187 
188  # special case of Skeleton, which requires to generate only given
189  # file type if self.pname has extension of that type
190  names = set([s.split('.')[0] for s in sources])
191  if names == set(['Skeleton']):
192  if self.pname.find('.') != -1:
193  _, ext = os.path.splitext(self.pname)
194  sources = [s for s in sources if s.rfind(ext) != -1]
195  self.pname = self.pname.replace(ext, '')
196  kwds = self.get_kwds()
197  if not sources:
198  msg = 'Unable to find skeleton for extension "%s"' % ext
199  print msg
200  sys.exit(1)
201  bdir = os.environ.get('CMSSW_BASE', '')
202  dirs = os.getcwd().replace(bdir, '').split('/')
203  ldir = os.getcwd().split('/')[-1]
204  idir = ''
205  subsys = kwds['__subsys__']
206  pkgname = kwds['__pkgname__']
207  if sources == ['Skeleton.cc', 'Skeleton.h']:
208  if ldir == 'interface' and os.getcwd().find(bdir) != -1:
209  idir = '%s/%s/interface/' % (subsys, pkgname)
210  # run within some directory of the Sybsystem/Pkg area
211  # and only for mkskel <file>.cc
212  elif sources == ['Skeleton.cc'] and \
213  len(dirs) == 5 and dirs[0] == '' and dirs[1] == 'src':
214  idir = '%s/%s/interface/' % (subsys, pkgname)
215  elif sources == ['Skeleton.h'] and ldir == 'interface' and \
216  len(dirs) == 5 and dirs[0] == '' and dirs[1] == 'src':
217  idir = '%s/%s/interface/' % (subsys, pkgname)
218  kwds.update({'__incdir__': idir})
219 
220  # loop over source files, create dirs as necessary and generate files
221  # names for writing templates
222  gen_files = []
223  for src in sources:
224  if tmpl_files != 'all':
225  fname, ext = os.path.splitext(src)
226  if tmpl_files != ext:
227  continue
228  src = src.split('/')[-1]
229  if self.debug:
230  print "Read", src
231  items = src.split('/')
232  if items[-1] == '/':
233  items = items[:-1]
234  tname = items[-1] # template file name
235  tmpl_name = os.path.join(sdir, items[-1]) # full tmpl file name
236  if os.path.isfile(tmpl_name):
237  ftype = 'file'
238  else:
239  ftype = 'dir'
240  name2gen = src # new file we'll create
241  if tname.split('.')[0] == self.tmpl: # need to substitute
242  name2gen = name2gen.replace(self.tmpl, self.pname)
243  name2gen = os.path.join(os.getcwd(), name2gen)
244  if self.debug:
245  print "Create", name2gen
246  if ftype == 'dir':
247  if not os.path.isdir(name2gen):
248  os.makedirs(name2gen)
249  continue # we're done with dir
250  fdir = os.path.dirname(name2gen)
251  if not os.path.isdir(fdir):
252  os.makedirs(fdir)
253  self.write(name2gen, tmpl_name, kwds)
254  gen_files.append(name2gen.split('/')[-1])
255  if tmpl_files == 'all' and self.tmpl not in self.not_in_dir:
256  msg = 'New package "%s" of %s type is successfully generated' \
257  % (self.pname, self.tmpl)
258  else:
259  msg = 'Generated %s file' % ', '.join(gen_files)
260  if len(gen_files) > 1:
261  msg += 's'
262  print msg
263  # return back where we started
264  os.chdir(cdir)
265  if msg.find('New package') != -1:
266  tree(self.pname)
267 
def get_kwds(self)
Definition: pkg.py:139
def replace(string, replacements)
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:20
def generate(self)
Definition: pkg.py:156
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
def write(self, fname, tmpl_name, kwds)
Definition: pkg.py:112
Definition: tree.py:1
double split
Definition: MVATrainer.cc:139
def pkg.AbstractPkg.get_kwds (   self)

Definition at line 139 of file pkg.py.

References pkg.AbstractPkg.author, pkg.AbstractPkg.date, DTDigiToRawModule.debug, CSCRPCData.debug, CSCDCCUnpacker.debug, ALIUtils.debug, CSCTMBScope.debug, crabFunctions.CrabController.debug, DTDigiToRaw.debug, EgammaProbeSelector.debug, DTTTrigCorrectionFirst.debug, DTSegmentsTask.debug, pkg.AbstractPkg.debug, DTTPDeadWriter.debug, CSCDCCEventData.debug, DTT0CalibrationRMS.debug, DTTTrigWriter.debug, SiPixelDigiToRaw.debug, SiPixelRawToDigi.debug, DTDDUUnpacker.debug, DTT0Calibration.debug, DTOccupancyPoint.debug, util.rrapi.RRApi.debug, DTNoiseComputation.debug, DTROS25Unpacker.debug, DTTTrigSyncT0Only.debug, DTTTrigCalibration.debug, DTEfficiencyTask.debug, CSCDDUEventData.debug, DTRunConditionVar.debug, HCALResponse.debug, DTChamberEfficiencyTask.debug, DTTMax.debug, DTChamberEfficiency.debug, CSCCLCTData.debug, DTVDriftCalibration.debug, PixelDataFormatter.debug, DTTTrigSyncFromDB.debug, CSCTMBData.debug, DTTTrigSyncTOFCorr.debug, BaseParticlePropagator.debug, RPCEfficiency.debug, CSCEventData.debug, CSCTMBHeader.debug, CSCALCTTrailer.debug, CSCALCTHeader.debug, crabFunctions.CrabTask.debug, and pkg.AbstractPkg.pname.

Referenced by pkg.AbstractPkg.generate().

139  def get_kwds(self):
140  "Return keyword arguments to be used in methods"
141  kwds = {'__pkgname__': self.config.get('pkgname', 'Package'),
142  '__author__': self.author,
143  '__user__': os.getlogin(),
144  '__date__': self.date,
145  '__class__': self.pname,
146  '__class_lowercase__': self.pname.lower(),
147  '__name__': self.pname,
148  '__subsys__': self.config.get('subsystem', 'Subsystem')}
149  args = self.config.get('args', None)
150  kwds.update(args)
151  if self.debug:
152  print "Template tags:"
153  pprint.pprint(kwds)
154  return kwds
155 
def get_kwds(self)
Definition: pkg.py:139
def pkg.AbstractPkg.parse_etags (   self,
  line 
)
Determine either skip or keep given line based on class tags
meta-strings

Definition at line 95 of file pkg.py.

References pkg.AbstractPkg.tmpl_etags().

Referenced by pkg.AbstractPkg.write().

95  def parse_etags(self, line):
96  """
97  Determine either skip or keep given line based on class tags
98  meta-strings
99  """
100  tmpl_etags = self.tmpl_etags()
101  keep_etags = self.config.get('tmpl_etags', [])
102  for tag in tmpl_etags:
103  for valid_tag in keep_etags:
104  if line.find(valid_tag) != -1:
105  line = line.replace(valid_tag, '')
106  return line
107  if line.find(tag) != -1:
108  line = ''
109  return line
110  return line
111 
def parse_etags(self, line)
Definition: pkg.py:95
def tmpl_etags(self)
Definition: pkg.py:52
def pkg.AbstractPkg.print_etags (   self)

Definition at line 69 of file pkg.py.

References pkg.AbstractPkg.tmpl_etags().

69  def print_etags(self):
70  "Print out template example tags"
71  for key in self.tmpl_etags():
72  print key
73 
def print_etags(self)
Definition: pkg.py:69
def tmpl_etags(self)
Definition: pkg.py:52
def pkg.AbstractPkg.print_tags (   self)

Definition at line 90 of file pkg.py.

References pkg.AbstractPkg.tmpl_tags().

90  def print_tags(self):
91  "Print out template keys"
92  for key in self.tmpl_tags():
93  print key
94 
def print_tags(self)
Definition: pkg.py:90
def tmpl_tags(self)
Definition: pkg.py:74
def pkg.AbstractPkg.tmpl_etags (   self)

Definition at line 52 of file pkg.py.

References pkg.AbstractPkg.tdir, and pkg.AbstractPkg.tmpl.

Referenced by pkg.AbstractPkg.parse_etags(), and pkg.AbstractPkg.print_etags().

52  def tmpl_etags(self):
53  "Scan template files and return example tags"
54  keys = []
55  sdir = '%s/%s' % (self.tdir, self.tmpl)
56  for name in os.listdir(sdir):
57  if name[-1] == '~':
58  continue
59  if name == 'CVS':
60  continue
61  fname = os.path.join(sdir, name)
62  with open(fname, 'r') as stream:
63  for line in stream.readlines():
64  if line.find('@example_') != -1: # possible tag
65  keys += [k for k in line.split() if \
66  k.find('@example_') != -1]
67  return set(keys)
68 
def tmpl_etags(self)
Definition: pkg.py:52
def pkg.AbstractPkg.tmpl_tags (   self)

Definition at line 74 of file pkg.py.

References utils.parse_word(), pkg.AbstractPkg.tdir, and pkg.AbstractPkg.tmpl.

Referenced by pkg.AbstractPkg.print_tags().

74  def tmpl_tags(self):
75  "Scan template files and return template tags"
76  keys = []
77  sdir = '%s/%s' % (self.tdir, self.tmpl)
78  for name in os.listdir(sdir):
79  if name[-1] == '~':
80  continue
81  if name == 'CVS':
82  continue
83  fname = os.path.join(sdir, name)
84  with open(fname, 'r') as stream:
85  for line in stream.readlines():
86  if line.find('__') != -1: # possible key
87  keys += [k for k in parse_word(line)]
88  return set(keys)
89 
def tmpl_tags(self)
Definition: pkg.py:74
def parse_word(word)
Definition: utils.py:21
def pkg.AbstractPkg.write (   self,
  fname,
  tmpl_name,
  kwds 
)

Definition at line 112 of file pkg.py.

References DTDigiToRawModule.debug, CSCRPCData.debug, CSCDCCUnpacker.debug, CSCTMBScope.debug, ALIUtils.debug, crabFunctions.CrabController.debug, DTDigiToRaw.debug, EgammaProbeSelector.debug, DTTTrigCorrectionFirst.debug, DTSegmentsTask.debug, pkg.AbstractPkg.debug, DTTPDeadWriter.debug, CSCDCCEventData.debug, DTTTrigWriter.debug, DTT0CalibrationRMS.debug, SiPixelDigiToRaw.debug, SiPixelRawToDigi.debug, DTDDUUnpacker.debug, DTT0Calibration.debug, DTOccupancyPoint.debug, util.rrapi.RRApi.debug, DTNoiseComputation.debug, DTROS25Unpacker.debug, DTTTrigSyncT0Only.debug, DTTTrigCalibration.debug, DTEfficiencyTask.debug, DTRunConditionVar.debug, CSCDDUEventData.debug, HCALResponse.debug, DTChamberEfficiencyTask.debug, DTTMax.debug, DTChamberEfficiency.debug, CSCCLCTData.debug, DTVDriftCalibration.debug, PixelDataFormatter.debug, DTTTrigSyncFromDB.debug, CSCTMBData.debug, DTTTrigSyncTOFCorr.debug, BaseParticlePropagator.debug, RPCEfficiency.debug, CSCEventData.debug, CSCTMBHeader.debug, CSCALCTTrailer.debug, CSCALCTHeader.debug, crabFunctions.CrabTask.debug, utils.functor(), and pkg.AbstractPkg.parse_etags().

Referenced by pkg.AbstractPkg.generate().

112  def write(self, fname, tmpl_name, kwds):
113  "Create new file from given template name and set of arguments"
114  code = ""
115  read_code = False
116  with open(fname, 'w') as stream:
117  for line in open(tmpl_name, 'r').readlines():
118  line = self.parse_etags(line)
119  if not line:
120  continue
121  if line.find('#python_begin') != -1:
122  read_code = True
123  continue
124  if line.find('#python_end') != -1:
125  read_code = False
126  if read_code:
127  code += line
128  if code and not read_code:
129  res = functor(code, kwds, self.debug)
130  stream.write(res)
131  code = ""
132  continue
133  if not read_code:
134  for key, val in kwds.items():
135  if isinstance(val, basestring):
136  line = line.replace(key, val)
137  stream.write(line)
138 
def parse_etags(self, line)
Definition: pkg.py:95
def write(self, fname, tmpl_name, kwds)
Definition: pkg.py:112
def functor(code, kwds, debug=0)
Definition: utils.py:54

Member Data Documentation

pkg.AbstractPkg.author

Definition at line 48 of file pkg.py.

Referenced by pkg.AbstractPkg.get_kwds().

pkg.AbstractPkg.config

Definition at line 41 of file pkg.py.

pkg.AbstractPkg.date

Definition at line 49 of file pkg.py.

Referenced by pkg.AbstractPkg.get_kwds().

pkg.AbstractPkg.debug
pkg.AbstractPkg.not_in_dir

Definition at line 50 of file pkg.py.

Referenced by pkg.AbstractPkg.generate().

pkg.AbstractPkg.pname

Definition at line 44 of file pkg.py.

Referenced by pkg.AbstractPkg.generate(), and pkg.AbstractPkg.get_kwds().

pkg.AbstractPkg.tdir
pkg.AbstractPkg.tmpl