]> Frank Brehm's Git Trees - profitbricks/jenkins-build-scripts.git/commitdiff
debian_build: include style and cleanup changes from tobias
authorMathias Klette <mathias.klette@profitbricks.com>
Tue, 28 May 2013 11:48:23 +0000 (13:48 +0200)
committerMathias Klette <mathias.klette@profitbricks.com>
Tue, 28 May 2013 11:48:23 +0000 (13:48 +0200)
debian_build.py

index 1fd9f3c95c3b781226c7930913a4c1d8b596e388..aee42e0006dad40542dafc49880af13f2904397c 100755 (executable)
@@ -1,13 +1,6 @@
 #!/usr/bin/env python
 # -*- coding: utf-8 -*-
 
-#
-# Achtung: gewachsener code, den man mal wirklich - wenn zeit ist - aufraeumen sollte.
-#
-# wirklich. wirklich. wirklich.
-#
-# mehr kommentare waeren auch gut. wirklich gut.
-
 # import standards
 import datetime
 import fileinput
@@ -15,15 +8,10 @@ import fnmatch
 import git
 import optparse
 import os
-import platform
-import pwd
 import re
-import shutil
 import subprocess
 import sys
-import time
 from ConfigParser import SafeConfigParser
-from multiprocessing import cpu_count
 from pprint import pformat
 
 # import 3rd parties
@@ -44,32 +32,32 @@ logger = logger_init(__file__)
 
 # jenkins environment - parameters
 ENV.setdefault('NO_UPLOAD','')
-ENV.setdefault('DISTRIBUTION','squeeze')
+#ENV.setdefault('DISTRIBUTION','squeeze')
 #GIT_REPO_PATH = ENV['GIT_REPO_PATH']
 
 # local constants
 BUILD_START = datetime.datetime.now()
-DEFAULT_PARALLEL_JOBS = cpu_count() + 1
-JOB_URL = 'http://jenkins:80/job/%(flavour)s/buildWithParameters?token=BuildIt&'
-JOB_DELAY = '0sec'
 
 AUTO_CHANGELOG_REPONAMES = (
     'ri-docs',
     'so-docs',
     'infrastructure-docs',
     'storage-docs',
-    )
+)
 STABLE_DISTRIBUTIONS = (
     'stable',
     'production',
     'production-proposed-updates'
-    )
+)
 STABLE_BRANCHES_RE = re.compile(
-    '^(origin/)?(master|hotfix/.*)$')
+    '^(origin/)?(master|hotfix/.*)$'
+)
 UNSTABLE_BRANCHES_RE = re.compile(
-    '^(origin/)?(develop|pre-staging|release/.*)$')
+    '^(origin/)?(develop|pre-staging|release/.*)$'
+)
 EXPERIMENTAL_BRANCHES_RE = re.compile(
-    '^(origin/)?(experimental|integration|(feature|poc|bugfix)/.*)$')
+    '^(origin/)?(experimental|integration|(feature|poc|bugfix)/.*)$'
+)
 COMMIT_TRIGGER_BRANCHES_RE = re.compile(
     '^(origin/)?(feature/.*)$')
 
@@ -104,7 +92,7 @@ def getopts():
     return parser.parse_args()
 
 if __name__ == '__main__':
-    logger.debug('Environment:\n%s' %(pformat(ENV)))
+    logger.debug('Environment:\n{env}'.format(env=pformat(ENV)))
 
     # Act I: prepare variables
     # .. command line arguments
@@ -148,12 +136,12 @@ if __name__ == '__main__':
 
     # Act II: make decissions
     # compatibility until call_jenkins was replaced
-    if ENV['DISTRIBUTION'] not in ('squeeze','wheezy'):
+    if curr_dist not in ('squeeze','wheezy'):
         logger.warn('Compatibility Mode: Change distribution {old} => {new}.'.format(
-            old = ENV['DISTRIBUTIONS'],
+            old = curr_dist,
             new = 'squeeze'
             ))
-        ENV['DISTRIBUTION'] = 'squeeze'
+        curr_dist = 'squeeze'
 
     if re.match(COMMIT_TRIGGER_BRANCHES_RE, ENV['GIT_BRANCH']):
         do_triggers = True
@@ -165,13 +153,12 @@ if __name__ == '__main__':
                 ))
             exit_error()
         new_dist = 'stable'
-        new_version = '{version}'.format(version = curr_version)
-        pb_suite = 'master-' + ENV['DISTRIBUTION']
-        reprepro_base = '/srv/pb-' + ENV['DISTRIBUTION']
+        new_version = curr_version
+        pb_suite = 'master-{dist}'.format(dist=curr_dist)
+        reprepro_base = '/srv/pb-{dist}'.format(dist=curr_dist)
 
         # replace valid debian version chars that are invalid for git tagging
-        new_tag = curr_version.replace('~','_')
-        new_tag = new_tag.replace(':',',')
+        new_tag = curr_version.replace('~', '_').replace(':', ',')
         # .. only take care of changelog automation if we want it to
         # FIXME: this must be reponame in ...
         if ENV['GIT_BRANCH'] in AUTO_CHANGELOG_REPONAMES:
@@ -207,11 +194,11 @@ if __name__ == '__main__':
             build = ENV['BUILD_NUMBER'],
             commit = curr_commit_id[0:7],
             )
-        pb_suite = 'develop-' + ENV['DISTRIBUTION']
+        pb_suite = 'develop-{dist}'.format(dist=curr_dist)
         reprepro_base = '/srv/pb-' + ENV['DISTRIBUTION']
         # reset actions
         # .. always include successful build packages into CIDB
-        if ENV['DISTRIBUTION'] == 'squeeze':
+        if curr_dist == 'squeeze':
             do_cidb = True
 
     elif re.match(EXPERIMENTAL_BRANCHES_RE, ENV['GIT_BRANCH']):
@@ -221,14 +208,14 @@ if __name__ == '__main__':
                 ))
             exit_error()
 
-        new_dist = 'dev-' + local_branch.replace("/","-")
+        new_dist = 'dev-{0}'.format(ENV['GIT_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],
             )
-        pb_suite = 'experimental-' + ENV['DISTRIBUTION']
+        pb_suite = 'experimental-{dist}'.format(dist=curr_dist)
         reprepro_base = '/srv/dev-' + ENV['DISTRIBUTION']
 
     else:
@@ -285,22 +272,30 @@ if __name__ == '__main__':
         gitrepo.git.fetch('--force','--prune').strip()
         gitrepo.git.reset('--hard','HEAD').strip()
         gitrepo.git.execute(['sudo','git','clean','-fdx']).strip()
-        gitrepo.git.branch('-D','%s' %(local_branch),with_exceptions=False).strip()
+        gitrepo.git.branch(
+            '-D',
+            '{branch}'.format(branch=local_branch),
+            with_exceptions=False
+        ).strip()
 
         # .. and re-checkout the requested branch
-        logger.info('Checkout branch %s.' %(remote_branch))
+        logger.info('Checkout branch {branch}.'.format(branch=remote_branch))
         gitrepo.git.checkout(
-            '-b','%s' %(local_branch),
-            '%s' %(remote_branch),
-            ).strip()
-        logger.debug('Status is now:\n%s' %(gitrepo.git.status().strip()))
+            '-b',
+            '{lbranch}'.format(lbranch=local_branch),
+            '{rbranch}'.format(rbranch=remote_branch),
+        ).strip()
+        logger.debug('Status is now:\n{status}'.format(
+            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'
-                         )]
+                        for file in fnmatch.filter(
+                            os.listdir(BUILD_AREA), '*.orig.tar.gz'
+                        )
+                    ]
 
             if len(files) > 0:
                 logger.info('Delete previous upstream tarball(s)')
@@ -358,20 +353,20 @@ if __name__ == '__main__':
         cmd = [
             BIN_DCH,
             '--newversion',
-            '%s' %(new_version),
+            '{version}'.format(version=new_version),
             '--force-bad-version',
             '--distribution',
-            '%s' %(new_dist),
+            '{dist}'.format(dist=new_dist),
             '--force-distribution',
             '--preserve',
             '--no-auto-nmu',
             '--',
             'Generated by Jenkins build of commit {commit}'.format(
                 commit = curr_commit_id[0:7]
-                ),
-            ]
+            ),
+        ]
 
-        logger.debug('Executing "%s" ...' %(' '.join(cmd)))
+        logger.debug('Executing "{command}" ...'.format(command=' '.join(cmd)))
         dch = subprocess.Popen(
             cmd,
             shell=False,
@@ -380,7 +375,7 @@ if __name__ == '__main__':
             stdout=sys.stdout,
             stderr=sys.stderr,
             cwd=os.getcwd()
-            )
+        )
         dch.stdin.write('\n')
         ret = dch.wait()
 
@@ -391,7 +386,11 @@ if __name__ == '__main__':
 
         if ret:
             raise Exception(
-                '"%s" returned with exitcode: %s' %(' '.join(cmd), ret))
+                '"{command}" returned with exitcode: {code}'.format(
+                    command=' '.join(cmd),
+                    code=ret
+                )
+            )
         else:
             logger.info('Changelog updated by Jenkins using debian_build.py!')
 
@@ -400,8 +399,9 @@ if __name__ == '__main__':
     # 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%s' %(
-            ''.join([block.__str__() for block in cl._blocks[0:2]])))
+    logger.info('New changelog:\n\n{changelog}'.format(
+        changelog=''.join([block.__str__() for block in cl._blocks[0:2]]))
+    )
 
 
     # ACT IV: preparations are done, let's build
@@ -409,7 +409,7 @@ if __name__ == '__main__':
     gbp = git_buildpackage.GitBuildPackage(
         upstream_branch = local_branch,
         debian_branch = local_branch,
-        dist = ENV['DISTRIBUTION'],
+        dist = curr_dist,
         arch = 'amd64',
         pb_suite = pb_suite,
         git_commit_id = curr_commit_id[0:7]
@@ -424,7 +424,7 @@ if __name__ == '__main__':
     # .. now handle the result
     if ret:
         logger.error(
-            'git-buildpackage failed with exitcode %d' % ret)
+            'git-buildpackage failed with exitcode {code}'.format(code=ret))
         figlet('Build failed')
         exit_error()
     else:
@@ -444,7 +444,7 @@ if __name__ == '__main__':
         try:
             if os.path.exists(reports_file):
                 for cmd in (['/bin/tar', 'xzvf', reports_file, '-C', ENV['WORKSPACE']],
-                            ['/usr/bin/sudo' , '/bin/rm', '-v', reports_file]):
+                            ['/usr/bin/sudo', '/bin/rm', '-v', reports_file]):
                     if subprocess.check_call(cmd) == 0:
                         logger.info(cmd + ' succeeded.')
                     else:
@@ -473,7 +473,7 @@ if __name__ == '__main__':
             figlet('Tagging failed')
             exit_error()
 
-        logger.info('Tagged as "%s".' % new_tag)
+        logger.info('Tagged as "{tag}".'.format(tag=new_tag))
         figlet('Tagging OK')
 
     # .. and upload
@@ -486,18 +486,21 @@ if __name__ == '__main__':
                 new_version = new_version.split(":", 1)[1]
 
             # construct path for changes file
-            changes_file =  os.path.join(
+            changes_file = os.path.join(
                 ENV['WORKSPACE'],
                 '../build-area/result/',
                 '{name}_{version}_amd64.changes'.format(
                     name = curr_name,
                     version = new_version,
-                    )
                 )
+            )
 
             # display changes file
             fh = open(changes_file, 'r')
-            logger.info('Contents of %s\n%s' %(changes_file, fh.read()))
+            logger.info('Content of {file}\n{content}'.format(
+                file=changes_file,
+                content=fh.read()
+            ))
             fh.close()
 
             # upload changes file
@@ -519,7 +522,7 @@ if __name__ == '__main__':
     if not do_cidb:
         figlet('CIDB skipped')
     else:
-        package_instances=[]
+        package_instances = []
         try:
             package_instances = add_package_instances(
                 "profitbricks",
@@ -529,7 +532,7 @@ if __name__ == '__main__':
                 new_version,
                 BUILD_START,
                 BUILD_END,
-                )
+            )
             figlet('CIDB OK')
 
         except Exception as error: