From e955d4be35da30977a7cc67131068f47b1739363 Mon Sep 17 00:00:00 2001 From: Mathias Klette Date: Fri, 3 May 2013 13:48:45 +0200 Subject: [PATCH] bugfixing --- common_code.py | 1 + debian_build.py | 97 ++++++++++++++++++++++--------------------- lib/dput.py | 108 ++++++++++++++++++++---------------------------- 3 files changed, 96 insertions(+), 110 deletions(-) diff --git a/common_code.py b/common_code.py index 0806432..48f7735 100755 --- a/common_code.py +++ b/common_code.py @@ -5,6 +5,7 @@ import datetime import logging import os import platform +import subprocess import sys from logging import Formatter diff --git a/debian_build.py b/debian_build.py index 15ce014..d3524ca 100755 --- a/debian_build.py +++ b/debian_build.py @@ -116,7 +116,7 @@ if __name__ == '__main__': do_autoincrement = False do_cidb = False do_cleanup = True - do_liveboot_request = True + do_liveboot_request = False do_reports = True do_tagging = False do_uploads = True @@ -126,6 +126,7 @@ if __name__ == '__main__': gitrepo = git.Repo('.') local_branch = ENV['GIT_BRANCH'] remote_branch = os.path.join('origin', ENV['GIT_BRANCH']) + reports_file = os.path.join(ENV['WORKSPACE'], '../build-area/result/reports.tgz',) # .. dput related (some overrides happening below, though) dput_obj = dput.Dput( @@ -144,12 +145,11 @@ if __name__ == '__main__': # .. changelog related daily_date = BUILD_START.strftime('%Y%m%d%H%M%S') - fh = open('debian/changelog') - cl = changlog(fh) + cl = changelog.Changelog() + cl.parse_changelog(open('debian/changelog')) curr_name = cl.package curr_version = cl.full_version curr_dist = cl.distributions - fh.close() logger.info( 'Current Changelog: Package {name} with version {version} in distribution {dist}'.format( name = curr_name, @@ -175,6 +175,7 @@ if __name__ == '__main__': # reset actions # .. always include successful build packages into CIDB do_cidb = True + do_liveboot_request = True # .. only take care of changelog automation if we want it to if ENV['GIT_BRANCH'] in AUTO_CHANGELOG_REPONAMES: do_autoincrement = True @@ -221,12 +222,12 @@ if __name__ == '__main__': #FIXME: enable this once Florian acks. #if GIT_REPO_PATH.startswith('/srv/git/dev/'): # if ENV['GIT_BRANCH_NAME'] == 'pre-staging': - # version = '%s~develop%s+%s+%s' %(version, daily_date, BUILD_NUMBER, current_commit[0:7]) + # version = '%s~develop%s+%s+%s' %(version, daily_date, BUILD_NUMBER, curr_commit[0:7]) # changelog_distro = 'pre-staging' # else: # # use shorter date because we can # daily_date = datetime.datetime.now().strftime('%Y%m%d') - # version = '%s~alpha%s+%s+%s' %(version, daily_date, BUILD_NUMBER, current_commit[0:7]) + # version = '%s~alpha%s+%s+%s' %(version, daily_date, BUILD_NUMBER, curr_commit[0:7]) # changelog_distro = 'unstable' # .. just used for GitBuildPackage() @@ -236,13 +237,13 @@ if __name__ == '__main__': # ACT III: do something actually # .. do some housekeeping first if not do_cleanup: - figlet('Skip cleanup') + figlet('Cleanup skipped') else: # .. reset local repository at first logger.info('Reset and clean repository.') gitrepo.git.fetch('--force','--prune').strip() gitrepo.git.reset('--hard','HEAD').strip() - gitrepo.git.execute('sudo','git','clean','-fdx').strip() + gitrepo.git.execute(['sudo','git','clean','-fdx']).strip() gitrepo.git.branch('-D','%s' %(local_branch),with_exceptions=False).strip() # .. and re-checkout the requested branch @@ -261,24 +262,23 @@ if __name__ == '__main__': )] if len(files) > 0: - logger.debug('Previous upstream tarball(s) found: %s' %(files)) + logger.debug('Delete previous upstream tarball(s)') cmd = [BIN_SUDO, BIN_RM, '-v'] cmd.extend(files) - logger.debug('Executing "%s" ...' %(' '.join(cmd))) - cmdobj = subprocess.Popen( - cmd, - shell=False, - cwd='/', - close_fds=True, - stdout=subprocess.PIPE, - stderr=subprocess.STDOUT, - env={'':''} - ).wait() - - for line in cmdobj.stdout.readlines(): - logger.debug('%s' %(line.strip())) - - logger.debug('Returned with status %d' %(cmdobj.returncode)) + ret = subprocess.Popen( + cmd, + shell=False, + cwd='/', + close_fds=True, + stdout=sys.stdout, + stderr=subprocess.STDOUT, + env={'':''} + ).wait() + logger.debug('"{cmd}" returns {exitcode}.'.format( + cmd = cmd, + exitcode = ret + )) + figlet('Cleanup OK') # .. update changelog if we trust the package if do_autoincrement: @@ -302,7 +302,7 @@ if __name__ == '__main__': '--no-auto-nmu', '--', 'Generated by Jenkins build of commit {commit}'.format( - commit = curr_commit + commit = curr_commit[0:7] ), ] @@ -345,7 +345,7 @@ if __name__ == '__main__': dist = 'squeeze', arch = 'amd64', pb_suite = pb_suite, - git_commit_id = current_commit[0:7] + git_commit_id = curr_commit[0:7] ) logger.info('Building ...') ret = gbp.build() @@ -355,10 +355,12 @@ if __name__ == '__main__': # .. now handle the result if ret: logger.error( - 'git-buildpackage returned non-zero. exitcode was: %s' % ret) + 'git-buildpackage failed with exitcode %d' % ret) + figlet('Build failed') exit_error() else: logger.debug('git-buildpackage executed successfully') + figlet('Build OK') # ACT V: post-build actions @@ -368,21 +370,19 @@ if __name__ == '__main__': # if reports.tgz exists untar it to workspace and delete it. # reports.tgz is generated by /root/.pbuilder/hooks.d/B01-test if not do_reports: - figlet('Skip reports') + figlet('Reports skipped') else: try: - reports_file = os.path.join( - ENV['WORKSPACE'], - '../build-area/result/reports.tgz', - ) - subprocess.check_call(['/bin/tar', 'xzvf', '-C', ENV['WORKSPACE'], reports_file]) - subprocess.check_call(['/usr/bin/sudo' , '/bin/rm', reports_file]) + if os.path.exists(reports_file): + subprocess.check_call(['/bin/tar', 'xzvf', '-C', ENV['WORKSPACE'], reports_file]) + subprocess.check_call(['/usr/bin/sudo' , '/bin/rm', reports_file]) + figlet('Reports OK') except: pass # .. let's tag if not do_tagging: - figlet('Skip tagging') + figlet('Tagging skipped') else: try: gitrepo.git.tag(version_tag) @@ -397,38 +397,41 @@ if __name__ == '__main__': exit_error() logger.info('Tagged as "%s".' % version_tag) + figlet('Tagging OK') # .. and upload - if not do_upload: - figlet('Skip upload') + if not do_uploads: + figlet('Upload skipped') else: try: dput_obj.configure() # strip epoch - if ":" in version: - version = version.split(":", 1)[1] + if ":" in new_version: + new_version = new_version.split(":", 1)[1] # construct path for changes file changes_file = os.path.join( ENV['WORKSPACE'], '../build-area/result/', '{name}_{version}_amd64.changes'.format( - name = new_name, + name = curr_name, version = new_version, ) ) # display changes file fh = open(changes_file, 'r') - logger.info('%s' % (fh.read())) + logger.info('Contents of %s\n%s' %(changes_file, fh.read())) fh.close() # upload changes file dput_obj.upload(changes_file) + figlet('Upload OK') + except Exception as error: - figlet('upload failed:') + figlet('Upload failed') logger.exception(error) @@ -437,7 +440,7 @@ if __name__ == '__main__': # .. and add all the records to CIDB if not do_cidb: - figlet('Skip CIDB') + figlet('CIDB skipped') else: package_instances=[] try: @@ -446,22 +449,24 @@ if __name__ == '__main__': ENV['JOB_NAME'], ENV['BUILD_NUMBER'], changes_file, - version, + new_version, BUILD_START, BUILD_END, ) + figlet('CIDB OK') except Exception as error: - figlet('CIDB failed:') + figlet('CIDB failed') logger.error("package instance not added to DB: ", exc_info=error) exit_error() # .. finally trigger the next liveboot automatically if we succeed with CIDB if not do_liveboot_request: - figlet('Skip Liveboot Request') + figlet('Liveboot skipped') else: if len(package_instances) > 0: try: add_liveboot_request(package_instances) + figlet('Liveboot OK') except: logger.debug("liveboot request failed") diff --git a/lib/dput.py b/lib/dput.py index c3999aa..472f0f6 100644 --- a/lib/dput.py +++ b/lib/dput.py @@ -1,6 +1,15 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- +# import standards +import os +import subprocess +import sys +from ConfigParser import SafeConfigParser + +# define constants +BIN_DPUT = '/usr/bin/dput' + class Dput(object): ''' TODO @@ -39,74 +48,45 @@ class Dput(object): def contents(self,value): self._contents = value - def configure( - self, - config = self.config, - section = self.section, - contents = self.contents, - ): - try: - parser = SafeConfigParser() - parser.add_section(section) - for key, value in contents.iteritems(): - parser.set(section, str(key), str(value)) - - filehandler = open(config, 'w') - config.write(filehandler) - filehandler.close() + def configure(self,): + parser = SafeConfigParser() + parser.add_section(self.section) + for key, value in self.contents.iteritems(): + parser.set(self.section, str(key), str(value)) - logger.info('Dput configuration successfully created.') - return True + filehandler = open(self.config, 'w') + parser.write(filehandler) + filehandler.close() - except Exception as error: - logger.error('Failed to configure dput: %s',exc_info=error) + return True - return False + def upload(self,changes_file=None,): - def upload( - self, - config = self.config, - files = [], - ): - - if len(files) == 0: - logger.debug('Please specify at least one file to upload.') - return False - - if type(files) == type(str): - files = files.spit() - - try: - cmd = [BIN_DPUT, - '-c', - '%s' %(config), - '--no-upload-log', - 'profitbricks', - '%s' %(' '.join(files)), - ] - - logger.debug('Executing "%s" ...' %(cmd)) - result = subprocess.Popen( - cmd, - shell=False, - stdout=sys.stdout, - stderr=subprocess.STDOUT, - close_fds=True, - cwd=os.path.dirname(config) - ).wait() - logger.debug('Returned with status %d' %(result)) - - if result != 0: - raise Exception('{cmd} failed with status code {code}'.format( - cmd = ' '.join(cmd), - code = result, - )) - - logger.info('Dput upload successfully completed.') + if changes_file == None: return True - except Exception as error: - logger.error('Failed to upload files: %s',exc_info=error)) - - return False + cmd = [BIN_DPUT, + '-c', + '%s' %(self.config), + '--no-upload-log', + 'profitbricks', + '%s' %(changes_file), + ] + + result = subprocess.Popen( + cmd, + shell=False, + stdout=sys.stdout, + stderr=subprocess.STDOUT, + close_fds=True, + cwd=os.path.dirname(self.config) + ).wait() + + if result != 0: + raise Exception('{cmd} failed with status code {code}'.format( + cmd = ' '.join(cmd), + code = result, + )) + + return True -- 2.39.5