Skip to content

Commit

Permalink
* use set_dir() in modflow_devtools.misc
Browse files Browse the repository at this point in the history
* remove `working_directory()` from conftest.py
* black and isort
  • Loading branch information
jdhughes-usgs committed Aug 6, 2023
1 parent 91ec074 commit 568c213
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
11 changes: 0 additions & 11 deletions autotest/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions autotest/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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":
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
18 changes: 14 additions & 4 deletions pymake/utils/_meson_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
_get_prepend,
)
from ._file_utils import _get_extrafiles_common_path
from .usgsprograms import usgs_program_data


@contextmanager
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -667,4 +677,4 @@ def _create_source_meson_build(source_path_dict, srcfiles):
for temp in pop_list:
srcfiles_copy.remove(temp)

return
return

0 comments on commit 568c213

Please sign in to comment.