summaryrefslogtreecommitdiffstats
path: root/nbb/nbblib/main.py
blob: 920a03f4e96fb406f45491545a9859d71b723069 (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
########################################################################
# Main program
########################################################################


"""\
nbb (ndim's branch builder) %(PACKAGE_VERSION)s
Build, install given branch of source code into a branch specific place
Copyright (C) 2007, 2008 Hans Ulrich Niedermann <hun@n-dimensional.de>
License conditions TBA

Usage: %(prog)s <to-be-determined>

Features:
 * supports git branches (TBD: git config)
 * supports bzr branches (requires useful branch nick, TBD: bzr config)
 * does out-of-source-tree builds (in-source-tree-builds unsupported)
 * direct support for automake/autoconf based build systems
 * TBD: supports execution of user commands in source, build, install dirs

TODO:
 * VCS config support ('git config', etc.)
 * Build system support: automake/autoconf, cmake
 * Design nice user interface. Requirements:
   * print top_srcdir, builddir, installdir
   * start subshell in top_srcdir, builddir, installdir
   * run 'autoreconf' type step
   * run 'configure' type step
   * run 'make' type step
   * run 'make install' type step
   * run custom (make) commands
 * Bash syntax completion for that user interface.
 * Man page or something similar.

Command line interface (TBD):

  Run default build commands:
    $ %(prog)s [general options] init [command specific options]
    $ %(prog)s [general options] configure [command specific options]
    $ %(prog)s [general options] build [command specific options]
    $ %(prog)s [general options] install [command specific options]

  Run cleanup commands:
    TBD

  Get/set config:
    $ %(prog)s [general options] config srcdir
    $ %(prog)s [general options] config builddir [<builddir>]
    $ %(prog)s [general options] config installdir [<installdir>]

  Start an interactive shell in either of the three directories:
    $ %(prog)s [general options] src-sh [command specific options]
    $ %(prog)s [general options] build-sh [command specific options]
    $ %(prog)s [general options] install-sh [command specific options]

  Run command in builddir:
    $ %(prog)s [general options] run <command> [<param>...]
    $ %(prog)s [general options] run [command specific options... <-->] <cmd>...

  (Not sure about these)
  Run a non-interactive shell command in either of the three directories:
    $ %(prog)s [general options] src-sh [command specific options] <command>...
    $ %(prog)s [general options] build-sh [command specific options] <command>...
    $ %(prog)s [general options] install-sh [command specific options] <cmd...>

Global options:

  -h --help          Print this help text
  -V --version       Print program version number

  -n --dry-run       Do not actually execute any commands

  -b --build-system  Force buildsystem detection (%(buildsystems)s)
  -v --vcs           Force VCS detection (%(vcssystems)s)
"""


from nbblib.bs import *
from nbblib.commands import *
from nbblib.package import *
from nbblib.vcs import *


def print_help(context):
    print __doc__ % context,


class Property(object):
    def __init__(self, **kwargs):
        if kwargs.has_key('default'):
            self.default = kwargs['default']
    def __get__(self, instance, owner):
        if hasattr(self, 'value'):
            return self.value
        elif hasattr(self, 'default'):
            return self.default
        else:
            return None
    def __set__(self, instance, value):
        if hasattr(self, 'value'):
            raise "Property cannot be set more than once"
        elif not self.isvalid(value):
            raise "Property cannot be set to invalid value '%s'" % value
        else:
            self.value = self.convert(value)
    def __str__(self):
        if hasattr(self, 'value'):
            return self.value
        else:
            return '<undefined property>'
    def isvalid(self, value):
        return True
    def convert(self, value):
        return value


class ProgProperty(Property):
    def convert(self, value):
        prog = value
        idx = prog.rfind('/')
        if idx >= 0:
            prog = prog[idx+1:]
        return prog


class VCSProperty(Property):
    def isvalid(self, value):
        return (value in VCSourceTree.plugins.keys())


class BSProperty(Property):
    def isvalid(self, value):
        return (value in BSSourceTree.plugins.keys())


class BoolProperty(Property):
    def __init__(self, default=False):
        super(BoolProperty, self).__init__(default=default)
    def isvalid(self, value):
        return (value in (True, False))

class DryRunProperty(BoolProperty):
    def __init__(self):
        super(DryRunProperty, self).__init__(default=False)


class Context(object):
    PACKAGE_VERSION = Property()
    prog = ProgProperty()
    vcs = VCSProperty()
    bs = BSProperty()
    dry_run = DryRunProperty()
    verbose = BoolProperty()
    vcssystems = Property()
    buildsystems = Property()

    def __getitem__(self, key):
        """emulate a dict() for the purpose of "%(prog)s" % context"""
        return getattr(self, key)


def main(argv):
    context = Context()
    context.PACKAGE_VERSION = PACKAGE_VERSION
    context.vcssystems = ", ".join(VCSourceTree.plugins.keys())
    context.buildsystems = ", ".join(BSSourceTree.plugins.keys())
    context.prog = argv[0]

    if len(argv) < 2:
        print "Fatal: %(prog)s requires some arguments" % context
        return 2

    i = 1
    while i<len(argv):
        if argv[i][0] != '-':
            break
        if argv[i] in ('-h', '--help'):
            print_help(context)
            return
        elif argv[i] in ('-V', '--version'):
            print "%(prog)s (ndim's branch builder) %(PACKAGE_VERSION)s" % context
            return
        elif argv[i] in ('-n', '--dry-run'):
            context.dry_run = True
        elif argv[i] in ('-b', '--build-system'):
            i = i + 1
            assert(i<len(argv))
            context.bs = argv[i]
        elif argv[i][:6] == '--build-system=':
            context.bs = argv[i][6:]
        elif argv[i] in ('-v', '--vcs'):
            i = i + 1
            assert(i<len(argv))
            context.vcs = argv[i]
        elif argv[i][:6] == '--vcs=':
            context.vcs = argv[i][6:]
        elif argv[i] in ('--verbose', ):
            context.verbose = True
        # print "", i, argv[i]
        i = i + 1
    cmd = argv[i]
    cmdargs = argv[i+1:]
    nbb = NBB_Command(cmd, cmdargs, context=context)