]> Frank Brehm's Git Trees - profitbricks/jenkins-build-scripts.git/commitdiff
add new behavior for building
authorRobin Wittler <robin.wittler@profitbricks.com>
Wed, 7 Sep 2011 09:08:34 +0000 (11:08 +0200)
committerRobin Wittler <robin.wittler@profitbricks.com>
Wed, 7 Sep 2011 09:08:34 +0000 (11:08 +0200)
build.py
lib/git_buildpackage.py

index 31b3a938ab3ec3ab42beb3171ad985b20b844afa..e1f86b5fe76f2ee575b13934927d98bbf00a1298 100755 (executable)
--- a/build.py
+++ b/build.py
@@ -54,11 +54,11 @@ GIT_TARGET_WORKSPACE = os.path.join(
         ('%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
+#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,
@@ -103,6 +103,8 @@ logger.setLevel(logging.DEBUG)
 
 def send_email(result):
     smtp = smtplib.SMTP(SMTP_SERVER)
+    if result == SMTP_BUILD_ERROR:
+        SMTP_TEXT += '\nThe filesystem path is: %s' %(GIT_TARGET_WORKSPACE)
     msg = (
             'From: %s\n' %(SMTP_FROM) +
             'To: %s\n' %(GIT_COMMITTER_EMAIL) +
@@ -132,8 +134,8 @@ def dput_package_upload(changes_path):
     cmd_obj = subprocess.Popen(
             cmd,
             shell=False,
-            stdout=subprocess.PIPE,
-            stderr=subprocess.PIPE,
+            stdout=sys.stdout,
+            stderr=sys.stderr,
             close_fds=True,
             cwd=os.path.dirname(DPUT_CF)
     )
@@ -141,22 +143,15 @@ def dput_package_upload(changes_path):
     ret = cmd_obj.wait()
 
     if ret:
-        errormsg = cmd_obj.stderr.read()
-        if not errormsg:
-            errormsg = cmd_obj.stdout.read()
-        if not errormsg:
-            errormsg = None
         message = (
-                '"%s" returned non-zero (returned with: %s). Error Msg was: %s'
-                %(cmd, ret, errormsg)
+                '"%s" returned non-zero (returned with: %s).'
+                %(' '.join(cmd), ret)
         )
         logger.debug(message)
         raise Exception(message)
 
-    output = cmd_obj.stdout.read() or None
     message = (
-            '"%s" returned zero. Output was: %s'
-            %(cmd, output)
+            '"%s" returned zero.' %(' '.join(cmd))
     )
     logger.debug(message)
     return True
@@ -173,6 +168,9 @@ def create_dput_cfg():
 
 
 def remove_git_target_workspace():
+    if not os.path.exists(GIT_TRAGET_WORKSPACE):
+        return True
+
     try:
         cmd = [BIN_SUDO, BIN_RM, '-rf', GIT_TARGET_WORKSPACE]
         cmdobj = subprocess.Popen(
@@ -180,25 +178,21 @@ def remove_git_target_workspace():
                 shell=False,
                 cwd='/',
                 close_fds=True,
-                stdout=subprocess.PIPE,
-                stderr=subprocess.PIPE,
+                stdout=sys.stdout,
+                stderr=sys.stderr,
                 env={'':''}
         )
 
         logger.debug(
                 'Trying to call "%s" to delete "%s"'
-                %(cmd, GIT_TARGET_WORKSPACE)
+                %(' '.join(cmd), GIT_TARGET_WORKSPACE)
         )
 
         ret = cmdobj.wait()
         if ret:
-            stderr_msg = cmdobj.stderr.read()
-            stdout_msg = cmdobj.stdout.read()
             _str = (
-                    'Cmd "%s" returned non-zero (exitcode: %s). '
-                    %(cmd, ret) +
-                    'Output was: stdout="%s", stderr="%s"'
-                    %(stdout_msg, stderr_msg)
+                    'Cmd "%s" returned non-zero (exitcode: %s).'
+                    %(' '.join(cmd), ret)
             )
             logger.debug(_str)
             raise Exception(_str)
@@ -207,14 +201,14 @@ def remove_git_target_workspace():
         raise
     else:
         logger.info('deleted %s' %(GIT_TARGET_WORKSPACE))
-        return cmdobj
+        return True
 
 def exit_ok():
-    remove_git_target_workspace()
     send_email(SMTP_BUILD_SUCCESS)
     sys.exit(0)
 
 def exit_error():
+    sys.exitfunc = lambda: None
     send_email(SMTP_BUILD_ERROR)
     sys.exit(1)
 
@@ -251,36 +245,52 @@ if __name__ == '__main__':
         logger.info('git clone was not successfull')
         exit_error()
 
-    #atexit.register(remove_git_target_workspace)
+    atexit.register(remove_git_target_workspace)
     os.chdir(GIT_TARGET_DIR)
 
-    if GIT_UPSTREAM_BRANCH != GIT_DEBIAN_BRANCH:
+    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)
         )
 
-    repo = git.repo.Repo()
-    # hacked by holger
-    cmd = ['/usr/bin/git', 'checkout', GIT_UPSTREAM_BRANCH]
-    subprocess.check_call(cmd)
-    
-    if repo.active_branch != 'master' and GIT_DEBIAN_BRANCH != 'master':
-        git_helper.git_checkout_branch(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.read().rstrip()
+        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'):
@@ -303,10 +313,8 @@ if __name__ == '__main__':
 
         if ret:
             raise Exception('git log was not successfull')
-        #git_log_output = map(
-        #        lambda x: x.rstrip(),
-        #        git_log.stdout.readlines()
-        #)
+        else:
+            logger.info('git log was successfull')
 
         cmd = [
                 '/usr/bin/dch',
@@ -320,29 +328,29 @@ if __name__ == '__main__':
                 '--',
                 '%s' %(''.join(git_log.stdout.read()))
         ]
-        #        '%s' %(git_log_output)
 
-        logger.debug('Trying to call: %s' %(cmd))      
+        logger.debug('Trying to call: %s' %(' '.join(cmd)))
 
         dch = subprocess.Popen(
                 cmd,
                 shell=False,
                 close_fds=True,
                 stdin=git_log.stdout,
-                stdout=subprocess.PIPE,
-                stderr=subprocess.PIPE,
+                stdout=sys.stdout,
+                stderr=sys.stderr,
                 cwd=os.getcwd()
         )
         ret = dch.wait()
 
         if ret:
             raise Exception(
-                    'git dch was not successfull ' +
-                    'Error msg was: %s' %(dch.stderr.read())
+                    '"%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... 
+        # 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']
@@ -356,14 +364,14 @@ if __name__ == '__main__':
                 cmd,
                 shell=False,
                 close_fds=True,
-                stdout=sys.stdout,
+                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) 
+                    '%s was not successfull, return code was %s ' % (' '.join(cmd), ret)
             )
            exit_error()
 
@@ -373,24 +381,20 @@ if __name__ == '__main__':
                 shell=False,
                 close_fds=True,
                stdin=parse_changelog.stdout,
-                stdout=sys.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) 
+                    '%s was not successfull, return code was %s ' % (' '.join(cmd), ret)
             )
            exit_error()
-   
-        version = grep_dctrl.stdout.read()
-       version = version.strip()
-       logger.info('version is %s' % (version))
-
-        #stdout_msg = parse_changelog.stdout.read()
-        #logger.info(stdout_msg)
 
+        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():
@@ -413,7 +417,7 @@ if __name__ == '__main__':
     logger.info('starting git-buildpackage')
     ret = gbp.build()
     logger.debug(
-            'This is the output of git-buildpackage: \n%s'
+            'This is the returncode of git-buildpackage: \n%s'
             %(ret)
     )
     if not ret:
@@ -426,41 +430,15 @@ if __name__ == '__main__':
          pass
         exit_error()
     else:
-        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()
-
-        #logger.debug(os.listdir('./'))
-        #logger.debug(os.listdir('../'))
-
-        exit_ok()
-
-#class GitBuildPackage(object):
-#        def __init__(self, upstream_branch=None,
-#                            debian_branch=None, dist=None, arch=None)
-#
-#        ftp = FTP(
-#                'alexandria.profitbricks.localdomain',
-#                'debian-uploader',
-#                'vae6tooZe1ec'
-#        )
-#
-#        logger.info('Log in on %s successfull' %(ftp.host))
-#
-#        ftp.cwd('experimental')
-#        for package in glob(
-#                os.path.join(GIT_TARGET_WORKSPACE, '*.deb')
-#        ):
-#            fh = open(package, 'rb', 1)
-#            ftp.storbinary(
-#                    'STOR %s' %(os.path.basename(package)),
-#                    fh
-#            )
-#            fh.close()
-#            logger.info('Successfully uploaded %s' %(package))
-#        ftp.quit()
-#    exit_ok()
+        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()
index 5a8fc3a4dd625f762c984c7ec4296e4721940c7c..36245d291ff9fbc3b90624b0e9cd01c6b63f2072 100644 (file)
@@ -2,6 +2,7 @@
 # -*- coding: utf-8 -*-
 
 import os
+import sys
 import logging
 import subprocess
 logger = logging.getLogger(__file__)
@@ -37,25 +38,31 @@ class GitBuildPackage(object):
                 cmd,
                 shell=False,
                 close_fds=True,
-                stdout=subprocess.PIPE,
-                stderr=subprocess.PIPE,
+                #stdout=subprocess.PIPE,
+                #stderr=subprocess.PIPE,
+                stdout=sys.stdout
+                stderr=sys.stderr,
                 env=env,
                 cwd=os.getcwd(),
         )
 
         ret = cmdobj.wait()
         if ret:
-            error_str = cmdobj.stderr.read()
-            if not error_str:
-                error_str = cmdobj.stdout.read()
-            if not error_str:
-                error_str = 'No Error Msg found'
             logger.error(
-                    '%s returned with %s. Output was: %s'
-                    %(' '.join(cmd), ret, error_str)
+                    '"%s" returned non-zero. exitcode was: %s'
+                    %(' '.join(cmd), ret)
             )
+#            error_str = cmdobj.stderr.read()
+#            if not error_str:
+#                error_str = cmdobj.stdout.read()
+#            if not error_str:
+#                error_str = 'No Error Msg found'
+#            logger.error(
+#                    '%s returned with %s. Output was: %s'
+#                    %(' '.join(cmd), ret, error_str)
+#            )
             return False
-        return cmdobj.stdout.read() or True
+        return True
 
 
 # vim: autoindent smartindent tabstop=4 expandtab shiftwidth=4 softtabstop=4 nu enc=utf-8 cinwords=if,elif,else,for,while,try,except,finally,def,class