xref: /titanic_50/usr/src/cmd/pyzfs/pyzfs.py (revision e4d060fb4c00d44cd578713eb9a921f594b733b8)
114843421SMatthew Ahrens#! /usr/bin/python2.4 -S
214843421SMatthew Ahrens#
314843421SMatthew Ahrens# CDDL HEADER START
414843421SMatthew Ahrens#
514843421SMatthew Ahrens# The contents of this file are subject to the terms of the
614843421SMatthew Ahrens# Common Development and Distribution License (the "License").
714843421SMatthew Ahrens# You may not use this file except in compliance with the License.
814843421SMatthew Ahrens#
914843421SMatthew Ahrens# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
1014843421SMatthew Ahrens# or http://www.opensolaris.org/os/licensing.
1114843421SMatthew Ahrens# See the License for the specific language governing permissions
1214843421SMatthew Ahrens# and limitations under the License.
1314843421SMatthew Ahrens#
1414843421SMatthew Ahrens# When distributing Covered Code, include this CDDL HEADER in each
1514843421SMatthew Ahrens# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1614843421SMatthew Ahrens# If applicable, add the following below this CDDL HEADER, with the
1714843421SMatthew Ahrens# fields enclosed by brackets "[]" replaced with your own identifying
1814843421SMatthew Ahrens# information: Portions Copyright [yyyy] [name of copyright owner]
1914843421SMatthew Ahrens#
2014843421SMatthew Ahrens# CDDL HEADER END
2114843421SMatthew Ahrens#
22*e4d060fbSSam Falkner# Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
2314843421SMatthew Ahrens# Use is subject to license terms.
2414843421SMatthew Ahrens#
2514843421SMatthew Ahrens
2614843421SMatthew Ahrens# Note, we want SIGINT (control-c) to exit the process quietly, to mimic
2714843421SMatthew Ahrens# the standard behavior of C programs.  The best we can do with pure
2814843421SMatthew Ahrens# Python is to run with -S (to disable "import site"), and start our
2914843421SMatthew Ahrens# program with a "try" statement.  Hopefully nobody hits ^C before our
3014843421SMatthew Ahrens# try statement is executed.
3114843421SMatthew Ahrens
3214843421SMatthew Ahrenstry:
3314843421SMatthew Ahrens	import site
3414843421SMatthew Ahrens	import gettext
3514843421SMatthew Ahrens	import zfs.util
3614843421SMatthew Ahrens	import zfs.ioctl
3714843421SMatthew Ahrens	import sys
3814843421SMatthew Ahrens	import errno
39*e4d060fbSSam Falkner	import solaris.misc
4014843421SMatthew Ahrens
4114843421SMatthew Ahrens	"""This is the main script for doing zfs subcommands.  It doesn't know
4214843421SMatthew Ahrens	what subcommands there are, it just looks for a module zfs.<subcommand>
4314843421SMatthew Ahrens	that implements that subcommand."""
4414843421SMatthew Ahrens
45*e4d060fbSSam Falkner	try:
4614843421SMatthew Ahrens		_ = gettext.translation("SUNW_OST_OSCMD", "/usr/lib/locale",
4714843421SMatthew Ahrens		    fallback=True).gettext
48*e4d060fbSSam Falkner	except:
49*e4d060fbSSam Falkner		_ = solaris.misc.gettext
5014843421SMatthew Ahrens
5114843421SMatthew Ahrens	if len(sys.argv) < 2:
5214843421SMatthew Ahrens		sys.exit(_("missing subcommand argument"))
5314843421SMatthew Ahrens
5414843421SMatthew Ahrens	zfs.ioctl.set_cmdstr(" ".join(["zfs"] + sys.argv[1:]))
5514843421SMatthew Ahrens
5614843421SMatthew Ahrens	try:
5714843421SMatthew Ahrens		# import zfs.<subcommand>
5814843421SMatthew Ahrens		# subfunc =  zfs.<subcommand>.do_<subcommand>
5914843421SMatthew Ahrens
6014843421SMatthew Ahrens		subcmd = sys.argv[1]
6114843421SMatthew Ahrens		__import__("zfs." + subcmd)
6214843421SMatthew Ahrens		submod = getattr(zfs, subcmd)
6314843421SMatthew Ahrens		subfunc = getattr(submod, "do_" + subcmd)
6414843421SMatthew Ahrens	except (ImportError, AttributeError):
6514843421SMatthew Ahrens		sys.exit(_("invalid subcommand"))
6614843421SMatthew Ahrens
6714843421SMatthew Ahrens	try:
6814843421SMatthew Ahrens		subfunc()
6914843421SMatthew Ahrens	except zfs.util.ZFSError, e:
7014843421SMatthew Ahrens		print(e)
7114843421SMatthew Ahrens		sys.exit(1)
7214843421SMatthew Ahrens
7314843421SMatthew Ahrensexcept IOError, e:
7414843421SMatthew Ahrens	import errno
7514843421SMatthew Ahrens	import sys
7614843421SMatthew Ahrens
7714843421SMatthew Ahrens	if e.errno == errno.EPIPE:
7814843421SMatthew Ahrens		sys.exit(1)
7914843421SMatthew Ahrens	raise
8014843421SMatthew Ahrensexcept KeyboardInterrupt:
8114843421SMatthew Ahrens	import sys
8214843421SMatthew Ahrens
8314843421SMatthew Ahrens	sys.exit(1)
84