From: Holger Levsen Date: Wed, 14 Sep 2011 08:07:47 +0000 (+0200) Subject: rename build.py to debian_build.py X-Git-Url: https://git.uhu-banane.org/?a=commitdiff_plain;h=04b275221d22c545e3613f4a9281a36e99edb651;p=profitbricks%2Fjenkins-build-scripts.git rename build.py to debian_build.py --- diff --git a/build.py b/build.py deleted file mode 100755 index ec6986e..0000000 --- a/build.py +++ /dev/null @@ -1,458 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- - -import os -import re -import pwd -import sys -import git -import errno -import atexit -import shutil -import logging -import smtplib -import optparse -import datetime -import platform -import subprocess -from glob import glob -from ftplib import FTP -from lib import git_helper -from logging import Formatter -from lib import git_buildpackage -from multiprocessing import cpu_count -from ConfigParser import SafeConfigParser - -GIT = '/usr/bin/git' -BIN_RM = '/bin/rm' -BIN_SUDO = '/usr/bin/sudo' -BIN_DPUT = '/usr/bin/dput' -MAKE_KPKG = '/usr/bin/make-kpkg' -DEFAULT_PARALLEL_JOBS = cpu_count() + 1 - -BUILD_ARCH_MAP = { - 'x86_64': 'amd64', - 'i386': '686' -} - -BUILD_ARCH = BUILD_ARCH_MAP.get(platform.machine(), '686') - -CWD = os.environ.get('WORKSPACE') -BUILD_NUMBER = os.environ.get('BUILD_NUMBER') -BUILD_ID = os.environ.get('BUILD_ID') -BUILD_URL = os.environ.get('BUILD_URL') - -GIT_REPO_PATH = os.environ.get('GIT_REPO_PATH') -GIT_REPO_NAME = os.path.basename(GIT_REPO_PATH) -GIT_OLD_ID = os.environ.get('GIT_OLD_ID') -GIT_NEW_ID = os.environ.get('GIT_NEW_ID') -GIT_UPSTREAM_BRANCH = os.environ.get('GIT_UPSTREAM_BRANCH') -GIT_DEBIAN_BRANCH = os.environ.get('GIT_DEBIAN_BRANCH') -GIT_DEBIAN_REMOTE_BRANCH = os.path.join('origin', '%s' %(GIT_DEBIAN_BRANCH)) -GIT_TARGET_WORKSPACE = os.path.join( - CWD, - ('%s-build%s' %(BUILD_ID, BUILD_NUMBER)) -) - -#if GIT_UPSTREAM_BRANCH == 'NONE': -# raise Exception('You must give a GIT_UPSTREAM_BRANCH') -# -#if GIT_DEBIAN_BRANCH == 'NONE': -# GIT_DEBIAN_BRANCH = GIT_UPSTREAM_BRANCH - -GIT_TARGET_DIR = os.path.join( - GIT_TARGET_WORKSPACE, - os.path.basename(GIT_REPO_PATH) -) - -JOB_NAME = os.environ.get('JOB_NAME') -GIT_COMMITTER_EMAIL = os.environ.get('GIT_COMMITTER_EMAIL') -SMTP_SERVER = 'roma.profitbricks.localdomain' -SMTP_SUBJECT = 'Build for job %s, branch %s, buildnumber %s was %s' -SMTP_TEXT = ( - 'Build for job %s, branch %s, buildnumber %s was %s. ' + - 'Take a close look at: ' + BUILD_URL -) -SMTP_BUILD_SUCCESS = 'SUCCESSFULL' -SMTP_BUILD_ERROR = 'NOT SUCCESSFULL' -SMTP_FROM = '%s@profitbricks.com' %(pwd.getpwuid(os.geteuid()).pw_name) - -CRE_URI = re.compile(r'(?P\w+)\:\/\/(?P[a-zA-Z0-9\-\_\.\+]+)\/(?P.*)$') -DPUT_URI = CRE_URI.match(GIT_REPO_PATH) -if not DPUT_URI: - raise Exception( - 'could not build DPUT_URI from "%s"' - %(GIT_REPO_PATH) - ) -DPUT_OPTIONS = { - 'fqdn': DPUT_URI.groupdict().get('url'), - 'method': 'scp', - 'login': 'reprepro', - 'incoming': '/srv/profitbricks-repository/incoming/profitbricks', - 'allow_unsigned_uploads': 1, - } -DPUT_CF = os.path.join(GIT_TARGET_WORKSPACE, 'dput.cf') - -log_format = '%(asctime)s %(name)s[%(process)d] %(levelname)s: %(message)s' -formatter = Formatter(log_format) -logging.basicConfig( - format=log_format, - level=logging.DEBUG -) -logger = logging.getLogger(__file__) -logger.setLevel(logging.DEBUG) - -def send_email(result): - smtp = smtplib.SMTP(SMTP_SERVER) - if result == SMTP_BUILD_ERROR: - global SMTP_TEXT - SMTP_TEXT += '\nThe filesystem path is: %s' %(GIT_TARGET_WORKSPACE) - msg = ( - 'From: %s\n' %(SMTP_FROM) + - 'To: %s\n' %(GIT_COMMITTER_EMAIL) + - 'Subject: %s\n' %(SMTP_SUBJECT %(JOB_NAME, GIT_DEBIAN_BRANCH, BUILD_NUMBER, - result)) + - '%s\n' %(SMTP_TEXT %(JOB_NAME, GIT_DEBIAN_BRANCH, BUILD_NUMBER, result)) - ) - logger.info('Sending this message: %s', msg) - smtp.sendmail(SMTP_FROM, GIT_COMMITTER_EMAIL, msg) - smtp.quit() - -def read_file(path): - try: - fh = open(path, 'r', 1) - except: - raise - else: - result = dict(enumerate(fh)) - fh.close() - return result - -def dput_package_upload(changes_path): - cmd = [BIN_DPUT, '-c', '%s' %(DPUT_CF), '--no-upload-log', 'profitbricks', '%s' %(changes_path)] - logger.debug( - 'Trying to execute: "%s"' - %(cmd) - ) - cmd_obj = subprocess.Popen( - cmd, - shell=False, - stdout=sys.stdout, - stderr=sys.stderr, - close_fds=True, - cwd=os.path.dirname(DPUT_CF) - ) - - ret = cmd_obj.wait() - - if ret: - message = ( - '"%s" returned non-zero (returned with: %s).' - %(' '.join(cmd), ret) - ) - logger.debug(message) - raise Exception(message) - - message = ( - '"%s" returned zero.' %(' '.join(cmd)) - ) - logger.debug(message) - return True - -def create_dput_cfg(): - fh = open(DPUT_CF, 'w') - config = SafeConfigParser() - config.add_section('profitbricks') - for option, value in DPUT_OPTIONS.iteritems(): - config.set('profitbricks', '%s' %(option), '%s' %(value)) - config.write(fh) - fh.close() - return True - - -def remove_git_target_workspace(): - if not os.path.exists(GIT_TARGET_WORKSPACE): - return True - - try: - cmd = [BIN_SUDO, BIN_RM, '-rf', GIT_TARGET_WORKSPACE] - cmdobj = subprocess.Popen( - cmd, - shell=False, - cwd='/', - close_fds=True, - stdout=sys.stdout, - stderr=sys.stderr, - env={'':''} - ) - - logger.debug( - 'Trying to call "%s" to delete "%s"' - %(' '.join(cmd), GIT_TARGET_WORKSPACE) - ) - - ret = cmdobj.wait() - if ret: - _str = ( - 'Cmd "%s" returned non-zero (exitcode: %s).' - %(' '.join(cmd), ret) - ) - logger.debug(_str) - raise Exception(_str) - except Exception, error: - logger.exception(error) - raise - else: - logger.info('deleted %s' %(GIT_TARGET_WORKSPACE)) - return True - -def exit_ok(): - send_email(SMTP_BUILD_SUCCESS) - sys.exit(0) - -def exit_error(): - sys.exitfunc = lambda: None - send_email(SMTP_BUILD_ERROR) - sys.exit(1) - -def getopts(): - usage = '%prog [options]' - parser = optparse.OptionParser(usage=usage, version='%prog 0.1') - parser.add_option( - '--distribution', - dest='distribution', - choices = ['stable', 'production-proposed-updates', 'testing', 'staging', 'unstable', 'pre-staging', 'experimental'], - default='unstable', - help='The pkg distribution. Default: %default' - ) - - return parser.parse_args() - -if __name__ == '__main__': - logger.debug( - 'Initialised with Enviroment: %s' - %( - ', '.join( - map( - lambda x: '%s => %s' %(x[0], x[1]), - os.environ.iteritems() - ) - ) - ) - ) - logging.getLogger('lib.git_helper').setLevel(logging.DEBUG) - options, args = getopts() - if git_helper.git_clone_remote_repository(GIT_REPO_PATH, GIT_TARGET_DIR): - logger.info('git clone was successfull') - else: - logger.info('git clone was not successfull') - exit_error() - - atexit.register(remove_git_target_workspace) - os.chdir(GIT_TARGET_DIR) - - if not GIT_DEBIAN_BRANCH: - if GIT_UPSTREAM_BRANCH: - GIT_DEBIAN_BRANCH = GIT_UPSTREAM_BRANCH - logging.debug( - 'setting GIT_DEBIAN_BRANCH to %s', - GIT_UPSTREAM_BRANCH - ) - else: - logger.error('Could not determine GIT_DEBIAN_BRANCH') - exit_error() - elif not GIT_UPSTREAM_BRANCH: - if GIT_DEBIAN_BRANCH: - GIT_UPSTREAM_BRANCH = GIT_DEBIAN_BRANCH - logging.debug( - 'setting GIT_UPSTREAM_BRANCH to %s', - GIT_UPSTREAM_BRANCH - ) - else: - logger.error('Could not determine GIT_UPSTREAM_BRANCH') - exit_error() - - repo = git.repo.Repo() - - if GIT_DEBIAN_BRANCH != 'master': - git_helper.git_new_branch_from( - GIT_DEBIAN_BRANCH, - os.path.join('origin', GIT_DEBIAN_BRANCH) - ) - - pb_version_path = os.path.join('debian', 'pb_version') - - if not os.path.exists(pb_version_path): - pb_version = '0.0' - else: - fh = open(pb_version_path) - pb_version = fh.readline().rstrip() - fh.close() - - fh = open('debian/control') - for line in fh: - if line.startswith('Source:'): - pkg_name = line.split(':')[-1].lstrip().rstrip() - break - fh.close() - - if options.distribution in ('unstable', 'experimental'): - - daily_date = datetime.datetime.now().strftime('%Y%m%d%H%M%S') - version = '%s~%s' %(pb_version, daily_date) - os.unlink('debian/changelog') - cmd = ['/usr/bin/git', 'log', '--since=yesterday', '--abbrev-commit', '--format=format:"%cD -- %aN%n[%h] %s%n"'] - git_log = subprocess.Popen( - cmd, - shell=False, - close_fds=True, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - cwd='./', - universal_newlines=True - ) - - ret = git_log.wait() - - if ret: - raise Exception('git log was not successfull') - else: - logger.info('git log was successfull') - - cmd = [ - '/usr/bin/dch', - '--create', - '--package', - pkg_name, - '--newversion', - '%s' %(version), - '--distribution', - '%s' %(options.distribution), - '--', - '%s' %(''.join(git_log.stdout.read())) - ] - - logger.debug('Trying to call: %s' %(' '.join(cmd))) - - dch = subprocess.Popen( - cmd, - shell=False, - close_fds=True, - stdin=git_log.stdout, - stdout=sys.stdout, - stderr=sys.stderr, - cwd=os.getcwd() - ) - ret = dch.wait() - - if ret: - raise Exception( - '"%s" returned with exitcode: %s' %(' '.join(cmd), ret) - ) - logger.info('debian/changelog written') - - # we need to commit here else git-buildpackage will use the existing debian/changelog... - # TODO: Later we should investigate why the "--ignore-new" trick - # did not work! - cmd = ['/usr/bin/git', 'add', '-A'] - subprocess.check_call(cmd) - cmd = ['/usr/bin/git', 'commit', '-a', '-m', 'add changelog'] - subprocess.check_call(cmd) - else: - # so we're in the master branch... - # (or maybe in other branches, but thats not specified _yet_) - # TODO errorchecking etc - cmd = ['dpkg-parsechangelog'] - parse_changelog = subprocess.Popen( - cmd, - shell=False, - close_fds=True, - stdout=subprocess.PIPE, - stderr=sys.stderr, - cwd='./' - ) - ret = parse_changelog.wait() - if ret: - raise Exception( - '%s was not successfull, return code was %s ' % (' '.join(cmd), ret) - ) - exit_error() - - cmd = ['grep-dctrl', '-n', '-s', 'Version', ''] - grep_dctrl = subprocess.Popen( - cmd, - shell=False, - close_fds=True, - stdin=parse_changelog.stdout, - stdout=subprocess.PIPE, - stderr=sys.stderr, - cwd=os.getcwd() - ) - ret = grep_dctrl.wait() - if ret: - raise Exception( - '%s was not successfull, return code was %s ' % (' '.join(cmd), ret) - ) - exit_error() - - version = grep_dctrl.stdout.readline() - version = version.strip() - logger.info('Packet version is %s' % (version)) - - if not GIT_COMMITTER_EMAIL: - for commit in repo.commits(): - if commit.id == GIT_NEW_ID: - GIT_COMMITTER_EMAIL = commit.committer.email - logger.debug( - 'Found "%s" in commit-id "%s" at "%s"' - %(GIT_COMMITTER_EMAIL, commit.id, repo.active_branch) - ) - else: - raise Exception('No git_committer_email found') - - if options.distribution in ('unstable' , 'experimental', 'pre-staging'): - pb_suite='pre-staging' - elif options.distribution in ('testing', 'staging'): - pb_suite='staging' - elif options.distribution in ('production', 'production-proposed-updates'): - pb_suite='production-proposed-updates' - - gbp = git_buildpackage.GitBuildPackage( - upstream_branch=GIT_UPSTREAM_BRANCH, - debian_branch=GIT_DEBIAN_BRANCH, - dist='squeeze', - arch='amd64', - pb_suite=pb_suite - ) - - logger.info('starting git-buildpackage') - ret = gbp.build() - if ret: - logger.error( - 'git-buildpackage returned non-zero. exitcode was: %s', ret - ) - exit_error() - else: - logger.debug('git-buildpackage executed successfully') -# if not ret: - build_log=os.path.join('../build-area/result/', '%s_%s_amd64.build' % (pkg_name, version)) - try: - fh = open(build_log, 'r') - logger.debug('Output of the deb build: %s' % (fh.read())) - fh.close() - except: - pass -# exit_error() -# else: - try: - create_dput_cfg() - changes_file = os.path.join(GIT_TARGET_WORKSPACE, 'build-area/result/', '%s_%s_amd64.changes' % (pkg_name, version)) - dput_package_upload(changes_file) - - fh = open(changes_file, 'r') - logger.info('%s' % (fh.read())) - fh.close() - exit_ok() - except Exception, error: - logger.exception(error) - exit_error() diff --git a/debian_build.py b/debian_build.py new file mode 100755 index 0000000..ec6986e --- /dev/null +++ b/debian_build.py @@ -0,0 +1,458 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import os +import re +import pwd +import sys +import git +import errno +import atexit +import shutil +import logging +import smtplib +import optparse +import datetime +import platform +import subprocess +from glob import glob +from ftplib import FTP +from lib import git_helper +from logging import Formatter +from lib import git_buildpackage +from multiprocessing import cpu_count +from ConfigParser import SafeConfigParser + +GIT = '/usr/bin/git' +BIN_RM = '/bin/rm' +BIN_SUDO = '/usr/bin/sudo' +BIN_DPUT = '/usr/bin/dput' +MAKE_KPKG = '/usr/bin/make-kpkg' +DEFAULT_PARALLEL_JOBS = cpu_count() + 1 + +BUILD_ARCH_MAP = { + 'x86_64': 'amd64', + 'i386': '686' +} + +BUILD_ARCH = BUILD_ARCH_MAP.get(platform.machine(), '686') + +CWD = os.environ.get('WORKSPACE') +BUILD_NUMBER = os.environ.get('BUILD_NUMBER') +BUILD_ID = os.environ.get('BUILD_ID') +BUILD_URL = os.environ.get('BUILD_URL') + +GIT_REPO_PATH = os.environ.get('GIT_REPO_PATH') +GIT_REPO_NAME = os.path.basename(GIT_REPO_PATH) +GIT_OLD_ID = os.environ.get('GIT_OLD_ID') +GIT_NEW_ID = os.environ.get('GIT_NEW_ID') +GIT_UPSTREAM_BRANCH = os.environ.get('GIT_UPSTREAM_BRANCH') +GIT_DEBIAN_BRANCH = os.environ.get('GIT_DEBIAN_BRANCH') +GIT_DEBIAN_REMOTE_BRANCH = os.path.join('origin', '%s' %(GIT_DEBIAN_BRANCH)) +GIT_TARGET_WORKSPACE = os.path.join( + CWD, + ('%s-build%s' %(BUILD_ID, BUILD_NUMBER)) +) + +#if GIT_UPSTREAM_BRANCH == 'NONE': +# raise Exception('You must give a GIT_UPSTREAM_BRANCH') +# +#if GIT_DEBIAN_BRANCH == 'NONE': +# GIT_DEBIAN_BRANCH = GIT_UPSTREAM_BRANCH + +GIT_TARGET_DIR = os.path.join( + GIT_TARGET_WORKSPACE, + os.path.basename(GIT_REPO_PATH) +) + +JOB_NAME = os.environ.get('JOB_NAME') +GIT_COMMITTER_EMAIL = os.environ.get('GIT_COMMITTER_EMAIL') +SMTP_SERVER = 'roma.profitbricks.localdomain' +SMTP_SUBJECT = 'Build for job %s, branch %s, buildnumber %s was %s' +SMTP_TEXT = ( + 'Build for job %s, branch %s, buildnumber %s was %s. ' + + 'Take a close look at: ' + BUILD_URL +) +SMTP_BUILD_SUCCESS = 'SUCCESSFULL' +SMTP_BUILD_ERROR = 'NOT SUCCESSFULL' +SMTP_FROM = '%s@profitbricks.com' %(pwd.getpwuid(os.geteuid()).pw_name) + +CRE_URI = re.compile(r'(?P\w+)\:\/\/(?P[a-zA-Z0-9\-\_\.\+]+)\/(?P.*)$') +DPUT_URI = CRE_URI.match(GIT_REPO_PATH) +if not DPUT_URI: + raise Exception( + 'could not build DPUT_URI from "%s"' + %(GIT_REPO_PATH) + ) +DPUT_OPTIONS = { + 'fqdn': DPUT_URI.groupdict().get('url'), + 'method': 'scp', + 'login': 'reprepro', + 'incoming': '/srv/profitbricks-repository/incoming/profitbricks', + 'allow_unsigned_uploads': 1, + } +DPUT_CF = os.path.join(GIT_TARGET_WORKSPACE, 'dput.cf') + +log_format = '%(asctime)s %(name)s[%(process)d] %(levelname)s: %(message)s' +formatter = Formatter(log_format) +logging.basicConfig( + format=log_format, + level=logging.DEBUG +) +logger = logging.getLogger(__file__) +logger.setLevel(logging.DEBUG) + +def send_email(result): + smtp = smtplib.SMTP(SMTP_SERVER) + if result == SMTP_BUILD_ERROR: + global SMTP_TEXT + SMTP_TEXT += '\nThe filesystem path is: %s' %(GIT_TARGET_WORKSPACE) + msg = ( + 'From: %s\n' %(SMTP_FROM) + + 'To: %s\n' %(GIT_COMMITTER_EMAIL) + + 'Subject: %s\n' %(SMTP_SUBJECT %(JOB_NAME, GIT_DEBIAN_BRANCH, BUILD_NUMBER, + result)) + + '%s\n' %(SMTP_TEXT %(JOB_NAME, GIT_DEBIAN_BRANCH, BUILD_NUMBER, result)) + ) + logger.info('Sending this message: %s', msg) + smtp.sendmail(SMTP_FROM, GIT_COMMITTER_EMAIL, msg) + smtp.quit() + +def read_file(path): + try: + fh = open(path, 'r', 1) + except: + raise + else: + result = dict(enumerate(fh)) + fh.close() + return result + +def dput_package_upload(changes_path): + cmd = [BIN_DPUT, '-c', '%s' %(DPUT_CF), '--no-upload-log', 'profitbricks', '%s' %(changes_path)] + logger.debug( + 'Trying to execute: "%s"' + %(cmd) + ) + cmd_obj = subprocess.Popen( + cmd, + shell=False, + stdout=sys.stdout, + stderr=sys.stderr, + close_fds=True, + cwd=os.path.dirname(DPUT_CF) + ) + + ret = cmd_obj.wait() + + if ret: + message = ( + '"%s" returned non-zero (returned with: %s).' + %(' '.join(cmd), ret) + ) + logger.debug(message) + raise Exception(message) + + message = ( + '"%s" returned zero.' %(' '.join(cmd)) + ) + logger.debug(message) + return True + +def create_dput_cfg(): + fh = open(DPUT_CF, 'w') + config = SafeConfigParser() + config.add_section('profitbricks') + for option, value in DPUT_OPTIONS.iteritems(): + config.set('profitbricks', '%s' %(option), '%s' %(value)) + config.write(fh) + fh.close() + return True + + +def remove_git_target_workspace(): + if not os.path.exists(GIT_TARGET_WORKSPACE): + return True + + try: + cmd = [BIN_SUDO, BIN_RM, '-rf', GIT_TARGET_WORKSPACE] + cmdobj = subprocess.Popen( + cmd, + shell=False, + cwd='/', + close_fds=True, + stdout=sys.stdout, + stderr=sys.stderr, + env={'':''} + ) + + logger.debug( + 'Trying to call "%s" to delete "%s"' + %(' '.join(cmd), GIT_TARGET_WORKSPACE) + ) + + ret = cmdobj.wait() + if ret: + _str = ( + 'Cmd "%s" returned non-zero (exitcode: %s).' + %(' '.join(cmd), ret) + ) + logger.debug(_str) + raise Exception(_str) + except Exception, error: + logger.exception(error) + raise + else: + logger.info('deleted %s' %(GIT_TARGET_WORKSPACE)) + return True + +def exit_ok(): + send_email(SMTP_BUILD_SUCCESS) + sys.exit(0) + +def exit_error(): + sys.exitfunc = lambda: None + send_email(SMTP_BUILD_ERROR) + sys.exit(1) + +def getopts(): + usage = '%prog [options]' + parser = optparse.OptionParser(usage=usage, version='%prog 0.1') + parser.add_option( + '--distribution', + dest='distribution', + choices = ['stable', 'production-proposed-updates', 'testing', 'staging', 'unstable', 'pre-staging', 'experimental'], + default='unstable', + help='The pkg distribution. Default: %default' + ) + + return parser.parse_args() + +if __name__ == '__main__': + logger.debug( + 'Initialised with Enviroment: %s' + %( + ', '.join( + map( + lambda x: '%s => %s' %(x[0], x[1]), + os.environ.iteritems() + ) + ) + ) + ) + logging.getLogger('lib.git_helper').setLevel(logging.DEBUG) + options, args = getopts() + if git_helper.git_clone_remote_repository(GIT_REPO_PATH, GIT_TARGET_DIR): + logger.info('git clone was successfull') + else: + logger.info('git clone was not successfull') + exit_error() + + atexit.register(remove_git_target_workspace) + os.chdir(GIT_TARGET_DIR) + + if not GIT_DEBIAN_BRANCH: + if GIT_UPSTREAM_BRANCH: + GIT_DEBIAN_BRANCH = GIT_UPSTREAM_BRANCH + logging.debug( + 'setting GIT_DEBIAN_BRANCH to %s', + GIT_UPSTREAM_BRANCH + ) + else: + logger.error('Could not determine GIT_DEBIAN_BRANCH') + exit_error() + elif not GIT_UPSTREAM_BRANCH: + if GIT_DEBIAN_BRANCH: + GIT_UPSTREAM_BRANCH = GIT_DEBIAN_BRANCH + logging.debug( + 'setting GIT_UPSTREAM_BRANCH to %s', + GIT_UPSTREAM_BRANCH + ) + else: + logger.error('Could not determine GIT_UPSTREAM_BRANCH') + exit_error() + + repo = git.repo.Repo() + + if GIT_DEBIAN_BRANCH != 'master': + git_helper.git_new_branch_from( + GIT_DEBIAN_BRANCH, + os.path.join('origin', GIT_DEBIAN_BRANCH) + ) + + pb_version_path = os.path.join('debian', 'pb_version') + + if not os.path.exists(pb_version_path): + pb_version = '0.0' + else: + fh = open(pb_version_path) + pb_version = fh.readline().rstrip() + fh.close() + + fh = open('debian/control') + for line in fh: + if line.startswith('Source:'): + pkg_name = line.split(':')[-1].lstrip().rstrip() + break + fh.close() + + if options.distribution in ('unstable', 'experimental'): + + daily_date = datetime.datetime.now().strftime('%Y%m%d%H%M%S') + version = '%s~%s' %(pb_version, daily_date) + os.unlink('debian/changelog') + cmd = ['/usr/bin/git', 'log', '--since=yesterday', '--abbrev-commit', '--format=format:"%cD -- %aN%n[%h] %s%n"'] + git_log = subprocess.Popen( + cmd, + shell=False, + close_fds=True, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + cwd='./', + universal_newlines=True + ) + + ret = git_log.wait() + + if ret: + raise Exception('git log was not successfull') + else: + logger.info('git log was successfull') + + cmd = [ + '/usr/bin/dch', + '--create', + '--package', + pkg_name, + '--newversion', + '%s' %(version), + '--distribution', + '%s' %(options.distribution), + '--', + '%s' %(''.join(git_log.stdout.read())) + ] + + logger.debug('Trying to call: %s' %(' '.join(cmd))) + + dch = subprocess.Popen( + cmd, + shell=False, + close_fds=True, + stdin=git_log.stdout, + stdout=sys.stdout, + stderr=sys.stderr, + cwd=os.getcwd() + ) + ret = dch.wait() + + if ret: + raise Exception( + '"%s" returned with exitcode: %s' %(' '.join(cmd), ret) + ) + logger.info('debian/changelog written') + + # we need to commit here else git-buildpackage will use the existing debian/changelog... + # TODO: Later we should investigate why the "--ignore-new" trick + # did not work! + cmd = ['/usr/bin/git', 'add', '-A'] + subprocess.check_call(cmd) + cmd = ['/usr/bin/git', 'commit', '-a', '-m', 'add changelog'] + subprocess.check_call(cmd) + else: + # so we're in the master branch... + # (or maybe in other branches, but thats not specified _yet_) + # TODO errorchecking etc + cmd = ['dpkg-parsechangelog'] + parse_changelog = subprocess.Popen( + cmd, + shell=False, + close_fds=True, + stdout=subprocess.PIPE, + stderr=sys.stderr, + cwd='./' + ) + ret = parse_changelog.wait() + if ret: + raise Exception( + '%s was not successfull, return code was %s ' % (' '.join(cmd), ret) + ) + exit_error() + + cmd = ['grep-dctrl', '-n', '-s', 'Version', ''] + grep_dctrl = subprocess.Popen( + cmd, + shell=False, + close_fds=True, + stdin=parse_changelog.stdout, + stdout=subprocess.PIPE, + stderr=sys.stderr, + cwd=os.getcwd() + ) + ret = grep_dctrl.wait() + if ret: + raise Exception( + '%s was not successfull, return code was %s ' % (' '.join(cmd), ret) + ) + exit_error() + + version = grep_dctrl.stdout.readline() + version = version.strip() + logger.info('Packet version is %s' % (version)) + + if not GIT_COMMITTER_EMAIL: + for commit in repo.commits(): + if commit.id == GIT_NEW_ID: + GIT_COMMITTER_EMAIL = commit.committer.email + logger.debug( + 'Found "%s" in commit-id "%s" at "%s"' + %(GIT_COMMITTER_EMAIL, commit.id, repo.active_branch) + ) + else: + raise Exception('No git_committer_email found') + + if options.distribution in ('unstable' , 'experimental', 'pre-staging'): + pb_suite='pre-staging' + elif options.distribution in ('testing', 'staging'): + pb_suite='staging' + elif options.distribution in ('production', 'production-proposed-updates'): + pb_suite='production-proposed-updates' + + gbp = git_buildpackage.GitBuildPackage( + upstream_branch=GIT_UPSTREAM_BRANCH, + debian_branch=GIT_DEBIAN_BRANCH, + dist='squeeze', + arch='amd64', + pb_suite=pb_suite + ) + + logger.info('starting git-buildpackage') + ret = gbp.build() + if ret: + logger.error( + 'git-buildpackage returned non-zero. exitcode was: %s', ret + ) + exit_error() + else: + logger.debug('git-buildpackage executed successfully') +# if not ret: + build_log=os.path.join('../build-area/result/', '%s_%s_amd64.build' % (pkg_name, version)) + try: + fh = open(build_log, 'r') + logger.debug('Output of the deb build: %s' % (fh.read())) + fh.close() + except: + pass +# exit_error() +# else: + try: + create_dput_cfg() + changes_file = os.path.join(GIT_TARGET_WORKSPACE, 'build-area/result/', '%s_%s_amd64.changes' % (pkg_name, version)) + dput_package_upload(changes_file) + + fh = open(changes_file, 'r') + logger.info('%s' % (fh.read())) + fh.close() + exit_ok() + except Exception, error: + logger.exception(error) + exit_error()