`getDevicesBySerial` has no callers and the other two don't offer any value beyond their list comprehension equivalents `[d for d in dt.devices if isinstance(d, cls)]` (`getDevicesByInstance`) and `[d for d in dt.devices if d.type == devtype]` (`getDevicesByType`).
The first patch establishes a basic procedure for deprecating things in blivet. The warnings will only be logged if someone has called `blivet.util.set_up_logging`.
From: David Lehman dlehman@redhat.com
--- blivet/__init__.py | 8 ++++++++ blivet/util.py | 5 +++++ 2 files changed, 13 insertions(+)
diff --git a/blivet/__init__.py b/blivet/__init__.py index e2afc16..43e5ff7 100644 --- a/blivet/__init__.py +++ b/blivet/__init__.py @@ -51,6 +51,7 @@ def cb(self, exn):
import sys import importlib +import warnings
from . import util, arch from .flags import flags @@ -59,6 +60,13 @@ def cb(self, exn): log = logging.getLogger("blivet") program_log = logging.getLogger("program")
+# Tell the warnings module not to ignore DeprecationWarning, which it does by +# default since python-2.7. +warnings.simplefilter('module', DeprecationWarning) + +# Enable logging of python warnings. +logging.captureWarnings(True) + # XXX: respect the level? Need to translate between C and Python log levels. log_bd_message = lambda level, msg: program_log.info(msg)
diff --git a/blivet/util.py b/blivet/util.py index cf329f9..97ca3b5 100644 --- a/blivet/util.py +++ b/blivet/util.py @@ -518,6 +518,11 @@ def set_up_logging(log_file='/tmp/blivet.log'): handler.setFormatter(formatter) log.addHandler(handler) program_log.addHandler(handler) + + # capture python warnings in our logs + warning_log = logging.getLogger("py.warnings") + warning_log.addHandler(handler) + log.info("sys.argv = %s", sys.argv)
def create_sparse_tempfile(name, size):
From: David Lehman dlehman@redhat.com
--- blivet/devicetree.py | 3 +++ 1 file changed, 3 insertions(+)
diff --git a/blivet/devicetree.py b/blivet/devicetree.py index 3349569..f8cd3ff 100644 --- a/blivet/devicetree.py +++ b/blivet/devicetree.py @@ -687,6 +687,7 @@ def getDeviceByUuid(self, uuid, incomplete=False, hidden=False): log_method_return(self, result) return result
+ @util.deprecated('1.7', '') def getDevicesBySerial(self, serial, incomplete=False, hidden=False): """ Return a list of devices with a matching serial.
@@ -771,6 +772,7 @@ def getDeviceByPath(self, path, incomplete=False, hidden=False): log_method_return(self, result) return result
+ @util.deprecated('1.7', '') def getDevicesByType(self, device_type, incomplete=False, hidden=False): """ Return a list of devices with a matching device type.
@@ -786,6 +788,7 @@ def getDevicesByType(self, device_type, incomplete=False, hidden=False): log_method_return(self, [r.name for r in result]) return result
+ @util.deprecated('1.7', '') def getDevicesByInstance(self, device_class, incomplete=False, hidden=False): """ Return a list of devices with a matching device class.
From: David Lehman dlehman@redhat.com
--- blivet/util.py | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 104 insertions(+), 1 deletion(-)
diff --git a/blivet/util.py b/blivet/util.py index 97ca3b5..6614001 100644 --- a/blivet/util.py +++ b/blivet/util.py @@ -11,9 +11,11 @@ import tempfile import uuid import hashlib +import warnings from decimal import Decimal from contextlib import contextmanager from gi.repository import BlockDev as blockdev +from functools import wraps
import six
@@ -25,7 +27,6 @@ # this will get set to anaconda's program_log_lock in enable_installer_mode program_log_lock = Lock()
- def _run_program(argv, root='/', stdin=None, env_prune=None, stderr_to_stdout=False, binary_output=False): if env_prune is None: env_prune = [] @@ -642,3 +643,105 @@ def eintr_retry_call(func, *args): if e.errno == errno.EINTR: continue raise + +# +# Deprecation decorator. +# +_DEPRECATION_MESSAGE = "will be removed in a future version." +def _default_deprecation_msg(func): + return "%s %s" % (func.__name__, _DEPRECATION_MESSAGE) + +def indent(text, spaces=4): + """ Indent text by a specified number of spaces. + + :param str text: the text to indent + :keyword int spaces: the number of spaces to indent text + """ + if not text or not text.strip(): + return text + + indentation = " " * spaces + indented = [] + for line in text.splitlines(): + indented.append("%s%s" % (indentation, line)) + + return "\n".join(indented) + +_SPHINX_DEPRECATE = """.. deprecated:: %(version)s + %(message)s +""" +def _add_deprecation_doc_text(func, version=None, message=None): + """ Add sphinx 'deprecated' markup to a function's docstring. + + :param :class:`function` func: the function + :param str version: version in which the deprecation is effective + :param str message: message suggesting a preferred alternative + + If your doctext is indented with something other than spaces the added + doctext's indentation will probably not match. That'd be your fault. + """ + base_text = func.__doc__ + if base_text is None: + base_text = " " # They contain leading and trailing spaces. *shrug* + else: + base_text = base_text[:-1] # Trim the trailing space. + + if ".. deprecated::" in base_text: + # Don't add multiple deprecation directives. + return + + # Figure out the number of spaces to indent docstring text. + indent_spaces = None + for l in base_text.splitlines()[1:]: + if not l.strip(): + continue + + spaces = 0 + _l = l[:] + while _l and _l.startswith(" "): + spaces += 1 + _l = _l[1:] + + if indent_spaces is None or indent_spaces > spaces: + indent_spaces = spaces + + if indent_spaces is None: + indent_spaces = 0 + + text = "" + if not re.search(r'\n\s*$', base_text): + # Make sure there's a newline after the last text. + text = "\n" + + message = message or "" + text += _SPHINX_DEPRECATE % {"version": version, "message": message} + func.__doc__ = base_text + "\n" + indent(text, indent_spaces) + +def deprecated(version, message): + """ Decorator to deprecate a function or method via warning and docstring. + + :param str version: version in which the deprecation is effective + :param str message: message suggesting a preferred alternative + + .. note:: + At the point this decorator gets applied to a method in a class the + method is just a function. It becomes a method later. + """ + def deprecate_func(func): + @wraps(func) + def the_func(*args, **kwargs): + """ Issue a deprecation warning for, then call, a function. """ + # Warnings look much better with default warning text than with + # no text. The sphinx doesn't benefit from it, so don't use it + # there. + warn_msg = _default_deprecation_msg(func) + if message: + warn_msg += " %s" % message + + warnings.warn(warn_msg, DeprecationWarning, stacklevel=2) + return func(*args, **kwargs) + + _add_deprecation_doc_text(the_func, message=message, version=version) + return the_func + + return deprecate_func
Removed label: ACK.
New version handles issuing the warning and munging the doc text for sphinx. Usage can be seen in dwlehman@ed736ab.
@@ -637,3 +643,105 @@ def eintr_retry_call(func, *args): if e.errno == errno.EINTR: continue raise
+# +# Deprecation decorator. +# +_DEPRECATION_MESSAGE = "will be removed in a future version." +def _default_deprecation_msg(func):
- return "%s %s" % (func.__name__, _DEPRECATION_MESSAGE)
+def indent(text, spaces=4):
You can use textwrap.indent for this (on py3)
@@ -637,3 +643,105 @@ def eintr_retry_call(func, *args): if e.errno == errno.EINTR: continue raise
+# +# Deprecation decorator. +# +_DEPRECATION_MESSAGE = "will be removed in a future version." +def _default_deprecation_msg(func):
- return "%s %s" % (func.__name__, _DEPRECATION_MESSAGE)
+def indent(text, spaces=4):
- """ Indent text by a specified number of spaces.
:param str text: the text to indent:keyword int spaces: the number of spaces to indent text- """
- if not text or not text.strip():
return text- indentation = " " * spaces
- indented = []
- for line in text.splitlines():
indented.append("%s%s" % (indentation, line))- return "\n".join(indented)
+_SPHINX_DEPRECATE = """.. deprecated:: %(version)s
- %(message)s
+""" +def _add_deprecation_doc_text(func, version=None, message=None):
- """ Add sphinx 'deprecated' markup to a function's docstring.
:param :class:`function` func: the function:param str version: version in which the deprecation is effective:param str message: message suggesting a preferred alternativeIf your doctext is indented with something other than spaces the addeddoctext's indentation will probably not match. That'd be your fault.- """
- base_text = func.__doc__
- if base_text is None:
base_text = " " # They contain leading and trailing spaces. *shrug*- else:
base_text = base_text[:-1] # Trim the trailing space.- if ".. deprecated::" in base_text:
# Don't add multiple deprecation directives.return- # Figure out the number of spaces to indent docstring text.
- indent_spaces = None
Why don't you just set this to 0 and skip all the None checks?
I'm a bit worried about the extra overhead added to the calls, but since they're deprecated I suppose that doesn't matter.
Added label: ACK.
@@ -637,3 +643,105 @@ def eintr_retry_call(func, *args): if e.errno == errno.EINTR: continue raise
+# +# Deprecation decorator. +# +_DEPRECATION_MESSAGE = "will be removed in a future version." +def _default_deprecation_msg(func):
- return "%s %s" % (func.__name__, _DEPRECATION_MESSAGE)
+def indent(text, spaces=4):
- """ Indent text by a specified number of spaces.
:param str text: the text to indent:keyword int spaces: the number of spaces to indent text- """
- if not text or not text.strip():
return text- indentation = " " * spaces
- indented = []
- for line in text.splitlines():
indented.append("%s%s" % (indentation, line))- return "\n".join(indented)
+_SPHINX_DEPRECATE = """.. deprecated:: %(version)s
- %(message)s
+""" +def _add_deprecation_doc_text(func, version=None, message=None):
- """ Add sphinx 'deprecated' markup to a function's docstring.
:param :class:`function` func: the function:param str version: version in which the deprecation is effective:param str message: message suggesting a preferred alternativeIf your doctext is indented with something other than spaces the addeddoctext's indentation will probably not match. That'd be your fault.- """
- base_text = func.__doc__
- if base_text is None:
base_text = " " # They contain leading and trailing spaces. *shrug*- else:
base_text = base_text[:-1] # Trim the trailing space.- if ".. deprecated::" in base_text:
# Don't add multiple deprecation directives.return- # Figure out the number of spaces to indent docstring text.
- indent_spaces = None
I had myself turned around at some point, clearly, thinking that would prevent it from ever getting set to a non-default value. I'll change it.
@@ -637,3 +643,105 @@ def eintr_retry_call(func, *args): if e.errno == errno.EINTR: continue raise
+# +# Deprecation decorator. +# +_DEPRECATION_MESSAGE = "will be removed in a future version." +def _default_deprecation_msg(func):
- return "%s %s" % (func.__name__, _DEPRECATION_MESSAGE)
+def indent(text, spaces=4):
Looks like I'll have to use the TextWrapper class if I want it to be portable to python2. I'll look into that.
Note that the decorator is only applied once, so the only part that runs on every call to a deprecated method is the warning.warn call. The docstring hack runs once per method.
@@ -637,3 +643,105 @@ def eintr_retry_call(func, *args): if e.errno == errno.EINTR: continue raise
+# +# Deprecation decorator. +# +_DEPRECATION_MESSAGE = "will be removed in a future version." +def _default_deprecation_msg(func):
- return "%s %s" % (func.__name__, _DEPRECATION_MESSAGE)
+def indent(text, spaces=4):
- """ Indent text by a specified number of spaces.
:param str text: the text to indent:keyword int spaces: the number of spaces to indent text- """
- if not text or not text.strip():
return text- indentation = " " * spaces
- indented = []
- for line in text.splitlines():
indented.append("%s%s" % (indentation, line))- return "\n".join(indented)
+_SPHINX_DEPRECATE = """.. deprecated:: %(version)s
- %(message)s
+""" +def _add_deprecation_doc_text(func, version=None, message=None):
- """ Add sphinx 'deprecated' markup to a function's docstring.
:param :class:`function` func: the function:param str version: version in which the deprecation is effective:param str message: message suggesting a preferred alternativeIf your doctext is indented with something other than spaces the addeddoctext's indentation will probably not match. That'd be your fault.- """
- base_text = func.__doc__
- if base_text is None:
base_text = " " # They contain leading and trailing spaces. *shrug*- else:
base_text = base_text[:-1] # Trim the trailing space.- if ".. deprecated::" in base_text:
# Don't add multiple deprecation directives.return- # Figure out the number of spaces to indent docstring text.
- indent_spaces = None
I was right to begin with. If indent_spaces starts at 0 that if statement will never be True and indent_spaces will always end up being 0. (The idea there is to get the minimum indentation in the docstring and use that for the sphinx directive.)
@@ -637,3 +643,105 @@ def eintr_retry_call(func, *args): if e.errno == errno.EINTR: continue raise
+# +# Deprecation decorator. +# +_DEPRECATION_MESSAGE = "will be removed in a future version." +def _default_deprecation_msg(func):
- return "%s %s" % (func.__name__, _DEPRECATION_MESSAGE)
+def indent(text, spaces=4):
- """ Indent text by a specified number of spaces.
:param str text: the text to indent:keyword int spaces: the number of spaces to indent text- """
- if not text or not text.strip():
return text- indentation = " " * spaces
- indented = []
- for line in text.splitlines():
indented.append("%s%s" % (indentation, line))- return "\n".join(indented)
+_SPHINX_DEPRECATE = """.. deprecated:: %(version)s
- %(message)s
+""" +def _add_deprecation_doc_text(func, version=None, message=None):
- """ Add sphinx 'deprecated' markup to a function's docstring.
:param :class:`function` func: the function:param str version: version in which the deprecation is effective:param str message: message suggesting a preferred alternativeIf your doctext is indented with something other than spaces the addeddoctext's indentation will probably not match. That'd be your fault.- """
- base_text = func.__doc__
- if base_text is None:
base_text = " " # They contain leading and trailing spaces. *shrug*- else:
base_text = base_text[:-1] # Trim the trailing space.- if ".. deprecated::" in base_text:
# Don't add multiple deprecation directives.return- # Figure out the number of spaces to indent docstring text.
- indent_spaces = None
- for l in base_text.splitlines()[1:]:
if not l.strip():continuespaces = 0_l = l[:]while _l and _l.startswith(" "):spaces += 1_l = _l[1:]if indent_spaces is None or indent_spaces > spaces:
Ah! ok, maybe a note here that it's finding the minumum amount of indent.
anaconda-patches@lists.fedorahosted.org