File: crm_utils.py.in

package info (click to toggle)
pacemaker 1.0.9.1%2Bhg15626-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 34,668 kB
  • ctags: 5,645
  • sloc: xml: 87,444; ansic: 57,853; python: 11,479; sh: 5,255; makefile: 663; perl: 387; sed: 262
file content (188 lines) | stat: -rw-r--r-- 4,763 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/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