From 568c213b998b3938f07658b6666a1bbc58ad6302 Mon Sep 17 00:00:00 2001 From: Joseph D Hughes Date: Sun, 6 Aug 2023 18:28:54 -0500 Subject: [PATCH] * use `set_dir()` in modflow_devtools.misc * remove `working_directory()` from conftest.py * black and isort --- autotest/conftest.py | 11 ----------- autotest/test_build.py | 10 +++++----- pymake/utils/_meson_build.py | 18 ++++++++++++++---- 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/autotest/conftest.py b/autotest/conftest.py index 5df2876b..1a113280 100644 --- a/autotest/conftest.py +++ b/autotest/conftest.py @@ -15,17 +15,6 @@ # misc utilities -@contextlib.contextmanager -def working_directory(path): - """Changes working directory and returns to previous on exit.""" - prev_cwd = os.getcwd() - os.chdir(path) - try: - yield - finally: - os.chdir(prev_cwd) - - def get_pymake_appdir(): appdir = Path.home() / ".pymake" appdir.mkdir(parents=True, exist_ok=True) diff --git a/autotest/test_build.py b/autotest/test_build.py index 0ad21aa0..5b6e2d65 100644 --- a/autotest/test_build.py +++ b/autotest/test_build.py @@ -4,9 +4,9 @@ import pytest from flaky import flaky +from modflow_devtools.misc import set_dir import pymake -from autotest.conftest import working_directory RERUNS = 3 @@ -20,7 +20,7 @@ def build_with_makefile(target, path, fc): success = True - with working_directory(path): + with set_dir(path): if os.path.isfile("makefile"): # wait to delete on windows if sys.platform.lower() == "win32": @@ -55,7 +55,7 @@ def build_with_makefile(target, path, fc): @flaky(max_runs=RERUNS) @pytest.mark.parametrize("target", targets) def test_build(function_tmpdir, target: str) -> None: - with working_directory(function_tmpdir): + with set_dir(function_tmpdir): assert ( pymake.build_apps( target, @@ -68,10 +68,10 @@ def test_build(function_tmpdir, target: str) -> None: @pytest.mark.base @flaky(max_runs=RERUNS) -#@pytest.mark.skipif(sys.platform == "win32", reason="do not run on Windows") +# @pytest.mark.skipif(sys.platform == "win32", reason="do not run on Windows") @pytest.mark.parametrize("target", targets) def test_meson_build(function_tmpdir, target: str) -> None: - with working_directory(function_tmpdir): + with set_dir(function_tmpdir): assert ( pymake.build_apps( target, diff --git a/pymake/utils/_meson_build.py b/pymake/utils/_meson_build.py index f8c7ea21..a2e1fae2 100644 --- a/pymake/utils/_meson_build.py +++ b/pymake/utils/_meson_build.py @@ -17,6 +17,7 @@ _get_prepend, ) from ._file_utils import _get_extrafiles_common_path +from .usgsprograms import usgs_program_data @contextmanager @@ -35,6 +36,7 @@ def _set_directory(path: Path): """ origin = Path().absolute() try: + Path(path).mkdir(exist_ok=True, parents=True) os.chdir(path) yield finally: @@ -72,8 +74,8 @@ def meson_build( return code """ - meson_test_path = os.path.join(mesondir, "meson.build") - if os.path.isfile(meson_test_path): + meson_test_path = Path(mesondir) / "meson.build" + if meson_test_path.is_file(): # setup meson returncode = meson_setup(mesondir, fc=fc, cc=cc, appdir=appdir) # build and install executable(s) using meson @@ -433,6 +435,12 @@ def _create_main_meson_build( appdir = os.path.relpath(os.path.dirname(target), mesondir) target = os.path.splitext((os.path.basename(target)))[0] + # get target version number + try: + target_version = usgs_program_data.get_version(target) + except: + target_version = None + # get main program file from list of source files mainfile = _get_main(srcfiles) @@ -520,11 +528,13 @@ def _create_main_meson_build( ) optlevel_int = int(optlevel.replace("-O", "").replace("/O", "")) - main_meson_file = os.path.join(mesondir, "meson.build") + main_meson_file = Path(mesondir) / "meson.build" with open(main_meson_file, "w") as f: line = f"project(\n\t'{target}',\n" for language in languages: line += f"\t'{language}',\n" + if target_version is not None: + line += f"\tversion: '{target_version}',\n" line += "\tmeson_version: '>= 1.1.0',\n" line += "\tdefault_options: [\n\t\t'b_vscrt=static_from_buildtype',\n" line += f"\t\t'optimization={optlevel_int}',\n" @@ -667,4 +677,4 @@ def _create_source_meson_build(source_path_dict, srcfiles): for temp in pop_list: srcfiles_copy.remove(temp) - return \ No newline at end of file + return