util.py revision 2
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
"""This module provides utility functions for ZFS.
import gettext
import errno
import os
import sys
# Note: this module (zfs.util) should not import zfs.ioctl, because that
# would introduce a circular dependency
try:
except:
def default_repr(self):
"""A simple __repr__ function."""
return str + ">"
else:
return "<%s %s>" % \
class ZFSError(StandardError):
"""This exception class represents a potentially user-visible
ZFS error. If uncaught, it will be printed and the process will
exit with exit code 1.
errno -- the error number (eg, from ioctl(2))."""
"""Create a ZFS exception.
eno -- the error number (errno)
task -- a string describing the task that failed
why -- a string describing why it failed (defaults to
strerror(eno))"""
s = ""
else:
return s
__strs = {
_("delegated administration is disabled on pool"),
_("snapshot permissions cannot be modified"),
}
return ""
"""Return a nice string (eg "1.23M") for this integer."""
index = 0;
n = num;
while n >= 1024:
n /= 1024
index += 1
u = " KMGTPE"[index]
if index == 0:
return "%u" % n;
# it's an exact multiple of its index, or it wouldn't
# fit as floating point, so print as an integer
return "%u%c" % (n, u)
else:
# due to rounding, it's tricky to tell what precision to
# use; try each precision and see which one fits
for i in (2, 1, 0):
if len(s) <= 5:
return s
"""A function for OptionParser which appends a tuple (opt, value)."""
import optparse
"""This option parser class raises errors as exception strings
rather than exiting immediately."""
if msg:
if msg: