xref: /onnv-gate/usr/src/cmd/pyzfs/pyzfs.py (revision 12961:f0448f1d899f)
1*12961Slori.alt@oracle.com#! /usr/bin/python2.6 -S
29396SMatthew.Ahrens@Sun.COM#
39396SMatthew.Ahrens@Sun.COM# CDDL HEADER START
49396SMatthew.Ahrens@Sun.COM#
59396SMatthew.Ahrens@Sun.COM# The contents of this file are subject to the terms of the
69396SMatthew.Ahrens@Sun.COM# Common Development and Distribution License (the "License").
79396SMatthew.Ahrens@Sun.COM# You may not use this file except in compliance with the License.
89396SMatthew.Ahrens@Sun.COM#
99396SMatthew.Ahrens@Sun.COM# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
109396SMatthew.Ahrens@Sun.COM# or http://www.opensolaris.org/os/licensing.
119396SMatthew.Ahrens@Sun.COM# See the License for the specific language governing permissions
129396SMatthew.Ahrens@Sun.COM# and limitations under the License.
139396SMatthew.Ahrens@Sun.COM#
149396SMatthew.Ahrens@Sun.COM# When distributing Covered Code, include this CDDL HEADER in each
159396SMatthew.Ahrens@Sun.COM# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
169396SMatthew.Ahrens@Sun.COM# If applicable, add the following below this CDDL HEADER, with the
179396SMatthew.Ahrens@Sun.COM# fields enclosed by brackets "[]" replaced with your own identifying
189396SMatthew.Ahrens@Sun.COM# information: Portions Copyright [yyyy] [name of copyright owner]
199396SMatthew.Ahrens@Sun.COM#
209396SMatthew.Ahrens@Sun.COM# CDDL HEADER END
219396SMatthew.Ahrens@Sun.COM#
22*12961Slori.alt@oracle.com# Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
239396SMatthew.Ahrens@Sun.COM#
249396SMatthew.Ahrens@Sun.COM
259396SMatthew.Ahrens@Sun.COM# Note, we want SIGINT (control-c) to exit the process quietly, to mimic
269396SMatthew.Ahrens@Sun.COM# the standard behavior of C programs.  The best we can do with pure
279396SMatthew.Ahrens@Sun.COM# Python is to run with -S (to disable "import site"), and start our
289396SMatthew.Ahrens@Sun.COM# program with a "try" statement.  Hopefully nobody hits ^C before our
299396SMatthew.Ahrens@Sun.COM# try statement is executed.
309396SMatthew.Ahrens@Sun.COM
319396SMatthew.Ahrens@Sun.COMtry:
329396SMatthew.Ahrens@Sun.COM	import site
339396SMatthew.Ahrens@Sun.COM	import gettext
349396SMatthew.Ahrens@Sun.COM	import zfs.util
359396SMatthew.Ahrens@Sun.COM	import zfs.ioctl
369396SMatthew.Ahrens@Sun.COM	import sys
379396SMatthew.Ahrens@Sun.COM	import errno
3811821SSam.Falkner@Sun.COM	import solaris.misc
399396SMatthew.Ahrens@Sun.COM
409396SMatthew.Ahrens@Sun.COM	"""This is the main script for doing zfs subcommands.  It doesn't know
419396SMatthew.Ahrens@Sun.COM	what subcommands there are, it just looks for a module zfs.<subcommand>
429396SMatthew.Ahrens@Sun.COM	that implements that subcommand."""
439396SMatthew.Ahrens@Sun.COM
4411821SSam.Falkner@Sun.COM	try:
4511821SSam.Falkner@Sun.COM		_ = gettext.translation("SUNW_OST_OSCMD", "/usr/lib/locale",
4611821SSam.Falkner@Sun.COM		    fallback=True).gettext
4711821SSam.Falkner@Sun.COM	except:
4811821SSam.Falkner@Sun.COM		_ = solaris.misc.gettext
499396SMatthew.Ahrens@Sun.COM
509396SMatthew.Ahrens@Sun.COM	if len(sys.argv) < 2:
519396SMatthew.Ahrens@Sun.COM		sys.exit(_("missing subcommand argument"))
529396SMatthew.Ahrens@Sun.COM
539396SMatthew.Ahrens@Sun.COM	zfs.ioctl.set_cmdstr(" ".join(["zfs"] + sys.argv[1:]))
549396SMatthew.Ahrens@Sun.COM
559396SMatthew.Ahrens@Sun.COM	try:
569396SMatthew.Ahrens@Sun.COM		# import zfs.<subcommand>
579396SMatthew.Ahrens@Sun.COM		# subfunc =  zfs.<subcommand>.do_<subcommand>
589396SMatthew.Ahrens@Sun.COM
599396SMatthew.Ahrens@Sun.COM		subcmd = sys.argv[1]
609396SMatthew.Ahrens@Sun.COM		__import__("zfs." + subcmd)
619396SMatthew.Ahrens@Sun.COM		submod = getattr(zfs, subcmd)
629396SMatthew.Ahrens@Sun.COM		subfunc = getattr(submod, "do_" + subcmd)
639396SMatthew.Ahrens@Sun.COM	except (ImportError, AttributeError):
649396SMatthew.Ahrens@Sun.COM		sys.exit(_("invalid subcommand"))
659396SMatthew.Ahrens@Sun.COM
669396SMatthew.Ahrens@Sun.COM	try:
679396SMatthew.Ahrens@Sun.COM		subfunc()
689396SMatthew.Ahrens@Sun.COM	except zfs.util.ZFSError, e:
699396SMatthew.Ahrens@Sun.COM		print(e)
709396SMatthew.Ahrens@Sun.COM		sys.exit(1)
719396SMatthew.Ahrens@Sun.COM
729396SMatthew.Ahrens@Sun.COMexcept IOError, e:
739396SMatthew.Ahrens@Sun.COM	import errno
749396SMatthew.Ahrens@Sun.COM	import sys
759396SMatthew.Ahrens@Sun.COM
769396SMatthew.Ahrens@Sun.COM	if e.errno == errno.EPIPE:
779396SMatthew.Ahrens@Sun.COM		sys.exit(1)
789396SMatthew.Ahrens@Sun.COM	raise
799396SMatthew.Ahrens@Sun.COMexcept KeyboardInterrupt:
809396SMatthew.Ahrens@Sun.COM	import sys
819396SMatthew.Ahrens@Sun.COM
829396SMatthew.Ahrens@Sun.COM	sys.exit(1)
83