Debian Codegraph @

lib/ClusterShell/CLI/Error.py

raw symbols SCIP
1 #
2 # Copyright (C) 2010-2012 CEA/DAM
3 #
4 # This file is part of ClusterShell.
5 #
6 # ClusterShell is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation; either
9 # version 2.1 of the License, or (at your option) any later version.
10 #
11 # ClusterShell is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # Lesser General Public License for more details.
15 #
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with ClusterShell; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
20 """
21 CLI error handling helper functions
22 """
23
24 from __future__ import print_function
25
26 try:
27 import configparser
28 except ImportError:
29 # Python 2 compat
30 import ConfigParser as configparser
31
32 import errno
33 import logging
34 import os.path
35 from resource import getrlimit, RLIMIT_NOFILE
36 import signal
37 import sys
38
39 from ClusterShell.Engine.Engine import EngineNotSupportedError
40 from ClusterShell.NodeUtils import GroupResolverConfigError
41 from ClusterShell.NodeUtils import GroupResolverIllegalCharError
42 from ClusterShell.NodeUtils import GroupResolverSourceError
43 from ClusterShell.NodeUtils import GroupSourceError
44 from ClusterShell.NodeUtils import GroupSourceNoUpcall
45 from ClusterShell.NodeSet import NodeSetExternalError, NodeSetParseError
46 from ClusterShell.NodeSet import RangeSetParseError
47 from ClusterShell.Propagation import RouteResolvingError
48 from ClusterShell.Topology import TopologyError
49 from ClusterShell.Worker.EngineClient import EngineClientError
50 from ClusterShell.Worker.Worker import WorkerError
51
52 GENERIC_ERRORS = (configparser.Error,
53 EngineNotSupportedError,
54 EngineClientError,
55 NodeSetExternalError,
56 NodeSetParseError,
57 RangeSetParseError,
58 GroupResolverConfigError,
59 GroupResolverIllegalCharError,
60 GroupResolverSourceError,
61 GroupSourceError,
62 GroupSourceNoUpcall,
63 RouteResolvingError,
64 TopologyError,
65 TypeError,
66 IOError,
67 OSError,
68 KeyboardInterrupt,
69 ValueError,
70 WorkerError)
71
72 LOGGER = logging.getLogger(__name__)
73
74 def handle_generic_error(excobj):
75 """handle error given `excobj' generic script exception"""
76 prog = os.path.basename(sys.argv[0])
77 try:
78 raise excobj
79 except EngineNotSupportedError as exc:
80 msgfmt = "%s: I/O events engine '%s' not supported on this host"
81 print(msgfmt % (prog, exc.engineid), file=sys.stderr)
82 except EngineClientError as exc:
83 print("%s: EngineClientError: %s" % (prog, exc), file=sys.stderr)
84 except NodeSetExternalError as exc:
85 print("%s: External error: %s" % (prog, exc), file=sys.stderr)
86 except (NodeSetParseError, RangeSetParseError) as exc:
87 print("%s: Parse error: %s" % (prog, exc), file=sys.stderr)
88 except GroupResolverIllegalCharError as exc:
89 print('%s: Illegal group character: "%s"' % (prog, exc),
90 file=sys.stderr)
91 except GroupResolverConfigError as exc:
92 print('%s: Group resolver error: %s' % (prog, exc), file=sys.stderr)
93 except GroupResolverSourceError as exc:
94 print('%s: Unknown group source: "%s"' % (prog, exc), file=sys.stderr)
95 except GroupSourceNoUpcall as exc:
96 msgfmt = '%s: No %s upcall defined for group source "%s"'
97 print(msgfmt % (prog, exc, exc.group_source.name), file=sys.stderr)
98 except GroupSourceError as exc:
99 print("%s: Group error: %s" % (prog, exc), file=sys.stderr)
100 except (RouteResolvingError, TopologyError) as exc:
101 print("%s: TREE MODE: %s" % (prog, exc), file=sys.stderr)
102 except configparser.Error as exc:
103 print("%s: %s" % (prog, exc), file=sys.stderr)
104 except (TypeError, ValueError, WorkerError) as exc:
105 print("%s: %s" % (prog, exc), file=sys.stderr)
106 except (IOError, OSError) as exc: # see PEP 3151
107 if exc.errno == errno.EPIPE:
108 # be quiet on broken pipe
109 LOGGER.debug(exc)
110 else:
111 print("ERROR: %s" % exc, file=sys.stderr)
112 if exc.errno == errno.EMFILE:
113 print("ERROR: maximum number of open file descriptors: "
114 "soft=%d hard=%d" % getrlimit(RLIMIT_NOFILE),
115 file=sys.stderr)
116 except KeyboardInterrupt as exc:
117 return 128 + signal.SIGINT
118 except:
119 assert False, "wrong GENERIC_ERRORS"
120
121 # Exit with error code 1 (generic failure)
122 return 1