3a9deaf52d9e3913fe8adbf5d5bb0d88e8e8b387
[auto-debuild.git] / auto_debuild.py
1 #!/usr/bin/python
2 import os, stat, time, subprocess, sys
3
4 def getArchitecture():
5         p = subprocess.Popen(['dpkg-architecture', '-qDEB_BUILD_ARCH'], stdout=subprocess.PIPE)
6         res = p.communicate()[0] # get only stdout
7         if p.returncode != 0: raise Exception("Querying dpkg for the architecture failed")
8         return res[0:len(res)-1] # chop of the \n at the end
9
10 # build-system specific part of rules file
11 def writeCmakeRulesFile(f, config):
12         print >>f, "%:"
13         print >>f, "\tdh $@ --buildsystem cmake --parallel --builddirectory=build.dir" # "build" is not a good idea, as that's also the name of a target...
14         print >>f, ""
15         print >>f, "override_dh_auto_configure:"
16         print >>f, "\tmkdir -p build.dir"
17         print >>f, "\tcd build.dir && cmake ..",' '.join(['-DCMAKE_INSTALL_PREFIX=/usr', '-DCMAKE_BUILD_TYPE=Release'] + config.get('cmakeParameters', []))
18         print >>f, ""
19         print >>f, "override_dh_auto_clean:"
20         print >>f, "    rm -f build.dir/CMakeCache.txt # clean old cmake cache"
21         print >>f, ""
22
23 def writeDebList(list):
24         return ', '.join(list)
25
26 def createDebianFiles(config):
27         sourceName = config['sourceName']
28         binaryName = config.get('binaryName', sourceName+'-local')
29         name = config.get('name', os.getlogin())
30         email = config.get('email', os.getlogin()+'@'+os.uname()[1])
31         debDir = os.path.expanduser(config['debDir'])
32         buildSystem = config['buildSystem']
33         version = config['version']
34         # we return the list of files generated
35         arch = getArchitecture()
36         files = []
37         # source format file
38         if not os.path.exists('debian/source'): os.mkdir('debian/source')
39         with open('debian/source/format', 'w') as f:
40                 print >>f, "3.0 (native)"
41         # compat file
42         with open('debian/compat', 'w') as f:
43                 print >>f, "9"
44         # copyright file
45         with open('debian/copyright', 'w') as f:
46                 print >>f, "Auto-generated by auto-debuild, not suited for distribution"
47         # changelog file
48         with open('debian/changelog', 'w') as f:
49                 print >>f, sourceName,"("+version+")","UNRELEASED; urgency=low"
50                 print >>f, ""
51                 print >>f, "  * Auto-generated by auto-debuild"
52                 print >>f, ""
53                 print >>f, " --",name,"<"+email+">  "+time.strftime('%a, %d %b %Y %H:%M:%S %z')
54         # control file
55         with open('debian/control', 'w') as f:
56                 print >>f, "Source:",sourceName
57                 print >>f, "Section:",config.get('section', 'misc')
58                 print >>f, "Priority: extra"
59                 print >>f, "Maintainer: %s <%s>" % (name, email)
60                 print >>f, "Build-Depends:",writeDebList(["debhelper (>= 9)"] + config.get('buildDepends', []))
61                 print >>f, "Standards-Version: 3.9.3"
62                 print >>f, ""
63                 print >>f, "Package:",binaryName
64                 print >>f, "Architecture:",config.get('architecture', 'any')
65                 print >>f, "Depends:",writeDebList(["${shlibs:Depends}", "${misc:Depends}"] + config.get('binaryDepends', []))
66                 print >>f, "Provides:",writeDebList(config.get('binaryProvides', [sourceName]))
67                 print >>f, "Description:",sourceName,"(auto-debuild)"
68                 print >>f, " Package auto-generated by auto-debuild"
69                 files.append(os.path.join(debDir, "%s_%s_%s.deb" % (binaryName, version, arch)))
70         # rules file: build system specific
71         with open('debian/rules', 'w') as f:
72                 print >>f, "#!/usr/bin/make -f"
73                 print >>f, ""
74                 if buildSystem == 'cmake':
75                         writeCmakeRulesFile(f, config)
76                 else:
77                         raise Exception("Invalid build system "+buildSystem)
78                 print >>f, ""
79                 print >>f, "override_dh_builddeb:" # put deb in other folder
80                 print >>f, "\tdh_builddeb --destdir="+debDir
81                 print >>f, "override_dh_auto_test:" # do not test
82                 print >>f, ""
83                 # overwrite: auto_config, auto_test, auto_clean, builddeb
84         mode = os.stat('debian/rules').st_mode
85         os.chmod('debian/rules', mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
86         # return list of files affected
87         return files
88
89 def buildDebianPackage():
90         os.putenv("DEB_BUILD_OPTIONS", "parallel=2")
91         subprocess.check_call(['dpkg-checkbuilddeps'])
92         subprocess.check_call(['debian/rules', 'clean'])
93         subprocess.check_call(['debian/rules', 'build'])
94         subprocess.check_call(['fakeroot', 'debian/rules', 'binary'])
95         subprocess.check_call(['debian/rules', 'clean'])
96
97 # if we are called directly as script
98 if __name__ == "__main__":
99         import imp
100         # generate debian files
101         config = imp.load_source('config', 'debian/auto-debuild.py')
102         files = createDebianFiles(config.__dict__)
103         # check if a file is overwritten
104         for file in files:
105                 if os.path.exists(file):
106                         if raw_input("Do you want to overwrite %s (y/N)? \n" % file).lower() != "y":
107                                 sys.exit(1)
108         # run compilation
109         buildDebianPackage()
110