Add utility function for stat() of disks
# HG changeset patch
# User john.levon@sun.com # Date 1228851945 28800 # Node ID 50ca2624e7ef5713153ef85c012326fdae125b7f # Parent 29d8886362e2993eaf26cf8d4e948b2de4b8d9ec Add utility function for stat() of disks Signed-off-by: John Levon <john.levon@sun.com> diff --git a/virtinst/CloneManager.py b/virtinst/CloneManager.py --- a/virtinst/CloneManager.py +++ b/virtinst/CloneManager.py @@ -19,7 +19,6 @@ # MA 02110-1301 USA. import os -import stat import libxml2 import logging import urlgrabber.progress as progress @@ -394,13 +393,9 @@ class CloneDesign(object): logging.debug("original device list: %s" % (lst)) for i in lst: - mode = os.stat(i)[stat.ST_MODE] - if stat.S_ISBLK(mode): - size.append(_util.blkdev_size(i)) - typ.append(False) - elif stat.S_ISREG(mode): - size.append(os.path.getsize(i)) - typ.append(True) + (t, sz) = _util.stat_disk(i) + typ.append(t) + size.append(sz) logging.debug("original device size: %s" % (size)) logging.debug("original device type: %s" % (typ)) @@ -443,18 +438,9 @@ class CloneDesign(object): typ = [] for i in cln_dev_lst: - if os.path.exists(i) == False: - size.append(0) - # if not exists, create file necessary - typ.append(True) - continue - mode = os.stat(i)[stat.ST_MODE] - if stat.S_ISBLK(mode): - size.append(_util.blkdev_size(i)) - typ.append(False) - elif stat.S_ISREG(mode): - size.append(os.path.getsize(i)) - typ.append(True) + (t, sz) = _util.stat_disk(i) + typ.append(t) + size.append(sz) logging.debug("clone device list: %s" % (cln_dev_lst)) logging.debug("clone device size: %s" % (size)) diff --git a/virtinst/_util.py b/virtinst/_util.py --- a/virtinst/_util.py +++ b/virtinst/_util.py @@ -22,6 +22,7 @@ import platform import platform import random import os.path +import stat import re import libxml2 import logging @@ -280,14 +281,24 @@ def xml_escape(str): str = str.replace(">", ">") return str -def blkdev_size(path): - """Return the size of the block device. We can't use os.stat() as - that returns zero on many platforms.""" - fd = os.open(path, os.O_RDONLY) - # os.SEEK_END is not present on all systems - size = os.lseek(fd, 0, 2) - os.close(fd) - return size +def stat_disk(path): + """Returns the tuple (isreg, size).""" + if not os.path.exists(path): + return True, 0 + + mode = os.stat(path)[stat.ST_MODE] + + # os.path.getsize('/dev/..') can be zero on some platforms + if stat.S_ISBLK(mode): + fd = os.open(path, os.O_RDONLY) + # os.SEEK_END is not present on all systems + size = os.lseek(fd, 0, 2) + os.close(fd) + return False, size + elif stat.S_ISREG(mode): + return True, os.path.getsize(path) + + return True, 0 def compareMAC(p, q): """Compare two MAC addresses""" _______________________________________________ et-mgmt-tools mailing list et-mgmt-tools@redhat.com https://www.redhat.com/mailman/listinfo/et-mgmt-tools |
Add utility function for stat() of disks
john.levon@sun.com wrote:
> # HG changeset patch > # User john.levon@sun.com > # Date 1228851945 28800 > # Node ID 50ca2624e7ef5713153ef85c012326fdae125b7f > # Parent 29d8886362e2993eaf26cf8d4e948b2de4b8d9ec > Add utility function for stat() of disks > ACK to this, I'll commit it once the util stuff is sorted out. Thanks, Cole _______________________________________________ et-mgmt-tools mailing list et-mgmt-tools@redhat.com https://www.redhat.com/mailman/listinfo/et-mgmt-tools |
Add utility function for stat() of disks
# HG changeset patch
# User john.levon@sun.com # Date 1228937605 28800 # Node ID 8638073ea45665c7531931515636a862f684c2a5 # Parent e9045f3ae5b2243a9ee00f0913c2dcefdddd7c36 Add utility function for stat() of disks Signed-off-by: John Levon <john.levon@sun.com> diff --git a/virtinst/CloneManager.py b/virtinst/CloneManager.py --- a/virtinst/CloneManager.py +++ b/virtinst/CloneManager.py @@ -19,7 +19,6 @@ # MA 02110-1301 USA. import os -import stat import libxml2 import logging import urlgrabber.progress as progress @@ -394,13 +393,9 @@ class CloneDesign(object): logging.debug("original device list: %s" % (lst)) for i in lst: - mode = os.stat(i)[stat.ST_MODE] - if stat.S_ISBLK(mode): - size.append(_util.blkdev_size(i)) - typ.append(False) - elif stat.S_ISREG(mode): - size.append(os.path.getsize(i)) - typ.append(True) + (t, sz) = _util.stat_disk(i) + typ.append(t) + size.append(sz) logging.debug("original device size: %s" % (size)) logging.debug("original device type: %s" % (typ)) @@ -443,18 +438,9 @@ class CloneDesign(object): typ = [] for i in cln_dev_lst: - if os.path.exists(i) == False: - size.append(0) - # if not exists, create file necessary - typ.append(True) - continue - mode = os.stat(i)[stat.ST_MODE] - if stat.S_ISBLK(mode): - size.append(_util.blkdev_size(i)) - typ.append(False) - elif stat.S_ISREG(mode): - size.append(os.path.getsize(i)) - typ.append(True) + (t, sz) = _util.stat_disk(i) + typ.append(t) + size.append(sz) logging.debug("clone device list: %s" % (cln_dev_lst)) logging.debug("clone device size: %s" % (size)) diff --git a/virtinst/_util.py b/virtinst/_util.py --- a/virtinst/_util.py +++ b/virtinst/_util.py @@ -23,10 +23,30 @@ # not be used by clients. # +import stat import os from virtinst import util +def stat_disk(path): + """Returns the tuple (isreg, size).""" + if not os.path.exists(path): + return True, 0 + + mode = os.stat(path)[stat.ST_MODE] + + # os.path.getsize('/dev/..') can be zero on some platforms + if stat.S_ISBLK(mode): + fd = os.open(path, os.O_RDONLY) + # os.SEEK_END is not present on all systems + size = os.lseek(fd, 0, 2) + os.close(fd) + return False, size + elif stat.S_ISREG(mode): + return True, os.path.getsize(path) + + return True, 0 + def blkdev_size(path): """Return the size of the block device. We can't use os.stat() as that returns zero on many platforms.""" _______________________________________________ et-mgmt-tools mailing list et-mgmt-tools@redhat.com https://www.redhat.com/mailman/listinfo/et-mgmt-tools |
Add utility function for stat() of disks
john.levon@sun.com wrote:
> # HG changeset patch > # User john.levon@sun.com > # Date 1228937605 28800 > # Node ID 8638073ea45665c7531931515636a862f684c2a5 > # Parent e9045f3ae5b2243a9ee00f0913c2dcefdddd7c36 > Add utility function for stat() of disks > > Signed-off-by: John Levon <john.levon@sun.com> > Applied now. Thanks, Cole _______________________________________________ et-mgmt-tools mailing list et-mgmt-tools@redhat.com https://www.redhat.com/mailman/listinfo/et-mgmt-tools |
| All times are GMT. The time now is 04:26 AM. |
VBulletin, Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.