f8ff6419255c39784c9a9ff21254629b36b67440
[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(config):
30         r = RulesFile()
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'] = [
33                 'mkdir -p build.dir',
34                 "cd build.dir && cmake .. "+' '.join(['-DCMAKE_INSTALL_PREFIX=/usr', '-DCMAKE_BUILD_TYPE=Release'] + config.get('cmakeParameters', []))
35         ]
36         r.rules['auto_clean'] = ['rm -f build.dir/CMakeCache.txt'] # clean old cmake cache
37         return r
38
39 def writeDebList(list):
40         return ', '.join(list)
41
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()
52         files = []
53         # source format file
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)"
57         # compat file
58         with open('debian/compat', 'w') as f:
59                 print >>f, "9"
60         # copyright file
61         with open('debian/copyright', 'w') as f:
62                 print >>f, "Auto-generated by auto-debuild, not suited for distribution"
63         # changelog file
64         with open('debian/changelog', 'w') as f:
65                 print >>f, sourceName,"("+version+")","UNRELEASED; urgency=low"
66                 print >>f, ""
67                 print >>f, "  * Auto-generated by auto-debuild"
68                 print >>f, ""
69                 print >>f, " --",name,"<"+email+">  "+time.strftime('%a, %d %b %Y %H:%M:%S %z')
70         # control file
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"
78                 print >>f, ""
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)
91                 else:
92                         raise Exception("Invalid build system "+buildSystem)
93                 # global rules
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...)
98                 else:
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'] = []
102                 # dump it to a file
103                 r.write(f)
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
107         return files
108
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'])
115
116 # if we are called directly as script
117 if __name__ == "__main__":
118         # generate debian files
119         import imp
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
124         for file in files:
125                 if os.path.exists(file):
126                         if raw_input("Do you want to overwrite %s (y/N)? " % file).lower() != "y":
127                                 sys.exit(1)
128         # run compilation
129         buildDebianPackage()