VirtualMailManager/common.py
changeset 571 a4aead244f75
parent 568 14abdd04ddf5
child 582 605f8c115711
equal deleted inserted replaced
465:c0e1fb1b0145 571:a4aead244f75
       
     1 # -*- coding: UTF-8 -*-
       
     2 # Copyright (c) 2010 - 2012, Pascal Volk
       
     3 # See COPYING for distribution information.
       
     4 """
       
     5     VirtualMailManager.common
       
     6     ~~~~~~~~~~~~~~~~~~~~~~~~~
       
     7 
       
     8     Some common functions
       
     9 """
       
    10 
       
    11 import locale
       
    12 import os
       
    13 import re
       
    14 import stat
       
    15 
       
    16 from VirtualMailManager import ENCODING
       
    17 from VirtualMailManager.constants import NOT_EXECUTABLE, NO_SUCH_BINARY, \
       
    18      TYPE_ACCOUNT, TYPE_ALIAS, TYPE_RELOCATED
       
    19 from VirtualMailManager.errors import VMMError
       
    20 
       
    21 VERSION_RE = re.compile(r'^(\d+)\.(\d+)\.(?:(\d+)|(alpha|beta|rc)(\d+))$')
       
    22 
       
    23 _version_level = dict(alpha=0xA, beta=0xB, rc=0xC)
       
    24 _version_cache = {}
       
    25 _ = lambda msg: msg
       
    26 
       
    27 
       
    28 def expand_path(path):
       
    29     """Expands paths, starting with ``.`` or ``~``, to an absolute path."""
       
    30     if path.startswith('.'):
       
    31         return os.path.abspath(path)
       
    32     if path.startswith('~'):
       
    33         return os.path.expanduser(path)
       
    34     return path
       
    35 
       
    36 
       
    37 def get_unicode(string):
       
    38     """Converts `string` to `unicode`, if necessary."""
       
    39     if isinstance(string, unicode):
       
    40         return string
       
    41     return unicode(string, ENCODING, 'replace')
       
    42 
       
    43 
       
    44 def lisdir(path):
       
    45     """Checks if `path` is a directory.  Doesn't follow symbolic links.
       
    46     Returns bool.
       
    47     """
       
    48     try:
       
    49         lstat = os.lstat(path)
       
    50     except OSError:
       
    51         return False
       
    52     return stat.S_ISDIR(lstat.st_mode)
       
    53 
       
    54 
       
    55 def exec_ok(binary):
       
    56     """Checks if the `binary` exists and if it is executable.
       
    57 
       
    58     Throws a `VMMError` if the `binary` isn't a file or is not
       
    59     executable.
       
    60     """
       
    61     binary = expand_path(binary)
       
    62     if not os.path.isfile(binary):
       
    63         raise VMMError(_(u"No such file: '%s'") % get_unicode(binary),
       
    64                        NO_SUCH_BINARY)
       
    65     if not os.access(binary, os.X_OK):
       
    66         raise VMMError(_(u"File is not executable: '%s'") %
       
    67                        get_unicode(binary), NOT_EXECUTABLE)
       
    68     return binary
       
    69 
       
    70 
       
    71 def human_size(size):
       
    72     """Converts the `size` in bytes in human readable format."""
       
    73     if not isinstance(size, (long, int)):
       
    74         try:
       
    75             size = long(size)
       
    76         except ValueError:
       
    77             raise TypeError("'size' must be a positive long or int.")
       
    78     if size < 0:
       
    79         raise ValueError("'size' must be a positive long or int.")
       
    80     if size < 1024:
       
    81         return str(size)
       
    82     prefix_multiply = ((_(u'TiB'), 1 << 40), (_(u'GiB'), 1 << 30),
       
    83                        (_(u'MiB'), 1 << 20), (_(u'KiB'), 1 << 10))
       
    84     for prefix, multiply in prefix_multiply:
       
    85         if size >= multiply:
       
    86             # TP: e.g.: '%(size)s %(prefix)s' -> '118.30 MiB'
       
    87             return _(u'%(size)s %(prefix)s') % {
       
    88                     'size': locale.format('%.2f', float(size) / multiply,
       
    89                                           True),
       
    90                     'prefix': prefix}
       
    91 
       
    92 
       
    93 def size_in_bytes(size):
       
    94     """Converts the string `size` to a long (size in bytes).
       
    95 
       
    96     The string `size` can be suffixed with *b* (bytes), *k* (kilobytes),
       
    97     *M* (megabytes) or *G* (gigabytes).
       
    98     """
       
    99     if not isinstance(size, basestring) or not size:
       
   100         raise TypeError('size must be a non empty string.')
       
   101     if size[-1].upper() in ('B', 'K', 'M', 'G'):
       
   102         try:
       
   103             num = int(size[:-1])
       
   104         except ValueError:
       
   105             raise ValueError('Not a valid integer value: %r' % size[:-1])
       
   106         unit = size[-1].upper()
       
   107         if unit == 'B':
       
   108             return num
       
   109         elif unit == 'K':
       
   110             return num << 10L
       
   111         elif unit == 'M':
       
   112             return num << 20L
       
   113         else:
       
   114             return num << 30L
       
   115     else:
       
   116         try:
       
   117             num = int(size)
       
   118         except ValueError:
       
   119             raise ValueError('Not a valid size value: %r' % size)
       
   120         return num
       
   121 
       
   122 
       
   123 def version_hex(version_string):
       
   124     """Converts a Dovecot version, e.g.: '1.2.3' or '2.0.beta4', to an int.
       
   125     Raises a `ValueError` if the *version_string* has the wrong™ format.
       
   126 
       
   127     version_hex('1.2.3') -> 270548736
       
   128     hex(version_hex('1.2.3')) -> '0x10203f00'
       
   129     """
       
   130     global _version_cache
       
   131     if version_string in _version_cache:
       
   132         return _version_cache[version_string]
       
   133     version = 0
       
   134     version_mo = VERSION_RE.match(version_string)
       
   135     if not version_mo:
       
   136         raise ValueError('Invalid version string: %r' % version_string)
       
   137     major, minor, patch, level, serial = version_mo.groups()
       
   138     major = int(major)
       
   139     minor = int(minor)
       
   140     if patch:
       
   141         patch = int(patch)
       
   142     if serial:
       
   143         serial = int(serial)
       
   144 
       
   145     if major > 0xFF or minor > 0xFF or \
       
   146       patch and patch > 0xFF or serial and serial > 0xFF:
       
   147         raise ValueError('Invalid version string: %r' % version_string)
       
   148 
       
   149     version += major << 28
       
   150     version += minor << 20
       
   151     if patch:
       
   152         version += patch << 12
       
   153     version += _version_level.get(level, 0xF) << 8
       
   154     if serial:
       
   155         version += serial
       
   156 
       
   157     _version_cache[version_string] = version
       
   158     return version
       
   159 
       
   160 
       
   161 def version_str(version):
       
   162     """Converts a Dovecot version previously converted with version_hex back to
       
   163     a string.
       
   164     Raises a `TypeError` if *version* is not an int/long.
       
   165     Raises a `ValueError` if *version* is an incorrect int version.
       
   166     """
       
   167     global _version_cache
       
   168     if version in _version_cache:
       
   169         return _version_cache[version]
       
   170     if not isinstance(version, (int, long)):
       
   171         raise TypeError('Argument is not a int/long: %r', version)
       
   172     major = (version >> 28) & 0xFF
       
   173     minor = (version >> 20) & 0xFF
       
   174     patch = (version >> 12) & 0xFF
       
   175     level = (version >> 8) & 0x0F
       
   176     serial = version & 0xFF
       
   177 
       
   178     levels = dict(zip(_version_level.values(), _version_level.keys()))
       
   179     if level == 0xF and not serial:
       
   180         version_string = '%u.%u.%u' % (major, minor, patch)
       
   181     elif level in levels and not patch:
       
   182         version_string = '%u.%u.%s%u' % (major, minor, levels[level], serial)
       
   183     else:
       
   184         raise ValueError('Invalid version: %r' % hex(version))
       
   185 
       
   186     _version_cache[version] = version_string
       
   187     return version_string
       
   188 
       
   189 def format_domain_default(domaindata):
       
   190     """Format info output when the value displayed is the domain default."""
       
   191     return _(u'%s [domain default]') % domaindata
       
   192 
       
   193 
       
   194 def search_addresses(dbh, typelimit=None, lpattern=None, llike=False,
       
   195                      dpattern=None, dlike=False):
       
   196     """'Search' for addresses by *pattern* in the database.
       
   197 
       
   198     The search is limited by *typelimit*, a bitfield with values TYPE_ACCOUNT,
       
   199     TYPE_ALIAS, TYPE_RELOCATED, or a bitwise OR thereof. If no limit is
       
   200     specified, all types will be searched.
       
   201 
       
   202     *lpattern* may be a local part or a partial local part - starting and/or
       
   203     ending with a '%' sign.  When the *lpattern* starts or ends with a '%' sign
       
   204     *llike* has to be `True` to perform a wildcard search. To retrieve all
       
   205     available addresses use the arguments' default values.
       
   206 
       
   207     *dpattern* and *dlike* behave analogously for the domain part of an
       
   208     address, allowing for separate pattern matching: testuser%@example.%
       
   209 
       
   210     The return value of this function is a tuple. The first element is a list
       
   211     of domain IDs sorted alphabetically by the corresponding domain names. The
       
   212     second element is a dictionary indexed by domain ID, holding lists to
       
   213     associated addresses. Each address is itself actually a tuple of address,
       
   214     type, and boolean indicating whether the address stems from an alias
       
   215     domain.
       
   216     """
       
   217     if typelimit == None:
       
   218             typelimit = TYPE_ACCOUNT | TYPE_ALIAS | TYPE_RELOCATED
       
   219     queries = []
       
   220     if typelimit & TYPE_ACCOUNT:
       
   221         queries.append('SELECT gid, local_part, %d AS type FROM users'
       
   222                        % TYPE_ACCOUNT)
       
   223     if typelimit & TYPE_ALIAS:
       
   224         queries.append('SELECT gid, address as local_part, %d AS type '
       
   225                        'FROM alias' % TYPE_ALIAS)
       
   226     if typelimit & TYPE_RELOCATED:
       
   227         queries.append('SELECT gid, address as local_part, %d AS type '
       
   228                        'FROM relocated' % TYPE_RELOCATED)
       
   229     sql  = "SELECT gid, local_part || '@' || domainname AS address, "
       
   230     sql += 'type, NOT is_primary AS from_aliasdomain FROM ('
       
   231     sql += ' UNION '.join(queries)
       
   232     sql += ') a JOIN domain_name USING (gid)'
       
   233     nextkw = 'WHERE'
       
   234     sqlargs = []
       
   235     for like, field, pattern in ((dlike, 'domainname', dpattern),
       
   236                                  (llike, 'local_part', lpattern)):
       
   237         if like:
       
   238             match = 'LIKE'
       
   239         else:
       
   240             if not pattern: continue
       
   241             match = '='
       
   242         sql += ' %s %s %s %%s' % (nextkw, field, match)
       
   243         sqlargs.append(pattern)
       
   244         nextkw = 'AND'
       
   245     sql += ' ORDER BY domainname, local_part'
       
   246     dbc = dbh.cursor()
       
   247     dbc.execute(sql, sqlargs)
       
   248     result = dbc.fetchall()
       
   249     dbc.close()
       
   250 
       
   251     gids = []
       
   252     daddrs = {}
       
   253     lastgid = None
       
   254     for gid, address, addrtype, aliasdomain in result:
       
   255         if gid != lastgid:
       
   256             gids.append(gid)
       
   257             lastgid = gid
       
   258             daddrs[gid] = []
       
   259         daddrs[gid].append((address, addrtype, aliasdomain))
       
   260     return gids, daddrs
       
   261 
       
   262 del _