author | Pascal Volk <neverseen@users.sourceforge.net> |
Sat, 24 Apr 2010 02:49:45 +0000 | |
branch | v0.6.x |
changeset 262 | 6eea85d8b91d |
child 263 | 07fdc93dde9f |
permissions | -rw-r--r-- |
262
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
1 |
# -*- coding: UTF-8 -*- |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
2 |
# Copyright (c) 2010, Pascal Volk |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
3 |
# See COPYING for distribution information. |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
4 |
|
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
5 |
""" |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
6 |
VirtualMailManager.common |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
7 |
|
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
8 |
Some common functions |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
9 |
""" |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
10 |
|
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
11 |
import os |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
12 |
|
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
13 |
from VirtualMailManager import ENCODING |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
14 |
from VirtualMailManager.constants.ERROR import \ |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
15 |
NOT_EXECUTABLE, NO_SUCH_BINARY, NO_SUCH_DIRECTORY |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
16 |
from VirtualMailManager.errors import VMMError |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
17 |
|
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
18 |
_ = lambda msg: msg |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
19 |
|
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
20 |
|
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
21 |
def expand_path(path): |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
22 |
"""Expands paths, starting with ``.`` or ``~``, to an absolute path.""" |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
23 |
if path.startswith('.'): |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
24 |
return os.path.abspath(path) |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
25 |
if path.startswith('~'): |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
26 |
return os.path.expanduser(path) |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
27 |
return path |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
28 |
|
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
29 |
|
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
30 |
def get_unicode(string): |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
31 |
"""Converts `string` to `unicode`, if necessary.""" |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
32 |
if isinstance(string, unicode): |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
33 |
return string |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
34 |
return unicode(string, ENCODING, 'replace') |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
35 |
|
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
36 |
|
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
37 |
def is_dir(path): |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
38 |
"""Checks if `path` is a directory. |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
39 |
|
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
40 |
Throws a `VMMError` if `path` is not a directory. |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
41 |
""" |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
42 |
path = expand_path(path) |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
43 |
if not os.path.isdir(path): |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
44 |
raise VMMError(_(u"'%s' is not a directory") % get_unicode(path), |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
45 |
NO_SUCH_DIRECTORY) |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
46 |
return path |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
47 |
|
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
48 |
|
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
49 |
def exec_ok(binary): |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
50 |
"""Checks if the `binary` exists and if it is executable. |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
51 |
|
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
52 |
Throws a `VMMError` if the `binary` isn't a file or is not |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
53 |
executable. |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
54 |
""" |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
55 |
binary = expand_path(binary) |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
56 |
if not os.path.isfile(binary): |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
57 |
raise VMMError(_(u"'%s' is not a file") % get_unicode(binary), |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
58 |
NO_SUCH_BINARY) |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
59 |
if not os.access(binary, os.X_OK): |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
60 |
raise VMMError(_(u"File is not executable: '%s'") % |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
61 |
get_unicode(binary), NOT_EXECUTABLE) |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
62 |
return binary |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
63 |
|
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
64 |
|
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
65 |
def version_hex(version_string): |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
66 |
"""Convert the version string '1.2.3' to an int. |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
67 |
hex(version_hex('1.2.3')) -> '0x10203' |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
68 |
""" |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
69 |
major, minor, patch = map(int, version_string.split('.')) |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
70 |
return (major << 16) + (minor << 8) + patch |
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
71 |
|
6eea85d8b91d
VMM: moved some non-init functions to the new common module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
72 |
del _ |