2 import os, stat, time, subprocess, sys
3 from collections import OrderedDict
5 # abstract representation of rules file
10 self.rules = OrderedDict()
13 print >>f, "#!/usr/bin/make -f"
16 print >>f, '\t'+' '.join(self.env)+' dh $@',' '.join(self.dh)
17 for rule in self.rules:
19 print >>f, "override_dh_"+rule+":"
20 for line in self.rules[rule]:
23 # build-system specific part of rules file
24 def cmakeRules(config):
26 r.dh += ["--buildsystem=cmake", "--builddirectory=build.dir"] # dh parameters: "build" is not a good idea, as that's also the name of a target...
27 r.rules['auto_configure'] = [
29 "cd build.dir && cmake .. -DCMAKE_INSTALL_PREFIX=/usr "+' '.join(config.get('cmakeParameters', []))
31 r.rules['auto_clean'] = ['rm -f build.dir/CMakeCache.txt'] # clean old cmake cache
34 def automakeRules(config):
36 r.dh += ["--buildsystem=autoconf"]
37 r.rules['auto_configure'] = [
38 'BUILD_TYPE=$$(dpkg-architecture -qDEB_BUILD_GNU_TYPE) && ' + # doing the expansion beforehand ensures that we cancel if it fails
39 'MULTIARCH=$$(dpkg-architecture -qDEB_BUILD_MULTIARCH) && '+
40 './configure --build=$$BUILD_TYPE ' +
41 '--prefix=/usr --includedir=/usr/include --mandir=/usr/share/man --infodir=/usr/share/info ' +
42 '--libdir=/usr/lib/$$MULTIARCH --libexecdir=/usr/lib/$$MULTIARCH '+
43 '--sysconfdir=/etc --localstatedir=/var ' +
44 ' '.join(config.get('automakeParameters', []))
46 r.rules['auto_clean'] = ['rm -f config.status'] # do not re-use old configuration
50 def commandInBuildEnv(config, command):
51 schroot = config.get('schroot')
52 if schroot is not None: command = ['schroot', '-c', schroot, '--'] + command
55 def getArchitecture(config):
56 cmd = commandInBuildEnv(config, ['dpkg-architecture', '-qDEB_BUILD_ARCH'])
57 p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
58 res = p.communicate()[0] # get only stdout
59 if p.returncode != 0: raise Exception("Querying dpkg for the architecture failed")
60 return res[0:len(res)-1] # chop of the \n at the end
62 def writeDependency(f, name, list):
64 print >>f, name+": "+', '.join(list)
66 # actual work functions
67 def createDebianFiles(config):
68 sourceName = config['sourceName']
69 binaryName = config.get('binaryName', sourceName+'-local')
70 name = config.get('name', os.getlogin())
71 email = config.get('email', os.getlogin()+'@'+os.uname()[1]) # user@hostname
72 debDir = os.path.expanduser(config['debDir'])
73 buildSystem = config['buildSystem']
74 version = config['version']
75 dbgPackage = config.get('dbgPackage', False)
76 packageArchitecture = config.get('architecture', 'any')
77 # we return the list of files generated, so we need to know the architecture
78 arch = getArchitecture(config)
81 if not os.path.exists('debian'): os.mkdir('debian')
82 if not os.path.exists('debian/source'): os.mkdir('debian/source')
83 if not os.path.exists(debDir): os.mkdir(debDir)
85 with open('debian/source/format', 'w') as f:
86 print >>f, "3.0 (native)"
88 with open('debian/compat', 'w') as f:
91 with open('debian/copyright', 'w') as f:
92 print >>f, "Auto-generated by auto-debuild, not suited for distribution"
94 with open('debian/changelog', 'w') as f:
95 print >>f, sourceName,"("+version+")","UNRELEASED; urgency=low"
97 print >>f, " * Auto-generated by auto-debuild"
99 print >>f, " --",name,"<"+email+"> "+time.strftime('%a, %d %b %Y %H:%M:%S %z')
101 with open('debian/control', 'w') as f:
102 print >>f, "Source:",sourceName
103 print >>f, "Section:",config.get('section', 'misc')
104 print >>f, "Priority: extra"
105 print >>f, "Maintainer: %s <%s>" % (name, email)
106 writeDependency(f, 'Build-Depends', ["debhelper (>= 9)"] + config.get('buildDepends', []))
107 print >>f, "Standards-Version: 3.9.3"
109 print >>f, "Package:",binaryName
110 print >>f, "Architecture:",packageArchitecture
111 writeDependency(f, "Pre-Depends", ["${misc:Pre-Depends}"] + config.get('binaryPreDepends', []))
112 writeDependency(f, "Depends", ["${shlibs:Depends}", "${misc:Depends}"] + config.get('binaryDepends', []))
113 writeDependency(f, "Recommends", config.get('binaryRecommends', []))
114 writeDependency(f, "Provides", config.get('binaryProvides', [sourceName]))
115 print >>f, "Description:",sourceName,"(auto-debuild)"
116 print >>f, " Package auto-generated by auto-debuild."
117 files.append(os.path.join(debDir, "%s_%s_%s.deb" % (binaryName, version, arch)))
120 print >>f, "Package:",binaryName+"-dbg"
121 print >>f, "Architecture:",packageArchitecture
122 writeDependency(f, "Depends", ["${misc:Depends}", binaryName+" (= ${binary:Version})"])
123 print >>f, "Description:",sourceName,"debug smbols (auto-debuild)"
124 print >>f, " Package containing debug symbols for "+sourceName+", auto-generated by auto-debuild."
125 files.append(os.path.join(debDir, "%s-dbg_%s_%s.deb" % (binaryName, version, arch)))
126 # rules file: build system specific
127 with open('debian/rules', 'w') as f:
128 # get rule file for build system: may only touch auto_config and auto_clean rules and the dh options
129 if buildSystem == 'cmake':
130 r = cmakeRules(config)
131 elif buildSystem == 'automake':
132 r = automakeRules(config)
134 raise Exception("Invalid build system "+buildSystem)
136 r.env += ["DEB_BUILD_OPTIONS='parallel=2'"]
137 if not dbgPackage: r.env += ["DEB_CFLAGS_APPEND='-g0'", "DEB_CXXFLAGS_APPEND='-g0'"] # disable debug information
138 r.dh += ['--parallel']
139 r.rules['builddeb'] = ['dh_builddeb --destdir='+debDir] # passing this gobally to dh results in weird problems (like stuff being installed there, and not in the package...)
140 r.rules['auto_test'] = []
141 r.rules['auto_install'] = ['dh_auto_install --destdir=debian/'+binaryName] # install everything into the binary package
144 r.rules['strip'] = ['dh_strip --dbg-package='+binaryName+"-dbg"] # put debug files in appropriate package
145 r.rules['installdocs'] = ['dh_installdocs --link-doc='+binaryName] # make the doc folder of the dbg package a symlink
148 mode = os.stat('debian/rules').st_mode
149 os.chmod('debian/rules', mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
150 # return list of files affected
153 def buildDebianPackage(config):
154 commands = ['dpkg-checkbuilddeps', 'debian/rules clean', 'debian/rules build', 'fakeroot debian/rules binary', 'debian/rules clean']
155 command = ['nice', 'bash', '-c', ' && '.join(commands)]
156 subprocess.check_call(commandInBuildEnv(config, command))
159 def createAndInstall(config, overwriteCheck = False):
160 # generate debian files
161 files = createDebianFiles(config)
162 # check if a file is overwritten
165 if os.path.exists(file):
166 if raw_input("Do you want to overwrite %s (y/N)? " % file).lower() != "y":
169 buildDebianPackage(config)
171 print "Installing created deb files..."
172 subprocess.check_call(['sudo', 'dpkg', '--install'] + files)
174 # if we are called directly as script
175 if __name__ == "__main__":
178 config = imp.load_source('config', 'debian/auto-debuild.conf').__dict__
179 os.remove('debian/auto-debuild.confc')
181 createAndInstall(config, overwriteCheck=True)