merge of '274cb1a3f97188420857e4b0650a1ace718148a6'
[vuplus_openembedded] / classes / package_deb.bbclass
1 inherit package
2 DEPENDS_prepend="${@["dpkg-native ", ""][(bb.data.getVar('PACKAGES', d, 1) == '')]}"
3 BOOTSTRAP_EXTRA_RDEPENDS += "dpkg"
4 DISTRO_EXTRA_RDEPENDS += "dpkg"
5 PACKAGE_WRITE_FUNCS += "do_package_deb"
6 IMAGE_PKGTYPE ?= "deb"
7
8 python package_deb_fn () {
9     from bb import data
10     bb.data.setVar('PKGFN', bb.data.getVar('PKG',d), d)
11 }
12
13 addtask package_deb_install
14 python do_package_deb_install () {
15     import os, sys
16     pkg = bb.data.getVar('PKG', d, 1)
17     pkgfn = bb.data.getVar('PKGFN', d, 1)
18     rootfs = bb.data.getVar('IMAGE_ROOTFS', d, 1)
19     debdir = bb.data.getVar('DEPLOY_DIR_DEB', d, 1)
20     stagingdir = bb.data.getVar('STAGING_DIR', d, 1)
21     stagingbindir = bb.data.getVar('STAGING_BINDIR_NATIVE', d, 1)
22     tmpdir = bb.data.getVar('TMPDIR', d, 1)
23
24     if None in (pkg,pkgfn,rootfs):
25         raise bb.build.FuncFailed("missing variables (one or more of PKG, PKGFN, IMAGE_ROOTFS)")
26     try:
27         if not os.exists(rootfs):
28             os.makedirs(rootfs)
29         os.chdir(rootfs)
30     except OSError:
31         raise bb.build.FuncFailed(str(sys.exc_value))
32
33     # update packages file
34     (exitstatus, output) = commands.getstatusoutput('dpkg-scanpackages %s > %s/Packages' % (debdir, debdir))
35     if (exitstatus != 0 ):
36         raise bb.build.FuncFailed(output)
37
38     f = open(os.path.join(tmpdir, "stamps", "do_packages"), "w")
39     f.close()
40
41     # NOTE: this env stuff is racy at best, we need something more capable
42     # than 'commands' for command execution, which includes manipulating the
43     # env of the fork+execve'd processs
44
45     # Set up environment
46     apt_config = os.getenv('APT_CONFIG')
47     os.putenv('APT_CONFIG', os.path.join(stagingdir, 'etc', 'apt', 'apt.conf'))
48     path = os.getenv('PATH')
49     os.putenv('PATH', '%s:%s' % (stagingbindir, os.getenv('PATH')))
50
51     # install package
52     commands.getstatusoutput('apt-get update')
53     commands.getstatusoutput('apt-get install -y %s' % pkgfn)
54
55     # revert environment
56     os.putenv('APT_CONFIG', apt_config)
57     os.putenv('PATH', path)
58 }
59
60 python do_package_deb () {
61     import copy # to back up env data
62     import sys
63     import re
64
65     workdir = bb.data.getVar('WORKDIR', d, 1)
66     if not workdir:
67         bb.error("WORKDIR not defined, unable to package")
68         return
69
70     import os # path manipulations
71     outdir = bb.data.getVar('DEPLOY_DIR_DEB', d, 1)
72     if not outdir:
73         bb.error("DEPLOY_DIR_DEB not defined, unable to package")
74         return
75
76     dvar = bb.data.getVar('D', d, 1)
77     if not dvar:
78         bb.error("D not defined, unable to package")
79         return
80     bb.mkdirhier(dvar)
81
82     packages = bb.data.getVar('PACKAGES', d, 1)
83     if not packages:
84         bb.debug(1, "PACKAGES not defined, nothing to package")
85         return
86
87     tmpdir = bb.data.getVar('TMPDIR', d, 1)
88     # Invalidate the packages file
89     if os.access(os.path.join(tmpdir, "stamps", "do_packages"),os.R_OK):
90         os.unlink(os.path.join(tmpdir, "stamps", "do_packages"))
91
92     if packages == []:
93         bb.debug(1, "No packages; nothing to do")
94         return
95
96     for pkg in packages.split():
97         localdata = bb.data.createCopy(d)
98         root = "%s/install/%s" % (workdir, pkg)
99
100         bb.data.setVar('ROOT', '', localdata)
101         bb.data.setVar('ROOT_%s' % pkg, root, localdata)
102         pkgname = bb.data.getVar('PKG_%s' % pkg, localdata, 1)
103         if not pkgname:
104             pkgname = pkg
105         bb.data.setVar('PKG', pkgname, localdata)
106
107         overrides = bb.data.getVar('OVERRIDES', localdata)
108         if not overrides:
109             raise bb.build.FuncFailed('OVERRIDES not defined')
110         overrides = bb.data.expand(overrides, localdata)
111         bb.data.setVar('OVERRIDES', overrides + ':' + pkg, localdata)
112
113         bb.data.update_data(localdata)
114         basedir = os.path.join(os.path.dirname(root))
115
116         pkgoutdir = os.path.join(outdir, bb.data.getVar('PACKAGE_ARCH', localdata, 1))
117         bb.mkdirhier(pkgoutdir)
118
119         os.chdir(root)
120         from glob import glob
121         g = glob('*')
122         try:
123             del g[g.index('DEBIAN')]
124             del g[g.index('./DEBIAN')]
125         except ValueError:
126             pass
127         if not g and not bb.data.getVar('ALLOW_EMPTY', localdata):
128             from bb import note
129             note("Not creating empty archive for %s-%s-%s" % (pkg, bb.data.getVar('PV', localdata, 1), bb.data.getVar('PR', localdata, 1)))
130             continue
131         controldir = os.path.join(root, 'DEBIAN')
132         bb.mkdirhier(controldir)
133         try:
134             ctrlfile = file(os.path.join(controldir, 'control'), 'wb')
135             # import codecs
136             # ctrlfile = codecs.open("someFile", "w", "utf-8")
137         except OSError:
138             raise bb.build.FuncFailed("unable to open control file for writing.")
139
140         fields = []
141         fields.append(["Version: %s-%s\n", ['PV', 'PR']])
142         fields.append(["Description: %s\n", ['DESCRIPTION']])
143         fields.append(["Section: %s\n", ['SECTION']])
144         fields.append(["Priority: %s\n", ['PRIORITY']])
145         fields.append(["Maintainer: %s\n", ['MAINTAINER']])
146         fields.append(["Architecture: %s\n", ['TARGET_ARCH']])
147         fields.append(["OE: %s\n", ['P']])
148         fields.append(["Homepage: %s\n", ['HOMEPAGE']])
149
150 #        Package, Version, Maintainer, Description - mandatory
151 #        Section, Priority, Essential, Architecture, Source, Depends, Pre-Depends, Recommends, Suggests, Conflicts, Replaces, Provides - Optional
152
153
154         def pullData(l, d):
155             l2 = []
156             for i in l:
157                 data = bb.data.getVar(i, d, 1)
158                 if data is None:
159                     raise KeyError(f)
160                 if i == 'TARGET_ARCH' and bb.data.getVar('PACKAGE_ARCH', d, 1) == 'all':
161                     data = 'all'
162                 l2.append(data)
163             return l2
164
165         ctrlfile.write("Package: %s\n" % pkgname)
166         # check for required fields
167         try:
168             for (c, fs) in fields:
169                 ctrlfile.write(unicode(c % tuple(pullData(fs, localdata))))
170         except KeyError:
171             (type, value, traceback) = sys.exc_info()
172             ctrlfile.close()
173             raise bb.build.FuncFailed("Missing field for deb generation: %s" % value)
174         # more fields
175
176         bb.build.exec_func("mapping_rename_hook", localdata)
177
178         rdepends = explode_deps(unicode(bb.data.getVar("RDEPENDS", localdata, 1) or ""))
179         rdepends = [dep for dep in rdepends if not '*' in dep]
180         rrecommends = explode_deps(unicode(bb.data.getVar("RRECOMMENDS", localdata, 1) or ""))
181         rrecommends = [rec for rec in rrecommends if not '*' in rec]
182         rsuggests = (unicode(bb.data.getVar("RSUGGESTS", localdata, 1) or "")).split()
183         rprovides = (unicode(bb.data.getVar("RPROVIDES", localdata, 1) or "")).split()
184         rreplaces = (unicode(bb.data.getVar("RREPLACES", localdata, 1) or "")).split()
185         rconflicts = (unicode(bb.data.getVar("RCONFLICTS", localdata, 1) or "")).split()
186         if rdepends:
187             ctrlfile.write(u"Depends: %s\n" % ", ".join(rdepends))
188         if rsuggests:
189             ctrlfile.write(u"Suggests: %s\n" % ", ".join(rsuggests))
190         if rrecommends:
191             ctrlfile.write(u"Recommends: %s\n" % ", ".join(rrecommends))
192         if rprovides:
193             ctrlfile.write(u"Provides: %s\n" % ", ".join(rprovides))
194         if rreplaces:
195             ctrlfile.write(u"Replaces: %s\n" % ", ".join(rreplaces))
196         if rconflicts:
197             ctrlfile.write(u"Conflicts: %s\n" % ", ".join(rconflicts))
198         ctrlfile.close()
199
200         for script in ["preinst", "postinst", "prerm", "postrm"]:
201             scriptvar = bb.data.getVar('pkg_%s' % script, localdata, 1)
202             if not scriptvar:
203                 continue
204             try:
205                 scriptfile = file(os.path.join(controldir, script), 'w')
206             except OSError:
207                 raise bb.build.FuncFailed("unable to open %s script file for writing." % script)
208             scriptfile.write(scriptvar)
209             scriptfile.close()
210             os.chmod(os.path.join(controldir, script), 0755)
211
212         conffiles_str = bb.data.getVar("CONFFILES", localdata, 1)
213         if conffiles_str:
214             try:
215                 conffiles = file(os.path.join(controldir, 'conffiles'), 'w')
216             except OSError:
217                 raise bb.build.FuncFailed("unable to open conffiles for writing.")
218             for f in conffiles_str.split():
219                 conffiles.write('%s\n' % f)
220             conffiles.close()
221
222         os.chdir(basedir)
223         ret = os.system("PATH=\"%s\" fakeroot dpkg-deb -b %s %s" % (bb.data.getVar("PATH", localdata, 1), root, pkgoutdir))
224         if ret != 0:
225             raise bb.build.FuncFailed("dpkg-deb execution failed")
226
227         for script in ["preinst", "postinst", "prerm", "postrm", "control" ]:
228             scriptfile = os.path.join(controldir, script)
229             try:
230                 os.remove(scriptfile)
231             except OSError:
232                 pass
233         try:
234             os.rmdir(controldir)
235         except OSError:
236             pass
237         del localdata
238 }