From 22009e5de1252b669dddd9517abfea3992b40137 Mon Sep 17 00:00:00 2001 From: Narayan Desai Date: Fri, 13 Jul 2007 18:41:32 +0000 Subject: Switch to newstyle exception raises git-svn-id: https://svn.mcs.anl.gov/repos/bcfg/trunk/bcfg2@3446 ce84e21b-d406-0410-9b95-82705330c041 --- src/sbin/bcfg2 | 92 +++++++++++++++++++++++++++++----------------------- src/sbin/bcfg2-admin | 24 +++++++------- src/sbin/bcfg2-info | 4 +-- 3 files changed, 65 insertions(+), 55 deletions(-) (limited to 'src') diff --git a/src/sbin/bcfg2 b/src/sbin/bcfg2 index d5ef99418..262cdc8b9 100755 --- a/src/sbin/bcfg2 +++ b/src/sbin/bcfg2 @@ -3,21 +3,35 @@ '''Bcfg2 Client''' __revision__ = '$Revision$' -import getopt, logging, os, signal, sys, tempfile, time, xmlrpclib -import Bcfg2.Options, Bcfg2.Client.XML, Bcfg2.Client.Frame, Bcfg2.Client.Tools +import getopt +import logging +import os +import signal +import sys +import tempfile +import time +import xmlrpclib +import Bcfg2.Options +import Bcfg2.Client.XML +import Bcfg2.Client.Frame +import Bcfg2.Client.Tools try: - import Bcfg2.Client.Proxy, Bcfg2.Logging + import Bcfg2.Client.Proxy + import Bcfg2.Logging except KeyError: print "Could not read options from configuration file" - raise SystemExit, 1 + raise SystemExit(1) + def cb_sigint_handler(signum, frame): '''Exit upon CTRL-C''' os._exit(1) + class Client: ''' The main bcfg2 client class ''' + def __init__(self): self.toolset = None self.config = None @@ -25,26 +39,26 @@ class Client: optinfo = { # 'optname': (('-a', argdesc, optdesc), # env, cfpath, default, boolean)), - 'verbose':(('-v', False,"enable verbose output"), - False, False, False, True), - 'extra':(('-e', False,"enable extra entry detailed output"), - False, False, False, True), - 'quick':(('-q', False, "disable some checksum verification"), - False, False, False, True), - 'debug':(('-d', False, "enable debugging output"), - False, False, False, True), - 'drivers':(('-D', ',', "Specify tool driver set"), - False, ('client', 'drivers'), False, False), - 'fingerprint':(('-F', '', "Server Fingerprint"), - False, ('communication', 'fingerprint'), False, False), - 'dryrun':(('-n', False, "do not actually change the system"), + 'verbose': (('-v', False, "enable verbose output"), + False, False, False, True), + 'extra': (('-e', False, "enable extra entry output"), + False, False, False, True), + 'quick': (('-q', False, "disable some checksum verification"), + False, False, False, True), + 'debug': (('-d', False, "enable debugging output"), False, False, False, True), + 'drivers': (('-D', ',', "Specify tool driver set"), + False, ('client', 'drivers'), False, False), + 'fingerprint': (('-F', '', "Server Fingerprint"), + False, ('communication', 'fingerprint'), False, False), + 'dryrun': (('-n', False, "do not actually change the system"), + False, False, False, True), 'build': (('-B', False, "run in build mode"), False, False, False, True), - 'paranoid':(('-P', False, "make automatic backups of config files"), - False, False, False, True), - 'bundle':(('-b', '', "only configure the given bundle"), - False, False, False, False), + 'paranoid': (('-P', False, "make automatic backups of config files"), + False, False, False, True), + 'bundle': (('-b', '', "only configure the given bundle"), + False, False, False, False), 'file': (('-f', "", "configure from a file rather than querying the server"), False, False, False, False), 'interactive': (('-I', False, "prompt the user for each change"), @@ -77,7 +91,7 @@ class Client: optparser.shortopt, optparser.longopt)[1]: print "Bcfg2 takes no arguments, only options" print optparser.helpmsg - raise SystemExit, 1 + raise SystemExit(1) level = 30 if self.setup['verbose']: level = 20 @@ -89,14 +103,14 @@ class Client: if 'drivers' in self.setup and self.setup['drivers'] == 'help': self.logger.info("The following drivers are available:") self.logger.info(Bcfg2.Client.Tools.drivers) - raise SystemExit, 0 + raise SystemExit(0) if self.setup['remove'] and 'services' in self.setup['remove']: self.logger.error("Service removal is nonsensical, disable services to get former behavior") if self.setup['remove'] not in [False, 'all', 'services', 'packages']: self.logger.error("Got unknown argument %s for -r" % (self.setup['remove'])) if (self.setup["file"] != False) and (self.setup["cache"] != False): print "cannot use -f and -c together" - raise SystemExit, 1 + raise SystemExit(1) def run_probe(self, probe): '''Execute probe''' @@ -110,24 +124,20 @@ class Client: (probe.attrib.get('interpreter', '/bin/sh'))) script.write(probe.text) script.close() - os.chmod(script.name, 0755) + os.chmod(script.name, 0755) ret.text = os.popen(script.name).read().strip() self.logger.info("Probe %s has result:\n%s" % (name, ret.text)) finally: os.unlink(script.name) except: self.logger.error("Failed to execute probe: %s" % (name), exc_info=1) - raise SystemExit, 1 + raise SystemExit(1) return ret def fatal_error(self, message): '''Signal a fatal error''' self.logger.error("Fatal error: %s" % (message)) - raise SystemExit, 1 - - def get_config(self): - '''Either download the config from the server or read it from file''' - + raise SystemExit(1) def run(self): ''' Perform client execution phase ''' @@ -165,17 +175,17 @@ class Client: except xmlrpclib.Fault, flt: self.logger.error("Failed to download probes from bcfg2") self.logger.error(flt.faultString) - raise SystemExit, 1 + raise SystemExit(1) times['probe_download'] = time.time() - + try: probes = Bcfg2.Client.XML.XML(probe_data) except Bcfg2.Client.XML.ParseError, syntax_error: self.fatal_error( "server returned invalid probe requests: %s" % (syntax_error)) - + # execute probes try: probedata = Bcfg2.Client.XML.Element("ProbeData") @@ -183,7 +193,7 @@ class Client: for probe in probes.findall(".//probe")] except: self.logger.error("Failed to Execute probes") - raise SystemExit, 1 + raise SystemExit(1) if len(probes.findall(".//probe")) > 0: try: @@ -191,15 +201,15 @@ class Client: proxy.RecvProbeData(Bcfg2.Client.XML.tostring(probedata)) except: self.logger.error("Failed to upload probe data", exc_info=1) - raise SystemExit, 1 - + raise SystemExit(1) + times['probe_upload'] = time.time() try: rawconfig = proxy.GetConfig() except xmlrpclib.Fault: self.logger.error("Failed to download configuration from bcfg2") - raise SystemExit, 2 + raise SystemExit(2) times['config_download'] = time.time() @@ -211,7 +221,7 @@ class Client: self.logger.warning("failed to write config cache file %s" % (self.setup['cache'])) times['caching'] = time.time() - + try: self.config = Bcfg2.Client.XML.XML(rawconfig) except Bcfg2.Client.XML.ParseError, syntax_error: @@ -219,7 +229,7 @@ class Client: (syntax_error)) times['config_parse'] = time.time() - + if self.config.tag == 'error': self.fatal_error("server error: %s" % (self.config.text)) @@ -235,7 +245,7 @@ class Client: proxy.RecvStats(Bcfg2.Client.XML.tostring(feedback)) except xmlrpclib.Fault: self.logger.error("Failed to upload configuration statistics") - raise SystemExit, 2 + raise SystemExit(2) if __name__ == '__main__': signal.signal(signal.SIGINT, cb_sigint_handler) diff --git a/src/sbin/bcfg2-admin b/src/sbin/bcfg2-admin index 1b4f20d82..05c51c3bc 100755 --- a/src/sbin/bcfg2-admin +++ b/src/sbin/bcfg2-admin @@ -81,7 +81,7 @@ h. FreeBSD def err_exit(emsg): print emsg - raise SystemExit, 1 + raise SystemExit(1) #build bcfg2.conf file def initialize_repo(cfile): @@ -254,13 +254,13 @@ def do_compare(cargs): new = lxml.etree.parse(new).getroot() except IOError: print "Failed to read %s" % (new) - raise SystemExit, 1 + raise SystemExit(1) try: old = lxml.etree.parse(old).getroot() except IOError: print "Failed to read %s" % (old) - raise SystemExit, 1 + raise SystemExit(1) for src in [new, old]: for bundle in src.findall('./Bundle'): @@ -319,7 +319,7 @@ def do_pull(cfile, repopath, client, etype, ename): bcore = Bcfg2.Server.Core.Core({}, cfile) except Bcfg2.Server.Core.CoreInitError, msg: print "Core load failed because %s" % msg - raise SystemExit, 1 + raise SystemExit(1) [bcore.fam.Service() for x in range(10)] while bcore.fam.Service(): pass @@ -392,7 +392,7 @@ def do_viz(repopath, myargs): opts, args = getopt.getopt(myargs, 'rhbko:', ['raw', 'includehosts', 'includebundles', 'includekey', 'outfile=']) except getopt.GetoptError, msg: print msg - raise SystemExit, 1 + raise SystemExit(1) options = [] for opt, arg in opts: @@ -427,7 +427,7 @@ def do_viz(repopath, myargs): dotpipe.tochild.write("digraph groups {\n") except: print "write to dot process failed. Is graphviz installed?" - raise SystemExit, 1 + raise SystemExit(1) dotpipe.tochild.write('\trankdir="LR";\n') if 'hosts' in options: clients = lxml.etree.parse(repopath + '/Metadata/clients.xml').getroot() @@ -506,7 +506,7 @@ def do_client(repopath, args): attr, val = i.split('=', 1) if not(attr in ['name', 'profile', 'uuid', 'password', 'address', 'secure', 'location']): print "Attribute %s unknown" % attr - raise SystemExit, 1 + raise SystemExit(1) element.attrib[attr] = val root.append(element) @@ -528,17 +528,17 @@ if __name__ == '__main__': opts, args = getopt.getopt(sys.argv[1:], 'hC:R:', ['help', 'configfile=', 'repopath=']) except getopt.GetoptError, msg: print msg - raise SystemExit, 1 + raise SystemExit(1) if not args: print usage - raise SystemExit, 1 + raise SystemExit(1) # First get the options... for opt, arg in opts: if opt in ("-h", "--help"): print usage - raise SystemExit, 1 + raise SystemExit(1) if opt in ("-C", "--configfile"): configfile = arg if opt in ("-R", "--repopath"): @@ -557,7 +557,7 @@ if __name__ == '__main__': elif args[0] == 'pull': if len(args) != 4: print usage - raise SystemExit, 1 + raise SystemExit(1) do_pull(configfile, Repopath, args[1], args[2], args[3]) elif args[0] == 'minestruct': @@ -578,7 +578,7 @@ if __name__ == '__main__': elif args[0] == 'client': if len(args) < 4: print usage - raise SystemExit, 1 + raise SystemExit(1) do_client(Repopath, args[1:]) else: print usage diff --git a/src/sbin/bcfg2-info b/src/sbin/bcfg2-info index 0bb315f7c..56503ab5b 100755 --- a/src/sbin/bcfg2-info +++ b/src/sbin/bcfg2-info @@ -26,7 +26,7 @@ class infoCore(cmd.Cmd, Bcfg2.Server.Core.Core): Bcfg2.Server.Core.Core.__init__(self, {}, cfpath) except Bcfg2.Server.Core.CoreInitError, msg: print "Core load failed because %s" % msg - raise SystemExit, 1 + raise SystemExit(1) self.prompt = '> ' self.cont = True for i in range(25): @@ -41,7 +41,7 @@ class infoCore(cmd.Cmd, Bcfg2.Server.Core.Core): def do_quit(self, _): """Exit program. Usage: [quit|exit]""" - raise SystemExit, 0 + raise SystemExit(0) do_EOF = do_quit -- cgit v1.2.3-1-g7c22