VirtualMailManager/common.py
branchv0.6.x
changeset 265 3c0173418d5d
parent 263 07fdc93dde9f
child 266 e14c345b44a1
equal deleted inserted replaced
264:04fea4d8b900 265:3c0173418d5d
    14 from VirtualMailManager import ENCODING
    14 from VirtualMailManager import ENCODING
    15 from VirtualMailManager.constants.ERROR import \
    15 from VirtualMailManager.constants.ERROR import \
    16      NOT_EXECUTABLE, NO_SUCH_BINARY, NO_SUCH_DIRECTORY
    16      NOT_EXECUTABLE, NO_SUCH_BINARY, NO_SUCH_DIRECTORY
    17 from VirtualMailManager.errors import VMMError
    17 from VirtualMailManager.errors import VMMError
    18 
    18 
       
    19 
       
    20 _version_re = re.compile(r'^(\d+)\.(\d+)\.(?:(\d+)|(alpha|beta|rc)(\d+))$')
    19 _ = lambda msg: msg
    21 _ = lambda msg: msg
    20 
    22 
    21 
    23 
    22 def expand_path(path):
    24 def expand_path(path):
    23     """Expands paths, starting with ``.`` or ``~``, to an absolute path."""
    25     """Expands paths, starting with ``.`` or ``~``, to an absolute path."""
    63     return binary
    65     return binary
    64 
    66 
    65 
    67 
    66 def version_hex(version_string):
    68 def version_hex(version_string):
    67     """Convert a Dovecot version, e.g.: '1.2.3' or '2.0.beta4', to an int.
    69     """Convert a Dovecot version, e.g.: '1.2.3' or '2.0.beta4', to an int.
    68     Returns 0 if the *version_string* has the wrong™ format.
    70     Raises a `ValueError` if the *version_string* has the wrong™ format.
    69 
    71 
    70     version_hex('1.2.3') -> 16909296
    72     version_hex('1.2.3') -> 16909296
    71     hex(version_hex('1.2.3')) -> '0x10203f0'
    73     hex(version_hex('1.2.3')) -> '0x10203f0'
    72     """
    74     """
    73     version = 0
    75     version = 0
    74     version_re = r'^(\d+)\.(\d+)\.(?:(\d+)|(alpha|beta|rc)(\d+))$'
       
    75     version_level = dict(alpha=0xA, beta=0xB, rc=0xC)
    76     version_level = dict(alpha=0xA, beta=0xB, rc=0xC)
    76     version_mo = re.match(version_re, version_string)
    77     version_mo = _version_re.match(version_string)
    77     if version_mo:
    78     if not version_mo:
    78         major, minor, patch, level, serial = version_mo.groups()
    79         raise ValueError('Invalid version string: %r' % version_string)
    79         version += int(major) << 24
    80     major, minor, patch, level, serial = version_mo.groups()
    80         version += int(minor) << 16
    81     version += int(major) << 24
    81         if patch:
    82     version += int(minor) << 16
    82             version += int(patch) << 8
    83     if patch:
    83         version += version_level.get(level, 0xF) << 4
    84         version += int(patch) << 8
    84         if serial:
    85     version += version_level.get(level, 0xF) << 4
    85             version += int(serial)
    86     if serial:
       
    87         version += int(serial)
    86     return version
    88     return version
    87 
    89 
    88 del _
    90 del _