Fetching contributors…
Cannot retrieve contributors at this time
executable file 534 lines (469 sloc) 23.8 KB
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from __future__ import print_function
import logging
import optparse
import os
import pexpect
import shutil
import signal
import subprocess
import tempfile
import time
import traceback
from xmlrpc import client as xmlrpclib
except ImportError:
import xmlrpclib
from contextlib import contextmanager
from glob import glob
from os.path import abspath, dirname, join
from sys import stdout, stderr
from tempfile import NamedTemporaryFile
# apt-get install rsync python-pexpect debhelper python-setuptools
# Utils
exec(open(join(dirname(__file__), '..', 'odoo', ''), 'rb').read())
version = version.split('-')[0].replace('saas~','')
docker_version = version.replace('+', '')
timestamp = time.strftime("%Y%m%d", time.gmtime())
GPGID = os.getenv('GPGID')
'debian': 'deb',
'redhat': 'rpm',
'tarball': 'src',
'windows': 'exe',
def move_glob(source, wildcards, destination):
"""Move files matched by wildcards from source to destination
wildcards can be a single string wildcard like '*.deb' or a list of wildcards
if not os.path.isdir(destination):
raise BaseException('Destination "{}" is not a directory'.format(destination))
if isinstance(wildcards, str):
wildcards = [wildcards]
for wc in wildcards:
for file_path in glob(os.path.join(source, wc)):
shutil.move(file_path, destination)
def mkdir(d):
if not os.path.isdir(d):
def system(l, chdir=None):"System call: {}".format(l))
if chdir:
cwd = os.getcwd()
if isinstance(l, list):
rc = os.spawnvp(os.P_WAIT, l[0], l)
elif isinstance(l, str):
tmp = ['sh', '-c', l]
rc = os.spawnvp(os.P_WAIT, tmp[0], tmp)
if chdir:
return rc
def _rpc_count_modules(addr='', port=8069, dbname='mycompany'):
modules = xmlrpclib.ServerProxy('%s:%s/xmlrpc/object' % (addr, port)).execute(
dbname, 1, 'admin', 'ir.module.module', 'search', [('state', '=', 'installed')]
if modules and len(modules) > 1:
toinstallmodules = xmlrpclib.ServerProxy('%s:%s/xmlrpc/object' % (addr, port)).execute(
dbname, 1, 'admin', 'ir.module.module', 'search', [('state', '=', 'to install')]
if toinstallmodules:
logging.error("Package test: FAILED. Not able to install dependencies of base.")
raise Exception("Installation of package failed")
else:"Package test: successfuly installed %s modules" % len(modules))
logging.error("Package test: FAILED. Not able to install base.")
raise Exception("Installation of package failed")
def publish(o, type, extensions):
def _publish(o, release):
arch = ''
filename = release.split(os.path.sep)[-1]
release_dir = PUBLISH_DIRS[type]
release_path = join(, release_dir, filename)
system('mkdir -p %s' % join(, release_dir))
shutil.move(join(o.build_dir, release), release_path)
# Latest/symlink handler
release_abspath = abspath(release_path)
latest_abspath = release_abspath.replace(timestamp, 'latest')
if os.path.islink(latest_abspath):
os.symlink(release_abspath, latest_abspath)
return release_path
published = []
for extension in extensions:
release = glob("%s/odoo_*.%s" % (o.build_dir, extension))
if release:
published.append(_publish(o, release[0]))
return published
class OdooDocker(object):
def __init__(self):
self.log_file = NamedTemporaryFile(mode='w+b', prefix="bash", suffix=".txt", delete=False)
self.port = 8069 # TODO sle: reliable way to get a free port?
self.prompt_re = '[root@nightly-tests] # '
self.timeout = 600
def system(self, command):
def start(self, docker_image, build_dir, pub_dir):
self.build_dir = build_dir
self.pub_dir = pub_dir
self.docker = pexpect.spawn(
'docker run -v %s:/opt/release -p'
' -t -i %s /bin/bash --noediting' % (self.build_dir, self.port, docker_image),
searchwindowsize=len(self.prompt_re) + 1,
time.sleep(2) # let the bash start
self.docker.logfile_read = self.log_file = subprocess.check_output('docker ps -l -q', shell=True).strip().decode('ascii')
def end(self):
except Exception as e:
logging.error('Exception during docker execution: %s:' % str(e))
logging.error('Error during docker execution: printing the bash output:')
with open( as f:
print('\n'.join(f.readlines()), file=stderr)
system('docker rm -f %s' %
def docker(docker_image, build_dir, pub_dir):
_docker = OdooDocker()
_docker.start(docker_image, build_dir, pub_dir)
yield _docker
except Exception:
class KVM(object):
def __init__(self, o, image, ssh_key='', login='openerp'):
self.o = o
self.image = image
self.ssh_key = ssh_key
self.login = login
def timeout(self,signum,frame):
logging.warning("vm timeout kill (pid: {})".format(
def start(self):
l="kvm -cpu core2duo -smp 2,sockets=2,cores=1,threads=1 -net nic,model=rtl8139 -net user,hostfwd=tcp:,hostfwd=tcp:,hostfwd=tcp: -m 1024 -drive".split(" ")
l.append('-nographic')"Starting kvm: {}".format( " ".join(l))), l[0], l)
signal.signal(signal.SIGALRM, self.timeout)
signal.signal(signal.SIGALRM, signal.SIG_DFL)
def ssh(self,cmd):
def rsync(self,args,options='--delete --exclude .git --exclude .tx --exclude __pycache__'):
cmd ='rsync -rt -e "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -p 10022 -i %s" %s %s' % (self.ssh_key, options, args)
def run(self):
class KVMWinBuildExe(KVM):
def run(self):
with open(join(self.o.build_dir, 'setup/win32/Makefile.version'), 'w') as f:
f.write("VERSION=%s\n" % version.replace('~', '_').replace('+', ''))
with open(join(self.o.build_dir, 'setup/win32/Makefile.python'), 'w') as f:
f.write("PYTHON_VERSION=%s\n" % self.o.vm_winxp_python_version.replace('.', ''))
with open(join(self.o.build_dir, 'setup/win32/Makefile.servicename'), 'w') as f:
f.write("SERVICENAME=%s\n" % nt_service_name)
remote_build_dir = '/cygdrive/c/odoobuild/server/'
self.ssh("mkdir -p build")"Syncing Odoo files to virtual machine...")
self.rsync('%s/ %s@' % (self.o.build_dir, self.login, remote_build_dir))
self.ssh("cd {}setup/win32;time make allinone;".format(remote_build_dir))
self.rsync('%s@ %s/' % (self.login, remote_build_dir, self.o.build_dir), '')" done")
class KVMWinTestExe(KVM):
def run(self):
setuppath = glob("%s/openerp-server-setup-*.exe" % self.o.build_dir)[0]
setupfile = setuppath.split('/')[-1]
setupversion = setupfile.split('openerp-server-setup-')[1].split('.exe')[0]
self.rsync('"%s" %s@' % (setuppath, self.login))
self.ssh("TEMP=/tmp ./%s /S" % setupfile)
self.ssh('PGPASSWORD=openpgpwd /cygdrive/c/"Program Files"/"Odoo %s"/PostgreSQL/bin/createdb.exe -e -U openpg mycompany' % setupversion)
self.ssh('netsh advfirewall set publicprofile state off')
self.ssh('/cygdrive/c/"Program Files"/"Odoo {sv}"/python/python.exe \'c:\\Program Files\\Odoo {sv}\\server\\odoo-bin\' -d mycompany -i base --stop-after-init'.format(sv=setupversion))
# Stage: building
def _prepare_build_dir(o, win32=False):
cmd = ['rsync', '-a', '--exclude', '.git', '--exclude', '*.pyc', '--exclude', '*.pyo']
if not win32:
cmd += ['--exclude', 'setup/win32']
system(cmd + ['%s/' % o.odoo_dir, o.build_dir])
for addon_path in glob(join(o.build_dir, 'addons/*')):
if addon_path.split(os.path.sep)[-1] not in ADDONS_NOT_TO_PUBLISH:
shutil.move(addon_path, join(o.build_dir, 'odoo/addons'))
except shutil.Error as e:
# Thrown when the add-on is already in odoo/addons (if _prepare_build_dir
# has already been called once)
logging.warning("Warning '{}' while moving addon '{}'".format(e,addon_path))
if addon_path.startswith(o.build_dir) and os.path.isdir(addon_path):"Removing '{}'".format(addon_path))
except shutil.Error as rm_error:
logging.warning("Cannot remove '{}': {}".format(addon_path, rm_error))
def build_tgz(o):
system(['python3', '', 'sdist', '--quiet', '--formats=gztar,zip'], o.build_dir)
system(['mv', glob('%s/dist/odoo-*.tar.gz' % o.build_dir)[0], '%s/odoo_%s.%s.tar.gz' % (o.build_dir, version, timestamp)])
system(['mv', glob('%s/dist/odoo-*.zip' % o.build_dir)[0], '%s/' % (o.build_dir, version, timestamp)])
def build_deb(o):
# Append timestamp to version for the .dsc to refer the right .tar.gz
cmd=['sed', '-i', '1s/^.*$/odoo (%s.%s) stable; urgency=low/'%(version,timestamp), 'debian/changelog'], cwd=o.build_dir)
if not o.no_debsign:
deb = pexpect.spawn('dpkg-buildpackage -rfakeroot -k%s' % GPGID, cwd=o.build_dir)
deb.logfile = stdout.buffer
deb.expect_exact('Enter passphrase: ', timeout=1200)
deb.send(GPGPASSPHRASE + '\r\n')
deb.expect_exact('Enter passphrase: ')
deb.send(GPGPASSPHRASE + '\r\n')
deb.expect(pexpect.EOF, timeout=1200)
else:['dpkg-buildpackage', '-rfakeroot', '-uc', '-us'], cwd=o.build_dir)
# As the packages are built in the parent of the buildir, we move them back to build_dir
build_dir_parent = '{}/../'.format(o.build_dir)
wildcards = ['odoo_{}'.format(wc) for wc in ('*.deb', '*.dsc', '*_amd64.changes', '*.tar.gz', '*.tar.xz')]
move_glob(build_dir_parent, wildcards, o.build_dir)
def build_rpm(o):
system(['python3', '', '--quiet', 'bdist_rpm'], o.build_dir)
system(['mv', glob('%s/dist/odoo-*.noarch.rpm' % o.build_dir)[0], '%s/odoo_%s.%s.noarch.rpm' % (o.build_dir, version, timestamp)])
def build_exe(o):
KVMWinBuildExe(o, o.vm_winxp_image, o.vm_winxp_ssh_key, o.vm_winxp_login).start()
system(['cp', glob('%s/openerp*.exe' % o.build_dir)[0], '%s/odoo_%s.%s.exe' % (o.build_dir, version, timestamp)])
# Stage: testing
def _prepare_testing(o):'Preparing testing')
if not o.no_tarball:'Preparing docker container instance for tarball')["mkdir", "docker_src"], cwd=o.build_dir)["cp", "package.dfsrc", os.path.join(o.build_dir, "docker_src", "Dockerfile")],
cwd=os.path.join(o.odoo_dir, "setup"))
# Use rsync to copy requirements.txt in order to keep original permissions["rsync", "-a", "requirements.txt", os.path.join(o.build_dir, "docker_src")],
cwd=os.path.join(o.odoo_dir))["docker", "build", "-t", "odoo-%s-src-nightly-tests" % docker_version, "."],
cwd=os.path.join(o.build_dir, "docker_src"))
if not o.no_debian:'Preparing docker container instance for debian')["mkdir", "docker_debian"], cwd=o.build_dir)["cp", "package.dfdebian", os.path.join(o.build_dir, "docker_debian", "Dockerfile")],
cwd=os.path.join(o.odoo_dir, "setup"))
# Use rsync to copy requirements.txt in order to keep original permissions["rsync", "-a", "requirements.txt", os.path.join(o.build_dir, "docker_debian")],
cwd=os.path.join(o.odoo_dir))["docker", "build", "-t", "odoo-%s-debian-nightly-tests" % docker_version, "."],
cwd=os.path.join(o.build_dir, "docker_debian"))
if not o.no_rpm:'Preparing docker container instance for RPM')["mkdir", "docker_fedora"], cwd=o.build_dir)["cp", "package.dffedora", os.path.join(o.build_dir, "docker_fedora", "Dockerfile")],
cwd=os.path.join(o.odoo_dir, "setup"))["docker", "build", "-t", "odoo-%s-fedora-nightly-tests" % docker_version, "."],
cwd=os.path.join(o.build_dir, "docker_fedora"))
def test_tgz(o):'Testing tarball in docker')
with docker('odoo-%s-src-nightly-tests' % docker_version, o.build_dir, as wheezy:
wheezy.release = '*.tar.gz'
wheezy.system("service postgresql start")
wheezy.system('pip3 install /opt/release/%s' % wheezy.release)
wheezy.system("useradd --system --no-create-home odoo")
wheezy.system('su postgres -s /bin/bash -c "createuser -s odoo"')
wheezy.system('su postgres -s /bin/bash -c "createdb mycompany"')
wheezy.system('mkdir /var/lib/odoo')
wheezy.system('chown odoo:odoo /var/lib/odoo')
wheezy.system('su odoo -s /bin/bash -c "odoo -d mycompany -i base --stop-after-init"')
wheezy.system('su odoo -s /bin/bash -c "odoo -d mycompany &"')
def test_deb(o):'Testing deb package in docker')
with docker('odoo-%s-debian-nightly-tests' % docker_version, o.build_dir, as wheezy:
wheezy.release = '*.deb'
wheezy.system("service postgresql start")
wheezy.system('su postgres -s /bin/bash -c "createdb mycompany"')
wheezy.system('/usr/bin/dpkg -i /opt/release/%s' % wheezy.release)
wheezy.system('/usr/bin/apt-get install -f -y')
wheezy.system('su odoo -s /bin/bash -c "odoo -c /etc/odoo/odoo.conf -d mycompany -i base --stop-after-init"')
wheezy.system('su odoo -s /bin/bash -c "odoo -c /etc/odoo/odoo.conf -d mycompany &"')
def test_rpm(o):'Testing rpm in docker')
with docker('odoo-%s-fedora-nightly-tests' % docker_version, o.build_dir, as fedora24:
fedora24.release = '*.noarch.rpm'
# Start postgresql
fedora24.system('su postgres -c "/usr/bin/pg_ctl -D /var/lib/postgres/data start"')
fedora24.system('sleep 5')
fedora24.system('su postgres -c "createdb mycompany"')
# Odoo install
fedora24.system('dnf install -d 0 -e 0 /opt/release/%s -y' % fedora24.release)
fedora24.system('su odoo -s /bin/bash -c "odoo -c /etc/odoo/odoo.conf -d mycompany -i base --stop-after-init"')
fedora24.system('su odoo -s /bin/bash -c "odoo -c /etc/odoo/odoo.conf -d mycompany &"')
def test_exe(o):'Testng windows installer in KVM')
KVMWinTestExe(o, o.vm_winxp_image, o.vm_winxp_ssh_key, o.vm_winxp_login).start()
# Generates Packages, Sources and Release files of debian package
def gen_deb_package(o, published_files):
# Executes command to produce file_name in path, and moves it to
def _gen_file(o, command, file_name, path):
cur_tmp_file_path = os.path.join(path, file_name)
with open(cur_tmp_file_path, 'w') as out:, stdout=out, cwd=path)
system(['cp', cur_tmp_file_path, os.path.join(, 'deb', file_name)])
# Copy files to a temp directory (required because the working directory must contain only the
# files of the last release)
temp_path = tempfile.mkdtemp(suffix='debPackages')
for pub_file_path in published_files:
system(['cp', pub_file_path, temp_path])
commands = [
(['dpkg-scanpackages', '.'], "Packages"), # Generate Packages file
(['dpkg-scansources', '.'], "Sources"), # Generate Sources file
(['apt-ftparchive', 'release', '.'], "Release") # Generate Release file
# Generate files
for command in commands:
_gen_file(o, command[0], command[-1], temp_path)
# Remove temp directory
if not o.no_debsign:
# Generate Release.gpg (= signed Release)
# Options -abs: -a (Create ASCII armored output), -b (Make a detach signature), -s (Make a signature)['gpg', '--default-key', GPGID, '--passphrase', GPGPASSPHRASE, '--yes', '-abs', '--no-tty', '-o', 'Release.gpg', 'Release'], cwd=os.path.join(, 'deb'))
# Generates an RPM repo
def gen_rpm_repo(o, file_name):
# Sign the RPM
rpmsign = pexpect.spawn('/bin/bash', ['-c', 'rpm --resign %s' % file_name], cwd=os.path.join(, 'rpm'))
rpmsign.expect_exact('Enter pass phrase: ')
rpmsign.send(GPGPASSPHRASE + '\r\n')
# Removes the old repodata['rm', '-rf', os.path.join(, 'rpm', 'repodata')])
# Copy files to a temp directory (required because the working directory must contain only the
# files of the last release)
temp_path = tempfile.mkdtemp(suffix='rpmPackages')['cp', file_name, temp_path])['createrepo', temp_path]) # creates a repodata folder in temp_path['cp', '-r', os.path.join(temp_path, "repodata"), os.path.join(, 'rpm')])
# Remove temp directory
# Options and Main
def options():
op = optparse.OptionParser()
root = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
build_dir = "%s-%s" % (root, timestamp)
log_levels = { "debug" : logging.DEBUG, "info": logging.INFO, "warning": logging.WARN, "error": logging.ERROR, "critical": logging.CRITICAL }
op.add_option("-b", "--build-dir", default=build_dir, help="build directory (%default)", metavar="DIR")
op.add_option("-p", "--pub", default=None, help="pub directory (%default)", metavar="DIR")
op.add_option("", "--no-testing", action="store_true", help="don't test the built packages")
op.add_option("", "--no-debian", action="store_true", help="don't build the debian package")
op.add_option("", "--no-debsign", action="store_true", help="don't sign the debian package")
op.add_option("", "--no-rpm", action="store_true", help="don't build the rpm package")
op.add_option("", "--no-tarball", action="store_true", help="don't build the tarball")
op.add_option("", "--no-windows", action="store_true", help="don't build the windows package")
# Windows VM
op.add_option("", "--vm-winxp-image", default='/home/odoo/vm/win1036/win10_winpy36.qcow2', help="%default")
op.add_option("", "--vm-winxp-ssh-key", default='/home/odoo/vm/win1036/id_rsa', help="%default")
op.add_option("", "--vm-winxp-login", default='Naresh', help="Windows login (%default)")
op.add_option("", "--vm-winxp-python-version", default='3.6', help="Windows Python version installed in the VM (default: %default)")
op.add_option("", "--no-remove", action="store_true", help="don't remove build dir")
op.add_option("", "--logging", action="store", type="choice", choices=list(log_levels.keys()), default="info", help="Logging level")
(o, args) = op.parse_args()
logging.basicConfig(format='%(asctime)s %(levelname)s: %(message)s', datefmt='%Y-%m-%d %I:%M:%S', level=log_levels[o.logging])
# derive other options
o.odoo_dir = root
o.pkg = join(o.build_dir, 'pkg') = join(o.build_dir, 'openerp-%s' % version)
o.work_addons = join(, 'odoo', 'addons')
return o
def main():
o = options()
if not o.no_testing:
if not o.no_tarball:
if not o.no_testing:
published_files = publish(o, 'tarball', ['tar.gz', 'zip'])
except Exception as e:
logging.error("Won't publish the tgz release.\n Exception: %s" % str(e))
if not o.no_debian:
if not o.no_testing:
published_files = publish(o, 'debian', ['deb', 'dsc', 'changes', 'tar.xz'])
gen_deb_package(o, published_files)
except Exception as e:
logging.error("Won't publish the deb release.\n Exception: %s" % str(e))
if not o.no_rpm:
if not o.no_testing:
published_files = publish(o, 'redhat', ['noarch.rpm'])
gen_rpm_repo(o, published_files[0])
except Exception as e:
logging.error("Won't publish the rpm release.\n Exception: %s" % str(e))
if not o.no_windows:
_prepare_build_dir(o, win32=True)
if not o.no_testing:
published_files = publish(o, 'windows', ['exe'])
except Exception as e:
logging.error("Won't publish the exe release.\n Exception: %s" % str(e))
except Exception as e:
logging.error('Something bad happened ! : {}'.format(e))
if o.no_remove:'Build dir "{}" not removed'.format(o.build_dir))
shutil.rmtree(o.build_dir)'Build dir %s removed' % o.build_dir)
if not o.no_testing and not (o.no_debian and o.no_rpm and o.no_tarball):
system("docker rm -f `docker ps -a | awk '{print $1 }'` 2>>/dev/null")'Remaining dockers removed')
if __name__ == '__main__':