forked from OCA/runbot-addons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runbot_build.py
171 lines (154 loc) · 6.23 KB
/
runbot_build.py
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
# -*- encoding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# This module copyright (C) 2010 - 2014 Savoir-faire Linux
# (<http://www.savoirfairelinux.com>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
import logging
import os
import sys
import shutil
import openerp
from openerp.osv import orm, fields
from openerp.addons.runbot.runbot import mkdirs
_logger = logging.getLogger(__name__)
MAGIC_PID_RUN_NEXT_JOB = -2
def custom_build(func):
"""Decorator for functions which should be overwritten only if
is_custom_build is enabled in repo.
"""
def custom_func(self, cr, uid, ids, context=None):
args = [
('id', 'in', ids),
('branch_id.repo_id.is_custom_build', '=', True)
]
custom_ids = self.search(cr, uid, args, context=context)
regular_ids = list(set(ids) - set(custom_ids))
ret = None
if regular_ids:
regular_func = getattr(super(runbot_build, self), func.func_name)
ret = regular_func(cr, uid, regular_ids, context=context)
if custom_ids:
assert ret is None
ret = func(self, cr, uid, custom_ids, context=context)
return ret
return custom_func
class runbot_build(orm.Model):
_inherit = "runbot.build"
_columns = {
'prebuilt': fields.boolean("Prebuilt"),
}
def job_00_init(self, cr, uid, build, lock_path, log_path):
res = super(runbot_build, self).job_00_init(
cr, uid, build, lock_path, log_path
)
if build.branch_id.repo_id.is_custom_build:
build.pre_build(lock_path, log_path)
build.prebuilt = True
return res
def job_10_test_base(self, cr, uid, build, lock_path, log_path):
if build.branch_id.repo_id.skip_test_jobs:
_logger.info('skipping job_10_test_base')
return MAGIC_PID_RUN_NEXT_JOB
else:
return super(runbot_build, self).job_10_test_base(
cr, uid, build, lock_path, log_path
)
def job_20_test_all(self, cr, uid, build, lock_path, log_path):
if build.branch_id.repo_id.skip_test_jobs:
_logger.info('skipping job_20_test_all')
with open(log_path, 'w') as f:
f.write('consider tests as passed: '
'.modules.loading: Modules loaded.')
return MAGIC_PID_RUN_NEXT_JOB
else:
return super(runbot_build, self).job_20_test_all(
cr, uid, build, lock_path, log_path
)
def sub_cmd(self, build, cmd):
if not cmd:
return []
if isinstance(cmd, basestring):
cmd = cmd.split()
internal_vals = {
'custom_build_dir': build.repo_id.custom_build_dir or '',
'custom_server_path': build.repo_id.custom_server_path,
'other_repo_path': build.repo_id.other_repo_id.path or '',
'build_dest': build.dest,
}
return [i % internal_vals for i in cmd]
def pre_build(self, cr, uid, ids, lock_path, log_path, context=None):
"""Run pre-build command if there is one
Substitute path variables after splitting command to avoid problems
with spaces in internal variables.
Run command in build path to avoid relative path issues.
"""
pushd = os.getcwd()
try:
for build in self.browse(cr, uid, ids, context=context):
if build.prebuilt:
continue
cmd = self.sub_cmd(build, build.repo_id.custom_pre_build_cmd)
if not cmd:
continue
os.chdir(build.path())
self.spawn(cmd, lock_path, log_path)
finally:
os.chdir(pushd)
@custom_build
def checkout(self, cr, uid, ids, context=None):
"""Checkout in custom build directories if they are specified
Do same as superclass except for git_export path.
"""
for build in self.browse(cr, uid, ids, context=context):
if build.prebuilt:
continue
# starts from scratch
if os.path.isdir(build.path()):
shutil.rmtree(build.path())
# runbot log path
mkdirs([build.path("logs")])
# checkout branch
build_path = build.path()
custom_build_dir = build.repo_id.custom_build_dir
if custom_build_dir:
mkdirs([build.path(custom_build_dir)])
build_path = os.path.join(build_path, custom_build_dir)
build.repo_id.git_export(build.name, build_path)
@custom_build
def cmd(self, cr, uid, ids, context=None):
"""Get server start script from build config
Overwrite superclass completely
Specify database user in the case of custom config, to allow viewing
after db has been created by Odoo (using current user).
Disable multiworker
"""
build = self.browse(cr, uid, ids[0], context=context)
server_path = build.path(build.repo_id.custom_server_path)
mods = build.repo_id.modules or "base"
params = self.sub_cmd(build, build.repo_id.custom_server_params)
# commandline
cmd = [
sys.executable,
server_path,
"--no-xmlrpcs",
"--xmlrpc-port=%d" % build.port,
"--db_user=%s" % openerp.tools.config['db_user'],
"--workers=0",
] + params
return cmd, mods