Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/devbot/build.py
blob: 874092791ea92c7dae3276f8d53d0e501c375b81 (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
import fnmatch
import os
import multiprocessing
import shutil
import subprocess
import time

from devbot import command
from devbot import config
from devbot import environ
from devbot import state
from devbot import utils

def build_one(module_name):
    environ.setup()

    for module in config.load_modules():
        if module.name == module_name:
            return _build_module(module)

    return False

def pull_one(module_name):
    environ.setup()

    for module in config.load_modules():
        if module.name == module_name:
            return _pull_module(module)

    return False

def pull():
    environ.setup()

    for module in config.load_modules():
        if not _pull_module(module):
            return False

    return True

def build():
    environ.setup()

    modules = config.load_modules()
    skipped = []

    for module in modules[:]:
        new_commit_id = module.get_commit_id()
        if new_commit_id is None:
            break

        old_commit_id = state.get_built_commit_id(module)
        if old_commit_id == new_commit_id:
            modules.pop(0)
            skipped.append(module.name)
        else:
            break

    if skipped:
        print "\n* Skipping unchanged modules *\n"
        print "\n".join(skipped)

    for module in modules:
        state.remove_built_commit_id(module)

    for module in modules:
        log = "build-%s" % time.strftime("%Y%m%d-%H%M%S")
        if not _build_module(module, log):
            return False

    return True

def clean():
    _rmtree(config.install_dir)
    _rmtree(config.prefix_dir)
    _rmtree(config.get_build_dir())

    for module in config.load_modules():
        if not module.out_of_source:
            if module.get_git_module().clean():
                print "Cleaned %s git repository." % module.name

def _unlink_libtool_files():
    def func(arg, dirname, fnames):
        for fname in fnmatch.filter(fnames, "*.la"):
            os.unlink(os.path.join(dirname, fname))

    os.path.walk(config.lib_dir, func, None)

def _pull_module(module):
    print "\n=== Pulling %s ===\n" % module.name

    try:
        module.get_git_module().update()
    except subprocess.CalledProcessError:
        return False

    return True

def _build_autotools(module, log):
    makefile_path = os.path.join(module.get_build_dir(), "Makefile")

    if not os.path.exists(makefile_path):
        autogen = os.path.join(module.get_source_dir(), "autogen.sh")

        args = [autogen,
                "--prefix", config.prefix_dir,
                "--libdir", config.lib_dir]
        args.extend(module.options)

        command.run(args, log)

    jobs = multiprocessing.cpu_count() * 2

    command.run(["make", "-j", "%d" % jobs], log)
    command.run(["make", "install"], log)

    _unlink_libtool_files()

def _build_activity(module, log):
    command.run(["./setup.py", "install", "--prefix", config.prefix_dir], log)

def _build_module(module, log=None):
    print "\n=== Building %s ===\n" % module.name

    source_dir = module.get_source_dir()

    if not os.path.exists(source_dir):
        print "Source directory does not exist. Please pull the sources " \
              "before building."
        return False

    if module.out_of_source:
        build_dir = module.get_build_dir()

        if not os.path.exists(build_dir):
            os.mkdir(build_dir)

        os.chdir(build_dir)
    else:
        os.chdir(source_dir)

    try:
        if os.path.exists(os.path.join(source_dir, "setup.py")):
            _build_activity(module, log)
        elif os.path.exists(os.path.join(source_dir, "autogen.sh")):
            _build_autotools(module, log)
        else:
            print "The source directory has unexpected content, please " \
                  "delete it and pull\nthe source again."                
            return False
    except subprocess.CalledProcessError:
        return False

    state.touch_built_commit_id(module)

    return True

def _rmtree(dir):
    print "Deleting %s" % dir
    shutil.rmtree(dir, ignore_errors=True)