2 import os, stat, time, subprocess, sys
3 from collections import OrderedDict
9 self.rules = OrderedDict()
12 print >>f, "#!/usr/bin/make -f"
15 print >>f, '\t'+' '.join(self.env)+' dh $@',' '.join(self.dh)
16 for rule in self.rules:
18 print >>f, "override_dh_"+rule+":"
19 for line in self.rules[rule]:
22 def getArchitecture():
23 p = subprocess.Popen(['dpkg-architecture', '-qDEB_BUILD_ARCH'], stdout=subprocess.PIPE)
24 res = p.communicate()[0] # get only stdout
25 if p.returncode != 0: raise Exception("Querying dpkg for the architecture failed")
26 return res[0:len(res)-1] # chop of the \n at the end
28 # build-system specific part of rules file
29 def cmakeRules(config):
31 r.dh += ["--buildsystem=cmake", "--builddirectory=build.dir"] # dh parameters: "build" is not a good idea, as that's also the name of a target...
32 r.rules['auto_configure'] = [
34 "cd build.dir && cmake .. -DCMAKE_INSTALL_PREFIX=/usr "+' '.join(config.get('cmakeParameters', []))
36 r.rules['auto_clean'] = ['rm -f build.dir/CMakeCache.txt'] # clean old cmake cache
39 def writeDebList(list):
40 return ', '.join(list)
42 def createDebianFiles(config):
43 sourceName = config['sourceName']
44 binaryName = config.get('binaryName', sourceName+'-local')
45 name = config.get('name', os.getlogin())
46 email = config.get('email', os.getlogin()+'@'+os.uname()[1])
47 debDir = os.path.expanduser(config['debDir'])
48 buildSystem = config['buildSystem']
49 version = config['version']
50 dbgPackage = config.get('dbgPackage', False)
51 packageArchitecture = config.get('architecture', 'any')
52 # we return the list of files generated
53 arch = getArchitecture()
56 if not os.path.exists('debian/source'): os.mkdir('debian/source')
57 with open('debian/source/format', 'w') as f:
58 print >>f, "3.0 (native)"
60 with open('debian/compat', 'w') as f:
63 with open('debian/copyright', 'w') as f:
64 print >>f, "Auto-generated by auto-debuild, not suited for distribution"
66 with open('debian/changelog', 'w') as f:
67 print >>f, sourceName,"("+version+")","UNRELEASED; urgency=low"
69 print >>f, " * Auto-generated by auto-debuild"
71 print >>f, " --",name,"<"+email+"> "+time.strftime('%a, %d %b %Y %H:%M:%S %z')
73 with open('debian/control', 'w') as f:
74 print >>f, "Source:",sourceName
75 print >>f, "Section:",config.get('section', 'misc')
76 print >>f, "Priority: extra"
77 print >>f, "Maintainer: %s <%s>" % (name, email)
78 print >>f, "Build-Depends:",writeDebList(["debhelper (>= 9)"] + config.get('buildDepends', []))
79 print >>f, "Standards-Version: 3.9.3"
81 print >>f, "Package:",binaryName
82 print >>f, "Architecture:",packageArchitecture
83 print >>f, "Depends:",writeDebList(["${shlibs:Depends}", "${misc:Depends}"] + config.get('binaryDepends', []))
84 print >>f, "Provides:",writeDebList(config.get('binaryProvides', [sourceName]))
85 print >>f, "Description:",sourceName,"(auto-debuild)"
86 print >>f, " Package auto-generated by auto-debuild."
87 files.append(os.path.join(debDir, "%s_%s_%s.deb" % (binaryName, version, arch)))
90 print >>f, "Package:",binaryName+"-dbg"
91 print >>f, "Architecture:",packageArchitecture
92 print >>f, "Depends:",writeDebList(["${misc:Depends}", binaryName+" (= ${binary:Version})"])
93 print >>f, "Description:",sourceName,"debug smbols (auto-debuild)"
94 print >>f, " Package containing debug symbols for "+sourceName+", auto-generated by auto-debuild."
95 files.append(os.path.join(debDir, "%s-dbg_%s_%s.deb" % (binaryName, version, arch)))
96 # rules file: build system specific
97 with open('debian/rules', 'w') as f:
98 # get rule file for build system: may only touch auto_config and auto_clean rules and the dh options
99 if buildSystem == 'cmake':
100 r = cmakeRules(config)
102 raise Exception("Invalid build system "+buildSystem)
104 r.env += ["DEB_BUILD_OPTIONS='parallel=2'"]
105 if not dbgPackage: r.env += ["DEB_CFLAGS_APPEND='-g0'", "DEB_CXXFLAGS_APPEND='-g0'"] # disable debug information
106 r.dh += ['--parallel']
107 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...)
108 r.rules['auto_test'] = []
111 r.rules['strip'] = ['dh_strip --dbg-package='+binaryName+"-dbg"]
112 r.rules['auto_install'] = ['dh_auto_install --destdir=debian/'+binaryName]
113 r.rules['installdocs'] = ['dh_installdocs --link-doc='+binaryName]
116 mode = os.stat('debian/rules').st_mode
117 os.chmod('debian/rules', mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
118 # return list of files affected
121 def buildDebianPackage():
122 subprocess.check_call(['dpkg-checkbuilddeps'])
123 subprocess.check_call(['debian/rules', 'clean'])
124 subprocess.check_call(['debian/rules', 'build'])
125 subprocess.check_call(['fakeroot', 'debian/rules', 'binary'])
126 subprocess.check_call(['debian/rules', 'clean'])
128 # if we are called directly as script
129 if __name__ == "__main__":
130 # generate debian files
132 config = imp.load_source('config', 'debian/auto-debuild.conf')
133 os.remove('debian/auto-debuild.confc')
134 files = createDebianFiles(config.__dict__)
135 # check if a file is overwritten
137 if os.path.exists(file):
138 if raw_input("Do you want to overwrite %s (y/N)? " % file).lower() != "y":
143 subprocess.check_call(['sudo', 'dpkg', '--install'] + files)