# Copyright 2016 Lars Wirzenius # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU 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 General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # # =*= License: GPL-3+ =*= import time import urlparse import icklib class CreateBuildInfo(icklib.BuildStep): def build(self): # Only store a build info if we're doing a build. if not self.going_to_build(self.run_state): return build_infos = self.statedir.get_build_infos(self.project.name) build_number = self.get_next_build_number(self.run_state) self.run_state.project_info.save() # Don't want to lose this if crash. self.run_state.build_info = self.statedir.create_new_build( self.project.name, build_number) self.run_state.build_info.status = 'RUNNING' self.pivot_to_real_build_log_file(self.run_state) # Fill in some fields from previous build, if any. if build_infos: for field in ['commit']: if hasattr(build_infos[-1], field): value = getattr(build_infos[-1], field) setattr(self.run_state.build_info, field, value) # Copy in new values from run_state. for field in ['commits']: if hasattr(self.run_state, field): value = getattr(self.run_state, field) if value is not None: setattr(self.run_state.build_info, field, value) self.run_state.build_info.pipeline = self.pipeline.name self.run_state.build_info.save() def going_to_build(self, run_state): names = [ 'build_using_shell', 'build_using_local_shell', 'build_using_debian_ci', 'build_tags_using_debian_release', ] return any(getattr(run_state, x, False) for x in names) def get_next_build_number(self, run_state): pi = run_state.project_info if pi.next_build_number is None: pi.next_build_number = 1 result = pi.next_build_number pi.next_build_number += 1 return result def pivot_to_real_build_log_file(self, run_state): filename = run_state.build_info.get_build_log_filename() fileurl = urlparse.urljoin('file://', filename) run_state.logger.important('Build log at {location}', location=fileurl) f = open(filename, 'w') f.write(run_state.log_catcher.getvalue()) run_state.logger.drop_output_file(run_state.log_catcher) run_state.logger.add_output_file(f, False) class FinishBuildInfo(icklib.BuildStep): def build(self): if self.run_state.build_info is not None: self.run_state.build_info.status = 'SUCCESS' self.run_state.build_info.duration = ( time.time() - self.run_state.started) self.run_state.build_info.save()