summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2017-12-07 12:38:12 +0100
committerGiampaolo Rodola <g.rodola@gmail.com>2017-12-07 12:38:12 +0100
commitea8d2ad57c938f311f736ed8e76c38d90ff91fc5 (patch)
tree29cc81f23555e59f051597c5e69784b7de062c1c
parentc3767da76a366cabbe1c84ad9cef007ae51c400e (diff)
downloadpsutil-set_procfs_path.tar.gz
add set_procfs_pathset_procfs_path
-rw-r--r--psutil/__init__.py7
-rw-r--r--psutil/_pslinux.py33
-rwxr-xr-xpsutil/tests/test_linux.py7
3 files changed, 42 insertions, 5 deletions
diff --git a/psutil/__init__.py b/psutil/__init__.py
index 7a170937..d6a66de2 100644
--- a/psutil/__init__.py
+++ b/psutil/__init__.py
@@ -2270,6 +2270,13 @@ def users():
return _psplatform.users()
+def set_procfs_path(path):
+ """Set an alternative path for /proc filesystem on Linux, Solaris
+ and AIX. This superseds PROCFS_PATH variable which is deprecated.
+ """
+ _psplatform.PROCFS_PATH = path
+
+
# =====================================================================
# --- Windows services
# =====================================================================
diff --git a/psutil/_pslinux.py b/psutil/_pslinux.py
index b57adb34..b9b43346 100644
--- a/psutil/_pslinux.py
+++ b/psutil/_pslinux.py
@@ -68,6 +68,7 @@ __extra__all__ = [
# =====================================================================
+PROCFS_PATH = None
POWER_SUPPLY_PATH = "/sys/class/power_supply"
HAS_SMAPS = os.path.exists('/proc/%s/smaps' % os.getpid())
HAS_PRLIMIT = hasattr(cext, "linux_prlimit")
@@ -210,8 +211,36 @@ else:
def get_procfs_path():
- """Return updated psutil.PROCFS_PATH constant."""
- return sys.modules['psutil'].PROCFS_PATH
+ """Return updated PROCFS_PATH constant.
+ Return value is cached after 10 calls.
+ """
+ global PROCFS_PATH
+
+ if PROCFS_PATH is not None:
+ return PROCFS_PATH
+
+ path = sys.modules['psutil'].PROCFS_PATH
+ if path != "/proc":
+ msg = \
+ "you used `psutil.PROCFS_PATH = %s` somewhere in your code; " \
+ "that is deprecated and will be ignored in the future; replace " \
+ "it with `set_procfs_path(%r)`" % (path, path)
+ warnings.warn(msg, category=FutureWarning, stacklevel=2)
+ PROCFS_PATH = path
+
+ # Cache the value if path remained the same after 10 calls.
+ # This means that from now on any change to psutil.PROCFS_PATH
+ # will be ignored.
+ # This is based on the assumption that it's likely that the user
+ # does "psutil.PROCFS_PATH" at import time, not later.
+ get_procfs_path.ncalls += 1
+ if get_procfs_path.ncalls >= 10:
+ PROCFS_PATH = path
+
+ return path
+
+
+get_procfs_path.ncalls = 0
def readlink(path):
diff --git a/psutil/tests/test_linux.py b/psutil/tests/test_linux.py
index 6ba17b25..5d345ae0 100755
--- a/psutil/tests/test_linux.py
+++ b/psutil/tests/test_linux.py
@@ -1058,7 +1058,7 @@ class TestMisc(unittest.TestCase):
self.assertRaises(
IOError, psutil.cpu_times_percent, percpu=True)
- psutil.PROCFS_PATH = my_procfs
+ psutil.set_procfs_path(my_procfs)
self.assertEqual(psutil.cpu_percent(), 0)
self.assertEqual(sum(psutil.cpu_times_percent()), 0)
@@ -1085,6 +1085,7 @@ class TestMisc(unittest.TestCase):
self.assertNotEqual(
sum(map(sum, psutil.cpu_times_percent(percpu=True))), 0)
finally:
+ psutil.set_procfs_path("/proc")
shutil.rmtree(my_procfs)
reload_module(psutil)
@@ -1120,7 +1121,7 @@ class TestMisc(unittest.TestCase):
def test_procfs_path(self):
tdir = tempfile.mkdtemp()
try:
- psutil.PROCFS_PATH = tdir
+ psutil.set_procfs_path(tdir)
self.assertRaises(IOError, psutil.virtual_memory)
self.assertRaises(IOError, psutil.cpu_times)
self.assertRaises(IOError, psutil.cpu_times, percpu=True)
@@ -1133,7 +1134,7 @@ class TestMisc(unittest.TestCase):
self.assertRaises(IOError, psutil.disk_partitions)
self.assertRaises(psutil.NoSuchProcess, psutil.Process)
finally:
- psutil.PROCFS_PATH = "/proc"
+ psutil.set_procfs_path("/proc")
os.rmdir(tdir)
def test_sector_size_mock(self):