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