install packages after creating them; add support for debug packages
[auto-debuild.git] / auto_debuild.py
1 #!/usr/bin/python
2 import os, stat, time, subprocess, sys
3 from collections import OrderedDict
4
5 class RulesFile:
6         def __init__(self):
7                 self.buildOptions = ''
8                 self.dh = []
9                 self.rules = OrderedDict()
10         
11         def write(self, f):
12                 print >>f, "#!/usr/bin/make -f"
13                 print >>f, ""
14                 print >>f, "%:"
15                 print >>f, '\tDEB_BUILD_OPTIONS="'+self.buildOptions+'" dh $@',' '.join(self.dh)
16                 for rule in self.rules:
17                         print >>f, ""
18                         print >>f, "override_dh_"+rule+":"
19                         for line in self.rules[rule]:
20                                 print >>f, "\t"+line
21
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
27
28 # build-system specific part of rules file
29 def cmakeRules(dbg, config):
30         buildType = 'RelWithDebInfo' if dbg else 'Release'
31         r = RulesFile()
32         r.dh += ["--buildsystem=cmake", "--builddirectory=build.dir"] # dh parameters: "build" is not a good idea, as that's also the name of a target...
33         r.rules['auto_configure'] = [
34                 'mkdir -p build.dir',
35                 "cd build.dir && cmake .. "+' '.join(['-DCMAKE_INSTALL_PREFIX=/usr', '-DCMAKE_BUILD_TYPE='+buildType] + config.get('cmakeParameters', []))
36         ]
37         r.rules['auto_clean'] = ['rm -f build.dir/CMakeCache.txt'] # clean old cmake cache
38         return r
39
40 def writeDebList(list):
41         return ', '.join(list)
42
43 def createDebianFiles(config):
44         sourceName = config['sourceName']
45         binaryName = config.get('binaryName', sourceName+'-local')
46         name = config.get('name', os.getlogin())
47         email = config.get('email', os.getlogin()+'@'+os.uname()[1])
48         debDir = os.path.expanduser(config['debDir'])
49         buildSystem = config['buildSystem']
50         version = config['version']
51         dbgPackage = config.get('dbgPackage', False)
52         packageArchitecture = config.get('architecture', 'any')
53         # we return the list of files generated
54         arch = getArchitecture()
55         files = []
56         # source format file
57         if not os.path.exists('debian/source'): os.mkdir('debian/source')
58         with open('debian/source/format', 'w') as f:
59                 print >>f, "3.0 (native)"
60         # compat file
61         with open('debian/compat', 'w') as f:
62                 print >>f, "9"
63         # copyright file
64         with open('debian/copyright', 'w') as f:
65                 print >>f, "Auto-generated by auto-debuild, not suited for distribution"
66         # changelog file
67         with open('debian/changelog', 'w') as f:
68                 print >>f, sourceName,"("+version+")","UNRELEASED; urgency=low"
69                 print >>f, ""
70                 print >>f, "  * Auto-generated by auto-debuild"
71                 print >>f, ""
72                 print >>f, " --",name,"<"+email+">  "+time.strftime('%a, %d %b %Y %H:%M:%S %z')
73         # control file
74         with open('debian/control', 'w') as f:
75                 print >>f, "Source:",sourceName
76                 print >>f, "Section:",config.get('section', 'misc')
77                 print >>f, "Priority: extra"
78                 print >>f, "Maintainer: %s <%s>" % (name, email)
79                 print >>f, "Build-Depends:",writeDebList(["debhelper (>= 9)"] + config.get('buildDepends', []))
80                 print >>f, "Standards-Version: 3.9.3"
81                 print >>f, ""
82                 print >>f, "Package:",binaryName
83                 print >>f, "Architecture:",packageArchitecture
84                 print >>f, "Depends:",writeDebList(["${shlibs:Depends}", "${misc:Depends}"] + config.get('binaryDepends', []))
85                 print >>f, "Provides:",writeDebList(config.get('binaryProvides', [sourceName]))
86                 print >>f, "Description:",sourceName,"(auto-debuild)"
87                 print >>f, " Package auto-generated by auto-debuild."
88                 files.append(os.path.join(debDir, "%s_%s_%s.deb" % (binaryName, version, arch)))
89                 if dbgPackage:
90                         print >>f, ""
91                         print >>f, "Package:",binaryName+"-dbg"
92                         print >>f, "Architecture:",packageArchitecture
93                         print >>f, "Depends:",writeDebList(["${misc:Depends}", binaryName+" (= ${binary:Version})"])
94                         print >>f, "Description:",sourceName,"debug smbols (auto-debuild)"
95                         print >>f, " Package containing debug symbols for "+sourceName+", auto-generated by auto-debuild."
96                         files.append(os.path.join(debDir, "%s-dbg_%s_%s.deb" % (binaryName, version, arch)))
97         # rules file: build system specific
98         with open('debian/rules', 'w') as f:
99                 # get rule file for build system: may only touch auto_config and auto_clean rules and the dh options
100                 if buildSystem == 'cmake':
101                         r = cmakeRules(dbgPackage, config)
102                 else:
103                         raise Exception("Invalid build system "+buildSystem)
104                 # global rules
105                 r.buildOptions = "parallel=2"
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'] = []
109                 # for debug packages
110                 if dbgPackage:
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]
114                 # dump it to a file
115                 r.write(f)
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
119         return files
120
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'])
127
128 # if we are called directly as script
129 if __name__ == "__main__":
130         # generate debian files
131         import imp
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
136         for file in files:
137                 if os.path.exists(file):
138                         if raw_input("Do you want to overwrite %s (y/N)? " % file).lower() != "y":
139                                 sys.exit(1)
140         # run compilation
141         buildDebianPackage()
142         # install files
143         subprocess.check_call(['sudo', 'dpkg', '--install'] + files)