~nick-moffitt/charms/trusty/thruk-agent/bionic-testing

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/__init__.py

  • Committer: Xav Paice
  • Date: 2017-07-25 09:36:07 UTC
  • mfrom: (34.1.1 thruk-agent)
  • Revision ID: xav.paice@canonical.com-20170725093607-aw08pmbfhda7hkp4
[xavpaice, r=elmo] update Thruk to 2.14 from OMD repo

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright 2014-2015 Canonical Limited.
2
2
#
3
 
# This file is part of charm-helpers.
4
 
#
5
 
# charm-helpers is free software: you can redistribute it and/or modify
6
 
# it under the terms of the GNU Lesser General Public License version 3 as
7
 
# published by the Free Software Foundation.
8
 
#
9
 
# charm-helpers is distributed in the hope that it will be useful,
10
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
# GNU Lesser General Public License for more details.
13
 
#
14
 
# You should have received a copy of the GNU Lesser General Public License
15
 
# along with charm-helpers.  If not, see <http://www.gnu.org/licenses/>.
 
3
# Licensed under the Apache License, Version 2.0 (the "License");
 
4
# you may not use this file except in compliance with the License.
 
5
# You may obtain a copy of the License at
 
6
#
 
7
#  http://www.apache.org/licenses/LICENSE-2.0
 
8
#
 
9
# Unless required by applicable law or agreed to in writing, software
 
10
# distributed under the License is distributed on an "AS IS" BASIS,
 
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
12
# See the License for the specific language governing permissions and
 
13
# limitations under the License.
16
14
 
17
15
# Bootstrap charm-helpers, installing its dependencies if necessary using
18
16
# only standard libraries.
 
17
from __future__ import print_function
 
18
from __future__ import absolute_import
 
19
 
 
20
import functools
 
21
import inspect
19
22
import subprocess
20
23
import sys
21
24
 
36
39
    else:
37
40
        subprocess.check_call(['apt-get', 'install', '-y', 'python3-yaml'])
38
41
    import yaml  # flake8: noqa
 
42
 
 
43
 
 
44
# Holds a list of mapping of mangled function names that have been deprecated
 
45
# using the @deprecate decorator below.  This is so that the warning is only
 
46
# printed once for each usage of the function.
 
47
__deprecated_functions = {}
 
48
 
 
49
 
 
50
def deprecate(warning, date=None, log=None):
 
51
    """Add a deprecation warning the first time the function is used.
 
52
    The date, which is a string in semi-ISO8660 format indicate the year-month
 
53
    that the function is officially going to be removed.
 
54
 
 
55
    usage:
 
56
 
 
57
    @deprecate('use core/fetch/add_source() instead', '2017-04')
 
58
    def contributed_add_source_thing(...):
 
59
        ...
 
60
 
 
61
    And it then prints to the log ONCE that the function is deprecated.
 
62
    The reason for passing the logging function (log) is so that hookenv.log
 
63
    can be used for a charm if needed.
 
64
 
 
65
    :param warning:  String to indicat where it has moved ot.
 
66
    :param date: optional sting, in YYYY-MM format to indicate when the
 
67
                 function will definitely (probably) be removed.
 
68
    :param log: The log function to call to log.  If not, logs to stdout
 
69
    """
 
70
    def wrap(f):
 
71
 
 
72
        @functools.wraps(f)
 
73
        def wrapped_f(*args, **kwargs):
 
74
            try:
 
75
                module = inspect.getmodule(f)
 
76
                file = inspect.getsourcefile(f)
 
77
                lines = inspect.getsourcelines(f)
 
78
                f_name = "{}-{}-{}..{}-{}".format(
 
79
                    module.__name__, file, lines[0], lines[-1], f.__name__)
 
80
            except (IOError, TypeError):
 
81
                # assume it was local, so just use the name of the function
 
82
                f_name = f.__name__
 
83
            if f_name not in __deprecated_functions:
 
84
                __deprecated_functions[f_name] = True
 
85
                s = "DEPRECATION WARNING: Function {} is being removed".format(
 
86
                    f.__name__)
 
87
                if date:
 
88
                    s = "{} on/around {}".format(s, date)
 
89
                if warning:
 
90
                    s = "{} : {}".format(s, warning)
 
91
                if log:
 
92
                    log(s)
 
93
                else:
 
94
                    print(s)
 
95
            return f(*args, **kwargs)
 
96
        return wrapped_f
 
97
    return wrap