it properly creates packages!
[auto-debuild.git] / auto_debuild.py
index 098bc8968a4ccae6dd89dd856bca98fe367bcf59..3a9deaf52d9e3913fe8adbf5d5bb0d88e8e8b387 100755 (executable)
@@ -1,5 +1,24 @@
 #!/usr/bin/python
-import os, stat
+import os, stat, time, subprocess, sys
+
+def getArchitecture():
+       p = subprocess.Popen(['dpkg-architecture', '-qDEB_BUILD_ARCH'], stdout=subprocess.PIPE)
+       res = p.communicate()[0] # get only stdout
+       if p.returncode != 0: raise Exception("Querying dpkg for the architecture failed")
+       return res[0:len(res)-1] # chop of the \n at the end
+
+# build-system specific part of rules file
+def writeCmakeRulesFile(f, config):
+       print >>f, "%:"
+       print >>f, "\tdh $@ --buildsystem cmake --parallel --builddirectory=build.dir" # "build" is not a good idea, as that's also the name of a target...
+       print >>f, ""
+       print >>f, "override_dh_auto_configure:"
+       print >>f, "\tmkdir -p build.dir"
+       print >>f, "\tcd build.dir && cmake ..",' '.join(['-DCMAKE_INSTALL_PREFIX=/usr', '-DCMAKE_BUILD_TYPE=Release'] + config.get('cmakeParameters', []))
+       print >>f, ""
+       print >>f, "override_dh_auto_clean:"
+       print >>f, "    rm -f build.dir/CMakeCache.txt # clean old cmake cache"
+       print >>f, ""
 
 def writeDebList(list):
        return ', '.join(list)
@@ -9,6 +28,12 @@ def createDebianFiles(config):
        binaryName = config.get('binaryName', sourceName+'-local')
        name = config.get('name', os.getlogin())
        email = config.get('email', os.getlogin()+'@'+os.uname()[1])
+       debDir = os.path.expanduser(config['debDir'])
+       buildSystem = config['buildSystem']
+       version = config['version']
+       # we return the list of files generated
+       arch = getArchitecture()
+       files = []
        # source format file
        if not os.path.exists('debian/source'): os.mkdir('debian/source')
        with open('debian/source/format', 'w') as f:
@@ -16,13 +41,20 @@ def createDebianFiles(config):
        # compat file
        with open('debian/compat', 'w') as f:
                print >>f, "9"
-       # changelog file
-       with open('debian/changlog', 'w') as f:
+       # copyright file
+       with open('debian/copyright', 'w') as f:
                print >>f, "Auto-generated by auto-debuild, not suited for distribution"
+       # changelog file
+       with open('debian/changelog', 'w') as f:
+               print >>f, sourceName,"("+version+")","UNRELEASED; urgency=low"
+               print >>f, ""
+               print >>f, "  * Auto-generated by auto-debuild"
+               print >>f, ""
+               print >>f, " --",name,"<"+email+">  "+time.strftime('%a, %d %b %Y %H:%M:%S %z')
        # control file
        with open('debian/control', 'w') as f:
                print >>f, "Source:",sourceName
-               print >>f, "Section: misc"
+               print >>f, "Section:",config.get('section', 'misc')
                print >>f, "Priority: extra"
                print >>f, "Maintainer: %s <%s>" % (name, email)
                print >>f, "Build-Depends:",writeDebList(["debhelper (>= 9)"] + config.get('buildDepends', []))
@@ -32,16 +64,47 @@ def createDebianFiles(config):
                print >>f, "Architecture:",config.get('architecture', 'any')
                print >>f, "Depends:",writeDebList(["${shlibs:Depends}", "${misc:Depends}"] + config.get('binaryDepends', []))
                print >>f, "Provides:",writeDebList(config.get('binaryProvides', [sourceName]))
-               print >>f, "Description:",sourceName
-               print >>f, " Auto-generated package for",sourceName
-       # rules file
+               print >>f, "Description:",sourceName,"(auto-debuild)"
+               print >>f, " Package auto-generated by auto-debuild"
+               files.append(os.path.join(debDir, "%s_%s_%s.deb" % (binaryName, version, arch)))
+       # rules file: build system specific
        with open('debian/rules', 'w') as f:
-               pass
+               print >>f, "#!/usr/bin/make -f"
+               print >>f, ""
+               if buildSystem == 'cmake':
+                       writeCmakeRulesFile(f, config)
+               else:
+                       raise Exception("Invalid build system "+buildSystem)
+               print >>f, ""
+               print >>f, "override_dh_builddeb:" # put deb in other folder
+               print >>f, "\tdh_builddeb --destdir="+debDir
+               print >>f, "override_dh_auto_test:" # do not test
+               print >>f, ""
+               # overwrite: auto_config, auto_test, auto_clean, builddeb
        mode = os.stat('debian/rules').st_mode
        os.chmod('debian/rules', mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
+       # return list of files affected
+       return files
+
+def buildDebianPackage():
+       os.putenv("DEB_BUILD_OPTIONS", "parallel=2")
+       subprocess.check_call(['dpkg-checkbuilddeps'])
+       subprocess.check_call(['debian/rules', 'clean'])
+       subprocess.check_call(['debian/rules', 'build'])
+       subprocess.check_call(['fakeroot', 'debian/rules', 'binary'])
+       subprocess.check_call(['debian/rules', 'clean'])
 
+# if we are called directly as script
 if __name__ == "__main__":
        import imp
+       # generate debian files
        config = imp.load_source('config', 'debian/auto-debuild.py')
-       createDebianFiles(config.__dict__)
+       files = createDebianFiles(config.__dict__)
+       # check if a file is overwritten
+       for file in files:
+               if os.path.exists(file):
+                       if raw_input("Do you want to overwrite %s (y/N)? \n" % file).lower() != "y":
+                               sys.exit(1)
+       # run compilation
+       buildDebianPackage()