1
1
# Copyright (C) 2008-2011 Dejan Muhamedagic <dmuhamedagic@suse.de>
3
3
# This program is free software; you can redistribute it and/or
4
4
# modify it under the terms of the GNU General Public
5
5
# License as published by the Free Software Foundation; either
6
6
# version 2 of the License, or (at your option) any later version.
8
8
# This software is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
11
# General Public License for more details.
13
13
# You should have received a copy of the GNU General Public
14
14
# License along with this library; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24
from utils import wait4dc, is_pcmk_118, is_program
25
25
from userprefs import Options, UserPrefs
26
26
from vars import Vars
27
27
from ui import cmd_exit
28
from msg import ErrorBuffer, syntax_err, skill_err
29
from msg import common_warn, common_info, common_debug, common_err
29
30
from levels import Levels
32
user_args = None # assigned in run()
31
35
def load_rc(rcfile):
34
40
save_stdin = sys.stdin
37
43
inp = multi_input()
40
try: parse_line(levels, shlex.split(inp))
47
parse_line(levels, shlex.split(inp))
41
48
except ValueError, msg:
44
51
sys.stdin = save_stdin
46
54
def multi_input(prompt=''):
48
56
Get input from user
127
143
if not options.interactive and i == len(s)-1:
128
144
set_interactive()
129
145
lvl.new_level(pt[token], token)
130
pt = lvl.parse_root # move to the next level
146
pt = lvl.parse_root # move to the next level
132
cmd = pt[token] # terminal symbol
148
cmd = pt[token] # terminal symbol
133
149
break # and stop parsing
135
151
syntax_err(s[i:])
138
if cmd: # found a terminal symbol
154
if cmd: # found a terminal symbol
139
155
if not user_prefs.check_skill_level(cmd[2]):
149
165
d = lambda: cmd[0](*args)
150
rv = d() # execute the command
166
rv = d() # execute the command
151
167
# should we wait till the command takes effect?
152
do_wait = user_prefs.get_wait() and rv != False and (cmd[3] == 1 or \
168
do_wait = user_prefs.get_wait() and rv != False and (cmd[3] == 1 or
153
169
(lvl.current_level.should_wait() and \
154
170
(lvl.is_in_transit() or not options.interactive)))
179
197
proglist = "which cibadmin crm_resource crm_attribute crm_mon"
180
198
for prog in proglist.split():
181
199
if not is_program(prog):
182
print >> sys.stderr, "%s not available, check your installation"%prog
200
print >> sys.stderr, "%s not available, check your installation" % prog
185
204
# prefer the user set PATH
187
206
mybinpath = os.path.dirname(sys.argv[0])
189
208
if p not in os.environ["PATH"].split(':'):
190
209
os.environ['PATH'] = "%s:%s" % (os.environ['PATH'], p)
192
212
# three modes: interactive (no args supplied), batch (input from
193
213
# a file), half-interactive (args supplied, but not batch)
194
214
def cib_prompt():
195
215
return vars.cib_in_use or "live"
269
290
vars = Vars.getInstance()
270
291
levels = Levels.getInstance()
272
294
def set_interactive():
273
295
'''Set the interactive option only if we're on a tty.'''
274
296
if sys.stdin.isatty():
275
297
options.interactive = True
277
300
def xdg_file(name, xdg_name, obj_type, semantics):
278
301
if not name or not xdg_name:
293
316
common_debug("moving %s to %s" % (name, new))
294
317
os.rename(name, new)
296
321
def mv_user_files():
297
vars.hist_file = xdg_file(vars.hist_file, \
298
vars.xdg_map["history"], "f", "cache")
299
vars.rc_file = xdg_file(vars.rc_file, \
300
vars.xdg_map["rc"], "f", "config")
301
vars.index_file = xdg_file(vars.index_file, \
302
vars.xdg_map["help_index"], "f", "cache")
303
vars.tmpl_conf_dir = xdg_file(vars.tmpl_conf_dir, \
304
vars.xdg_map["crmconf"], "d", "config")
322
vars.hist_file = xdg_file(vars.hist_file,
323
vars.xdg_map["history"], "f", "cache")
324
vars.rc_file = xdg_file(vars.rc_file,
325
vars.xdg_map["rc"], "f", "config")
326
vars.index_file = xdg_file(vars.index_file,
327
vars.xdg_map["help_index"], "f", "cache")
328
vars.tmpl_conf_dir = xdg_file(vars.tmpl_conf_dir,
329
vars.xdg_map["crmconf"], "d", "config")
306
332
def compatibility_setup():
307
333
if is_pcmk_118():
398
426
options.interactive = True
401
opts, user_args = getopt.getopt(sys.argv[1:], \
402
'whdc:f:FX:RD:H:', ("wait", "version", "help", "debug", \
403
"cib=", "file=", "force", "profile=", \
404
"regression-tests", "display=", "history="))
429
opts, user_args = getopt.getopt(
432
("wait", "version", "help", "debug",
433
"cib=", "file=", "force", "profile=",
434
"regression-tests", "display=", "history="))
405
435
for o, p in opts:
406
436
if o in ("-h", "--help"):
408
438
elif o in ("--version"):
409
print >> sys.stdout,("%s" % vars.crm_version)
439
print >> sys.stdout, ("%s" % vars.crm_version)
412
442
user_prefs.set_debug()
436
466
if options.profile:
438
468
cProfile.run('main.do_work()', options.profile)
439
print "python -c 'import pstats; s = pstats.Stats(\"%s\"); s.sort_stats(\"cumulative\").print_stats()' | less" % options.profile
469
# print how to use the profile file, but don't disturb
470
# the regression tests
471
if not options.regression_tests:
472
stats_cmd = "; ".join(['import pstats',
473
's = pstats.Stats("%s")' % options.profile,
474
's.sort_stats("cumulative").print_stats()'])
475
print "python -c '%s' | less" % (stats_cmd)