Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate lib.util.which #4340

Merged
merged 3 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package/CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ Changes
`analysis.nucleicacids.WatsonCrickDist.results.distances` (Issue #3720, PR #3735)

Deprecations
* MDAnalysis.lib.util is deprecated and will be removed in version 3.0
(Issue #3649)
* The TRZ reader & writer are deprecated and will be removed in version 3.0
(PR #4335)
* X3DNA has been deprecated and will be removed in 3.0.0 (Issue #3788)
Expand Down
13 changes: 7 additions & 6 deletions package/MDAnalysis/analysis/hole2/hole.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#
import os
import errno
import shutil
import tempfile
import textwrap
import logging
Expand Down Expand Up @@ -228,7 +229,7 @@
warnings.warn(msg.format(output_level))

# get executable
exe = util.which(executable)
exe = shutil.which(executable)

Check warning on line 232 in package/MDAnalysis/analysis/hole2/hole.py

View check run for this annotation

Codecov / codecov/patch

package/MDAnalysis/analysis/hole2/hole.py#L232

Added line #L232 was not covered by tests
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not looking to add tests to cover these if the migration is also being done in hole2-mdakit

if exe is None:
raise OSError(errno.ENOENT, exe_err.format(name=executable,
kw='executable'))
Expand Down Expand Up @@ -521,23 +522,23 @@
self.ignore_residues = ignore_residues

# --- finding executables ----
hole = util.which(executable)
hole = shutil.which(executable)

Check warning on line 525 in package/MDAnalysis/analysis/hole2/hole.py

View check run for this annotation

Codecov / codecov/patch

package/MDAnalysis/analysis/hole2/hole.py#L525

Added line #L525 was not covered by tests
if hole is None:
raise OSError(errno.ENOENT, exe_err.format(name=executable,
kw='executable'))
self.base_path = os.path.dirname(hole)

sos_triangle_path = util.which(sos_triangle)
sos_triangle_path = shutil.which(sos_triangle)

Check warning on line 531 in package/MDAnalysis/analysis/hole2/hole.py

View check run for this annotation

Codecov / codecov/patch

package/MDAnalysis/analysis/hole2/hole.py#L531

Added line #L531 was not covered by tests
if sos_triangle_path is None:
path = os.path.join(self.base_path, sos_triangle)
sos_triangle_path = util.which(path)
sos_triangle_path = shutil.which(path)

Check warning on line 534 in package/MDAnalysis/analysis/hole2/hole.py

View check run for this annotation

Codecov / codecov/patch

package/MDAnalysis/analysis/hole2/hole.py#L534

Added line #L534 was not covered by tests
if sos_triangle_path is None:
raise OSError(errno.ENOENT, exe_err.format(name=sos_triangle,
kw='sos_triangle'))
sph_process_path = util.which(sph_process)
sph_process_path = shutil.which(sph_process)

Check warning on line 538 in package/MDAnalysis/analysis/hole2/hole.py

View check run for this annotation

Codecov / codecov/patch

package/MDAnalysis/analysis/hole2/hole.py#L538

Added line #L538 was not covered by tests
if sph_process_path is None:
path = os.path.join(self.base_path, sph_process)
sph_process_path = util.which(path)
sph_process_path = shutil.which(path)

Check warning on line 541 in package/MDAnalysis/analysis/hole2/hole.py

View check run for this annotation

Codecov / codecov/patch

package/MDAnalysis/analysis/hole2/hole.py#L541

Added line #L541 was not covered by tests
if sph_process_path is None:
raise OSError(errno.ENOENT, exe_err.format(name=sph_process,
kw='sph_process'))
Expand Down
9 changes: 9 additions & 0 deletions package/MDAnalysis/lib/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,16 @@ def which(program):
path : str or None
absolute path to the executable if it can be found, else ``None``


.. deprecated:: 2.7.0
This method is deprecated and will be removed in version 3.0.0.
Please use shutil.which instead.
"""
# Can't use decorator because it's declared after this method
wmsg = ("This method is deprecated as of MDAnalysis version 2.7.0 "
"and will be removed in version 3.0.0. Please use shutil.which "
"instead.")
warnings.warn(wmsg, DeprecationWarning)

def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
Expand Down
10 changes: 10 additions & 0 deletions testsuite/MDAnalysisTests/lib/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from unittest.mock import Mock, patch
import sys
import copy
import shutil

import numpy as np
from numpy.testing import (assert_equal, assert_almost_equal,
Expand Down Expand Up @@ -2222,3 +2223,12 @@ def test_store_arguments_withkwargs(self):

store2 = store.copy()
assert store2.__dict__ == store.__dict__


@pytest.mark.xfail(os.name == 'nt',
reason="util.which does not get right binary on Windows")
def test_which():
wmsg = "This method is deprecated"

with pytest.warns(DeprecationWarning, match=wmsg):
assert util.which('python') == shutil.which('python')
6 changes: 2 additions & 4 deletions testsuite/MDAnalysisTests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from contextlib import contextmanager
from functools import wraps
import importlib
import shutil
from unittest import mock
import os
import warnings
Expand Down Expand Up @@ -81,11 +82,8 @@ def executable_not_found(*args):

@dec.skipif(executable_not_found("binary_name"), msg="skip test because binary_name not available")
"""
# This must come here so that MDAnalysis isn't imported prematurely,
# which spoils coverage accounting (see Issue 344).
import MDAnalysis.lib.util
for name in args:
if MDAnalysis.lib.util.which(name) is not None:
if shutil.which(name) is not None:
return False
return True

Expand Down
Loading