xref: /titanic_50/usr/src/cmd/pyzfs/pyzfs.py (revision 6d52f363e3b2c0c5da672c5b8c8adec99d345f38)
1*6d52f363SLori Alt#! /usr/bin/python2.6 -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*6d52f363SLori Alt# Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
2314843421SMatthew Ahrens#
2414843421SMatthew Ahrens
2514843421SMatthew Ahrens# Note, we want SIGINT (control-c) to exit the process quietly, to mimic
2614843421SMatthew Ahrens# the standard behavior of C programs.  The best we can do with pure
2714843421SMatthew Ahrens# Python is to run with -S (to disable "import site"), and start our
2814843421SMatthew Ahrens# program with a "try" statement.  Hopefully nobody hits ^C before our
2914843421SMatthew Ahrens# try statement is executed.
3014843421SMatthew Ahrens
3114843421SMatthew Ahrenstry:
3214843421SMatthew Ahrens	import site
3314843421SMatthew Ahrens	import gettext
3414843421SMatthew Ahrens	import zfs.util
3514843421SMatthew Ahrens	import zfs.ioctl
3614843421SMatthew Ahrens	import sys
3714843421SMatthew Ahrens	import errno
38e4d060fbSSam Falkner	import solaris.misc
3914843421SMatthew Ahrens
4014843421SMatthew Ahrens	"""This is the main script for doing zfs subcommands.  It doesn't know
4114843421SMatthew Ahrens	what subcommands there are, it just looks for a module zfs.<subcommand>
4214843421SMatthew Ahrens	that implements that subcommand."""
4314843421SMatthew Ahrens
44e4d060fbSSam Falkner	try:
4514843421SMatthew Ahrens		_ = gettext.translation("SUNW_OST_OSCMD", "/usr/lib/locale",
4614843421SMatthew Ahrens		    fallback=True).gettext
47e4d060fbSSam Falkner	except:
48e4d060fbSSam Falkner		_ = solaris.misc.gettext
4914843421SMatthew Ahrens
5014843421SMatthew Ahrens	if len(sys.argv) < 2:
5114843421SMatthew Ahrens		sys.exit(_("missing subcommand argument"))
5214843421SMatthew Ahrens
5314843421SMatthew Ahrens	zfs.ioctl.set_cmdstr(" ".join(["zfs"] + sys.argv[1:]))
5414843421SMatthew Ahrens
5514843421SMatthew Ahrens	try:
5614843421SMatthew Ahrens		# import zfs.<subcommand>
5714843421SMatthew Ahrens		# subfunc =  zfs.<subcommand>.do_<subcommand>
5814843421SMatthew Ahrens
5914843421SMatthew Ahrens		subcmd = sys.argv[1]
6014843421SMatthew Ahrens		__import__("zfs." + subcmd)
6114843421SMatthew Ahrens		submod = getattr(zfs, subcmd)
6214843421SMatthew Ahrens		subfunc = getattr(submod, "do_" + subcmd)
6314843421SMatthew Ahrens	except (ImportError, AttributeError):
6414843421SMatthew Ahrens		sys.exit(_("invalid subcommand"))
6514843421SMatthew Ahrens
6614843421SMatthew Ahrens	try:
6714843421SMatthew Ahrens		subfunc()
6814843421SMatthew Ahrens	except zfs.util.ZFSError, e:
6914843421SMatthew Ahrens		print(e)
7014843421SMatthew Ahrens		sys.exit(1)
7114843421SMatthew Ahrens
7214843421SMatthew Ahrensexcept IOError, e:
7314843421SMatthew Ahrens	import errno
7414843421SMatthew Ahrens	import sys
7514843421SMatthew Ahrens
7614843421SMatthew Ahrens	if e.errno == errno.EPIPE:
7714843421SMatthew Ahrens		sys.exit(1)
7814843421SMatthew Ahrens	raise
7914843421SMatthew Ahrensexcept KeyboardInterrupt:
8014843421SMatthew Ahrens	import sys
8114843421SMatthew Ahrens
8214843421SMatthew Ahrens	sys.exit(1)
83