diff --git a/nds-h/nds_h_gen_data.py b/nds-h/nds_h_gen_data.py index 8293812..7197d99 100644 --- a/nds-h/nds_h_gen_data.py +++ b/nds-h/nds_h_gen_data.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- # -# SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: Apache-2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -36,10 +36,8 @@ import subprocess import shutil -#For adding utils to path -parent_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) -utils_dir = os.path.join(parent_dir, 'utils') -sys.path.insert(0, utils_dir) +from setup_utils import add_utils_to_sys_path +add_utils_to_sys_path() from check import check_build_nds_h, check_version, get_abs_path, get_dir_size, parallel_value_type, valid_range diff --git a/nds-h/nds_h_gen_query_stream.py b/nds-h/nds_h_gen_query_stream.py index fad9380..5f5eebf 100644 --- a/nds-h/nds_h_gen_query_stream.py +++ b/nds-h/nds_h_gen_query_stream.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- # -# SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: Apache-2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -34,9 +34,8 @@ import subprocess import sys -parent_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) -utils_dir = os.path.join(parent_dir, 'utils') -sys.path.insert(0, utils_dir) +from setup_utils import add_utils_to_sys_path +add_utils_to_sys_path() from check import check_build_nds_h, check_version, get_abs_path diff --git a/nds-h/nds_h_power.py b/nds-h/nds_h_power.py index b6a4cff..a49d864 100644 --- a/nds-h/nds_h_power.py +++ b/nds-h/nds_h_power.py @@ -40,12 +40,8 @@ import re import subprocess -# Python doesn't automatically include sibling directories in the import path. -# We need to explicitly add the utils directory to sys.path to import shared utilities. -parent_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) -utils_dir = os.path.join(parent_dir, 'utils') -if utils_dir not in sys.path: - sys.path.insert(0, utils_dir) +from setup_utils import add_utils_to_sys_path +add_utils_to_sys_path() from spark_utils import setQueryName, clearQueryName from profiler import Profiler diff --git a/nds-h/setup_utils.py b/nds-h/setup_utils.py new file mode 100644 index 0000000..86cffe5 --- /dev/null +++ b/nds-h/setup_utils.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 +# +# 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 inspect +import os +import sys + + +def add_utils_to_sys_path(): + """Add the sibling utils/ directory to sys.path so shared modules can be imported. + + Uses inspect.stack() to resolve the calling script's location from the bytecode, + which works both in standard execution and in Databricks exec(compile(...)) contexts + where __file__ is not defined. + """ + caller_file = inspect.stack()[1].filename + parent_dir = os.path.abspath(os.path.join(os.path.dirname(caller_file), '..')) + utils_dir = os.path.join(parent_dir, 'utils') + if utils_dir not in sys.path: + sys.path.insert(0, utils_dir) diff --git a/nds/nds_maintenance.py b/nds/nds_maintenance.py index 295ca4d..925e88e 100644 --- a/nds/nds_maintenance.py +++ b/nds/nds_maintenance.py @@ -43,12 +43,8 @@ from nds_schema import get_maintenance_schemas from nds_power import register_delta_tables -# Python doesn't automatically include sibling directories in the import path. -# We need to explicitly add the utils directory to sys.path to import shared utilities. -parent_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) -utils_dir = os.path.join(parent_dir, 'utils') -if utils_dir not in sys.path: - sys.path.insert(0, utils_dir) +from setup_utils import add_utils_to_sys_path +add_utils_to_sys_path() from spark_utils import setQueryName, clearQueryName INSERT_FUNCS = [ diff --git a/nds/nds_power.py b/nds/nds_power.py index 8e82dd1..4514798 100644 --- a/nds/nds_power.py +++ b/nds/nds_power.py @@ -44,12 +44,8 @@ from check import check_json_summary_folder, check_query_subset_exists, check_version from nds_schema import get_schemas -# Python doesn't automatically include sibling directories in the import path. -# We need to explicitly add the utils directory to sys.path to import shared utilities. -parent_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) -utils_dir = os.path.join(parent_dir, 'utils') -if utils_dir not in sys.path: - sys.path.insert(0, utils_dir) +from setup_utils import add_utils_to_sys_path +add_utils_to_sys_path() from spark_utils import setQueryName, clearQueryName from profiler import Profiler diff --git a/nds/setup_utils.py b/nds/setup_utils.py new file mode 100644 index 0000000..86cffe5 --- /dev/null +++ b/nds/setup_utils.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 +# +# 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 inspect +import os +import sys + + +def add_utils_to_sys_path(): + """Add the sibling utils/ directory to sys.path so shared modules can be imported. + + Uses inspect.stack() to resolve the calling script's location from the bytecode, + which works both in standard execution and in Databricks exec(compile(...)) contexts + where __file__ is not defined. + """ + caller_file = inspect.stack()[1].filename + parent_dir = os.path.abspath(os.path.join(os.path.dirname(caller_file), '..')) + utils_dir = os.path.join(parent_dir, 'utils') + if utils_dir not in sys.path: + sys.path.insert(0, utils_dir)