xref: /titanic_44/usr/src/cmd/pyzfs/pyzfs.py (revision bde3d612a7c090234c60e6e4578821237a5db135)
1#! /usr/bin/python2.6 -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 (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
23#
24
25# Note, we want SIGINT (control-c) to exit the process quietly, to mimic
26# the standard behavior of C programs.  The best we can do with pure
27# Python is to run with -S (to disable "import site"), and start our
28# program with a "try" statement.  Hopefully nobody hits ^C before our
29# try statement is executed.
30
31try:
32	import site
33	import gettext
34	import zfs.util
35	import zfs.ioctl
36	import sys
37	import errno
38	import solaris.misc
39
40	"""This is the main script for doing zfs subcommands.  It doesn't know
41	what subcommands there are, it just looks for a module zfs.<subcommand>
42	that implements that subcommand."""
43
44	try:
45		_ = gettext.translation("SUNW_OST_OSCMD", "/usr/lib/locale",
46		    fallback=True).gettext
47	except:
48		_ = solaris.misc.gettext
49
50	if len(sys.argv) < 2:
51		sys.exit(_("missing subcommand argument"))
52
53	zfs.ioctl.set_cmdstr(" ".join(["zfs"] + sys.argv[1:]))
54
55	try:
56		# import zfs.<subcommand>
57		# subfunc =  zfs.<subcommand>.do_<subcommand>
58
59		subcmd = sys.argv[1]
60		__import__("zfs." + subcmd)
61		submod = getattr(zfs, subcmd)
62		subfunc = getattr(submod, "do_" + subcmd)
63	except (ImportError, AttributeError):
64		sys.exit(_("invalid subcommand"))
65
66	try:
67		subfunc()
68	except zfs.util.ZFSError, e:
69		print(e)
70		sys.exit(1)
71
72except IOError, e:
73	import errno
74	import sys
75
76	if e.errno == errno.EPIPE:
77		sys.exit(1)
78	raise
79except KeyboardInterrupt:
80	import sys
81
82	sys.exit(1)
83