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, '\tDEB_BUILD_OPTIONS="'+self.buildOptions+'" 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 .. "+' '.join(['-DCMAKE_INSTALL_PREFIX=/usr', '-DCMAKE_BUILD_TYPE=Release'] + 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 # we return the list of files generated
51 arch = getArchitecture()
54 if not os.path.exists('debian/source'): os.mkdir('debian/source')
55 with open('debian/source/format', 'w') as f:
56 print >>f, "3.0 (native)"
58 with open('debian/compat', 'w') as f:
61 with open('debian/copyright', 'w') as f:
62 print >>f, "Auto-generated by auto-debuild, not suited for distribution"
64 with open('debian/changelog', 'w') as f:
65 print >>f, sourceName,"("+version+")","UNRELEASED; urgency=low"
67 print >>f, " * Auto-generated by auto-debuild"
69 print >>f, " --",name,"<"+email+"> "+time.strftime('%a, %d %b %Y %H:%M:%S %z')
71 with open('debian/control', 'w') as f:
72 print >>f, "Source:",sourceName
73 print >>f, "Section:",config.get('section', 'misc')
74 print >>f, "Priority: extra"
75 print >>f, "Maintainer: %s <%s>" % (name, email)
76 print >>f, "Build-Depends:",writeDebList(["debhelper (>= 9)"] + config.get('buildDepends', []))
77 print >>f, "Standards-Version: 3.9.3"
79 print >>f, "Package:",binaryName
80 print >>f, "Architecture:",config.get('architecture', 'any')
81 print >>f, "Depends:",writeDebList(["${shlibs:Depends}", "${misc:Depends}"] + config.get('binaryDepends', []))
82 print >>f, "Provides:",writeDebList(config.get('binaryProvides', [sourceName]))
83 print >>f, "Description:",sourceName,"(auto-debuild)"
84 print >>f, " Package auto-generated by auto-debuild"
85 files.append(os.path.join(debDir, "%s_%s_%s.deb" % (binaryName, version, arch)))
86 # rules file: build system specific
87 with open('debian/rules', 'w') as f:
88 # get rule file for build system
89 if buildSystem == 'cmake':
90 r = cmakeRules(config)
92 raise Exception("Invalid build system "+buildSystem)
94 r.buildOptions = "parallel=2"
95 r.dh += ['--parallel']
96 if not 'builddeb' in r.rules:
97 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...)
99 print "WARNING: Build system messes with global rules"
100 if not 'auto_test' in r.rules: # unless the build system has something in mind for this
101 r.rules['auto_test'] = []
104 mode = os.stat('debian/rules').st_mode
105 os.chmod('debian/rules', mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
106 # return list of files affected
109 def buildDebianPackage():
110 subprocess.check_call(['dpkg-checkbuilddeps'])
111 subprocess.check_call(['debian/rules', 'clean'])
112 subprocess.check_call(['debian/rules', 'build'])
113 subprocess.check_call(['fakeroot', 'debian/rules', 'binary'])
114 subprocess.check_call(['debian/rules', 'clean'])
116 # if we are called directly as script
117 if __name__ == "__main__":
118 # generate debian files
120 config = imp.load_source('config', 'debian/auto-debuild.conf')
121 os.remove('debian/auto-debuild.confc')
122 files = createDebianFiles(config.__dict__)
123 # check if a file is overwritten
125 if os.path.exists(file):
126 if raw_input("Do you want to overwrite %s (y/N)? " % file).lower() != "y":