1
# Copyright (C) 2004 Aaron Bentley
2
# <aaron.bentley@utoronto.ca>
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24
__docformat__ = "restructuredtext"
25
__doc__ = "Support for aba-style external command scripts"
29
This class supports external aba-style subcommands in any programming
35
"""Determine the path to the aba commands"""
36
self.abadir=os.path.expanduser("~/.fai/aba")
37
if not os.access(self.abadir, os.X_OK):
38
self.abadir="/etc/fai/aba"
39
if not os.access(self.abadir, os.X_OK):
40
self.abadir=sys.path[0]+"/aba"
41
if not os.access(self.abadir, os.X_OK):
44
def list_commands(self):
46
Display a list of short descriptions of available commands
49
if (self.abadir == ""):
51
for command in self.get_commands():
52
if commands.suggestions.has_key(command.name) or \
53
commands.commands.has_key(command.name):
55
command.command_exec(['desc'])
57
def get_commands(self):
59
Return a sorted list of available aba commands.
61
:rtype: a list of `AbaCommand`
65
for k in os.listdir(self.abadir+"/commands"):
66
cmd = self.is_command(k)
68
extcommands.append(cmd)
72
def is_command(self, command):
74
Determine whether a given command is available.
76
:param command: The name of the command to look for
80
if (self.abadir == ""):
82
cmd = AbaCommand(command, self.abadir)
90
An external, aba-style command script
92
def __init__(self, name, abadir):
94
self.path = abadir + "/commands/" + name
100
def __lt__(self, arg):
101
return self.name < arg.name
103
def __gt__(self, arg):
104
return self.name > arg.name
108
Determines whether this command really exists.
110
Using commands that do not exist is frowned upon.
112
return os.access(self.path, os.X_OK) and os.path.isfile(self.path)
114
def __call__(self, cmdline):
116
self.run(cmdline.split())
120
def command_exec(self, args, expected=0):
124
:param args: The arguments to supply to the command
125
:type args: List of strings
126
:param expected: Return values to expect
127
:type expected: tuple of integers
130
if (self.abadir == ""):
132
os.environ['abadir'] = self.abadir
133
os.environ['abaname'] = 'fai'
134
arch.util.exec_safe(self.path, args, stderr=sys.stderr,
135
stdout=sys.stdout, expected=expected)
141
:param args: The arguments to pass to the command
142
:type args: list of strings
146
self.command_exec(exarg, [0, 1, 2])
150
Print command help to stdout.
152
self.command_exec(['exec', '-H'])
154
# arch-tag: c4325683-03f6-4436-bf8a-04e59195f3c1