Path: blob/main/cddl/contrib/opensolaris/cmd/pyzfs/pyzfs.py
39492 views
#! /usr/bin/python2.4 -S1#2# CDDL HEADER START3#4# The contents of this file are subject to the terms of the5# Common Development and Distribution License (the "License").6# You may not use this file except in compliance with the License.7#8# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE9# or http://www.opensolaris.org/os/licensing.10# See the License for the specific language governing permissions11# and limitations under the License.12#13# When distributing Covered Code, include this CDDL HEADER in each14# file and include the License file at usr/src/OPENSOLARIS.LICENSE.15# If applicable, add the following below this CDDL HEADER, with the16# fields enclosed by brackets "[]" replaced with your own identifying17# information: Portions Copyright [yyyy] [name of copyright owner]18#19# CDDL HEADER END20#21# Copyright 2009 Sun Microsystems, Inc. All rights reserved.22# Use is subject to license terms.23#2425# Note, we want SIGINT (control-c) to exit the process quietly, to mimic26# the standard behavior of C programs. The best we can do with pure27# Python is to run with -S (to disable "import site"), and start our28# program with a "try" statement. Hopefully nobody hits ^C before our29# try statement is executed.3031try:32import site33import gettext34import zfs.util35import zfs.ioctl36import sys37import errno3839"""This is the main script for doing zfs subcommands. It doesn't know40what subcommands there are, it just looks for a module zfs.<subcommand>41that implements that subcommand."""4243_ = gettext.translation("SUNW_OST_OSCMD", "/usr/lib/locale",44fallback=True).gettext4546if len(sys.argv) < 2:47sys.exit(_("missing subcommand argument"))4849zfs.ioctl.set_cmdstr(" ".join(["zfs"] + sys.argv[1:]))5051try:52# import zfs.<subcommand>53# subfunc = zfs.<subcommand>.do_<subcommand>5455subcmd = sys.argv[1]56__import__("zfs." + subcmd)57submod = getattr(zfs, subcmd)58subfunc = getattr(submod, "do_" + subcmd)59except (ImportError, AttributeError):60sys.exit(_("invalid subcommand"))6162try:63subfunc()64except zfs.util.ZFSError, e:65print(e)66sys.exit(1)6768except IOError, e:69import errno70import sys7172if e.errno == errno.EPIPE:73sys.exit(1)74raise75except KeyboardInterrupt:76import sys7778sys.exit(1)798081