Package pybaz :: Module compat
[frames] | no frames]

Source Code for Module pybaz.compat

  1  # arch-tag: 8f2927c7-53a3-4a84-9055-1f99466c8a8d 
  2  # Copyright (C) 2005 Canonical Ltd. 
  3  #               Author: David Allouche <david@canonical.com> 
  4  # 
  5  #    This program is free software; you can redistribute it and/or modify 
  6  #    it under the terms of the GNU General Public License as published by 
  7  #    the Free Software Foundation; either version 2 of the License, or 
  8  #    (at your option) any later version. 
  9  # 
 10  #    This program is distributed in the hope that it will be useful, 
 11  #    but WITHOUT ANY WARRANTY; without even the implied warranty of 
 12  #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 13  #    GNU General Public License for more details. 
 14  # 
 15  #    You should have received a copy of the GNU General Public License 
 16  #    along with this program; if not, write to the Free Software 
 17  #    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 18   
 19  """Utilities for handling compatibility with multiple versions.""" 
 20   
 21  __all__ = ['status', 'BazaarCommandVersion'] 
 22   
 23   
24 -class _Enum(object):
25 - def __init__(self, name):
26 self.__name = name
27
28 - def __repr__(self):
29 class_name = self.__class__.__name__ 30 return '%s.status.%s' % (__name__, self.__name)
31
32 - def __str__(self):
33 return self.__name
34
35 - def __eq__(self, rhs):
36 return self is rhs
37
38 - def __ne__(self, rhs):
39 return self is not rhs
40 41
42 -class status(object):
43
44 - def __init__(self):
45 raise AssertionError('Tried to instanciate enum class')
46 47 # baz is a supported release 48 OK = _Enum('OK') 49 50 # baz a future release, pybaz must be upgraded 51 BAZ_TOO_NEW = _Enum('BAZ_TOO_NEW') 52 53 # baz is an unsupported obsolete release, baz should be upgraded 54 UNSUPPORTED = _Enum('UNSUPPORTED') 55 56 # baz and pybaz are prereleases for the same version, if a problem arises, 57 # both must be upgraded to the latest release before reporting an issue. 58 COTM = _Enum('COTM')
59 60
61 -def _get_status(pybaz, baz):
62 bazminor = baz.release[:2] 63 pybazminor = pybaz.release[:2] 64 if bazminor == pybazminor: 65 if pybaz.cotm and not baz.cotm: 66 return status.BAZ_TOO_NEW 67 if baz.cotm and pybaz.cotm: 68 return status.COTM 69 if baz.cotm and not pybaz.cotm: 70 return status.UNSUPPORTED 71 return status.OK 72 if bazminor < pybazminor: 73 if baz.cotm: 74 return status.UNSUPPORTED 75 return status.OK 76 if bazminor > pybazminor: 77 return status.BAZ_TOO_NEW
78 79 80
81 -class BazaarCommandVersion(object):
82 83 """Parsing and comparison of Bazaar version strings.""" 84
85 - def __init__(self, version_line):
86 """Parse a Bazaar version string. 87 88 :param version_line: line of the form 89 ``'baz Bazaar version MAJOR.MINOR[.POINT][[-]~COTM]'`` 90 :type version_line: str 91 """ 92 self._version_line = version_line 93 vstr, vtuple, cotm = self._parse(version_line) 94 self._string = vstr 95 self._release_tuple = vtuple 96 self._cotm = cotm
97
98 - def __repr__(self):
99 class_name = self.__class__.__name__ 100 return '%s.%s(%r)' % (__name__, class_name, self._version_line)
101
102 - def _parse(line):
103 """Actual version string parsing method. 104 105 Extracted as a static method for unit testing. 106 """ 107 words = line.split(' ') 108 assert words[:3] == ['baz', 'Bazaar', 'version'] 109 full_version_str = words[3] 110 version_cotm = full_version_str.split('~') 111 if len(version_cotm) == 1: 112 version_str = version_cotm[0] 113 cotm = None 114 elif len(version_cotm) == 2: 115 version_str, cotm = version_cotm 116 cotm = long(cotm) 117 else: 118 raise AssertionError('Version string seems to have multiple' 119 ' COTM qualifiers: ' + full_version_str) 120 version_str = version_str.rstrip('-') 121 version = map(int, version_str.split('.')) 122 if len(version) == 2: 123 version.append(0) 124 else: 125 assert len(version) == 3 126 return full_version_str, tuple(version), cotm
127 128 _parse = staticmethod(_parse) 129
130 - def _get_string(self):
131 return self._string
132 133 string = property(_get_string, doc=""" 134 Unparsed version string, without the "baz Bazaar version" prefix. 135 Use this attribute in user-visible messages. 136 """) 137
138 - def _get_release(self):
139 return self._release_tuple
140 141 release = property(_get_release, doc=""" 142 Tuple of integers representing the Bazaar release version.""") 143
144 - def _get_cotm(self):
145 return self._cotm
146 147 cotm = property(_get_cotm, doc=""" 148 Long integer identifying the Crack Of The Minute build. 149 None if the version is a release. 150 """) 151 152 _end_of_times = (10 ** (4 + 2 + 2 + 2 + 2)) - 1 153
154 - def _comparable(self):
155 a = list(self.release) 156 if self.cotm is None: 157 a.append(0) 158 else: 159 a.append(self.cotm - self._end_of_times) 160 return a
161
162 - def __cmp__(self, rhs):
163 if not isinstance(rhs, BazaarCommandVersion): 164 msg = 'Tried to compare BazaarCommandVersion with %r' % rhs 165 raise TypeError(msg) 166 a = self._comparable() 167 b = rhs._comparable() 168 return cmp(a, b)
169