From f7f9cde4e7990baba7932ce791b07837f7640d73 Mon Sep 17 00:00:00 2001
From: Marco Clemencic <marco.clemencic@cern.ch>
Date: Wed, 1 Dec 2021 15:56:35 +0100
Subject: [PATCH] genconfuser.py: exclude <Project>.confdb when scanning the
 environment

---
 .gitlab-ci.yml                     |  2 --
 GaudiKernel/scripts/genconfuser.py | 28 +++++++++++++++++++++++-----
 cmake/GaudiToolbox.cmake           |  1 +
 3 files changed, 24 insertions(+), 7 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 1bd7bc2664..61798ab418 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -100,8 +100,6 @@ view-gcc8:
     # Override CMake from the view (too old in LCG 97a)
     - export PATH="/cvmfs/sft.cern.ch/lcg/contrib/CMake/3.18.3/Linux-x86_64/bin:$PATH"
     - PYTHON_MAJOR_VERSION=$(python -c "import sys; print(sys.version_info[0])")
-    # Ignore .confdb file from Gaudi installation in LCG view
-    - export CONFIGURABLE_DB_IGNORE=/cvmfs/sft.cern.ch/lcg/views/LCG_${LCG_VERSION}/x86_64-centos7-gcc8-opt/lib/Gaudi.confdb
     - cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DGAUDI_USE_PYTHON_MAJOR=${PYTHON_MAJOR_VERSION} -DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache
     - ccache -z
     # pre-heat ccache cache for GaudiKernel
diff --git a/GaudiKernel/scripts/genconfuser.py b/GaudiKernel/scripts/genconfuser.py
index 6339506503..cec25a75a0 100755
--- a/GaudiKernel/scripts/genconfuser.py
+++ b/GaudiKernel/scripts/genconfuser.py
@@ -49,7 +49,7 @@ def _inheritsfrom(derived, basenames):
     return False
 
 
-def loadConfigurableDb(build_dir=None):
+def loadConfigurableDb(build_dir=None, project_name=None):
     '''
     Equivalent to GaudiKernel.ConfigurableDb.loadConfigurableDb(), but does a
     deep search and executes the '*.confdb' files instead of importing them.
@@ -79,10 +79,20 @@ def loadConfigurableDb(build_dir=None):
                 if f.endswith('.confdb')
             ]
         ]
+    #  - get the list of ignored files
+    ignored_files = set(
+        os.environ.get("CONFIGURABLE_DB_IGNORE", "").split(","))
     #  - load the confdb files
-    for confDb in (set(confDbFiles) - set(
-            os.environ.get("CONFIGURABLE_DB_IGNORE", "").split(","))):
-        log.debug("\t-loading [%s]..." % confDb)
+    for confDb in set(confDbFiles):
+        if confDb in ignored_files or (project_name
+                                       and os.path.basename(confDb) ==
+                                       ("{}.confdb".format(project_name))):
+            # skip ignored files and the project's own confdb
+            log.debug("\t-ignoring [%s]", confDb)
+            # make sure we count also <project_name>.confdb for GaudiKernel
+            ignored_files.add(confDb)
+            continue
+        log.debug("\t-loading [%s]...", confDb)
         try:
             cfgDb._loadModule(confDb)
         except Exception as err:
@@ -91,6 +101,8 @@ def loadConfigurableDb(build_dir=None):
             log.warning("Could not load file [%s] !", confDb)
             log.warning("Reason: %s", err)
     # top up with the regular merged confDb (for the used projects)
+    # (make sure GaudiKernel gets the right exclusions)
+    os.environ["CONFIGURABLE_DB_IGNORE"] = ",".join(ignored_files)
     GaudiKernel.ConfigurableDb.loadConfigurableDb()
 
 
@@ -188,6 +200,12 @@ def main():
         help=
         "build directory where to look for .confdb files (search all subdirectories)"
     )
+    parser.add_option(
+        "--project-name",
+        action="store",
+        help=
+        "name of the current project (used to exclude spurious versions of the .confdb file for the current project)"
+    )
     parser.set_defaults(root=os.path.join("..", "python"))
 
     opts, args = parser.parse_args()
@@ -233,7 +251,7 @@ def main():
     except:
         pass
     # load configurables database to avoid fake duplicates
-    loadConfigurableDb(opts.build_dir)
+    loadConfigurableDb(opts.build_dir, opts.project_name)
     # ensure that local configurables are in the database
     try:
         # Add the local python directories to the python path to be able to import the local
diff --git a/cmake/GaudiToolbox.cmake b/cmake/GaudiToolbox.cmake
index bf548eaa67..5786e6b385 100644
--- a/cmake/GaudiToolbox.cmake
+++ b/cmake/GaudiToolbox.cmake
@@ -988,6 +988,7 @@ function(gaudi_generate_confuserdb)
     add_custom_command(OUTPUT "${output_file}"
         COMMAND run genconfuser.py
                 --build-dir ${CMAKE_BINARY_DIR}
+                --project-name ${PROJECT_NAME}
                 --root ${CMAKE_CURRENT_SOURCE_DIR}/python
                 --output "${output_file}"
                 ${ARG_OPTIONS}
-- 
GitLab