287 """create a new AthAnalysisAlgorithm inside the current package. Call from within the package directory
290 $ acmd cmake new-analysisalg MyAlg
294 full_alg_name = args.algname
299 if not os.path.isdir(cwd+
"/src")
or not os.path.isfile(cwd+
"/CMakeLists.txt"):
300 print(
"ERROR you must call new-analysisalg from within the package you want to add the algorithm to")
304 full_pkg_name = os.path.basename(cwd)
305 print(textwrap.dedent(
"""\
306 ::: create alg [%(full_alg_name)s] in pkg [%(full_pkg_name)s]""" %locals()))
311 hasxAODEventInfo=
False
317 hasComponentLine=
False
318 for line
in open(
'CMakeLists.txt'):
320 if "atlas_add_library" in line: hasLibraryLine=
True
321 if "atlas_add_component" in line: hasComponentLine=
True
330 hdr = Templates.alg_hdr_template
331 cxx = Templates.alg_cxx_template
333 namespace_klass = full_alg_name.replace(
'::',
'__')
334 namespace_begin,namespace_end =
"",
""
336 if full_alg_name.count(
"::")>0:
337 namespace = full_alg_name.split(
"::")[0]
338 full_alg_name = full_alg_name.split(
"::")[1]
339 namespace_begin =
"namespace %s {" % namespace
340 namespace_end =
"} //> end namespace %s" % namespace
343 guard =
"%s_%s_H" % (full_pkg_name.upper(), namespace_klass.upper())
345 d = dict( pkg=full_pkg_name,
348 namespace_begin=namespace_begin,
349 namespace_end=namespace_end,namespace_klass=namespace_klass,namespace=namespace
351 fname = os.path.splitext(
"src/%s"%namespace_klass)[0]
353 if os.path.isfile(fname+
'.h'):
354 print(
"::: ERROR %s.h already exists" % fname)
356 print(
"::: INFO Creating %s.h" % fname)
357 o_hdr =
open(fname+
'.h',
'w')
358 o_hdr.writelines(hdr%d)
362 if os.path.isfile(fname+
'.cxx'):
363 print(
"::: ERROR %s.cxx already exists" % fname)
365 print(
"::: INFO Creating %s.cxx" % fname)
366 o_cxx =
open(fname+
'.cxx',
'w')
367 o_cxx.writelines(cxx%d)
374 if not os.path.exists(
"src/components"): os.mkdir(
"src/components")
375 if not os.path.isfile(
"src/components/%s_entries.cxx"%full_pkg_name):
376 print(
"::: INFO Creating src/components/%s_entries.cxx"%full_pkg_name)
377 loadFile =
open(
"src/components/%s_entries.cxx"%full_pkg_name,
'w')
378 if len(namespace_begin)>0:
379 d[
"namespace"] = args.algname.split(
"::")[0]
380 loadFile.writelines(
"""
381 #include "../%(namespace_klass)s.h"
382 DECLARE_COMPONENT(%(namespace)s::%(klass)s )
385 loadFile.writelines(
"""
386 #include "../%(namespace_klass)s.h"
387 DECLARE_COMPONENT( %(klass)s )
394 for line
in open(
"src/components/%s_entries.cxx"%full_pkg_name):
395 if len(namespace_begin)==0
and "DECLARE_COMPONENT" in line
and d[
"klass"]
in line: inFile=
True
396 if len(namespace_begin)>0
and "DECLARE_COMPONENT" in line
and d[
"klass"]
in line
and d[
"namespace"]: inFile=
True
399 print(
"::: INFO Adding %s to src/components/%s_entries.cxx"% (args.algname,full_pkg_name))
401 with open(
"src/components/%s_entries.cxx"%full_pkg_name,
"a")
as f:
402 if len(namespace_begin)>0:
403 f.write(
""" DECLARE_COMPONENT(%(namespace)s::%(klass)s );"""%d)
405 f.write(
""" DECLARE_COMPONENT( %(klass)s );"""%d)
410 full_jobo_name = namespace_klass +
"JobOptions"
411 full_script_name =
"run" + namespace_klass
412 full_alg_name = namespace_klass
414 print(textwrap.dedent(
"""\
415 ::: create jobo [%(full_jobo_name)s] and script [%(full_script_name)s] for alg [%(full_alg_name)s]""" %locals()))
418 jobo = Templates.jobo_template
420 e = dict( klass=full_alg_name,
421 inFile=os.environ[
'ASG_TEST_FILE_MC'],
423 fname =
'share/%s.py' % full_jobo_name
425 if os.path.isfile(fname):
426 print(
"::: WARNING %s already exists .. will not overwrite" % fname)
428 o_hdr =
open(fname,
'w')
429 o_hdr.writelines(jobo%e)
433 scripto = Templates.script_template
435 e = dict( klass=full_alg_name,
436 inFile=os.environ[
'ASG_TEST_FILE_MC'],
438 fname =
'scripts/%s.py' % full_script_name
440 if os.path.isfile(fname):
441 print(
"::: WARNING %s already exists .. will not overwrite" % fname)
443 o_hdr =
open(fname,
'w')
444 o_hdr.writelines(scripto%e)
447 os.chmod(fname, 0o755)
451 workDir = os.environ.get(
"WorkDir_DIR")
453 print(
"::: ERROR No WorkDir_DIR env var, did you forget to source the setup.sh script?")
454 print(
"::: ERROR Please do this and reconfigure cmake manually!")
456 print(
"::: INFO Reconfiguring cmake %s/../." % workDir)
457 res = subprocess.getstatusoutput(
'cmake %s/../.' % workDir)
459 print(
"::: WARNING reconfigure unsuccessful. Please reconfigure manually!")
462 print(
"::: INFO Please ensure your CMakeLists.txt file has ")
463 print(
"::: atlas_add_component( %s src/component/*.cxx ... )" % full_pkg_name)
464 print(
"::: INFO and necessary dependencies declared ")
465 print(
"::: INFO Minimum dependency is: Control/AthAnalysisBaseComps")