/usr/share/ganeti/2.16/ganeti/compat.py is in ganeti-2.16 2.16.0~rc2-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | #
#
# Copyright (C) 2010, 2011 Google Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""Module containing backported language/library functionality.
"""
import itertools
import operator
try:
# pylint: disable=F0401
import functools
except ImportError:
functools = None
try:
# pylint: disable=F0401
import roman
except ImportError:
roman = None
# compat.md5_hash and compat.sha1_hash can be called to generate and md5 and a
# sha1 hashing modules, under python 2.4, 2.5 and 2.6, even though some changes
# went on. compat.sha1 is python-version specific and is used for python
# modules (hmac, for example) which have changed their behavior as well from
# one version to the other.
try:
# Yes, these don't always exist, that's why we're testing
# Yes, we're not using the imports in this module.
from hashlib import md5 as md5_hash # pylint: disable=W0611,E0611,F0401
from hashlib import sha1 as sha1_hash # pylint: disable=W0611,E0611,F0401
# this additional version is needed for compatibility with the hmac module
sha1 = sha1_hash # pylint: disable=C0103
except ImportError:
from md5 import new as md5_hash # pylint: disable=W0611
import sha
sha1 = sha
sha1_hash = sha.new # pylint: disable=C0103
def _all(seq):
"""Returns True if all elements in the iterable are True.
"""
for _ in itertools.ifilterfalse(bool, seq):
return False
return True
def _any(seq):
"""Returns True if any element of the iterable are True.
"""
for _ in itertools.ifilter(bool, seq):
return True
return False
try:
# pylint: disable=E0601
# pylint: disable=W0622
all = all
except NameError:
all = _all
try:
# pylint: disable=E0601
# pylint: disable=W0622
any = any
except NameError:
any = _any
def partition(seq, pred=bool): # pylint: disable=W0622
"""Partition a list in two, based on the given predicate.
"""
return (list(itertools.ifilter(pred, seq)),
list(itertools.ifilterfalse(pred, seq)))
# Even though we're using Python's built-in "partial" function if available,
# this one is always defined for testing.
def _partial(func, *args, **keywords): # pylint: disable=W0622
"""Decorator with partial application of arguments and keywords.
This function was copied from Python's documentation.
"""
def newfunc(*fargs, **fkeywords):
newkeywords = keywords.copy()
newkeywords.update(fkeywords)
return func(*(args + fargs), **newkeywords)
newfunc.func = func
newfunc.args = args
newfunc.keywords = keywords
return newfunc
if functools is None:
partial = _partial
else:
partial = functools.partial
def RomanOrRounded(value, rounding, convert=True):
"""Try to round the value to the closest integer and return it as a roman
numeral. If the conversion is disabled, or if the roman module could not be
loaded, round the value to the specified level and return it.
@type value: number
@param value: value to convert
@type rounding: integer
@param rounding: how many decimal digits the number should be rounded to
@type convert: boolean
@param convert: if False, don't try conversion at all
@rtype: string
@return: roman numeral for val, or formatted string representing val if
conversion didn't succeed
"""
def _FormatOutput(val, r):
format_string = "%0." + str(r) + "f"
return format_string % val
if roman is not None and convert:
try:
return roman.toRoman(round(value, 0))
except roman.RomanError:
return _FormatOutput(value, rounding)
return _FormatOutput(value, rounding)
def TryToRoman(val, convert=True):
"""Try to convert a value to roman numerals
If the roman module could be loaded convert the given value to a roman
numeral. Gracefully fail back to leaving the value untouched.
@type val: integer
@param val: value to convert
@type convert: boolean
@param convert: if False, don't try conversion at all
@rtype: string or typeof(val)
@return: roman numeral for val, or val if conversion didn't succeed
"""
if roman is not None and convert:
try:
return roman.toRoman(val)
except roman.RomanError:
return val
else:
return val
def UniqueFrozenset(seq):
"""Makes C{frozenset} from sequence after checking for duplicate elements.
@raise ValueError: When there are duplicate elements
"""
if isinstance(seq, (list, tuple)):
items = seq
else:
items = list(seq)
result = frozenset(items)
if len(items) != len(result):
raise ValueError("Duplicate values found")
return result
#: returns the first element of a list-like value
fst = operator.itemgetter(0)
#: returns the second element of a list-like value
snd = operator.itemgetter(1)
|