~ubuntu-branches/ubuntu/precise/virt-manager/precise-updates

« back to all changes in this revision

Viewing changes to src/virtManager/util.py

  • Committer: Bazaar Package Importer
  • Author(s): Laurent Léonard
  • Date: 2010-03-25 09:14:38 UTC
  • mto: (1.2.1 upstream) (2.1.15 sid)
  • mto: This revision was merged to the branch mainline in revision 31.
  • Revision ID: james.westby@ubuntu.com-20100325091438-03vv382wj8jqgr26
Tags: upstream-0.8.4
Import upstream version 0.8.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
# MA 02110-1301 USA.
19
19
#
20
20
 
21
 
import logging
22
21
import gtk
 
22
import gobject
 
23
 
 
24
import libvirt
23
25
import libxml2
 
26
 
 
27
import logging
24
28
import os.path
25
29
 
26
 
import libvirt
27
 
 
28
30
import virtManager
29
31
import virtinst
30
32
 
32
34
DEFAULT_POOL_PATH = "/var/lib/libvirt/images"
33
35
 
34
36
def build_default_pool(conn):
35
 
    """Helper to build the 'default' storage pool"""
 
37
    """
 
38
    Helper to build the 'default' storage pool
 
39
    """
36
40
    # FIXME: This should use config.get_default_image_path ?
37
41
 
38
42
    if not virtinst.util.is_storage_capable(conn):
39
43
        # VirtualDisk will raise an error for us
40
44
        return
 
45
 
41
46
    pool = None
42
47
    try:
43
48
        pool = conn.storagePoolLookupByName(DEFAULT_POOL_NAME)
59
64
        raise RuntimeError(_("Couldn't create default storage pool '%s': %s") %
60
65
                             (DEFAULT_POOL_PATH, str(e)))
61
66
 
 
67
def get_ideal_path_info(conn, config, name):
 
68
    path = get_default_dir(conn, config)
 
69
    suffix = ".img"
 
70
    return (path, name, suffix)
 
71
 
 
72
def get_ideal_path(conn, config, name):
 
73
    target, name, suffix = get_ideal_path_info(conn, config, name)
 
74
    return os.path.join(target, name) + suffix
 
75
 
 
76
def get_default_pool(conn):
 
77
    pool = None
 
78
    for uuid in conn.list_pool_uuids():
 
79
        p = conn.get_pool(uuid)
 
80
        if p.get_name() == DEFAULT_POOL_NAME:
 
81
            pool = p
 
82
 
 
83
    return pool
 
84
 
 
85
def get_default_dir(conn, config):
 
86
    pool = get_default_pool(conn)
 
87
 
 
88
    if pool:
 
89
        return pool.get_target_path()
 
90
    else:
 
91
        return config.get_default_image_dir(conn)
 
92
 
 
93
def get_default_path(conn, config, name):
 
94
    pool = get_default_pool(conn)
 
95
 
 
96
    default_dir = get_default_dir(conn, config)
 
97
 
 
98
    if not pool:
 
99
        # Use old generating method
 
100
        origf = os.path.join(default_dir, name + ".img")
 
101
        f = origf
 
102
 
 
103
        n = 1
 
104
        while os.path.exists(f) and n < 100:
 
105
            f = os.path.join(default_dir, name +
 
106
                             "-" + str(n) + ".img")
 
107
            n += 1
 
108
 
 
109
        if os.path.exists(f):
 
110
            f = origf
 
111
 
 
112
        path = f
 
113
    else:
 
114
        target, ignore, suffix = get_ideal_path_info(conn, config, name)
 
115
 
 
116
        path = virtinst.Storage.StorageVolume.find_free_name(name,
 
117
                        pool_object=pool.pool, suffix=suffix)
 
118
 
 
119
        path = os.path.join(target, path)
 
120
 
 
121
    return path
 
122
 
 
123
 
62
124
def tooltip_wrapper(obj, txt, func="set_tooltip_text"):
63
125
    # Catch & ignore errors - set_tooltip_* is in gtk >= 2.12
64
126
    # and we can easily work with lower versions
238
300
 
239
301
    return label
240
302
 
 
303
def connect_once(obj, signal, func, *args):
 
304
    id_list = []
 
305
 
 
306
    def wrap_func(*wrapargs):
 
307
        if id_list:
 
308
            obj.disconnect(id_list[0])
 
309
 
 
310
        return func(*wrapargs)
 
311
 
 
312
    conn_id = obj.connect(signal, wrap_func, *args)
 
313
    id_list.append(conn_id)
 
314
 
 
315
    return conn_id
 
316
 
 
317
def connect_opt_out(obj, signal, func, *args):
 
318
    id_list = []
 
319
 
 
320
    def wrap_func(*wrapargs):
 
321
        ret = func(*wrapargs)
 
322
        if ret and id_list:
 
323
            obj.disconnect(id_list[0])
 
324
 
 
325
    conn_id = obj.connect(signal, wrap_func, *args)
 
326
    id_list.append(conn_id)
 
327
 
 
328
    return conn_id
 
329
 
241
330
def idle_emit(self, signal, *args):
242
331
    """
243
332
    Safe wrapper for using 'self.emit' with gobject.idle_add
245
334
    self.emit(signal, *args)
246
335
    return False
247
336
 
 
337
def _safe_wrapper(func, *args):
 
338
    gtk.gdk.threads_enter()
 
339
    try:
 
340
        return func(*args)
 
341
    finally:
 
342
        gtk.gdk.threads_leave()
 
343
 
 
344
def safe_idle_add(func, *args):
 
345
    """
 
346
    Make sure idle functions are run thread safe
 
347
    """
 
348
    return gobject.idle_add(_safe_wrapper, func, *args)
 
349
 
 
350
def safe_timeout_add(timeout, func, *args):
 
351
    """
 
352
    Make sure timeout functions are run thread safe
 
353
    """
 
354
    return gobject.timeout_add(timeout, _safe_wrapper, func, *args)
 
355
 
248
356
def uuidstr(rawuuid):
249
357
    hx = ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f']
250
358
    uuid = []
286
394
            use_str += iface.get_name()
287
395
 
288
396
    return use_str
 
397