From: Tobias von der Krone Date: Tue, 28 May 2013 12:28:14 +0000 (+0200) Subject: * debian_build.py: pep8 compliance X-Git-Url: https://git.uhu-banane.de/?a=commitdiff_plain;h=c15d3b728375f6232ba79d2b84533af9fdbde729;p=profitbricks%2Fjenkins-build-scripts.git * debian_build.py: pep8 compliance * debian_build.py: use more STRING.format() --- diff --git a/debian_build.py b/debian_build.py index f3f1b2f..1a5b33d 100755 --- a/debian_build.py +++ b/debian_build.py @@ -31,8 +31,8 @@ from lib import git_helper logger = logger_init(__file__) # jenkins environment - parameters -ENV.setdefault('NO_UPLOAD','') -#ENV.setdefault('DISTRIBUTION','squeeze') +ENV.setdefault('NO_UPLOAD', '') +#ENV.setdefault('DISTRIBUTION', 'squeeze') #GIT_REPO_PATH = ENV['GIT_REPO_PATH'] # local constants @@ -47,7 +47,7 @@ AUTO_CHANGELOG_REPONAMES = ( STABLE_DISTRIBUTIONS = ( 'stable', 'production', - 'production-proposed-updates' + 'production-proposed-updates', ) STABLE_BRANCHES_RE = re.compile( '^(origin/)?(master|hotfix/.*)$' @@ -59,7 +59,9 @@ EXPERIMENTAL_BRANCHES_RE = re.compile( '^(origin/)?(experimental|integration|(feature|poc|bugfix)/.*)$' ) COMMIT_TRIGGER_BRANCHES_RE = re.compile( - '^(origin/)?(feature/.*)$') + '^(origin/)?(feature/.*)$' +) + def getopts(): usage = '%prog [options]' @@ -67,7 +69,7 @@ def getopts(): parser.add_option( '--distribution', dest='distribution', - choices = [ + choices=[ 'squeeze', 'wheezy', ], @@ -77,7 +79,7 @@ def getopts(): parser.add_option( '--loglevel', dest='loglevel', - choices = [ + choices=[ 'DEBUG', 'INFO', 'WARN', @@ -117,7 +119,7 @@ if __name__ == '__main__': daily_date = BUILD_START.strftime('%Y%m%d%H%M%S') # .. repository related - local_branch = re.match('^(origin/)?(.*)',ENV['GIT_BRANCH']).groups()[1] + local_branch = re.match('^(origin/)?(.*)', ENV['GIT_BRANCH']).groups()[1] remote_branch = os.path.join('origin', local_branch) reports_file = os.path.join(ENV['WORKSPACE'], '../build-area/result/reports.tgz',) gitrepo = git.Repo('.') @@ -133,14 +135,13 @@ if __name__ == '__main__': elif hasattr(curr_commit, 'id'): curr_commit_id = curr_commit.id - # Act II: make decissions # compatibility until call_jenkins was replaced - if curr_dist not in ('squeeze','wheezy'): + if curr_dist not in ('squeeze', 'wheezy'): logger.warn('Compatibility Mode: Change distribution {old} => {new}.'.format( - old = curr_dist, - new = 'squeeze' - )) + old=curr_dist, + new='squeeze' + )) curr_dist = 'squeeze' if re.match(COMMIT_TRIGGER_BRANCHES_RE, ENV['GIT_BRANCH']): @@ -148,9 +149,9 @@ if __name__ == '__main__': if re.match(STABLE_BRANCHES_RE, ENV['GIT_BRANCH']): if re.match('^(ps_|dev_).*$', ENV['JOB_NAME']): - logger.error('Use the appropriate job to build for branch "%s"!' %( - ENV['GIT_BRANCH'] - )) + logger.error('Use the appropriate job to build for branch "{branch}"!'.format( + branch=ENV['GIT_BRANCH'], + )) exit_error() new_dist = 'stable' new_version = curr_version @@ -183,17 +184,17 @@ if __name__ == '__main__': elif re.match(UNSTABLE_BRANCHES_RE, ENV['GIT_BRANCH']): if not re.match('^ps_.*$', ENV['JOB_NAME']): - logger.error('Use the appropriate job to build for branch "%s"!' %( - ENV['GIT_BRANCH'] - )) + logger.error('Use the appropriate job to build for branch "{branch}"!'.format( + branch=ENV['GIT_BRANCH'], + )) exit_error() new_dist = 'unstable' new_version = '{version}~develop{date}+{build}+{commit}'.format( - version = curr_version, - date = daily_date, - build = ENV['BUILD_NUMBER'], - commit = curr_commit_id[0:7], - ) + version=curr_version, + date=daily_date, + build=ENV['BUILD_NUMBER'], + commit=curr_commit_id[0:7], + ) pb_suite = 'develop-{dist}'.format(dist=curr_dist) reprepro_base = '/srv/pb-{dist}'.format(dist=curr_dist) # reset actions @@ -203,28 +204,27 @@ if __name__ == '__main__': elif re.match(EXPERIMENTAL_BRANCHES_RE, ENV['GIT_BRANCH']): if not re.match('^dev_.*$', ENV['JOB_NAME']): - logger.error('Use the appropriate job to build for branch "%s"!' %( - ENV['GIT_BRANCH'] - )) + logger.error('Use the appropriate job to build for branch "{branch}"!'.format( + branch=ENV['GIT_BRANCH'], + )) exit_error() new_dist = 'dev-{0}'.format(local_branch.replace("/", "-")) new_version = '{version}~experimental{date}+{build}+{commit}'.format( - version = curr_version, - date = daily_date, - build = ENV['BUILD_NUMBER'], - commit = curr_commit_id[0:7], - ) + version=curr_version, + date=daily_date, + build=ENV['BUILD_NUMBER'], + commit=curr_commit_id[0:7], + ) pb_suite = 'experimental-{dist}'.format(dist=curr_dist) reprepro_base = '/srv/dev-{dist}'.format(dist=curr_dist) else: logger.error('Don\'t know how to handle branch "{branch}".'.format( - branch = ENV['GIT_BRANCH'], - )) + branch=ENV['GIT_BRANCH'], + )) exit_error() - #FIXME: enable this once Florian acks. #FIXME: get rid of jenkins parameter, instead use git remote #if GIT_REPO_PATH.startswith('/srv/git/dev/'): @@ -239,43 +239,55 @@ if __name__ == '__main__': # create dput object with data gathered from reprepro_base defined above dput_obj = dput.Dput( - config = os.path.join(ENV['WORKSPACE'], '..', 'dput.cf'), - section = 'profitbricks', - contents = { + config=os.path.join(ENV['WORKSPACE'], '..', 'dput.cf'), + section='profitbricks', + contents={ 'fqdn': 'alexandria.pb.local', 'method': 'scp', 'login': 'reprepro', 'incoming': reprepro_base + '/incoming/profitbricks', 'allow_unsigned_uploads': 1, 'post_upload_command': 'ssh reprepro@alexandria.pb.local ' + reprepro_base + '/bin/pb_processincoming', - } - ) + } + ) # show what we got so far - logger.debug('Distribution: "%s" => "%s"' %(curr_dist, new_dist)) - logger.debug('Version: "%s" => "%s"' %(curr_version, new_version)) - logger.debug('PB_SUITE: ' + pb_suite) + logger.debug('Distribution: "{old_dist}" => "{new_dist}"'.format( + old_dist=curr_dist, + new_dist=new_dist, + )) + logger.debug('Version: "{old_version}" => "{new_version}"'format( + old_version=curr_version, + new_version=new_version, + )) + logger.debug('PB_SUITE: {suite}'.format(pb_suite)) if do_triggers: - logger.debug('Triggers found: ' + ', '.join(commit_triggers)) + logger.debug('Triggers found: {tags}'.format( + tags=', '.join(commit_triggers), + )) if do_tagging: - logger.debug('Tag to create: ' + new_tag) - logger.debug('Upload configuration:\n%s' %(pformat(dput_obj.contents))) - + logger.debug('Tag to create: {tag}'.format(new_tag)) + logger.debug('Upload configuration:\n{config}'.format( + config=pformat(dput_obj.contents), + )) + # # ACT III: do something actually + # + # .. do some housekeeping first if not do_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.fetch('--force', '--prune').strip() + gitrepo.git.reset('--hard', 'HEAD').strip() + gitrepo.git.execute(['sudo', 'git', 'clean', '-fdx']).strip() gitrepo.git.branch( '-D', '{branch}'.format(branch=local_branch), - with_exceptions=False + with_exceptions=False, ).strip() # .. and re-checkout the requested branch @@ -286,28 +298,27 @@ if __name__ == '__main__': '{rbranch}'.format(rbranch=remote_branch), ).strip() logger.debug('Status is now:\n{status}'.format( - status=gitrepo.git.status().strip() + status=gitrepo.git.status().strip(), )) # .. and also cleanup existing *orig.tar.gz if os.path.isdir(BUILD_AREA): - files = [os.path.join(BUILD_AREA, file) - for file in fnmatch.filter( - os.listdir(BUILD_AREA), '*.orig.tar.gz' - ) - ] + files = [ + os.path.join(BUILD_AREA, file) for file in fnmatch.filter( + os.listdir(BUILD_AREA), '*.orig.tar.gz' + ) + ] if len(files) > 0: logger.info('Delete previous upstream tarball(s)') cmd = [BIN_SUDO, BIN_RM, '-v'] + files if subprocess.check_call(cmd) == 0: - logger.info('%s succeeded.' % cmd) + logger.info('{command} succeeded.'.format(command=cmd)) else: - logger.warn('%s failed.' % cmd) + logger.warn('{command} failed.'.format(cmd)) figlet('Cleanup OK') - # .. do some CI magic now, if requested if do_triggers: @@ -322,16 +333,17 @@ if __name__ == '__main__': # FIXME: error handling when rebase doesn't cleanly complete? gitrepo.git.rebase('--onto', dst_branch, src_branch) gitrepo.git.checkout(dst_branch) - logger.info('Checked out "%s" branch after rebase completed from branch "%s".' %( - dst_branch, src_branch)) + logger.info('Checked out "{dst_branch}" branch after rebase completed from branch "{src_branch}".'.format( + dst_branch=dst_branch, + src_branch=src_branch, + )) if 'no-test' in commit_triggers: # debian/rules from vdc-bundles determines maven options for tests # depending whether DEB_NOTEST environment variable is defined # FIXME: ensure this environment variable is available in chroot # (i.e. sudo) - ENV.update({'DEB_NOTEST':'TRUE'}) - + ENV.update({'DEB_NOTEST': 'TRUE'}) # .. NOW we can verify debian/control contents for line in fileinput.input('debian/control'): @@ -339,12 +351,11 @@ if __name__ == '__main__': raise Exception('debian/control sets "section" to unknown. This is not allowed, failing...') exit_error() - # .. and update changelog if we trust the package if do_autoincrement: subprocess.check_call([BIN_DCH, '-i', 'Released by jenkins.']) subprocess.check_call([BIN_GIT_DCH, '-R', '-a', '--spawn-editor=none']) - gitrepo.git.commit('-a','-s','-m','Released by jenkins') + gitrepo.git.commit('-a', '-s', '-m', 'Released by jenkins') gitrepo.git.push('origin', 'master') logger.info('Changelog incremented by jenkins using debian_build.py!') @@ -362,7 +373,7 @@ if __name__ == '__main__': '--no-auto-nmu', '--', 'Generated by Jenkins build of commit {commit}'.format( - commit = curr_commit_id[0:7] + commit=curr_commit_id[0:7], ), ] @@ -374,7 +385,7 @@ if __name__ == '__main__': stdin=subprocess.PIPE, stdout=sys.stdout, stderr=sys.stderr, - cwd=os.getcwd() + cwd=os.getcwd(), ) dch.stdin.write('\n') ret = dch.wait() @@ -388,32 +399,32 @@ if __name__ == '__main__': raise Exception( '"{command}" returned with exitcode: {code}'.format( command=' '.join(cmd), - code=ret + code=ret, ) ) else: logger.info('Changelog updated by Jenkins using debian_build.py!') - # let me see the first two entries or our adjusted changelog: # TODO: use changelog python libs instead of DCH calls, see above cl = changelog.Changelog() cl.parse_changelog(open('debian/changelog')) logger.info('New changelog:\n\n{changelog}'.format( - changelog=''.join([block.__str__() for block in cl._blocks[0:2]])) + changelog=''.join([block.__str__() for block in cl._blocks[0:2]])), ) - + # # ACT IV: preparations are done, let's build + # logger.info('Building ...') gbp = git_buildpackage.GitBuildPackage( - upstream_branch = local_branch, - debian_branch = local_branch, - dist = curr_dist, - arch = 'amd64', - pb_suite = pb_suite, - git_commit_id = curr_commit_id[0:7] - ) + upstream_branch=local_branch, + debian_branch=local_branch, + dist=curr_dist, + arch='amd64', + pb_suite=pb_suite, + git_commit_id=curr_commit_id[0:7], + ) ret = gbp.build() # .. remove last commit (the one where we added the changelog entry) @@ -431,9 +442,10 @@ if __name__ == '__main__': logger.debug('git-buildpackage executed successfully') figlet('Build OK') - + # # ACT V: post-build actions # + # .. make test results available in jenkins: # # if reports.tgz exists untar it to workspace and delete it. @@ -446,9 +458,9 @@ if __name__ == '__main__': for cmd in (['/bin/tar', 'xzvf', reports_file, '-C', ENV['WORKSPACE']], ['/usr/bin/sudo', '/bin/rm', '-v', reports_file]): if subprocess.check_call(cmd) == 0: - logger.info(cmd + ' succeeded.') + logger.info('{command} succeeded.'.format(command=cmd)) else: - logger.warn(cmd + ' failed.') + logger.warn('{command} failed.'.format(command=cmd)) else: logger.info('No reports found.') figlet('Reports OK') @@ -490,8 +502,8 @@ if __name__ == '__main__': ENV['WORKSPACE'], '../build-area/result/', '{name}_{version}_amd64.changes'.format( - name = curr_name, - version = new_version, + name=curr_name, + version=new_version, ) ) @@ -499,7 +511,7 @@ if __name__ == '__main__': fh = open(changes_file, 'r') logger.info('Content of {file}\n{content}'.format( file=changes_file, - content=fh.read() + content=fh.read(), )) fh.close() @@ -514,7 +526,6 @@ if __name__ == '__main__': figlet('Upload failed') exit_error() - # .. define the time of completion BUILD_END = datetime.datetime.now() @@ -557,4 +568,3 @@ if __name__ == '__main__': logger.info('---------------------------------------------------------------------------------------------------------') figlet('Success!!!') exit_ok() -