~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to fai/abacmds.py

  • Committer: Robert Collins
  • Date: 2005-09-13 12:39:26 UTC
  • mto: (147.2.6) (364.1.3 bzrtools)
  • mto: This revision was merged to the branch mainline in revision 324.
  • Revision ID: robertc@robertcollins.net-20050913123926-b72242bdacc1ae52
create the output directory

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2004 Aaron Bentley
 
2
# <aaron.bentley@utoronto.ca>
 
3
#
 
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.
 
8
#
 
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.
 
13
#
 
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
 
17
import sys
 
18
import os
 
19
import os.path
 
20
import pybaz as arch
 
21
import pybaz.util
 
22
import commands
 
23
 
 
24
__docformat__ = "restructuredtext"
 
25
__doc__ = "Support for aba-style external command scripts"
 
26
 
 
27
class AbaCmds:
 
28
    """
 
29
    This class supports external aba-style subcommands in any programming
 
30
    language.
 
31
    """
 
32
    
 
33
    abadir = []
 
34
    def __init__(self):
 
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):
 
42
            self.abadir=""
 
43
 
 
44
    def list_commands(self):
 
45
        """
 
46
        Display a list of short descriptions of available commands
 
47
        """
 
48
 
 
49
        if (self.abadir == ""):
 
50
            return
 
51
        for command in self.get_commands():
 
52
            if commands.suggestions.has_key(command.name) or \
 
53
                commands.commands.has_key(command.name):
 
54
                continue
 
55
            command.command_exec(['desc'])
 
56
 
 
57
    def get_commands(self):
 
58
        """
 
59
        Return a sorted list of available aba commands.
 
60
 
 
61
        :rtype: a list of `AbaCommand`
 
62
        """
 
63
        extcommands = []
 
64
        if self.abadir != "":
 
65
            for k in os.listdir(self.abadir+"/commands"):
 
66
                cmd = self.is_command(k)
 
67
                if cmd:
 
68
                    extcommands.append(cmd)
 
69
        extcommands.sort()
 
70
        return extcommands
 
71
 
 
72
    def is_command(self, command):
 
73
        """
 
74
        Determine whether a given command is available.
 
75
 
 
76
        :param command: The name of the command to look for
 
77
        :type command: string
 
78
        :rtype: `AbaCommand`
 
79
        """
 
80
        if (self.abadir == ""):
 
81
            return False
 
82
        cmd = AbaCommand(command, self.abadir)
 
83
        if cmd.exists():
 
84
            return cmd
 
85
        else:
 
86
            return None
 
87
 
 
88
class AbaCommand:
 
89
    """
 
90
    An external, aba-style command script
 
91
    """
 
92
    def __init__(self, name, abadir):
 
93
        self.name = name
 
94
        self.path = abadir + "/commands/" + name
 
95
        self.abadir = abadir
 
96
 
 
97
    def __str__(self):
 
98
        return self.name
 
99
 
 
100
    def __lt__(self, arg):
 
101
        return self.name < arg.name
 
102
 
 
103
    def __gt__(self, arg):
 
104
        return self.name > arg.name
 
105
 
 
106
    def exists(self):
 
107
        """
 
108
        Determines whether this command really exists.
 
109
 
 
110
        Using commands that do not exist is frowned upon.
 
111
        """
 
112
        return os.access(self.path, os.X_OK) and os.path.isfile(self.path)
 
113
 
 
114
    def __call__(self, cmdline):
 
115
        try:
 
116
            self.run(cmdline.split())
 
117
        except Exception, e:
 
118
            print e
 
119
 
 
120
    def command_exec(self, args, expected=0):
 
121
        """
 
122
        Invoke a command.
 
123
 
 
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
 
128
        :rtype: integer
 
129
        """
 
130
        if (self.abadir == ""):
 
131
            return
 
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)
 
136
 
 
137
    def run(self, args):
 
138
        """
 
139
        Run the command.
 
140
 
 
141
        :param args: The arguments to pass to the command
 
142
        :type args: list of strings
 
143
        """
 
144
        exarg = ['exec']
 
145
        exarg.extend(args)
 
146
        self.command_exec(exarg, [0, 1, 2])
 
147
 
 
148
    def help(self):
 
149
        """
 
150
        Print command help to stdout.
 
151
        """
 
152
        self.command_exec(['exec', '-H'])
 
153
 
 
154
# arch-tag: c4325683-03f6-4436-bf8a-04e59195f3c1