Skip to content
This repository was archived by the owner on Jan 22, 2026. It is now read-only.

Commit f557878

Browse files
committed
Factor out common UUID validation code.
1 parent 377b25c commit f557878

3 files changed

Lines changed: 22 additions & 25 deletions

File tree

virtinst/Guest.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -606,19 +606,7 @@ def set_maxmemory(self, val):
606606
def get_uuid(self):
607607
return self._uuid
608608
def set_uuid(self, val):
609-
# need better validation
610-
if type(val) is not type("string"):
611-
raise ValueError, _("UUID must be a string.")
612-
613-
form = re.match("[a-fA-F0-9]{8}[-]([a-fA-F0-9]{4}[-]){3}[a-fA-F0-9]{12}$", val)
614-
if form is None:
615-
form = re.match("[a-fA-F0-9]{32}$", val)
616-
if form is None:
617-
raise ValueError, _("UUID must be a 32-digit hexadecimal number. It may take the form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX or may omit hyphens altogether.")
618-
619-
else: # UUID had no dashes, so add them in
620-
val=val[0:8] + "-" + val[8:12] + "-" + val[12:16] + \
621-
"-" + val[16:20] + "-" + val[20:32]
609+
val = _util.validate_uuid(val)
622610
self._uuid = val
623611
uuid = property(get_uuid, set_uuid)
624612

virtinst/Storage.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -305,18 +305,7 @@ def set_host(self, val):
305305
def get_uuid(self):
306306
return self._uuid
307307
def set_uuid(self, val):
308-
if type(val) is not type("string"):
309-
raise ValueError, _("UUID must be a string.")
310-
311-
form = re.match("[a-fA-F0-9]{8}[-]([a-fA-F0-9]{4}[-]){3}[a-fA-F0-9]{12}$", val)
312-
if form is None:
313-
form = re.match("[a-fA-F0-9]{32}$", val)
314-
if form is None:
315-
raise ValueError, _("UUID must be a 32-digit hexadecimal number. It may take the form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX or may omit hyphens altogether.")
316-
317-
else: # UUID had no dashes, so add them in
318-
val=val[0:8] + "-" + val[8:12] + "-" + val[12:16] + \
319-
"-" + val[16:20] + "-" + val[20:32]
308+
val = _util.validate_uuid(val)
320309
self._uuid = val
321310
uuid = property(get_uuid, set_uuid)
322311

virtinst/_util.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import libvirt
3232

3333
from virtinst import util
34+
from virtinst import _virtinst as _
3435

3536
def is_vdisk(path):
3637
if not os.path.exists("/usr/sbin/vdiskadm"):
@@ -101,6 +102,25 @@ def vm_uuid_collision(conn, uuid):
101102
pass
102103
return check
103104

105+
def validate_uuid(val):
106+
if type(val) is not str:
107+
raise ValueError, _("UUID must be a string.")
108+
109+
form = re.match("[a-fA-F0-9]{8}[-]([a-fA-F0-9]{4}[-]){3}[a-fA-F0-9]{12}$",
110+
val)
111+
if form is None:
112+
form = re.match("[a-fA-F0-9]{32}$", val)
113+
if form is None:
114+
raise ValueError, \
115+
_("UUID must be a 32-digit hexadecimal number. It may take "
116+
"the form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX or may omit "
117+
"hyphens altogether.")
118+
119+
else: # UUID had no dashes, so add them in
120+
val = (val[0:8] + "-" + val[8:12] + "-" + val[12:16] +
121+
"-" + val[16:20] + "-" + val[20:32])
122+
return val
123+
104124
#
105125
# These functions accidentally ended up in the API under virtinst.util
106126
#

0 commit comments

Comments
 (0)