]> pilppa.org Git - familiar-h63xx-build.git/blob - org.handhelds.familiar/classes/package_ipk.bbclass
6d3e1eb686bd65cfcf735a7d91d1a7170b3b4aec
[familiar-h63xx-build.git] / org.handhelds.familiar / classes / package_ipk.bbclass
1 inherit package
2 DEPENDS_prepend="${@["ipkg-utils-native ", ""][(bb.data.getVar('PACKAGES', d, 1) == '')]}"
3 BOOTSTRAP_EXTRA_RDEPENDS += "ipkg-collateral ipkg ipkg-link"
4 BOOTSTRAP_EXTRA_DEPENDS  += "ipkg-collateral ipkg ipkg-link"
5 PACKAGEFUNCS += "do_package_ipk"
6
7 python package_ipk_fn () {
8         from bb import data
9         bb.data.setVar('PKGFN', bb.data.getVar('PKG',d), d)
10 }
11
12 python package_ipk_install () {
13         import os, sys
14         pkg = bb.data.getVar('PKG', d, 1)
15         pkgfn = bb.data.getVar('PKGFN', d, 1)
16         rootfs = bb.data.getVar('IMAGE_ROOTFS', d, 1)
17         ipkdir = bb.data.getVar('DEPLOY_DIR_IPK', d, 1)
18         stagingdir = bb.data.getVar('STAGING_DIR', d, 1)
19         tmpdir = bb.data.getVar('TMPDIR', d, 1)
20
21         if None in (pkg,pkgfn,rootfs):
22                 raise bb.build.FuncFailed("missing variables (one or more of PKG, PKGFN, IMAGEROOTFS)")
23         try:
24                 bb.mkdirhier(rootfs)
25                 os.chdir(rootfs)
26         except OSError:
27                 (type, value, traceback) = sys.exc_info()
28                 print value
29                 raise bb.build.FuncFailed
30
31         # Generate ipk.conf if it or the stamp doesnt exist
32         conffile = os.path.join(stagingdir,"ipkg.conf")
33         if not  os.access(conffile, os.R_OK):
34                 ipkg_archs = bb.data.getVar('IPKG_ARCHS',d)
35                 if ipkg_archs is None:
36                         bb.error("IPKG_ARCHS missing")
37                         raise FuncFailed
38                 ipkg_archs = ipkg_archs.split()
39                 arch_priority = 1
40
41                 f = open(conffile,"w")
42                 for arch in ipkg_archs:
43                         f.write("arch %s %s\n" % ( arch, arch_priority ))
44                         arch_priority += 1
45                 f.write("src local file:%s" % ipkdir)
46                 f.close()
47
48
49         if (not os.access(os.path.join(ipkdir,"Packages"), os.R_OK) or
50                 not os.access(os.path.join(os.path.join(tmpdir, "stamps"),"do_packages"),os.R_OK)):
51                 ret = os.system('ipkg-make-index -p %s %s ' % (os.path.join(ipkdir, "Packages"), ipkdir))
52                 if (ret != 0 ):
53                         raise bb.build.FuncFailed
54                 f=open(os.path.join(os.path.join(tmpdir, "stamps"),"do_packages"),"w")
55                 f.close()
56
57         ret = os.system('ipkg-cl  -o %s -f %s update' % (rootfs, conffile))
58         ret = os.system('ipkg-cl  -o %s -f %s install %s' % (rootfs, conffile, pkgfn))
59         if (ret != 0 ):
60                 raise bb.build.FuncFailed
61 }
62
63 python do_package_ipk () {
64         import copy # to back up env data
65         import sys
66         import re
67
68         workdir = bb.data.getVar('WORKDIR', d, 1)
69         if not workdir:
70                 bb.error("WORKDIR not defined, unable to package")
71                 return
72
73         import os # path manipulations
74         outdir = bb.data.getVar('DEPLOY_DIR_IPK', d, 1)
75         if not outdir:
76                 bb.error("DEPLOY_DIR_IPK not defined, unable to package")
77                 return
78         bb.mkdirhier(outdir)
79
80         dvar = bb.data.getVar('D', d, 1)
81         if not dvar:
82                 bb.error("D not defined, unable to package")
83                 return
84         bb.mkdirhier(dvar)
85
86         packages = bb.data.getVar('PACKAGES', d, 1)
87         if not packages:
88                 bb.debug(1, "PACKAGES not defined, nothing to package")
89                 return
90
91         tmpdir = bb.data.getVar('TMPDIR', d, 1)
92         # Invalidate the packages file
93         if os.access(os.path.join(os.path.join(tmpdir, "stamps"),"do_packages"),os.R_OK):
94                 os.unlink(os.path.join(os.path.join(tmpdir, "stamps"),"do_packages"))
95
96         if packages == []:
97                 bb.debug(1, "No packages; nothing to do")
98                 return
99
100         for pkg in packages.split():
101                 localdata = bb.data.createCopy(d)
102                 root = "%s/install/%s" % (workdir, pkg)
103
104                 bb.data.setVar('ROOT', '', localdata)
105                 bb.data.setVar('ROOT_%s' % pkg, root, localdata)
106                 pkgname = bb.data.getVar('PKG_%s' % pkg, localdata, 1)
107                 if not pkgname:
108                         pkgname = pkg
109                 bb.data.setVar('PKG', pkgname, localdata)
110
111                 overrides = bb.data.getVar('OVERRIDES', localdata)
112                 if not overrides:
113                         raise bb.build.FuncFailed('OVERRIDES not defined')
114                 overrides = bb.data.expand(overrides, localdata)
115                 bb.data.setVar('OVERRIDES', overrides + ':' + pkg, localdata)
116
117                 bb.data.update_data(localdata)
118                 basedir = os.path.join(os.path.dirname(root))
119                 pkgoutdir = outdir
120                 bb.mkdirhier(pkgoutdir)
121                 os.chdir(root)
122                 from glob import glob
123                 g = glob('*')
124                 try:
125                         del g[g.index('CONTROL')]
126                         del g[g.index('./CONTROL')]
127                 except ValueError:
128                         pass
129                 if not g and not bb.data.getVar('ALLOW_EMPTY', localdata):
130                         from bb import note
131                         note("Not creating empty archive for %s-%s-%s" % (pkg, bb.data.getVar('PV', localdata, 1), bb.data.getVar('PR', localdata, 1)))
132                         continue
133                 controldir = os.path.join(root, 'CONTROL')
134                 bb.mkdirhier(controldir)
135                 try:
136                         ctrlfile = file(os.path.join(controldir, 'control'), 'w')
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", ['PACKAGE_ARCH']])
147                 fields.append(["OE: %s\n", ['P']])
148                 fields.append(["Homepage: %s\n", ['HOMEPAGE']])
149
150                 def pullData(l, d):
151                         l2 = []
152                         for i in l:
153                                 l2.append(bb.data.getVar(i, d, 1))
154                         return l2
155
156                 ctrlfile.write("Package: %s\n" % pkgname)
157                 # check for required fields
158                 try:
159                         for (c, fs) in fields:
160                                 for f in fs:
161                                         if bb.data.getVar(f, localdata) is None:
162                                                 raise KeyError(f)
163                                 ctrlfile.write(c % tuple(pullData(fs, localdata)))
164                 except KeyError:
165                         (type, value, traceback) = sys.exc_info()
166                         ctrlfile.close()
167                         raise bb.build.FuncFailed("Missing field for ipk generation: %s" % value)
168                 # more fields
169                 rdepends = explode_deps(bb.data.getVar("RDEPENDS", localdata, 1) or "")
170                 rrecommends = explode_deps(bb.data.getVar("RRECOMMENDS", localdata, 1) or "")
171                 rsuggests = (bb.data.getVar("RSUGGESTS", localdata, 1) or "").split()
172                 rprovides = (bb.data.getVar("RPROVIDES", localdata, 1) or "").split()
173                 rreplaces = (bb.data.getVar("RREPLACES", localdata, 1) or "").split()
174                 rconflicts = (bb.data.getVar("RCONFLICTS", localdata, 1) or "").split()
175                 if rdepends:
176                         ctrlfile.write("Depends: %s\n" % ", ".join(rdepends))
177                 if rsuggests:
178                         ctrlfile.write("Suggests: %s\n" % ", ".join(rsuggests))
179                 if rrecommends:
180                         ctrlfile.write("Recommends: %s\n" % ", ".join(rrecommends))
181                 if rprovides:
182                         ctrlfile.write("Provides: %s\n" % ", ".join(rprovides))
183                 if rreplaces:
184                         ctrlfile.write("Replaces: %s\n" % ", ".join(rreplaces))
185                 if rconflicts:
186                         ctrlfile.write("Conflicts: %s\n" % ", ".join(rconflicts))
187                 src_uri = bb.data.getVar("SRC_URI", localdata, 1)
188                 if src_uri:
189                         src_uri = re.sub("\s+", " ", src_uri)
190                         ctrlfile.write("Source: %s\n" % " ".join(src_uri.split()))
191                 ctrlfile.close()
192
193                 for script in ["preinst", "postinst", "prerm", "postrm"]:
194                         scriptvar = bb.data.getVar('pkg_%s' % script, localdata, 1)
195                         if not scriptvar:
196                                 continue
197                         try:
198                                 scriptfile = file(os.path.join(controldir, script), 'w')
199                         except OSError:
200                                 raise bb.build.FuncFailed("unable to open %s script file for writing." % script)
201                         scriptfile.write(scriptvar)
202                         scriptfile.close()
203                         os.chmod(os.path.join(controldir, script), 0755)
204
205                 conffiles_str = bb.data.getVar("CONFFILES", localdata, 1)
206                 if conffiles_str:
207                         try:
208                                 conffiles = file(os.path.join(controldir, 'conffiles'), 'w')
209                         except OSError:
210                                 raise bb.build.FuncFailed("unable to open conffiles for writing.")
211                         for f in conffiles_str.split():
212                                 conffiles.write('%s\n' % f)
213                         conffiles.close()
214
215                 os.chdir(basedir)
216                 ret = os.system("PATH=\"%s\" %s %s %s" % (bb.data.getVar("PATH", localdata, 1), 
217                                                           bb.data.getVar("IPKGBUILDCMD",d,1), pkg, pkgoutdir))
218                 if ret != 0:
219                         raise bb.build.FuncFailed("ipkg-build execution failed")
220
221                 for script in ["preinst", "postinst", "prerm", "postrm", "control" ]:
222                         scriptfile = os.path.join(controldir, script)
223                         try:
224                                 os.remove(scriptfile)
225                         except OSError:
226                                 pass
227                 try:
228                         os.rmdir(controldir)
229                 except OSError:
230                         pass
231                 del localdata
232 }