Skip to content

Commit

Permalink
Make load_file available for dependent packages (key4hep#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmadlener authored Apr 12, 2024
1 parent bd3a539 commit 0d50e1f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 8 deletions.
7 changes: 3 additions & 4 deletions k4FWCore/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
51 changes: 51 additions & 0 deletions k4FWCore/python/k4FWCore/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/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, str, os.PathLike]) -> None:
"""Loads and executes the content of a given file in the current interpreter session.
This function takes a file object or a path to a file, reads its content,
and then executes it as Python code within the global scope of the current
interpreter session. If `opt_file` is a file handle it will not be closed.
Args:
opt_file (Union[TextIOWrapper, str, os.PathLike]): A file object or a
path to the file that
contains Python code
to be executed.
Raises:
FileNotFoundError: If `opt_file` is a path and no file exists at that path.
IOError: If there's an error opening or reading the file.
SyntaxError: If there's a syntax error in the code being executed.
Exception: Any exception raised by the executed code will be propagated.
"""
if isinstance(opt_file, (str, os.PathLike)):
with open(opt_file, "r") as file:
code = compile(file.read(), file.name, "exec")
else:
code = compile(opt_file.read(), opt_file.name, "exec")

exec(code, globals())
6 changes: 2 additions & 4 deletions k4FWCore/scripts/k4run
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0d50e1f

Please sign in to comment.