VirtualMailManager/common.py
branchv0.6.x
changeset 263 07fdc93dde9f
parent 262 6eea85d8b91d
child 265 3c0173418d5d
equal deleted inserted replaced
262:6eea85d8b91d 263:07fdc93dde9f
     7 
     7 
     8     Some common functions
     8     Some common functions
     9 """
     9 """
    10 
    10 
    11 import os
    11 import os
       
    12 import re
    12 
    13 
    13 from VirtualMailManager import ENCODING
    14 from VirtualMailManager import ENCODING
    14 from VirtualMailManager.constants.ERROR import \
    15 from VirtualMailManager.constants.ERROR import \
    15      NOT_EXECUTABLE, NO_SUCH_BINARY, NO_SUCH_DIRECTORY
    16      NOT_EXECUTABLE, NO_SUCH_BINARY, NO_SUCH_DIRECTORY
    16 from VirtualMailManager.errors import VMMError
    17 from VirtualMailManager.errors import VMMError
    61                        get_unicode(binary), NOT_EXECUTABLE)
    62                        get_unicode(binary), NOT_EXECUTABLE)
    62     return binary
    63     return binary
    63 
    64 
    64 
    65 
    65 def version_hex(version_string):
    66 def version_hex(version_string):
    66     """Convert the version string '1.2.3' to an int.
    67     """Convert a Dovecot version, e.g.: '1.2.3' or '2.0.beta4', to an int.
    67     hex(version_hex('1.2.3')) -> '0x10203'
    68     Returns 0 if the *version_string* has the wrong™ format.
       
    69 
       
    70     version_hex('1.2.3') -> 16909296
       
    71     hex(version_hex('1.2.3')) -> '0x10203f0'
    68     """
    72     """
    69     major, minor, patch = map(int, version_string.split('.'))
    73     version = 0
    70     return (major << 16) + (minor << 8) + patch
    74     version_re = r'^(\d+)\.(\d+)\.(?:(\d+)|(alpha|beta|rc)(\d+))$'
       
    75     version_level = dict(alpha=0xA, beta=0xB, rc=0xC)
       
    76     version_mo = re.match(version_re, version_string)
       
    77     if version_mo:
       
    78         major, minor, patch, level, serial = version_mo.groups()
       
    79         version += int(major) << 24
       
    80         version += int(minor) << 16
       
    81         if patch:
       
    82             version += int(patch) << 8
       
    83         version += version_level.get(level, 0xF) << 4
       
    84         if serial:
       
    85             version += int(serial)
       
    86     return version
    71 
    87 
    72 del _
    88 del _