1
1
# Copyright 2014-2015 Canonical Limited.
3
# This file is part of charm-helpers.
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.
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.
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
7
# http://www.apache.org/licenses/LICENSE-2.0
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.
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
37
40
subprocess.check_call(['apt-get', 'install', '-y', 'python3-yaml'])
38
41
import yaml # flake8: noqa
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 = {}
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.
57
@deprecate('use core/fetch/add_source() instead', '2017-04')
58
def contributed_add_source_thing(...):
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.
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
73
def wrapped_f(*args, **kwargs):
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
83
if f_name not in __deprecated_functions:
84
__deprecated_functions[f_name] = True
85
s = "DEPRECATION WARNING: Function {} is being removed".format(
88
s = "{} on/around {}".format(s, date)
90
s = "{} : {}".format(s, warning)
95
return f(*args, **kwargs)