summaryrefslogtreecommitdiffstats
path: root/src/nbblib/commands.py
blob: 126500b739bec257909343422b5bf7598c2c2406 (plain)
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
"""\
nbblib.commands - cmdline command infrastructure and implementation
Copyright (C) 2008 Hans Ulrich Niedermann <hun@n-dimensional.de>
"""

import os
import sys
import logging


import nbblib.package as package
import nbblib.plugins as plugins
import nbblib.progutils as progutils
import nbblib.vcs as vcs
import nbblib.bs as bs


__all__ = ['Commander']


def adjust_doc(doc):
    """Remove common whitespace at beginning of doc string lines"""
    if not doc: return doc
    i = 0
    for i in range(len(doc)):
        if doc[i] not in " \t":
            break
    prefix = doc[:i]
    rest_doc = doc[i:]
    almost_doc = rest_doc.replace("\n%s" % prefix, "\n")
    i = -1
    while almost_doc[i] == '\n':
        i = i - 1
    return almost_doc[:i]


__all__.append('CommandLineError')
class CommandLineError(Exception):
    def __init__(self, message):
        super(CommandLineError, self).__init__()
        self.msg = message
    def __str__(self):
        return "Command line error: %s" % self.msg


__all__.append('UnknownCommand')
class UnknownCommand(Exception):
    def __init__(self, context, cmd):
        super(UnknownCommand, self).__init__()
        self.prog = context.prog
        self.cmd = cmd
    def __str__(self):
        return "Unknown %(prog)s command '%(cmd)s'" % self.__dict__


########################################################################
# Command plugin system
########################################################################


__all__.append('Command')
class Command(object):
    """
    Mount point for plugins which refer to commands that can be performed.

    Plugins implementing this reference should provide the following
    interface:

    name  attribute
        The text to be displayed, describing the version control system
    summary attribute
        Short (less than 50 chars) command summary line
    usage attribute
        Usage string (defaults to '')

    validate_args(*args, **kwargs)  function
        Must raise CommandLineError() if it encounters invalid arguments in cmdargs
    run() function
        Actually run the function

    FFF(*args, **kwargs)
        *args are the arguments from the command line
        **kwargs are additional parameters from within the program
    """
    __metaclass__ = plugins.GenericPluginMeta

    usage = ''

    def __init__(self, context, *args, **kwargs):
        super(Command, self).__init__()
        self.validate_args(*args, **kwargs)
        self.args = args
        self.kwargs = kwargs
        self.context = context

    @plugins.abstractmethod
    def run(self):
        """Run the command"""
        pass

    @plugins.abstractmethod
    def validate_args(self, *cmdargs, **kwargs):
        """Validate command line arguments: Abstract method.

        May make use of self.context.
        """
        pass

    def validate_args_none(self, *cmdargs, **kwargs):
        """Validate command line arguments: no argument at all"""
        logging.debug("Command:  %s", self.name)
        logging.debug("*cmdargs: %s", cmdargs)
        logging.debug("**kwargs: %s", kwargs)
        if len(cmdargs) > 0:
            raise CommandLineError("'%s' command takes no parameters",
                                   self.name)
        logging.debug("Command match!")
        return True

    def validate_args_any(self, *cmdargs, **kwargs):
        """Validate command line arguments: accept any argument"""
        logging.debug("Command:  %s", self.name)
        logging.debug("*cmdargs: %s", cmdargs)
        logging.debug("**kwargs: %s", kwargs)
        logging.debug("Command match!")
        return True

    def __str__(self):
        return "Command(%s, %s)" % (self.cmd_name, self.cmdargs)


class HelpCommand(Command):
    """\
    If the optional <command> is given, print the help for <command>.
    Else, print a list of commands and general help.
    """

    name = 'help'
    summary = 'print help text'
    usage = '[<command>]'

    def validate_args(self, *args, **kwargs):
        if len(args) == 1 and args[0] not in Command.plugins.keys():
            raise CommandLineError("'%s' is an invalid command name", args[0])
        elif len(args) > 1:
            raise CommandLineError("'%s' command only takes one optional parameter", self.name)

    def _print_command_list(self):
        print "List of commands:"
        keys = Command.plugins.keys()
        if not keys:
            raise Exception("No commands found. Please lart the developer.")
        keys.sort()
        keys2 = Command.plugins.keys()
	keys2.sort(lambda a,b: cmp(len(b),len(a)))
	print "keys ", keys
	print "keys2", keys2
        fmt = "\t%%-%ds\t%%s" % len(keys2[0])
        for k in keys:
           print fmt % (k, Command.plugins[k].summary)

    def _print_command_help(self, cmd):
        """print help for command cmd"""
        c = Command.plugins[cmd]
        print "Purpose:", c.summary
        if c.usage:
            print "Usage:  ", self.context.prog, cmd, c.usage
        else:
            print "Usage:  ", self.context.prog, cmd
        if hasattr(c, '__doc__'):
            if c.__doc__:
                print
                print adjust_doc(c.__doc__)

    def run(self):
        if len(self.args) == 0:
            self._print_command_list()
        elif len(self.args) == 1:
            self._print_command_help(self.args[0])
        else:
            assert(False)


class InternalConfigCommand(Command):
    name = 'internal-config'
    summary = 'print internal program configuration'
    validate_args = Command.validate_args_none
    def run(self):
        print "Source tree types:",  ", ".join(vcs.VCSourceTree.plugins.keys())
        print "Build system types:", ", ".join(bs.BSSourceTree.plugins.keys())
        print "Commands:",           ", ".join(Command.plugins.keys())


class SourceClassCommand(Command):
    """Base class for commands acting on source trees"""
    name = None # abstract command class
    def __init__(self, context, *args, **kwargs):
        super(SourceClassCommand, self).__init__(context, *args, **kwargs)

        srcdir = os.getcwd()
        absdir = os.path.abspath(srcdir)

        self.vcs_sourcetree = vcs.VCSourceTree.detect(context, absdir)
        logging.debug("vcs_sourcetree %s", self.vcs_sourcetree)

        self.bs_sourcetree = bs.BSSourceTree.detect(context,
                                                    self.vcs_sourcetree)
        logging.debug("bs_sourcetree %s", self.bs_sourcetree)

        cfg = self.vcs_sourcetree.config
        for x in ('srcdir', 'builddir', 'installdir'):
            logging.info("CONFIG %s %s", x, getattr(cfg, x))


class DetectCommand(Command):
    name = None
    def __init__(self, context, *args, **kwargs):
        super(DetectCommand, self).__init__(context, *args, **kwargs)
        self.srcdir = os.getcwd()
        self.absdir = os.path.abspath(self.srcdir)
    validate_args = Command.validate_args_none


class DetectVCSCommand(DetectCommand):
    name = "detect-vcs"
    summary = "detect source tree VCS"
    def __init__(self, context, *args, **kwargs):
        super(DetectVCSCommand, self).__init__(context, *args, **kwargs)
        self.vcs_sourcetree = vcs.VCSourceTree.detect(self.context, self.absdir)
        logging.debug("vcs_sourcetree %s", self.vcs_sourcetree)
    def run(self):
        if self.vcs_sourcetree:
            print 'VCS:', self.vcs_sourcetree.name, self.vcs_sourcetree.tree_root
        else:
            print 'VCS:', 'Not detected'
    validate_args = Command.validate_args_none


class DetectBSCommand(DetectCommand):
    name = "detect-bs"
    summary = "detect source tree BS"
    def __init__(self, context, *args, **kwargs):
        super(DetectBSCommand, self).__init__(context, *args, **kwargs)
        self.vcs_sourcetree = vcs.VCSourceTree.detect(self.context, self.absdir)
        logging.debug("vcs_sourcetree %s", self.vcs_sourcetree)
        self.bs_sourcetree = bs.BSSourceTree.detect(self.context,
                                                    self.vcs_sourcetree)
        logging.debug("bs_sourcetree %s", self.bs_sourcetree)
    def run(self):
        if self.bs_sourcetree:
            print 'BS:', self.bs_sourcetree.name, self.bs_sourcetree.tree_root
        else:
            print 'BS:', 'Not detected'
    validate_args = Command.validate_args_none


class BuildTestCommand(SourceClassCommand):
    name = 'build-test'
    summary = 'simple build test'
    def run(self):
        self.bs_sourcetree.init()
        self.bs_sourcetree.configure()
        self.bs_sourcetree.build()
        self.bs_sourcetree.install()
    validate_args = Command.validate_args_none


class InitCommand(SourceClassCommand):
    name = 'init'
    summary = 'initialize buildsystem (e.g. "autoreconf")'
    validate_args = Command.validate_args_none
    def run(self):
        self.bs_sourcetree.init()


class ConfigureCommand(SourceClassCommand):
    name = 'configure'
    summary = 'configure buildsystem (e.g. "./configure")'
    validate_args = Command.validate_args_none
    def run(self):
        self.bs_sourcetree.configure()


class BuildCommand(SourceClassCommand):
    name = 'build'
    summary = 'build from source (e.g. "make")'
    validate_args = Command.validate_args_none
    def run(self):
        self.bs_sourcetree.build()


class InstallCommand(SourceClassCommand):
    name = 'install'
    summary = 'install the built things (e.g. "make install")'
    validate_args = Command.validate_args_none
    def run(self):
        self.bs_sourcetree.install()


class MakeCommand(SourceClassCommand):
    name = 'make'
    summary = 'run make in builddir'
    validate_args = Command.validate_args_any
    def run(self):
        os.chdir(self.bs_sourcetree.config.builddir)
        progutils.prog_run(["make"] + list(self.args),
                           self.context)


class GeneralRunCommand(SourceClassCommand):
    """Run general command in some branch specific dir

    """
    name = 'run'
    summary = 'run some command in branch specific dir'
    validate_args = Command.validate_args_any
    def __init__(self, context, *args, **kwargs):
        super(GeneralRunCommand, self).__init__(context, *args, **kwargs)
        self.rundir = None
        self.run_in = 'builddir'
        if len(self.args):
            if self.args[0] == '--srcdir':
                self.args = self.args[1:]
                self.rundir = self.bs_sourcetree.config.srcdir
                self.run_in = 'srcdir'
            elif self.args[0] == '--installdir':
                self.args = self.args[1:]
                self.rundir = self.bs_sourcetree.config.installdir
                self.run_in = 'installdir'
            elif self.args[0] == '--builddir':
                self.args = self.args[1:]
        if not self.rundir:
            self.rundir = self.bs_sourcetree.config.builddir
    def chdir(self):
        if os.path.exists(self.rundir):
            os.chdir(self.rundir)
        else:
            raise RuntimeError("The %s directory %s does not exist"
                               % (self.run_in, repr(self.rundir)))
    def run(self):
        self.chdir()
        progutils.prog_run(list(self.args), self.context)


class GeneralShellCommand(GeneralRunCommand):
    name    = 'sh'
    summary = 'run shell in branch specific dir'
    def get_shell_prompt(self):
        return r",--[Ctrl-d or 'exit' to quit this %s shell for branch '%s']--\n| <%s %s> %s\n\`--[\u@\h \W]\$ " \
            % (self.context.prog, self.vcs_sourcetree.branch_name,
               self.context.prog, self.name, self.get_run_in_dir(), )
    def run(self):
        self.chdir()
        # FIXME: Allow using $SHELL or similar.
        progutils.prog_run(['sh'] + list(self.args), self.context,
                           env_update = {'PS1': self.get_shell_prompt()})


class SrcShellCommand(GeneralShellCommand):
    name    = 'sh-src'
    summary = 'run interactive shell in source dir'
    run_in  = 'srcdir'


class BuildShellCommand(GeneralShellCommand):
    name    = 'sh-build'
    summary = 'run interactive shell in build dir'
    run_in  = 'builddir'


class InstallShellCommand(GeneralShellCommand):
    name    = 'sh-install'
    summary = 'run interactive shell in install dir'
    run_in  = 'installdir'


class ConfigCommand(SourceClassCommand):
    name = 'config'
    summary = 'set/get config values'
    usage = '(srcdir|builddir|installdir)'

    def validate_args(self, *args, **kwargs):
        items = ('srcdir', 'builddir', 'installdir', )
        if len(args) == 0:
            raise CommandLineError("'%s' command requires at least one parameter (%s)",
                                   self.name, ', '.join(items))
        elif len(args) == 1 and args[0] in items:
            pass
        elif len(args) == 2 and args[0] in items:
            if args[0] in ('srcdir', ):
                raise CommandLineError("'%s' command cannot change 'srcdir'",
                                       self.name)
            else:
                pass
        else:
            raise CommandLineError("'%s' requires less or different parameters",
                                   self.name)

    def run(self):
        git_get_items = ('builddir', 'installdir', 'srcdir')
        git_set_items = ('builddir', 'installdir', )
        if len(self.args) == 1:
            if self.args[0] in git_get_items:
                print getattr(self.vcs_sourcetree.config, self.args[0])
            else:
                assert(False)
        elif len(self.args) == 2:
            if self.args[0] == 'builddir':
                self.vcs_sourcetree.config.builddir = self.args[1]
            elif self.args[0] == 'installdir':
                self.vcs_sourcetree.config.installdir = self.args[1]
            else:
                assert(False)
        else:
            assert(False)


class Commander(object):

    def __init__(self, context, cmd, *cmdargs):
        self.context = context
        logging.debug("Commander() %s %s", cmd, cmdargs)
        if cmd in Command.plugins:
            self.command = Command.plugins[cmd](context, *cmdargs)
        else:
            raise UnknownCommand(context, cmd)

    def run(self):
        self.command.run()

# End of file.