xref: /titanic_44/usr/src/cmd/pyzfs/pyzfs.py (revision 148434217c040ea38dc844384f6ba68d9b325906)
1*14843421SMatthew Ahrens#! /usr/bin/python2.4 -S
2*14843421SMatthew Ahrens#
3*14843421SMatthew Ahrens# CDDL HEADER START
4*14843421SMatthew Ahrens#
5*14843421SMatthew Ahrens# The contents of this file are subject to the terms of the
6*14843421SMatthew Ahrens# Common Development and Distribution License (the "License").
7*14843421SMatthew Ahrens# You may not use this file except in compliance with the License.
8*14843421SMatthew Ahrens#
9*14843421SMatthew Ahrens# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*14843421SMatthew Ahrens# or http://www.opensolaris.org/os/licensing.
11*14843421SMatthew Ahrens# See the License for the specific language governing permissions
12*14843421SMatthew Ahrens# and limitations under the License.
13*14843421SMatthew Ahrens#
14*14843421SMatthew Ahrens# When distributing Covered Code, include this CDDL HEADER in each
15*14843421SMatthew Ahrens# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*14843421SMatthew Ahrens# If applicable, add the following below this CDDL HEADER, with the
17*14843421SMatthew Ahrens# fields enclosed by brackets "[]" replaced with your own identifying
18*14843421SMatthew Ahrens# information: Portions Copyright [yyyy] [name of copyright owner]
19*14843421SMatthew Ahrens#
20*14843421SMatthew Ahrens# CDDL HEADER END
21*14843421SMatthew Ahrens#
22*14843421SMatthew Ahrens# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23*14843421SMatthew Ahrens# Use is subject to license terms.
24*14843421SMatthew Ahrens#
25*14843421SMatthew Ahrens
26*14843421SMatthew Ahrens# Note, we want SIGINT (control-c) to exit the process quietly, to mimic
27*14843421SMatthew Ahrens# the standard behavior of C programs.  The best we can do with pure
28*14843421SMatthew Ahrens# Python is to run with -S (to disable "import site"), and start our
29*14843421SMatthew Ahrens# program with a "try" statement.  Hopefully nobody hits ^C before our
30*14843421SMatthew Ahrens# try statement is executed.
31*14843421SMatthew Ahrens
32*14843421SMatthew Ahrenstry:
33*14843421SMatthew Ahrens	import site
34*14843421SMatthew Ahrens	import gettext
35*14843421SMatthew Ahrens	import zfs.util
36*14843421SMatthew Ahrens	import zfs.ioctl
37*14843421SMatthew Ahrens	import sys
38*14843421SMatthew Ahrens	import errno
39*14843421SMatthew Ahrens
40*14843421SMatthew Ahrens	"""This is the main script for doing zfs subcommands.  It doesn't know
41*14843421SMatthew Ahrens	what subcommands there are, it just looks for a module zfs.<subcommand>
42*14843421SMatthew Ahrens	that implements that subcommand."""
43*14843421SMatthew Ahrens
44*14843421SMatthew Ahrens	_ = gettext.translation("SUNW_OST_OSCMD", "/usr/lib/locale",
45*14843421SMatthew Ahrens	    fallback=True).gettext
46*14843421SMatthew Ahrens
47*14843421SMatthew Ahrens	if len(sys.argv) < 2:
48*14843421SMatthew Ahrens		sys.exit(_("missing subcommand argument"))
49*14843421SMatthew Ahrens
50*14843421SMatthew Ahrens	zfs.ioctl.set_cmdstr(" ".join(["zfs"] + sys.argv[1:]))
51*14843421SMatthew Ahrens
52*14843421SMatthew Ahrens	try:
53*14843421SMatthew Ahrens		# import zfs.<subcommand>
54*14843421SMatthew Ahrens		# subfunc =  zfs.<subcommand>.do_<subcommand>
55*14843421SMatthew Ahrens
56*14843421SMatthew Ahrens		subcmd = sys.argv[1]
57*14843421SMatthew Ahrens		__import__("zfs." + subcmd)
58*14843421SMatthew Ahrens		submod = getattr(zfs, subcmd)
59*14843421SMatthew Ahrens		subfunc = getattr(submod, "do_" + subcmd)
60*14843421SMatthew Ahrens	except (ImportError, AttributeError):
61*14843421SMatthew Ahrens		sys.exit(_("invalid subcommand"))
62*14843421SMatthew Ahrens
63*14843421SMatthew Ahrens	try:
64*14843421SMatthew Ahrens		subfunc()
65*14843421SMatthew Ahrens	except zfs.util.ZFSError, e:
66*14843421SMatthew Ahrens		print(e)
67*14843421SMatthew Ahrens		sys.exit(1)
68*14843421SMatthew Ahrens
69*14843421SMatthew Ahrensexcept IOError, e:
70*14843421SMatthew Ahrens	import errno
71*14843421SMatthew Ahrens	import sys
72*14843421SMatthew Ahrens
73*14843421SMatthew Ahrens	if e.errno == errno.EPIPE:
74*14843421SMatthew Ahrens		sys.exit(1)
75*14843421SMatthew Ahrens	raise
76*14843421SMatthew Ahrensexcept KeyboardInterrupt:
77*14843421SMatthew Ahrens	import sys
78*14843421SMatthew Ahrens
79*14843421SMatthew Ahrens	sys.exit(1)
80