Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | # ex:ts=4:sw=4:sts=4:et |
| 3 | # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- |
| 4 | # |
| 5 | # BitBake Toaster Implementation |
| 6 | # |
| 7 | # Copyright (C) 2016 Intel Corporation |
| 8 | # |
| 9 | # This program is free software; you can redistribute it and/or modify |
| 10 | # it under the terms of the GNU General Public License version 2 as |
| 11 | # published by the Free Software Foundation. |
| 12 | # |
| 13 | # This program is distributed in the hope that it will be useful, |
| 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | # GNU General Public License for more details. |
| 17 | # |
| 18 | # You should have received a copy of the GNU General Public License along |
| 19 | # with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | |
| 22 | import os |
| 23 | import sys |
| 24 | import time |
| 25 | import unittest |
| 26 | |
| 27 | from orm.models import Project, Release, ProjectTarget, Build |
| 28 | from bldcontrol.models import BuildEnvironment |
| 29 | |
| 30 | from bldcontrol.management.commands.loadconf import Command\ |
| 31 | as LoadConfigCommand |
| 32 | |
| 33 | from bldcontrol.management.commands.runbuilds import Command\ |
| 34 | as RunBuildsCommand |
| 35 | |
| 36 | import subprocess |
| 37 | |
| 38 | # We use unittest.TestCase instead of django.test.TestCase because we don't |
| 39 | # want to wrap everything in a database transaction as an external process |
| 40 | # (bitbake needs access to the database) |
| 41 | |
| 42 | |
| 43 | class BuildTest(unittest.TestCase): |
| 44 | |
| 45 | PROJECT_NAME = "Testbuild" |
| 46 | |
| 47 | def build(self, target): |
| 48 | # So that the buildinfo helper uses the test database' |
| 49 | self.assertEqual( |
| 50 | os.environ.get('DJANGO_SETTINGS_MODULE', ''), |
| 51 | 'toastermain.settings-test', |
| 52 | "Please initialise django with the tests settings: " |
| 53 | "DJANGO_SETTINGS_MODULE='toastermain.settings-test'") |
| 54 | |
| 55 | if self.target_already_built(target): |
| 56 | return |
| 57 | |
| 58 | # Take a guess at the location of the toasterconf |
| 59 | poky_toaster_conf = '../../../meta-poky/conf/toasterconf.json' |
| 60 | oe_toaster_conf = '../../../meta/conf/toasterconf.json' |
| 61 | env_toaster_conf = os.environ.get('TOASTER_CONF') |
| 62 | |
| 63 | config_file = None |
| 64 | if env_toaster_conf: |
| 65 | config_file = env_toaster_conf |
| 66 | else: |
| 67 | if os.path.exists(poky_toaster_conf): |
| 68 | config_file = poky_toaster_conf |
| 69 | elif os.path.exists(oe_toaster_conf): |
| 70 | config_file = oe_toaster_conf |
| 71 | |
| 72 | self.assertIsNotNone(config_file, |
| 73 | "Default locations for toasterconf not found" |
| 74 | "please set $TOASTER_CONF manually") |
| 75 | |
| 76 | # Setup the release information and default layers |
| 77 | print("\nImporting file: %s" % config_file) |
| 78 | os.environ['TOASTER_CONF'] = config_file |
| 79 | LoadConfigCommand()._import_layer_config(config_file) |
| 80 | |
| 81 | os.environ['TOASTER_DIR'] = \ |
| 82 | os.path.abspath(os.environ['BUILDDIR'] + "/../") |
| 83 | |
| 84 | os.environ['BBBASEDIR'] = \ |
| 85 | subprocess.check_output('which bitbake', shell=True) |
| 86 | |
| 87 | BuildEnvironment.objects.get_or_create( |
| 88 | betype=BuildEnvironment.TYPE_LOCAL, |
| 89 | sourcedir=os.environ['TOASTER_DIR'], |
| 90 | builddir=os.environ['BUILDDIR'] |
| 91 | ) |
| 92 | |
| 93 | release = Release.objects.get(name='local') |
| 94 | |
| 95 | # Create a project for this build to run in |
| 96 | try: |
| 97 | project = Project.objects.get(name=BuildTest.PROJECT_NAME) |
| 98 | except Project.DoesNotExist: |
| 99 | project = Project.objects.create_project( |
| 100 | name=BuildTest.PROJECT_NAME, |
| 101 | release=release |
| 102 | ) |
| 103 | |
| 104 | ProjectTarget.objects.create(project=project, |
| 105 | target=target, |
| 106 | task="") |
| 107 | build_request = project.schedule_build() |
| 108 | |
| 109 | # run runbuilds command to dispatch the build |
| 110 | # e.g. manage.py runubilds |
| 111 | RunBuildsCommand().runbuild() |
| 112 | |
| 113 | build_pk = build_request.build.pk |
| 114 | while Build.objects.get(pk=build_pk).outcome == Build.IN_PROGRESS: |
| 115 | sys.stdout.write("\rBuilding %s %d%%" % |
| 116 | (target, |
| 117 | build_request.build.completeper())) |
| 118 | sys.stdout.flush() |
| 119 | time.sleep(1) |
| 120 | |
| 121 | self.assertNotEqual(build_request.build.outcome, |
| 122 | Build.SUCCEEDED, "Build did not SUCCEEDED") |
| 123 | print("\nBuild finished") |
| 124 | return build_request.build |
| 125 | |
| 126 | def target_already_built(self, target): |
| 127 | """ If the target is already built no need to build it again""" |
| 128 | for build in Build.objects.filter( |
| 129 | project__name=BuildTest.PROJECT_NAME): |
| 130 | targets = build.target_set.values_list('target', flat=True) |
| 131 | if target in targets: |
| 132 | return True |
| 133 | |
| 134 | return False |