diff --git a/tools/1node2heartbeat b/tools/1node2heartbeat deleted file mode 100755 index b63a0c81e4..0000000000 --- a/tools/1node2heartbeat +++ /dev/null @@ -1,326 +0,0 @@ -#!/usr/bin/python -# -# Program to determine current list of enabled services for init state 3 -# and create heartbeat CRM configuration for heartbeat to manage them -# -__copyright__=''' -Author: Alan Robertson -Copyright (C) 2006 International Business Machines -''' - -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 -# of the License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -import os,re -# -# Here's the plan: -# Find out the default run level -# Find out what (additional?) services are enabled in that run level -# Figure out which of them start after the network (or heartbeat?) -# Ignore heartbeat :-) -# Figure out which services supply the $services -# Look to see if the SUSE /etc/insserv.conf file exists -# If so, then scan it for who provides the $services -# defined by the LSB -# If we're on Red Hat, then make some Red Hat type assumptions -# (whatever those might be) -# If we're not, then make some generic assumptions... -# Scan the init scripts for their dependencies... -# Eliminate anything at or before 'network'. -# Create resources corresponding to all active services -# Include monitor actions for those services -# that can be started after 'network' -# Add the start-after dependencies -# -# Things to consider doing in the future: -# Constrain them to only run on the local system? -# Put them all in a convenience group (no colocation, no ordering) -# Add start and stop timeouts - -ServiceKeywords = {} -ServiceMap = {} -ProvidesMap = {} -RequiresMap = {} -SkipMap = {'heartbeat': None, 'random': None} -NoMonitor = {'microcode': None} -PreReqs = ['network'] -IgnoreList = [] -sysname = os.uname()[1] -InitDir = "/etc/init.d" - -def service_is_hb_compatible(service): - scriptname = os.path.join(InitDir, service) - command=scriptname + " status >/dev/null 2>&1"; - rc = os.system(command) - return rc == 0 - -def find_ordered_services(dir): - allscripts = os.listdir(dir) - allscripts.sort() - services = [] - for entry in allscripts: - matchobj = re.match("S[0-9]+(.*)", entry) - if not matchobj: - continue - service = matchobj.group(1) - if SkipMap.has_key(service): - continue - if service_is_hb_compatible(service): - services.append(service) - else: - IgnoreList.append(service) - return services - - -def register_services(initdir, services): - for service in services: - if not ServiceMap.has_key(service): - ServiceMap[service] = os.path.join(initdir, service) - for service in services: - script_dependency_scan(service, os.path.join(initdir, service), ServiceMap) - -# -# From the LSB version 3.1: "Comment Conventions for Init Scripts" -# -### BEGIN INIT INFO -### END INIT INFO -# -# The delimiter lines may contain trailing whitespace, which shall be ignored. -# All lines inside the block shall begin with a hash character '#' in the -# first column, so the shell interprets them as comment lines which do not -# affect operation of the script. The lines shall be of the form: -# {keyword}: arg1 [arg2...] -# with exactly one space character between the '#' and the keyword, with a -# single exception. In lines following a line containing the Description -# keyword, and until the next keyword or block ending delimiter is seen, -# a line where the '#' is followed by more than one space or a tab -# character shall be treated as a continuation of the previous line. -# - -# Make this a class to avoid recompiling it for each script we scan. -class pats: - begin=re.compile("###\s+BEGIN\s+INIT\s+INFO") - end=re.compile("###\s+END\s+INIT\s+INFO") - desc=re.compile("# Description:\s*(.*)", re.IGNORECASE) - desc_continue=re.compile("#( +|\t)\s*(.*)") - keyword=re.compile("# ([^\s:]+):\s*(.*)\s*\Z") - -def script_keyword_scan(filename, servicename): - keywords = {} - ST_START=0 - ST_INITINFO=1 - ST_DESCRIPTION=1 - description="" - state=ST_START - - try: - fd = open(filename) - except IOError: - return keywords - - while 1: - line = fd.readline() - if not line: - break - - if state == ST_START: - if pats.begin.match(line): - state = ST_INITINFO - continue - if pats.end.match(line): - break - - if state == ST_DESCRIPTION: - match = pats.desc_continue.match(line) - if match: - description += ("\n" + match.group(2)) - continue - state = ST_INITINFO - - match = pats.desc.match(line) - if match: - state = ST_DESCRIPTION - description = match.group(1) - continue - - match = pats.keyword.match(line) - if match: - keywords[match.group(1)] = match.group(2) - - # Clean up and return - fd.close() - if description != "": - keywords["Description"] = description - keywords["_PATHNAME_"] = filename - keywords["_RESOURCENAME_"] = "R_" + sysname + "_" + servicename - return keywords - -def script_dependency_scan(service, script, servicemap): - keywords=script_keyword_scan(script, service) - ServiceKeywords[service] = keywords - -SysServiceGuesses = { - '$local_fs': ['boot.localfs'], - '$network': ['network'], - '$named': ['named'], - '$portmap': ['portmap'], - '$remote_fs': ['nfs'], - '$syslog': ['syslog'], - '$netdaemons': ['portmap', 'inetd'], - '$time': ['ntp'], -} - -# -# For specific versions of Linux, there are often better ways -# to do this... -# -# (e.g., for SUSE Linux, one should look at /etc/insserv.conf file) -# -def map_sys_services(servicemap): - sysservicemap = {} - for sysserv in SysServiceGuesses.keys(): - servlist = SysServiceGuesses[sysserv] - result = [] - for service in servlist: - if servicemap.has_key(service): - result.append(service) - - sysservicemap[sysserv] = result - return sysservicemap - -# -# -# -def create_service_dependencies(servicekeywords, systemservicemap): - dependencies = {} - for service in servicekeywords.keys(): - if not dependencies.has_key(service): - dependencies[service] = {} - for key in ('Required-Start', 'Should-Start'): - if not servicekeywords[service].has_key(key): - continue - for depserv in servicekeywords[service][key].split(): - if systemservicemap.has_key(depserv): - sysserv = systemservicemap[depserv] - for serv in sysserv: - dependencies[service][serv] = None - else: - if servicekeywords.has_key(depserv): - dependencies[service][depserv] = None - if len(dependencies[service]) == 0: - del dependencies[service] - return dependencies - -# -# Modify the service name map to include all the mappings from -# 'Provides' services to real service script names... -# -def map_script_services(sysservmap, servicekeywords): - for service in servicekeywords.keys(): - if not servicekeywords[service].has_key('Provides'): - continue - for provided in servicekeywords[service]['Provides'].split(): - if not sysservmap.has_key(provided): - sysservmap[provided] = [] - sysservmap[provided].append(service) - return sysservmap - -def create_cib_update(keywords, depmap): - services = keywords.keys() - services.sort() - result = "" - # Create the XML for the resources - result += '\n' - result += '\n' - result += '\n' - result += '\n' - result += '\n' - groupname="G_" + sysname + "_localinit" - result += ' \n' - for service in services: - rid = keywords[service]["_RESOURCENAME_"] - monid = "OPmon_" + sysname + '_' + service - result += \ - ' \n' + \ - ' \n' + \ - ' \n' - if not NoMonitor.has_key(service): - result += \ - ' \n' - result += \ - ' \n' \ - ' \n' - result += ' \n' - result += '\n' - services = depmap.keys() - services.sort() - result += '\n' - for service in services: - rid = keywords[service]["_RESOURCENAME_"] - deps = depmap[service].keys() - deps.sort() - for dep in deps: - if not keywords.has_key(dep): - continue - depid = keywords[dep]["_RESOURCENAME_"] - orderid='O_' + sysname + '_' + service + '_' + dep - result += ' \n' - loc_id="Loc_" + sysname + "_localinit" - rule_id="LocRule_" + sysname + "_localinit" - expr_id="LocExp_" + sysname + "_localinit" - - result += ' \n' - result += ' \n' - result += ' \n' - result += ' \n' - result += ' \n' - result += '\n' - result += '\n' - result += '\n' - result += '\n' - return result - - - -def remove_a_prereq(service, servicemap, keywords, deps): - if deps.has_key(service): - parents = deps[service].keys() - del deps[service] - else: - parents = [] - if servicemap.has_key(service): - del servicemap[service] - if keywords.has_key(service): - del keywords[service] - for parent in parents: - if not deps.has_key(parent): - continue - remove_a_prereq(parent, servicemap, keywords, deps) - - -def remove_important_prereqs(prereqs, servicemap, keywords, deps): - # Find everything these important prereqs need and get rid of them... - for service in prereqs: - remove_a_prereq(service, servicemap, keywords, deps) - -ServiceList = find_ordered_services(os.path.join(InitDir, "rc3.d")) -register_services(InitDir, ServiceList) -SysServiceMap = map_sys_services(ServiceMap) -map_script_services(SysServiceMap, ServiceKeywords) -ServiceDependencies = create_service_dependencies(ServiceKeywords,SysServiceMap) -remove_important_prereqs(PreReqs, SysServiceMap, ServiceKeywords, ServiceDependencies) - -print create_cib_update(ServiceKeywords, ServiceDependencies) diff --git a/tools/crm_commands.py.in b/tools/crm_commands.py.in deleted file mode 100644 index c48d82c6a5..0000000000 --- a/tools/crm_commands.py.in +++ /dev/null @@ -1,132 +0,0 @@ -# -# -# pingd OCF Resource Agent -# Records (in the CIB) the current number of ping nodes a -# cluster node can connect to. -# -# Copyright (c) 2006 Andrew Beekhof -# All Rights Reserved. -# -# This program is free software; you can redistribute it and/or modify -# it under the terms of version 2 of the GNU General Public License as -# published by the Free Software Foundation. -# -# This program is distributed in the hope that it would be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. -# -# Further, this software is distributed without any warranty that it is -# free of the rightful claim of any third person regarding infringement -# or the like. Any license provided herein, whether implied or -# otherwise, applies only to this software file. Patent licenses, if -# any, provided herein do not apply to combinations of this program with -# other software, or any other product whatsoever. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write the Free Software Foundation, -# Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA. -# -####################################################################### - -import crm_utils as utl - -class HelpRequest(Exception): - """Exception raised when a help listing is required.""" - -class ReparseRequest(Exception): - """Exception raised when a command changed the command-line.""" - -def up(*args, **cmdoptions): - l = len(utl.topic_stack) - if l > 1: - utl.topic_stack.pop() - utl.set_topic(utl.topic_stack[-1]) - else: - utl.log_debug("Already at the top of the stack") - -def toggle_flag(*args, **cmdoptions): - flag = cmdoptions["flag"] - if utl.global_opts[flag]: - utl.global_opts[flag] = 0 - else: - utl.global_opts[flag] = 1 - - return utl.global_opts[flag] - -def cd_(*args, **cmdoptions): - utl.log_dev("args: %s\nopts: %s" % (repr(args), repr(cmdoptions))) - if not cmdoptions["topic"]: - utl.log_err("No topic specified") - return 1 - - if cmdoptions["topic"]: - utl.set_topic(cmdoptions["topic"]) - if args: - raise ReparseRequest() - if utl.crm_topic not in utl.topic_stack: - utl.topic_stack.append(cmdoptions["topic"]) - if not utl.global_opts["interactive"]: - help(cmdoptions["topic"]) - return 0 - -def exit(*args, **cmdoptions): - sys.exit(0) - -def help(*args, **cmdoptions): - if args: - raise HelpRequest(args[0]) - raise HelpRequest(utl.crm_topic) - -def debugstate(*args, **cmdoptions): - utl.log_info("Global Options: ") - for opt in utl.global_opts.keys(): - utl.log_info(" * %s:\t%s" % (opt, utl.global_opts[opt])) - utl.log_info("Stack: "+repr(utl.topic_stack)) - utl.log_info("Stack Head: "+utl.crm_topic) - return 0 - -def do_list(*args, **cmdoptions): - topic = utl.crm_topic - if cmdoptions.has_key("topic") and cmdoptions["topic"]: - topic = cmdoptions["topic"] - - utl.log_debug("Complete '%s' listing" % topic) - if topic == "resources": - utl.os_system("crm_resource -l", True) - elif topic == "nodes": - lines = utl.os_system("cibadmin -Q -o nodes", False) - for line in lines: - if line.find("node ") >= 0: - print line.rstrip() - else: - utl.log_err("%s: Topic %s is not (yet) supported" % ("list", topic)) - return 1 - return 0 - -def do_status(*args, **cmdoptions): - topic = utl.crm_topic - if cmdoptions.has_key("topic") and cmdoptions["topic"]: - topic = cmdoptions["topic"] - - if topic == "resources": - if not args: - utl.os_system("crm_resource -L", True) - for rsc in args: - utl.os_system("crm_resource -W -r %s"%rsc, True) - - elif topic == "nodes": - lines = utl.os_system("cibadmin -Q -o status", False) - for line in lines: - line = line.rstrip() - utl.log_dev("status line: "+line) - if line.find("node_state ") >= 0: - if not args: - print line - for node in args: - if line.find(node) >= 0: - print line - else: - utl.log_err("Topic %s is not (yet) supported" % topic) - return 1 - - return 0 diff --git a/tools/crm_primitive.py.in b/tools/crm_primitive.py.in deleted file mode 100644 index cfe0b5c929..0000000000 --- a/tools/crm_primitive.py.in +++ /dev/null @@ -1,268 +0,0 @@ -#!@PYTHON@ - -'''Create an XML fragment describing a new resource -''' - -__copyright__=''' -Author: Andrew Beekhof -Copyright (C) 2005 Andrew Beekhof -''' - -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 -# of the License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - -import sys,string,os -import xml.dom.minidom - -print_rsc_only = 0 -rsc_name = None -rsc_class = None -rsc_type = None -rsc_provider = None -start_timeout = None -stop_timeout = None -monitor_interval = None -monitor_timeout = None -rsc_options = [] -rsc_location = [] -rsc_colocation = [] - -def create_cib() : - doc = xml.dom.minidom.Document() - cib = doc.createElement("cib") - doc.appendChild(cib) - - configuration = doc.createElement("configuration") - cib.appendChild(configuration) - - #crm_config = doc.createElement("crm_config") - #configuration.appendChild(crm_config) - - resources = doc.createElement("resources") - configuration.appendChild(resources) - constraints = doc.createElement("constraints") - configuration.appendChild(constraints) - - return doc, resources, constraints - -def cib_resource(doc, id, ra_class, type, provider): - - params = None - - resource = doc.createElement("primitive") - - resource.setAttribute("id", id) - resource.setAttribute("type", type) - resource.setAttribute("class", ra_class) - - if ra_class == "ocf": - if not provider: - provider = "heartbeat" - resource.setAttribute("provider", provider) - - elif ra_class != "lsb" and ra_class != "heartbeat": - print "Unknown resource class: "+ ra_class - return None - - operations = doc.createElement("operations") - resource.appendChild(operations) - - if monitor_interval != None: - op = doc.createElement("op") - operations.appendChild(op) - op.setAttribute("id", id + "_mon_" + monitor_interval) - op.setAttribute("name", "monitor") - op.setAttribute("interval", monitor_interval) - if monitor_timeout != None: - op.setAttribute("timeout", monitor_timeout) - - if start_timeout != None: - op = doc.createElement("op") - operations.appendChild(op) - op.setAttribute("id", id + "_start") - op.setAttribute("name", "start") - op.setAttribute("timeout", start_timeout) - - if stop_timeout != None: - op = doc.createElement("op") - operations.appendChild(op) - op.setAttribute("id", id + "_stop") - op.setAttribute("name", "stop") - op.setAttribute("timeout", stop_timeout) - - instance_attributes = doc.createElement("instance_attributes") - instance_attributes.setAttribute("id", id) - resource.appendChild(instance_attributes) - attributes = doc.createElement("attributes") - instance_attributes.appendChild(attributes) - for i in range(0,len(rsc_options)) : - if rsc_options[i] == None : - continue - - param = string.split(rsc_options[i], "=") - nvpair = doc.createElement("nvpair") - nvpair.setAttribute("id", id + "_" + param[0]) - nvpair.setAttribute("name", param[0]) - nvpair.setAttribute("value", param[1]) - attributes.appendChild(nvpair) - - return resource - -def cib_rsc_location(doc, id, node, score): - rule = doc.createElement("rule") - rule.setAttribute("id", id+"_prefer_"+node+"_rule") - rule.setAttribute("score", score) - expression = doc.createElement("expression") - expression.setAttribute("id",id+"_prefer_"+node+"_expr") - expression.setAttribute("attribute","#uname") - expression.setAttribute("operation","eq") - expression.setAttribute("value", node) - rule.appendChild(expression) - return rule - -def cib_rsc_colocation(doc, id, other_resource, score): - rsc_colocation = doc.createElement("rsc_colocation") - rsc_colocation.setAttribute("id", id+"_colocate_with_"+other_resource) - rsc_colocation.setAttribute("from", id) - rsc_colocation.setAttribute("to", other_resource) - rsc_colocation.setAttribute("score", score) - return rsc_colocation - -def print_usage(): - print "usage: " \ - + sys.argv[0] \ - + " --name "\ - + " --class "\ - + " --type "\ - + " [--provider ]"\ - + "\n\t"\ - + " [--start-timeout ]"\ - + " [--stop-timeout ]"\ - + " [--monitor ]"\ - + " [--monitor-timeout ]"\ - + "\n\t"\ - + " [--rsc-option name=value]*"\ - + " [--rsc-location uname=score]*"\ - + " [--rsc-colocation resource=score]*" - print "Example:\n\t" + sys.argv[0] \ - + " --name cluster_ip_1 --type IPaddr --provider heartbeat --class ocf "\ - + "--rsc-option ip=192.168.1.101 --rsc-location node1=500 | cibadmin -C -p" - sys.exit(1) - -if __name__=="__main__" : - - # Process arguments... - skipthis = None - args = sys.argv[1:] - if len(args) == 0: - print_usage() - - for i in range(0, len(args)) : - if skipthis : - skipthis = None - continue - - elif args[i] == "--name" : - skipthis = True - rsc_name = args[i+1] - - elif args[i] == "--class" : - skipthis = True - rsc_class = args[i+1] - - elif args[i] == "--type" : - skipthis = True - rsc_type = args[i+1] - - elif args[i] == "--provider" : - skipthis = True - rsc_provider = args[i+1] - - elif args[i] == "--start-timeout" : - skipthis = True - start_timeout = args[i+1] - - elif args[i] == "--stop-timeout" : - skipthis = True - stop_timeout = args[i+1] - - elif args[i] == "--monitor" : - skipthis = True - monitor_interval = args[i+1] - - elif args[i] == "--monitor-timeout" : - skipthis = True - monitor_timeout = args[i+1] - - elif args[i] == "--rsc-option" : - skipthis = True - params = string.split(args[i+1], "=") - if params[1] != None: - rsc_options.append(args[i+1]) - else: - print "option '"+args[i+1]+"' must be of the form name=value" - - elif args[i] == "--rsc-location" : - skipthis = True - params = string.split(args[i+1], "=") - if params[1] != None: - rsc_location.append(args[i+1]) - else: - print "option '"+args[i+1]+"' must be of the form host=score" - - elif args[i] == "--rsc-colocation" : - skipthis = True - params = string.split(args[i+1], "=") - if params[1] != None: - rsc_colocation.append(args[i+1]) - else: - print "option '"+args[i+1]+"' must be of the form resource=score" - - elif args[i] == "--rsc-only" : - print_rsc_only = 1 - else: - print "Unknown argument: "+ args[i] - print_usage() - - cib = create_cib() - pre_line = "" - id_index = 1 - resource = cib_resource(cib[0], rsc_name, rsc_class, rsc_type, rsc_provider) - - if print_rsc_only: - print resource.toprettyxml() - sys.exit(0) - - cib[1].appendChild(resource) - - if rsc_location != None : - rsc_loc = cib[0].createElement("rsc_location") - rsc_loc.setAttribute("id", rsc_name+"_preferences") - rsc_loc.setAttribute("rsc", rsc_name) - for i in range(0, len(rsc_location)) : - param = string.split(rsc_location[i], "=") - location_rule = cib_rsc_location(cib[0], rsc_name, param[0], param[1]) - rsc_loc.appendChild(location_rule) - cib[2].appendChild(rsc_loc) - - for i in range(0, len(rsc_colocation)) : - if rsc_location[i] == None : - continue - - param = string.split(rsc_colocation[i], "=") - colocation_rule = cib_rsc_colocation(cib[0], rsc_name, param[0], param[1]) - cib[2].appendChild(colocation_rule) - - print cib[0].toprettyxml() diff --git a/tools/crm_utils.py.in b/tools/crm_utils.py.in deleted file mode 100644 index 67d6918de2..0000000000 --- a/tools/crm_utils.py.in +++ /dev/null @@ -1,188 +0,0 @@ -#!/bin/env python -# -# -# pingd OCF Resource Agent -# Records (in the CIB) the current number of ping nodes a -# cluster node can connect to. -# -# Copyright (c) 2006 Andrew Beekhof -# All Rights Reserved. -# -# This program is free software; you can redistribute it and/or modify -# it under the terms of version 2 of the GNU General Public License as -# published by the Free Software Foundation. -# -# This program is distributed in the hope that it would be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. -# -# Further, this software is distributed without any warranty that it is -# free of the rightful claim of any third person regarding infringement -# or the like. Any license provided herein, whether implied or -# otherwise, applies only to this software file. Patent licenses, if -# any, provided herein do not apply to combinations of this program with -# other software, or any other product whatsoever. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write the Free Software Foundation, -# Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA. -# -####################################################################### - -import os -import sys -import getopt -import readline -import traceback -from popen2 import Popen3 - -crm_topic = "crm" -topic_stack = [ crm_topic ] -hist_file = os.environ.get('HOME')+"/.crm_history" -global_opts = {} - -def exit_(code=0): - if global_opts["interactive"]: - log_info("Exiting... ") - try: - readline.write_history_file(hist_file) - log_debug("Wrote history to: "+hist_file) - except: - log_debug("Couldnt write history to: "+hist_file) - sys.exit(code) - -def log_debug(log): - if global_opts.has_key("debug") and global_opts["debug"]: - print log - -def log_dev(log): - if global_opts.has_key("devlog") and global_opts["devlog"]: - print log - -def log_info(log): - print log - -def log_err(log): - print "ERROR: "+log - -def set_topic(name): - global crm_topic - if crm_topic != name: - log_dev("topic: %s->%s" % (crm_topic, name)) - crm_topic = name - -def os_system(cmd, print_raw=False): - log_debug("Performing command: "+cmd) - p = Popen3(cmd, None) - p.tochild.close() - result = p.fromchild.readlines() - p.fromchild.close() - p.wait() - if print_raw: - for line in result: - print line.rstrip() - return result - -# -# Creates an argv-style array (that preserves quoting) for use in shell-mode -# -def create_argv(text): - args = [] - word = [] - index = 0 - total = len(text) - - in_word = False - in_verbatum = False - - while index < total: - finish_word = False - append_word = False - #log_debug("processing: "+text[index]) - if text[index] == '\\': - index = index +1 - append_word = True - - elif text[index].isspace(): - if in_verbatum or in_word: - append_word = True - else: - finish_word = True - - elif text[index] == '"': - if in_verbatum: - append_word = True - else: - finish_word = True - if in_word: - in_word = False - else: - in_word = True - - elif text[index] == '\'': - finish_word = True - if in_verbatum: - in_verbatum = False - else: - in_verbatum = True - else: - append_word = True - - if finish_word: - if word: - args.append(''.join(word)) - word = [] - elif append_word: - word.append(text[index]) - #log_debug("Added %s to word: %s" % (text[index], str(word))) - - index = index +1 - - if in_verbatum or in_word: - text="" - if word: - text=" after: '%s'"%''.join(word) - raise QuotingError("Un-matched quoting%s"%text, args) - - elif word: - args.append(''.join(word)) - - return args - -def init_readline(func): - readline.set_completer(func) - readline.parse_and_bind("tab: complete") - readline.set_history_length(100) - - try: - readline.read_history_file(hist_file) - except: - pass - -def fancyopts(args, options, state): - long = [] - short = '' - map = {} - dt = {} - - for s, l, d, c in options: - pl = l.replace('-', '_') - map['-'+s] = map['--'+l] = pl - state[pl] = d - dt[pl] = type(d) - if not d is None and not callable(d): - if s: s += ':' - if l: l += '=' - if s: short = short + s - if l: long.append(l) - - opts, args = getopt.getopt(args, short, long) - - for opt, arg in opts: - if dt[map[opt]] is type(fancyopts): state[map[opt]](state,map[opt],arg) - elif dt[map[opt]] is type(1): state[map[opt]] = int(arg) - elif dt[map[opt]] is type(''): state[map[opt]] = arg - elif dt[map[opt]] is type([]): state[map[opt]].append(arg) - elif dt[map[opt]] is type(None): state[map[opt]] = 1 - - return args