Home | History | Annotate | Download | only in common_lib
      1 # Copyright 2017 The Chromium OS Authors. All rights reserved.
      2 # Use of this source code is governed by a BSD-style license that can be
      3 # found in the LICENSE file.
      4 
      5 """This module contains stuff for reporting deprecation.
      6 
      7 TODO(ayatane): The reason for putting this module here is so client tests can use it.
      8 """
      9 
     10 import urllib
     11 import warnings
     12 
     13 
     14 def warn(deprecated_name, stacklevel=3):
     15     """Convenience function for making deprecation warnings.
     16 
     17     @param deprecated_name: The name of the deprecated function or module
     18     @param stacklevel: See warnings.warn().
     19     """
     20     warnings.warn(APIDeprecationWarning(deprecated_name),
     21                   stacklevel=stacklevel)
     22 
     23 
     24 class APIDeprecationWarning(UserWarning):
     25     """API deprecation warning.
     26 
     27     This is different from DeprecationWarning.  DeprecationWarning is
     28     for Python deprecations, this class is for our deprecations.
     29     """
     30 
     31     def __init__(self, deprecated_name):
     32         """Initialize instance.
     33 
     34         @param deprecated_name: The name of the deprecated function or module
     35         """
     36         super(APIDeprecationWarning, self).__init__(deprecated_name)
     37         self._deprecated_name = deprecated_name
     38 
     39     def __str__(self):
     40         return ('%s is deprecated; please file a fixit bug: %s'
     41                 % (self._deprecated_name, self._get_fixit_bug_url()))
     42 
     43     def _get_fixit_bug_url(self):
     44         """Return the URL for making a fixit bug."""
     45         return ('https://bugs.chromium.org/p/chromium/issues/entry?'
     46                 + urllib.urlencode({
     47                     'summary': 'Deprecated use of %s' % self._deprecated_name,
     48                     'description': 'Please paste the warning message below\n',
     49                     'components': 'Infra>Client>ChromeOS',
     50                     'labels': 'Pri-3,Type-Bug,Hotlist-Fixit',
     51                 }))
     52