From 1ae8aa09ec297db8e0e3ae9bce52787252af00e2 Mon Sep 17 00:00:00 2001 From: tmadlener Date: Mon, 12 Feb 2024 15:11:02 +0100 Subject: [PATCH] Move load_file to utils module to make it usable in user code --- k4FWCore/CMakeLists.txt | 7 +++---- k4FWCore/python/k4FWCore/utils.py | 30 ++++++++++++++++++++++++++++++ k4FWCore/scripts/k4run | 6 ++---- 3 files changed, 35 insertions(+), 8 deletions(-) create mode 100644 k4FWCore/python/k4FWCore/utils.py diff --git a/k4FWCore/CMakeLists.txt b/k4FWCore/CMakeLists.txt index 8b6bb8af..93db9804 100644 --- a/k4FWCore/CMakeLists.txt +++ b/k4FWCore/CMakeLists.txt @@ -54,7 +54,6 @@ install(TARGETS k4FWCore k4FWCorePlugins # Copy python parsing file to genConfDir in Gaudi add_custom_command( TARGET k4FWCore POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy - ${PROJECT_SOURCE_DIR}/k4FWCore/python/k4FWCore/parseArgs.py - ${CMAKE_CURRENT_BINARY_DIR}/genConfDir/k4FWCore/parseArgs.py) - + COMMAND ${CMAKE_COMMAND} -E copy_directory + ${PROJECT_SOURCE_DIR}/k4FWCore/python/k4FWCore + ${CMAKE_CURRENT_BINARY_DIR}/genConfDir/k4FWCore) diff --git a/k4FWCore/python/k4FWCore/utils.py b/k4FWCore/python/k4FWCore/utils.py new file mode 100644 index 00000000..c210e0b0 --- /dev/null +++ b/k4FWCore/python/k4FWCore/utils.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python3 +# +# Copyright (c) 2014-2024 Key4hep-Project. +# +# This file is part of Key4hep. +# See https://key4hep.github.io/key4hep-doc/ for further info. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +import os +from io import TextIOWrapper +from typing import Union + + +def load_file(opt_file: Union[TextIOWrapper, os.PathLike]) -> None: + """Load the file content and run it in the current interpreter session""" + if isinstance(opt_file, os.PathLike): + opt_file = open(opt_file, "r") + code = compile(opt_file.read(), opt_file.name, "exec") + exec(code, globals()) diff --git a/k4FWCore/scripts/k4run b/k4FWCore/scripts/k4run index 3e1049d7..40e8df7c 100755 --- a/k4FWCore/scripts/k4run +++ b/k4FWCore/scripts/k4run @@ -6,6 +6,8 @@ import argparse from multiprocessing import cpu_count import logging +from k4FWCore.utils import load_file + # these default properties are filtered as otherwise they will clutter the argument list FILTER_GAUDI_PROPS = [ "ContextService", @@ -55,10 +57,6 @@ seen_files = set() option_db = {} -def load_file(file): - exec(file.read(), {}) - - def add_arguments(parser, app_mgr): # length increases when properties of an algorithm with tools are inspected # see https://github.com/key4hep/k4FWCore/pull/138