~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): Marc Deslauriers
  • Date: 2011-02-01 15:40:11 UTC
  • mfrom: (1.3.16 experimental)
  • Revision ID: james.westby@ubuntu.com-20110201154011-op0nusgc240xajvb
Tags: 0.500.5-1ubuntu1
* Merge from debian experimental. Remaining changes:
  - debian/patches/9001_Ubuntu.patch:
     + Updated to add maverick and natty to OS list and enable virtio
       for them.
  - debian/patches/9003-fix-path-to-hvmloader-in-testsuite.patch: adjust
    testsuite for 0001-fix-path-to-hvmloader.patch and
    0002-Fix-path-to-pygrub.patch. (refreshed)
  - debian/control: added acl package to depends.
  - Demote virt-viewer to Suggests, as it's in universe.
  - Recommends libvirt-bin
* Removed patches:
  - debian/patches/9002-libvirt_disk_format.patch: Upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
except ImportError:
44
44
    selinux = None
45
45
 
 
46
def listify(l):
 
47
    if l is None:
 
48
        return []
 
49
    elif type(l) != list:
 
50
        return [ l ]
 
51
    else:
 
52
        return l
 
53
 
46
54
def is_vdisk(path):
47
55
    if not os.path.exists("/usr/sbin/vdiskadm"):
48
56
        return False
125
133
 
126
134
def validate_uuid(val):
127
135
    if type(val) is not str:
128
 
        raise ValueError, _("UUID must be a string.")
 
136
        raise ValueError(_("UUID must be a string."))
129
137
 
130
138
    form = re.match("[a-fA-F0-9]{8}[-]([a-fA-F0-9]{4}[-]){3}[a-fA-F0-9]{12}$",
131
139
                    val)
132
140
    if form is None:
133
141
        form = re.match("[a-fA-F0-9]{32}$", val)
134
142
        if form is None:
135
 
            raise ValueError, \
 
143
            raise ValueError(
136
144
                  _("UUID must be a 32-digit hexadecimal number. It may take "
137
145
                    "the form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX or may omit "
138
 
                    "hyphens altogether.")
 
146
                    "hyphens altogether."))
139
147
 
140
148
        else:   # UUID had no dashes, so add them in
141
149
            val = (val[0:8] + "-" + val[8:12] + "-" + val[12:16] +
144
152
 
145
153
def validate_name(name_type, val):
146
154
    if type(val) is not type("string") or len(val) > 50 or len(val) == 0:
147
 
        raise ValueError, _("%s name must be a string between 0 and 50 "
148
 
                            "characters") % name_type
 
155
        raise ValueError(_("%s name must be a string between 0 and 50 "
 
156
                           "characters") % name_type)
149
157
    if re.match("^[0-9]+$", val):
150
 
        raise ValueError, _("%s name can not be only numeric characters") % \
151
 
                          name_type
 
158
        raise ValueError(_("%s name can not be only numeric characters") %
 
159
                          name_type)
152
160
    if re.match("^[a-zA-Z0-9._-]+$", val) == None:
153
 
        raise ValueError, _("%s name can only contain alphanumeric, '_', '.', "
154
 
                            "or '-' characters") % name_type
 
161
        raise ValueError(_("%s name can only contain alphanumeric, '_', '.', "
 
162
                           "or '-' characters") % name_type)
155
163
 
156
164
def validate_macaddr(val):
157
165
    if val is None:
158
166
        return
159
167
 
160
168
    if type(val) is not str:
161
 
        raise ValueError, _("MAC address must be a string.")
 
169
        raise ValueError(_("MAC address must be a string."))
162
170
 
163
171
    form = re.match("^([0-9a-fA-F]{1,2}:){5}[0-9a-fA-F]{1,2}$", val)
164
172
    if form is None:
361
369
 
362
370
    return dev
363
371
 
364
 
def default_bridge2(conn = None):
 
372
def default_bridge2(conn=None):
365
373
    if platform.system() == 'SunOS':
366
374
        return ["bridge", default_nic()]
367
375
 
397
405
        return True
398
406
    return False
399
407
 
 
408
def is_qemu(conn):
 
409
    if not conn:
 
410
        return False
 
411
 
 
412
    if type(conn) is str:
 
413
        uri = conn
 
414
    else:
 
415
        uri = conn.getURI()
 
416
 
 
417
    scheme = uri_split(uri)[0]
 
418
    return scheme.startswith("qemu")
 
419
 
400
420
def parse_node_helper(xml, root_name, callback, exec_class=ValueError):
401
421
    """
402
422
    Parse the passed XML, expecting root as root_name, and pass the
432
452
 
433
453
    return ret
434
454
 
 
455
def find_xkblayout(path):
 
456
    """
 
457
    Reads a keyboard layout from a file that defines an XKBLAYOUT
 
458
    variable, e.g. /etc/default/{keyboard,console-setup}.
 
459
    The format of these files is such that they can be 'sourced'
 
460
    in a shell script.
 
461
    """
 
462
 
 
463
    kt = None
 
464
    try:
 
465
        f = open(path, "r")
 
466
    except IOError, e:
 
467
        logging.debug('Could not open "%s": %s ' % (path, str(e)))
 
468
    else:
 
469
        keymap_re = re.compile(r'\s*XKBLAYOUT="(?P<kt>[a-z-]+)"')
 
470
        for line in f:
 
471
            m = keymap_re.match(line)
 
472
            if m:
 
473
                kt = m.group('kt')
 
474
                break
 
475
        else:
 
476
            logging.debug("Didn't find keymap in '%s'!" % path)
 
477
        f.close()
 
478
    return kt
 
479
 
 
480
def find_keymap_from_etc_default():
 
481
    """
 
482
    Look under /etc/default for the host machine's keymap.
 
483
 
 
484
    This checks both /etc/default/keyboard and /etc/default/console-setup.
 
485
    The former is used by Debian 6.0 (Squeeze) and later.  The latter is
 
486
    used by older versions of Debian, and Ubuntu.
 
487
    """
 
488
 
 
489
    KEYBOARD_DEFAULT = "/etc/default/keyboard"
 
490
    paths = [ KEYBOARD_DEFAULT, util.CONSOLE_SETUP_CONF ]
 
491
    for path in paths:
 
492
        kt = find_xkblayout(path)
 
493
        if kt != None:
 
494
            break
 
495
    return kt
 
496
 
435
497
#
436
498
# These functions accidentally ended up in the API under virtinst.util
437
499
#