~ubuntu-branches/ubuntu/quantal/virtinst/quantal-proposed

« back to all changes in this revision

Viewing changes to virtinst/util.py

  • Committer: Bazaar Package Importer
  • Author(s): Laurent Léonard
  • Date: 2011-01-29 21:41:21 UTC
  • mto: (1.6.3 sid)
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: james.westby@ubuntu.com-20110129214121-pjuxf2xz08l5zqew
Tags: upstream-0.500.5
Import upstream version 0.500.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
import libxml2
35
35
import logging
36
36
import subprocess
37
 
from sys import stderr
38
37
 
39
38
import libvirt
40
39
from virtinst import _virtinst as _
47
46
XORG_CONF = "/etc/X11/xorg.conf"
48
47
CONSOLE_SETUP_CONF = "/etc/default/console-setup"
49
48
 
50
 
def default_route(nic = None):
 
49
def default_route(nic=None):
51
50
    if platform.system() == 'SunOS':
52
51
        cmd = [ '/usr/bin/netstat', '-rn' ]
53
52
        if nic:
67
66
    for line in d.xreadlines():
68
67
        info = line.split()
69
68
        if (len(info) != 11): # 11 = typical num of fields in the file
70
 
            print >> stderr, _("Invalid line length while parsing %s.") %(route_file)
71
 
            print >> stderr, _("Defaulting bridge to xenbr%d") % (defn)
 
69
            logging.warn(_("Invalid line length while parsing %s.") %
 
70
                         (route_file))
 
71
            logging.warn(_("Defaulting bridge to xenbr%d") % (defn))
72
72
            break
73
73
        try:
74
 
            route = int(info[1],16)
 
74
            route = int(info[1], 16)
75
75
            if route == 0:
76
76
                return info[0]
77
77
        except ValueError:
100
100
def default_connection():
101
101
    if os.path.exists('/var/lib/xend'):
102
102
        if os.path.exists('/dev/xen/evtchn'):
103
 
            return 'xen' 
 
103
            return 'xen'
104
104
        if os.path.exists("/proc/xen"):
105
 
            return 'xen' 
 
105
            return 'xen'
106
106
 
107
107
    if os.path.exists("/usr/bin/qemu") or \
108
108
        os.path.exists("/usr/bin/qemu-kvm") or \
131
131
        return flst
132
132
    return []
133
133
 
134
 
def is_pae_capable(conn = None):
 
134
def is_pae_capable(conn=None):
135
135
    """Determine if a machine is PAE capable or not."""
136
136
    if not conn:
137
137
        conn = libvirt.open('')
178
178
# available under the LGPL,
179
179
# Copyright 2004, 2005 Mike Wray <mike.wray@hp.com>
180
180
# Copyright 2005 XenSource Ltd
181
 
def randomMAC(type = "xen"):
 
181
def randomMAC(type="xen"):
182
182
    """Generate a random MAC address.
183
183
 
184
184
    00-16-3E allocated to xensource
210
210
    mac = oui + [
211
211
            random.randint(0x00, 0xff),
212
212
            random.randint(0x00, 0xff),
213
 
            random.randint(0x00, 0xff) ]
 
213
            random.randint(0x00, 0xff)]
214
214
    return ':'.join(map(lambda x: "%02x" % x, mac))
215
215
 
216
216
# the following three functions are from xend/uuid.py and are thus
270
270
def system(cmd):
271
271
    st = os.system(cmd)
272
272
    if os.WIFEXITED(st) and os.WEXITSTATUS(st) != 0:
273
 
        raise OSError("Failed to run %s, exited with %d" % 
 
273
        raise OSError("Failed to run %s, exited with %d" %
274
274
                      (cmd, os.WEXITSTATUS(st)))
275
275
 
276
276
def xml_escape(str):
281
281
    str = str.replace("<", "&lt;")
282
282
    str = str.replace(">", "&gt;")
283
283
    return str
284
 
 
 
284
 
285
285
def compareMAC(p, q):
286
286
    """Compare two MAC addresses"""
287
287
    pa = p.split(":")
325
325
def _console_setup_keymap():
326
326
    """Look in /etc/default/console-setup for the host machine's keymap, and attempt to
327
327
       map it to a keymap supported by qemu"""
328
 
 
329
 
    kt = None
330
 
    try:
331
 
        f = open(CONSOLE_SETUP_CONF, "r")
332
 
    except IOError, e:
333
 
        logging.debug('Could not open "%s": %s ' % (CONSOLE_SETUP_CONF, str(e)))
334
 
    else:
335
 
        keymap_re = re.compile(r'\s*XKBLAYOUT="(?P<kt>[a-z-]+)"')
336
 
        for line in f:
337
 
            m = keymap_re.match(line)
338
 
            if m:
339
 
                kt = m.group('kt')
340
 
                break
341
 
        else:
342
 
            logging.debug("Didn't find keymap in '%s'!" % XORG_CONF)
343
 
        f.close()
344
 
    return kt
 
328
    return virtinst._util.find_xkblayout(CONSOLE_SETUP_CONF)
345
329
 
346
330
def default_keymap():
347
331
    """Look in /etc/sysconfig for the host machine's keymap, and attempt to
358
342
        logging.debug('Could not open "/etc/sysconfig/keyboard" ' + str(e))
359
343
        kt = _xorg_keymap()
360
344
        if not kt:
361
 
            kt = _console_setup_keymap()
 
345
            kt = virtinst._util.find_keymap_from_etc_default()
362
346
    else:
363
347
        while 1:
364
348
            s = f.readline()
427
411
    username = netloc = query = fragment = ''
428
412
    i = uri.find(":")
429
413
    if i > 0:
430
 
        scheme, uri = uri[:i].lower(), uri[i+1:]
 
414
        scheme, uri = uri[:i].lower(), uri[i + 1:]
431
415
        if uri[:2] == '//':
432
416
            netloc, uri = splitnetloc(uri, 2)
433
417
            offset = netloc.find("@")
434
418
            if offset > 0:
435
419
                username = netloc[0:offset]
436
 
                netloc = netloc[offset+1:]
 
420
                netloc = netloc[offset + 1:]
437
421
        if '#' in uri:
438
422
            uri, fragment = uri.split('#', 1)
439
423
        if '?' in uri:
475
459
        if scheme:
476
460
            offset = scheme.index("+")
477
461
            if offset > 0:
478
 
                return [scheme[offset+1:], username]
 
462
                return [scheme[offset + 1:], username]
479
463
    except:
480
464
        pass
481
465
    return [None, None]
562
546
    import keytable
563
547
    keymap = None
564
548
    # Try a simple lookup in the keytable
565
 
    if keytable.keytable.has_key(kt.lower()):
 
549
    if kt.lower() in keytable.keytable:
566
550
        return keytable.keytable[kt]
567
551
    else:
568
552
        # Try a more intelligent lookup: strip out all '-' and '_', sort
576
560
 
577
561
        for key in sorted_keys:
578
562
            origkey = key
579
 
            key = key.replace("-", "").replace("_","")
 
563
            key = key.replace("-", "").replace("_", "")
580
564
 
581
565
            if clean_kt.startswith(key):
582
566
                return keytable.keytable[origkey]
586
570
def _test():
587
571
    import doctest
588
572
    doctest.testmod()
589
 
 
 
573
 
590
574
if __name__ == "__main__":
591
575
    _test()