1209962Smm#! /usr/bin/python2.4 -S
2209962Smm#
3209962Smm# CDDL HEADER START
4209962Smm#
5209962Smm# The contents of this file are subject to the terms of the
6209962Smm# Common Development and Distribution License (the "License").
7209962Smm# You may not use this file except in compliance with the License.
8209962Smm#
9209962Smm# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10209962Smm# or http://www.opensolaris.org/os/licensing.
11209962Smm# See the License for the specific language governing permissions
12209962Smm# and limitations under the License.
13209962Smm#
14209962Smm# When distributing Covered Code, include this CDDL HEADER in each
15209962Smm# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16209962Smm# If applicable, add the following below this CDDL HEADER, with the
17209962Smm# fields enclosed by brackets "[]" replaced with your own identifying
18209962Smm# information: Portions Copyright [yyyy] [name of copyright owner]
19209962Smm#
20209962Smm# CDDL HEADER END
21209962Smm#
22209962Smm# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23209962Smm# Use is subject to license terms.
24209962Smm#
25209962Smm
26209962Smm# Note, we want SIGINT (control-c) to exit the process quietly, to mimic
27209962Smm# the standard behavior of C programs.  The best we can do with pure
28209962Smm# Python is to run with -S (to disable "import site"), and start our
29209962Smm# program with a "try" statement.  Hopefully nobody hits ^C before our
30209962Smm# try statement is executed.
31209962Smm
32209962Smmtry:
33209962Smm	import site
34209962Smm	import gettext
35209962Smm	import zfs.util
36209962Smm	import zfs.ioctl
37209962Smm	import sys
38209962Smm	import errno
39209962Smm
40209962Smm	"""This is the main script for doing zfs subcommands.  It doesn't know
41209962Smm	what subcommands there are, it just looks for a module zfs.<subcommand>
42209962Smm	that implements that subcommand."""
43209962Smm
44209962Smm	_ = gettext.translation("SUNW_OST_OSCMD", "/usr/lib/locale",
45209962Smm	    fallback=True).gettext
46209962Smm
47209962Smm	if len(sys.argv) < 2:
48209962Smm		sys.exit(_("missing subcommand argument"))
49209962Smm
50209962Smm	zfs.ioctl.set_cmdstr(" ".join(["zfs"] + sys.argv[1:]))
51209962Smm
52209962Smm	try:
53209962Smm		# import zfs.<subcommand>
54209962Smm		# subfunc =  zfs.<subcommand>.do_<subcommand>
55209962Smm
56209962Smm		subcmd = sys.argv[1]
57209962Smm		__import__("zfs." + subcmd)
58209962Smm		submod = getattr(zfs, subcmd)
59209962Smm		subfunc = getattr(submod, "do_" + subcmd)
60209962Smm	except (ImportError, AttributeError):
61209962Smm		sys.exit(_("invalid subcommand"))
62209962Smm
63209962Smm	try:
64209962Smm		subfunc()
65209962Smm	except zfs.util.ZFSError, e:
66209962Smm		print(e)
67209962Smm		sys.exit(1)
68209962Smm
69209962Smmexcept IOError, e:
70209962Smm	import errno
71209962Smm	import sys
72209962Smm
73209962Smm	if e.errno == errno.EPIPE:
74209962Smm		sys.exit(1)
75209962Smm	raise
76209962Smmexcept KeyboardInterrupt:
77209962Smm	import sys
78209962Smm
79209962Smm	sys.exit(1)
80