summaryrefslogtreecommitdiffstats
path: root/src/nbblib/bs.py
blob: ac09b97f12f4d74044b3ddd3530162eb096d6949 (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
########################################################################
# Buildsystem Source Tree plugins
########################################################################


import os
import logging

from nbblib.plugins import *
from nbblib.progutils import *


class NotABSSourceTree(Exception):
    def __init__(self, vcs_tree):
        super(NotABSSourceTree, self).__init__()
        self.vcs_tree = vcs_tree
    def __str__(self):
        return ("Source tree build system type for '%s' not detected"
                % (self.vcs_tree,))


class AmbigousBSSource(Exception):
    def __init__(self, srcdir, matches):
        super(AmbigousBSSource, self).__init__()
        self.srcdir = srcdir
        self.matches = matches
    def __str__(self):
        fmt = "  %-9s %s"
        def strmatch(m):
            return fmt % (m.name, m.tree_root())
        alist = [fmt % ('VCS Type', 'Source tree root')]
        alist.extend(map(strmatch, self.matches))
        return ("More than one source tree VCS type detected for '%s':\n#%s"
                % (self.srcdir, '\n '.join(alist)))


class BSSourceTree(object):
    __metaclass__ = GenericPluginMeta

    def __init__(self, context):
        super(BSSourceTree, self).__init__()
        self.context = context

    @classmethod
    def detect(cls, vcs_tree, context):
        """Find BS tree type and return it"""
        if len(cls.plugins) < 1:
            raise NoPluginsRegistered(cls)
        logging.debug("CLASS %s", cls)
        matches = PluginDict()
        for key, klass in cls.plugins.iteritems():
            try:
                t = klass(vcs_tree, context)
                if t.tree_root() == vcs_tree.tree_root():
                    logging.debug("KLASS %s", klass)
                    matches[key] = t
            except NotABSSourceTree, e:
                pass
        if len(matches) > 1:
            raise ("More than one source tree BS type detected for '%s': %s"
                   % (vcs_tree, ", ".join([str(x) for x in matches])))
        elif len(matches) < 1:
            raise NotABSSourceTree(vcs_tree)
        return matches[matches.keys()[0]]

    def __str__(self):
        return "BS-Source-Tree(%s, %s)" % (self.name,
                                           repr(self.tree_root()))

    # Abstract methods
    def tree_root(self): raise NotImplementedError()
    def init(self): raise NotImplementedError()
    def configure(self): raise NotImplementedError()
    def build(self): raise NotImplementedError()
    def install(self): raise NotImplementedError()


class AutomakeSourceTree(BSSourceTree):
    name = 'automake'
    def __init__(self, vcs_tree, context):
        super(AutomakeSourceTree, self).__init__(context)
        srcdir = vcs_tree.tree_root()
        self.config = vcs_tree.config
        flag = False
        for f in [ os.path.join(srcdir, 'configure.ac'),
                   os.path.join(srcdir, 'configure.in'),
                   ]:
            if os.path.exists(f):
                flag = True
                break
        if not flag:
            raise NotABSSourceTree(vcs_tree)

    def tree_root(self):
        return self.config.srcdir

    def init(self):
        """'autoreconf'"""
        prog_run(["autoreconf", "-v", "-i", "-s", self.config.srcdir],
                 self.context)

    def configure(self):
        """'configure --prefix'"""
        if not os.path.exists(os.path.join(self.config.srcdir, 'configure')):
            self.init()
        builddir = self.config.builddir
        if not os.path.exists(builddir): os.makedirs(builddir)
        os.chdir(builddir)
        prog_run(["%s/configure" % self.config.srcdir,
                  "--prefix=%s" % self.config.installdir,
                  "--enable-maintainer-mode",
                  ], self.context)

    def build(self):
        """'make'"""
        builddir = self.config.builddir
        if not os.path.exists(os.path.join(builddir, 'config.status')):
            self.configure()
        os.chdir(builddir)
        prog_run(["make", ], self.context)

    def install(self):
        """'make install'"""
        builddir = self.config.builddir
        if not os.path.exists(os.path.join(builddir, 'config.status')):
            self.configure()
        os.chdir(builddir)
        prog_run(["make", "install", "INSTALL=/usr/bin/install -p"],
                 self.context)