~abentley/bzrtools/bzrtools.dev

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
# Copyright (C) 2004 Aaron Bentley
# <aaron.bentley@utoronto.ca>
#
#    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
import os
import os.path
import pybaz as arch
import pybaz.util
import commands

__docformat__ = "restructuredtext"
__doc__ = "Support for aba-style external command scripts"

class AbaCmds:
    """
    This class supports external aba-style subcommands in any programming
    language.
    """
    
    abadir = []
    def __init__(self):
        """Determine the path to the aba commands"""
        self.abadir=os.path.expanduser("~/.fai/aba")
        if not os.access(self.abadir, os.X_OK):
            self.abadir="/etc/fai/aba"
        if not os.access(self.abadir, os.X_OK):
            self.abadir=sys.path[0]+"/aba"
        if not os.access(self.abadir, os.X_OK):
            self.abadir=""

    def list_commands(self):
        """
        Display a list of short descriptions of available commands
        """

        if (self.abadir == ""):
            return
        for command in self.get_commands():
            if commands.suggestions.has_key(command.name) or \
                commands.commands.has_key(command.name):
                continue
            command.command_exec(['desc'])

    def get_commands(self):
        """
        Return a sorted list of available aba commands.

        :rtype: a list of `AbaCommand`
        """
        extcommands = []
        if self.abadir != "":
            for k in os.listdir(self.abadir+"/commands"):
                cmd = self.is_command(k)
                if cmd:
                    extcommands.append(cmd)
        extcommands.sort()
        return extcommands

    def is_command(self, command):
        """
        Determine whether a given command is available.

        :param command: The name of the command to look for
        :type command: string
        :rtype: `AbaCommand`
        """
        if (self.abadir == ""):
            return False
        cmd = AbaCommand(command, self.abadir)
        if cmd.exists():
            return cmd
        else:
            return None

class AbaCommand:
    """
    An external, aba-style command script
    """
    def __init__(self, name, abadir):
        self.name = name
        self.path = abadir + "/commands/" + name
        self.abadir = abadir

    def __str__(self):
        return self.name

    def __lt__(self, arg):
        return self.name < arg.name

    def __gt__(self, arg):
        return self.name > arg.name

    def exists(self):
        """
        Determines whether this command really exists.

        Using commands that do not exist is frowned upon.
        """
        return os.access(self.path, os.X_OK) and os.path.isfile(self.path)

    def __call__(self, cmdline):
        try:
            self.run(cmdline.split())
        except Exception, e:
            print e

    def command_exec(self, args, expected=0):
        """
        Invoke a command.

        :param args: The arguments to supply to the command
        :type args: List of strings
        :param expected: Return values to expect
        :type expected: tuple of integers
        :rtype: integer
        """
        if (self.abadir == ""):
            return
        os.environ['abadir'] = self.abadir
        os.environ['abaname'] = 'fai'
        arch.util.exec_safe(self.path, args, stderr=sys.stderr, 
                            stdout=sys.stdout, expected=expected)

    def run(self, args):
        """
        Run the command.

        :param args: The arguments to pass to the command
        :type args: list of strings
        """
        exarg = ['exec']
        exarg.extend(args)
        self.command_exec(exarg, [0, 1, 2])

    def help(self):
        """
        Print command help to stdout.
        """
        self.command_exec(['exec', '-H'])

# arch-tag: c4325683-03f6-4436-bf8a-04e59195f3c1