Create all necessary paths automatically
[auto-debuild.git] / auto_debuild.py
index 0db538871e70e43fb814c2bfcb0c506ffff05c87..eda72e3a5f366b478077c42a1b07907f8ad87933 100755 (executable)
@@ -35,12 +35,11 @@ def automakeRules(config):
        r = RulesFile()
        r.dh += ["--buildsystem=autoconf"]
        r.rules['auto_configure'] = [
        r = RulesFile()
        r.dh += ["--buildsystem=autoconf"]
        r.rules['auto_configure'] = [
-               '@dpkg-architecture -qDEB_BUILD_GNU_TYPE > /dev/null', # make sure this command runs successfully (and hope it does so again)
-               '@dpkg-architecture -qDEB_BUILD_MULTIARCH > /dev/null', # make sure this command runs successfully (and hope it does so again)
-               './configure --build=$$(dpkg-architecture -qDEB_BUILD_GNU_TYPE) ' +
+               'BUILD_TYPE=$$(dpkg-architecture -qDEB_BUILD_GNU_TYPE) && ' + # doing the expansion beforehand ensures that we cancel if it fails
+               'MULTIARCH=$$(dpkg-architecture -qDEB_BUILD_MULTIARCH) && '+
+                       './configure --build=$$BUILD_TYPE ' +
                        '--prefix=/usr --includedir=/usr/include --mandir=/usr/share/man --infodir=/usr/share/info ' +
                        '--prefix=/usr --includedir=/usr/include --mandir=/usr/share/man --infodir=/usr/share/info ' +
-                       '--libdir=/usr/lib/$$(dpkg-architecture -qDEB_BUILD_MULTIARCH) '+
-                       '--libexecdir=/usr/lib/$$(dpkg-architecture -qDEB_BUILD_MULTIARCH) '+
+                       '--libdir=/usr/lib/$$MULTIARCH --libexecdir=/usr/lib/$$MULTIARCH '+
                        '--sysconfdir=/etc --localstatedir=/var ' +
                        ' '.join(config.get('automakeParameters', []))
        ]
                        '--sysconfdir=/etc --localstatedir=/var ' +
                        ' '.join(config.get('automakeParameters', []))
        ]
@@ -60,8 +59,9 @@ def getArchitecture(config):
        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
 
        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
 
-def writeDebList(list):
-       return ', '.join(list)
+def writeDependency(f, name, list):
+       if len(list):
+               print >>f, name+": "+', '.join(list)
 
 # actual work functions
 def createDebianFiles(config):
 
 # actual work functions
 def createDebianFiles(config):
@@ -74,11 +74,14 @@ def createDebianFiles(config):
        version = config['version']
        dbgPackage = config.get('dbgPackage', False)
        packageArchitecture = config.get('architecture', 'any')
        version = config['version']
        dbgPackage = config.get('dbgPackage', False)
        packageArchitecture = config.get('architecture', 'any')
-       # we return the list of files generated
+       # we return the list of files generated, so we need to know the architecture
        arch = getArchitecture(config)
        files = []
        arch = getArchitecture(config)
        files = []
-       # source format file
+       # create folders
+       if not os.path.exists('debian'): os.mkdir('debian')
        if not os.path.exists('debian/source'): os.mkdir('debian/source')
        if not os.path.exists('debian/source'): os.mkdir('debian/source')
+       if not os.path.exists(debDir): os.mkdir(debDir)
+       # source format file
        with open('debian/source/format', 'w') as f:
                print >>f, "3.0 (native)"
        # compat file
        with open('debian/source/format', 'w') as f:
                print >>f, "3.0 (native)"
        # compat file
@@ -100,13 +103,15 @@ def createDebianFiles(config):
                print >>f, "Section:",config.get('section', 'misc')
                print >>f, "Priority: extra"
                print >>f, "Maintainer: %s <%s>" % (name, email)
                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', []))
+               writeDependency(f, 'Build-Depends', ["debhelper (>= 9)"] + config.get('buildDepends', []))
                print >>f, "Standards-Version: 3.9.3"
                print >>f, ""
                print >>f, "Package:",binaryName
                print >>f, "Architecture:",packageArchitecture
                print >>f, "Standards-Version: 3.9.3"
                print >>f, ""
                print >>f, "Package:",binaryName
                print >>f, "Architecture:",packageArchitecture
-               print >>f, "Depends:",writeDebList(["${shlibs:Depends}", "${misc:Depends}"] + config.get('binaryDepends', []))
-               print >>f, "Provides:",writeDebList(config.get('binaryProvides', [sourceName]))
+               writeDependency(f, "Pre-Depends", ["${misc:Pre-Depends}"] + config.get('binaryPreDepends', []))
+               writeDependency(f, "Depends", ["${shlibs:Depends}", "${misc:Depends}"] + config.get('binaryDepends', []))
+               writeDependency(f, "Recommends", config.get('binaryRecommends', []))
+               writeDependency(f, "Provides", config.get('binaryProvides', [sourceName]))
                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)))
                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)))
@@ -114,7 +119,7 @@ def createDebianFiles(config):
                        print >>f, ""
                        print >>f, "Package:",binaryName+"-dbg"
                        print >>f, "Architecture:",packageArchitecture
                        print >>f, ""
                        print >>f, "Package:",binaryName+"-dbg"
                        print >>f, "Architecture:",packageArchitecture
-                       print >>f, "Depends:",writeDebList(["${misc:Depends}", binaryName+" (= ${binary:Version})"])
+                       writeDependency(f, "Depends", ["${misc:Depends}", binaryName+" (= ${binary:Version})"])
                        print >>f, "Description:",sourceName,"debug smbols (auto-debuild)"
                        print >>f, " Package containing debug symbols for "+sourceName+", auto-generated by auto-debuild."
                        files.append(os.path.join(debDir, "%s-dbg_%s_%s.deb" % (binaryName, version, arch)))
                        print >>f, "Description:",sourceName,"debug smbols (auto-debuild)"
                        print >>f, " Package containing debug symbols for "+sourceName+", auto-generated by auto-debuild."
                        files.append(os.path.join(debDir, "%s-dbg_%s_%s.deb" % (binaryName, version, arch)))
@@ -136,8 +141,8 @@ def createDebianFiles(config):
                r.rules['auto_install'] = ['dh_auto_install --destdir=debian/'+binaryName] # install everything into the binary package
                # for debug packages
                if dbgPackage:
                r.rules['auto_install'] = ['dh_auto_install --destdir=debian/'+binaryName] # install everything into the binary package
                # for debug packages
                if dbgPackage:
-                       r.rules['strip'] = ['dh_strip --dbg-package='+binaryName+"-dbg"]
-                       r.rules['installdocs'] = ['dh_installdocs --link-doc='+binaryName]
+                       r.rules['strip'] = ['dh_strip --dbg-package='+binaryName+"-dbg"] # put debug files in appropriate package
+                       r.rules['installdocs'] = ['dh_installdocs --link-doc='+binaryName] # make the doc folder of the dbg package a symlink
                # dump it to a file
                r.write(f)
        mode = os.stat('debian/rules').st_mode
                # dump it to a file
                r.write(f)
        mode = os.stat('debian/rules').st_mode
@@ -147,22 +152,30 @@ def createDebianFiles(config):
 
 def buildDebianPackage(config):
        commands = ['dpkg-checkbuilddeps', 'debian/rules clean', 'debian/rules build', 'fakeroot debian/rules binary', 'debian/rules clean']
 
 def buildDebianPackage(config):
        commands = ['dpkg-checkbuilddeps', 'debian/rules clean', 'debian/rules build', 'fakeroot debian/rules binary', 'debian/rules clean']
-       command = ['bash', '-c', ' && '.join(commands)]
+       command = ['nice', 'bash', '-c', ' && '.join(commands)]
        subprocess.check_call(commandInBuildEnv(config, command))
 
        subprocess.check_call(commandInBuildEnv(config, command))
 
-# if we are called directly as script
-if __name__ == "__main__":
+# all at once
+def createAndInstall(config, overwriteCheck = False):
        # generate debian files
        # generate debian files
-       import imp
-       config = imp.load_source('config', 'debian/auto-debuild.conf').__dict__
-       os.remove('debian/auto-debuild.confc')
        files = createDebianFiles(config)
        # check if a file is overwritten
        files = createDebianFiles(config)
        # 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)? " % file).lower() != "y":
-                               sys.exit(1)
+       if overwriteCheck:
+               for file in files:
+                       if os.path.exists(file):
+                               if raw_input("Do you want to overwrite %s (y/N)? " % file).lower() != "y":
+                                       sys.exit(1)
        # run compilation
        buildDebianPackage(config)
        # install files
        # run compilation
        buildDebianPackage(config)
        # install files
+       print "Installing created deb files..."
        subprocess.check_call(['sudo', 'dpkg', '--install'] + files)
        subprocess.check_call(['sudo', 'dpkg', '--install'] + files)
+
+# if we are called directly as script
+if __name__ == "__main__":
+       # get config
+       import imp
+       config = imp.load_source('config', 'debian/auto-debuild.conf').__dict__
+       os.remove('debian/auto-debuild.confc')
+       # and go for it
+       createAndInstall(config, overwriteCheck=True)