~ubuntu-branches/ubuntu/vivid/ironic/vivid-updates

« back to all changes in this revision

Viewing changes to ironic/openstack/common/versionutils.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2015-03-30 11:14:57 UTC
  • mfrom: (1.2.6)
  • Revision ID: package-import@ubuntu.com-20150330111457-kr4ju3guf22m4vbz
Tags: 2015.1~b3-0ubuntu1
* New upstream release.
  + d/control: 
    - Align with upstream dependencies.
    - Add dh-python to build-dependencies.
    - Add psmisc as a dependency. (LP: #1358820)
  + d/p/fix-requirements.patch: Rediffed.
  + d/ironic-conductor.init.in: Fixed typos in LSB headers,
    thanks to JJ Asghar. (LP: #1429962)

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
"""
19
19
 
20
20
import functools
 
21
import inspect
 
22
import logging
21
23
 
 
24
from oslo_config import cfg
22
25
import pkg_resources
 
26
import six
23
27
 
24
 
from ironic.openstack.common.gettextutils import _
25
 
from ironic.openstack.common import log as logging
 
28
from ironic.openstack.common._i18n import _
26
29
 
27
30
 
28
31
LOG = logging.getLogger(__name__)
 
32
CONF = cfg.CONF
 
33
 
 
34
 
 
35
opts = [
 
36
    cfg.BoolOpt('fatal_deprecations',
 
37
                default=False,
 
38
                help='Enables or disables fatal status of deprecations.'),
 
39
]
29
40
 
30
41
 
31
42
class deprecated(object):
72
83
    ICEHOUSE = 'I'
73
84
    JUNO = 'J'
74
85
    KILO = 'K'
 
86
    LIBERTY = 'L'
75
87
 
76
88
    _RELEASES = {
77
89
        # NOTE(morganfainberg): Bexar is used for unit test purposes, it is
83
95
        'I': 'Icehouse',
84
96
        'J': 'Juno',
85
97
        'K': 'Kilo',
 
98
        'L': 'Liberty',
86
99
    }
87
100
 
88
101
    _deprecated_msg_with_alternative = _(
116
129
        self.remove_in = remove_in
117
130
        self.what = what
118
131
 
119
 
    def __call__(self, func):
 
132
    def __call__(self, func_or_cls):
120
133
        if not self.what:
121
 
            self.what = func.__name__ + '()'
122
 
 
123
 
        @functools.wraps(func)
124
 
        def wrapped(*args, **kwargs):
125
 
            msg, details = self._build_message()
126
 
            LOG.deprecated(msg, details)
127
 
            return func(*args, **kwargs)
128
 
        return wrapped
 
134
            self.what = func_or_cls.__name__ + '()'
 
135
        msg, details = self._build_message()
 
136
 
 
137
        if inspect.isfunction(func_or_cls):
 
138
 
 
139
            @six.wraps(func_or_cls)
 
140
            def wrapped(*args, **kwargs):
 
141
                report_deprecated_feature(LOG, msg, details)
 
142
                return func_or_cls(*args, **kwargs)
 
143
            return wrapped
 
144
        elif inspect.isclass(func_or_cls):
 
145
            orig_init = func_or_cls.__init__
 
146
 
 
147
            # TODO(tsufiev): change `functools` module to `six` as
 
148
            # soon as six 1.7.4 (with fix for passing `assigned`
 
149
            # argument to underlying `functools.wraps`) is released
 
150
            # and added to the oslo-incubator requrements
 
151
            @functools.wraps(orig_init, assigned=('__name__', '__doc__'))
 
152
            def new_init(self, *args, **kwargs):
 
153
                report_deprecated_feature(LOG, msg, details)
 
154
                orig_init(self, *args, **kwargs)
 
155
            func_or_cls.__init__ = new_init
 
156
            return func_or_cls
 
157
        else:
 
158
            raise TypeError('deprecated can be used only with functions or '
 
159
                            'classes')
129
160
 
130
161
    def _get_safe_to_remove_release(self, release):
131
162
        # TODO(dstanek): this method will have to be reimplemented once
181
212
        return False
182
213
 
183
214
    return current_parts >= requested_parts
 
215
 
 
216
 
 
217
# Track the messages we have sent already. See
 
218
# report_deprecated_feature().
 
219
_deprecated_messages_sent = {}
 
220
 
 
221
 
 
222
def report_deprecated_feature(logger, msg, *args, **kwargs):
 
223
    """Call this function when a deprecated feature is used.
 
224
 
 
225
    If the system is configured for fatal deprecations then the message
 
226
    is logged at the 'critical' level and :class:`DeprecatedConfig` will
 
227
    be raised.
 
228
 
 
229
    Otherwise, the message will be logged (once) at the 'warn' level.
 
230
 
 
231
    :raises: :class:`DeprecatedConfig` if the system is configured for
 
232
             fatal deprecations.
 
233
    """
 
234
    stdmsg = _("Deprecated: %s") % msg
 
235
    CONF.register_opts(opts)
 
236
    if CONF.fatal_deprecations:
 
237
        logger.critical(stdmsg, *args, **kwargs)
 
238
        raise DeprecatedConfig(msg=stdmsg)
 
239
 
 
240
    # Using a list because a tuple with dict can't be stored in a set.
 
241
    sent_args = _deprecated_messages_sent.setdefault(msg, list())
 
242
 
 
243
    if args in sent_args:
 
244
        # Already logged this message, so don't log it again.
 
245
        return
 
246
 
 
247
    sent_args.append(args)
 
248
    logger.warn(stdmsg, *args, **kwargs)
 
249
 
 
250
 
 
251
class DeprecatedConfig(Exception):
 
252
    message = _("Fatal call to deprecated config: %(msg)s")
 
253
 
 
254
    def __init__(self, msg):
 
255
        super(Exception, self).__init__(self.message % dict(msg=msg))