ATLAS Offline Software
Classes | Functions | Variables
python.scripts.cmake_newanalysisalg Namespace Reference

Classes

class  Templates
 

Functions

def main (args)
 

Variables

 __version__
 
 __author__
 
 __doc__
 

Function Documentation

◆ main()

def python.scripts.cmake_newanalysisalg.main (   args)
create a new AthAnalysisAlgorithm inside the current package. Call from within the package directory

ex:
 $ acmd cmake new-analysisalg MyAlg

Definition at line 288 of file cmake_newanalysisalg.py.

288 def main(args):
289  """create a new AthAnalysisAlgorithm inside the current package. Call from within the package directory
290 
291  ex:
292  $ acmd cmake new-analysisalg MyAlg
293  """
294  sc = 0
295 
296  full_alg_name = args.algname
297 
298  #determine the package from the cwd
299  cwd = os.getcwd()
300  #check that src dir exists and CMakeLists.txt exists (i.e. this is a package)
301  if not os.path.isdir(cwd+"/src") or not os.path.isfile(cwd+"/CMakeLists.txt"):
302  print("ERROR you must call new-analysisalg from within the package you want to add the algorithm to")
303  return -1
304 
305 
306  full_pkg_name = os.path.basename(cwd)
307  print(textwrap.dedent("""\
308  ::: create alg [%(full_alg_name)s] in pkg [%(full_pkg_name)s]""" %locals()))
309 
310 
311  #first we must check that CMakeLists.txt file has the AthAnalysisBaseComps dependency in it
312  foundBaseComps=False
313  hasxAODEventInfo=False
314  hasAtlasROOT=False
315  hasAsgTools=False
316  lastUse=0
317  lineCount=0
318  hasLibraryLine=False
319  hasComponentLine=False
320  for line in open('CMakeLists.txt'):
321  lineCount +=1
322  if "atlas_add_library" in line: hasLibraryLine=True
323  if "atlas_add_component" in line: hasComponentLine=True
324 
325 #GOT THIS FAR WITH EDITING
326 
327 
328 
329 
330 
331  #following code borrowed from gen_klass
332  hdr = Templates.alg_hdr_template
333  cxx = Templates.alg_cxx_template
334 
335  namespace_klass = full_alg_name.replace('::','__')
336  namespace_begin,namespace_end = "",""
337  namespace = ""
338  if full_alg_name.count("::")>0:
339  namespace = full_alg_name.split("::")[0]
340  full_alg_name = full_alg_name.split("::")[1]
341  namespace_begin = "namespace %s {" % namespace
342  namespace_end = "} //> end namespace %s" % namespace
343  pass
344 
345  guard = "%s_%s_H" % (full_pkg_name.upper(), namespace_klass.upper())
346 
347  d = dict( pkg=full_pkg_name,
348  klass=full_alg_name,
349  guard=guard,
350  namespace_begin=namespace_begin,
351  namespace_end=namespace_end,namespace_klass=namespace_klass,namespace=namespace
352  )
353  fname = os.path.splitext("src/%s"%namespace_klass)[0]
354  #first check doesn't exist
355  if os.path.isfile(fname+'.h'):
356  print("::: ERROR %s.h already exists" % fname)
357  return -1
358  print("::: INFO Creating %s.h" % fname)
359  o_hdr = open(fname+'.h', 'w')
360  o_hdr.writelines(hdr%d)
361  o_hdr.flush()
362  o_hdr.close()
363 
364  if os.path.isfile(fname+'.cxx'):
365  print("::: ERROR %s.cxx already exists" % fname)
366  return -1
367  print("::: INFO Creating %s.cxx" % fname)
368  o_cxx = open(fname+'.cxx', 'w')
369  o_cxx.writelines(cxx%d)
370  o_cxx.flush()
371  o_cxx.close()
372 
373 
374  #now add the algorithm to the _entries.cxx file in the components folder
375  #first check they exist
376  if not os.path.exists("src/components"): os.mkdir("src/components")
377  if not os.path.isfile("src/components/%s_entries.cxx"%full_pkg_name):
378  print("::: INFO Creating src/components/%s_entries.cxx"%full_pkg_name)
379  loadFile = open("src/components/%s_entries.cxx"%full_pkg_name,'w')
380  if len(namespace_begin)>0:
381  d["namespace"] = args.algname.split("::")[0]
382  loadFile.writelines("""
383 #include "../%(namespace_klass)s.h"
384 DECLARE_COMPONENT(%(namespace)s::%(klass)s )
385 """%d)
386  else:
387  loadFile.writelines("""
388 #include "../%(namespace_klass)s.h"
389 DECLARE_COMPONENT( %(klass)s )
390 """%d)
391  loadFile.flush()
392  loadFile.close()
393  else:
394  #first check algorithm not already in _entries file
395  inFile=False
396  for line in open("src/components/%s_entries.cxx"%full_pkg_name):
397  if len(namespace_begin)==0 and "DECLARE_COMPONENT" in line and d["klass"] in line: inFile=True
398  if len(namespace_begin)>0 and "DECLARE_COMPONENT" in line and d["klass"] in line and d["namespace"]: inFile=True
399 
400  if not inFile:
401  print("::: INFO Adding %s to src/components/%s_entries.cxx"% (args.algname,full_pkg_name))
402  nextAdd=True
403  with open("src/components/%s_entries.cxx"%full_pkg_name, "a") as f:
404  if len(namespace_begin)>0:
405  f.write(""" DECLARE_COMPONENT(%(namespace)s::%(klass)s );"""%d)
406  else:
407  f.write(""" DECLARE_COMPONENT( %(klass)s );"""%d)
408 
409 
410  if args.newJobo:
411  #make the joboptions file too
412  full_jobo_name = namespace_klass + "JobOptions"
413  full_script_name = "run" + namespace_klass
414  full_alg_name = namespace_klass
415 
416  print(textwrap.dedent("""\
417  ::: create jobo [%(full_jobo_name)s] and script [%(full_script_name)s] for alg [%(full_alg_name)s]""" %locals()))
418 
419  #following code borrowed from gen_klass
420  jobo = Templates.jobo_template
421 
422  e = dict( klass=full_alg_name,
423  inFile=os.environ['ASG_TEST_FILE_MC'],
424  )
425  fname = 'share/%s.py' % full_jobo_name
426  #first check doesn't exist
427  if os.path.isfile(fname):
428  print("::: WARNING %s already exists .. will not overwrite" % fname)
429  else:
430  o_hdr = open(fname, 'w')
431  o_hdr.writelines(jobo%e)
432  o_hdr.flush()
433  o_hdr.close()
434 
435  scripto = Templates.script_template
436 
437  e = dict( klass=full_alg_name,
438  inFile=os.environ['ASG_TEST_FILE_MC'],
439  )
440  fname = 'scripts/%s.py' % full_script_name
441  #first check doesn't exist
442  if os.path.isfile(fname):
443  print("::: WARNING %s already exists .. will not overwrite" % fname)
444  else:
445  o_hdr = open(fname, 'w')
446  o_hdr.writelines(scripto%e)
447  o_hdr.flush()
448  o_hdr.close()
449  os.chmod(fname, 0o755)
450 
451  #need to reconfigure cmake so it knows about the new files
452  #rely on the WorkDir_DIR env var for this
453  workDir = os.environ.get("WorkDir_DIR")
454  if workDir is None:
455  print("::: ERROR No WorkDir_DIR env var, did you forget to source the setup.sh script?")
456  print("::: ERROR Please do this and reconfigure cmake manually!")
457  else:
458  print("::: INFO Reconfiguring cmake %s/../." % workDir)
459  res = subprocess.getstatusoutput('cmake %s/../.' % workDir)
460  if res[0]!=0:
461  print("::: WARNING reconfigure unsuccessful. Please reconfigure manually!")
462 
463 
464  print("::: INFO Please ensure your CMakeLists.txt file has ")
465  print("::: atlas_add_component( %s src/component/*.cxx ... )" % full_pkg_name)
466  print("::: INFO and necessary dependencies declared ")
467  print("::: INFO Minimum dependency is: Control/AthAnalysisBaseComps")
468 

Variable Documentation

◆ __author__

python.scripts.cmake_newanalysisalg.__author__
private

Definition at line 13 of file cmake_newanalysisalg.py.

◆ __doc__

python.scripts.cmake_newanalysisalg.__doc__
private

Definition at line 14 of file cmake_newanalysisalg.py.

◆ __version__

python.scripts.cmake_newanalysisalg.__version__
private

Definition at line 12 of file cmake_newanalysisalg.py.

python.scripts.cmake_newanalysisalg.main
def main(args)
Definition: cmake_newanalysisalg.py:288
Trk::open
@ open
Definition: BinningType.h:40
dbg::print
void print(std::FILE *stream, std::format_string< Args... > fmt, Args &&... args)
Definition: SGImplSvc.cxx:70