meta-opendreambox: stop using PR
[opendreambox.git] / meta-opendreambox / classes / squashfs-img.bbclass
1 SQUASHFS_IMG_NAMES ?= ""
2 SQUASHFS_IMG_REPLACES ?= ""
3 SQUASHFS_IMG_BLOCKSIZES ?= ""
4 SQUASHFS_IMG_FILESREGEX ?= ""
5 SQUASHFS_IMG_COMPRESSOR ?= ""
6 SQUASHFS_IMG_SUPPORT_ENABLED ?= "${@base_less_or_equal('DREAMBOX_IMAGE_SIZE', 64, 1, 0, d)}"
7
8 DEPENDS += '${@base_contains('SQUASHFS_IMG_SUPPORT_ENABLED', '1', 'squashfs-tools-native dreambox-squashfs-support', '', d)}'
9
10 PACKAGE_ARCH := "${@base_contains('SQUASHFS_IMG_SUPPORT_ENABLED', '1', '${MACHINE_ARCH}', '${PACKAGE_ARCH}', d)}"
11
12 python emit_pkgdata_prepend() {
13     import os, subprocess, glob, re, shutil
14     enabled = d.getVar('SQUASHFS_IMG_SUPPORT_ENABLED', True)
15
16     if enabled != '1':
17         bb.note('squashfs-image-support is disabled..')
18     else:
19         def makedirs(dir):
20             try:
21                 os.makedirs(dir)
22             except:
23                 pass
24
25         def rename(src, dest):
26             try:
27                 os.rename(src, dest)
28             except:
29                 pass
30
31         def symlink(source, link_name):
32             try:
33                 os.symlink(source, link_name)
34             except:
35                 pass
36
37         def rmtree(path):
38             try:
39                 shutil.rmtree(path)
40             except:
41                 pass
42
43         packages = d.getVar('PACKAGES', True).split(' ')
44         packagesdir = d.getVar('PKGDEST', True)
45
46         package_names = d.getVar('SQUASHFS_IMG_PACKAGES', True).split(':')
47         package_replaces = d.getVar('SQUASHFS_IMG_REPLACES', True).split(':')
48         package_blocksizes = d.getVar('SQUASHFS_IMG_BLOCKSIZES', True).split(':')
49         package_filesregex = d.getVar('SQUASHFS_IMG_FILESREGEX', True).split(':')
50         package_compressor = d.getVar('SQUASHFS_IMG_COMPRESSOR', True)
51
52         if not package_compressor:
53             kernel_version = d.getVar('DREAMBOX_KERNEL_VERSION', True)
54             package_compressor = kernel_version <= '2.6.18' and 'lzma' or 'xz'
55
56         num_replaces = len(package_replaces)
57         num_blocksizes = len(package_blocksizes)
58         num_filesregex = len(package_filesregex)
59         idx = 0
60
61         for package in package_names:
62             bb.note('building squashfs img %s' %package)
63
64             package_files = [ ]
65             squashfs_files = [ ]
66             packagedir = packagesdir+'/'+package
67
68             if num_replaces <= idx:
69                 bb.fatal('no replaces for %s' %package)
70
71             replaces = package_replaces[idx].split(' ')
72             replaces = [ repl.strip('\t ') for repl in replaces if repl.strip('\t ') ]
73
74             blocksize = ''
75             if num_blocksizes > idx:
76                 blocksize = package_blocksizes[idx]
77             if blocksize == '':
78                 blocksize = str(128*1024)
79
80             if num_filesregex > idx:
81                 filesregex = package_filesregex[idx]
82
83             if filesregex != '':
84                 filesre = re.compile(filesregex)
85             else:
86                 filesre = None
87
88             bb.note('replaces %s, blocksize %s, file_regex %s' %(replaces, blocksize, filesregex))
89
90             for repl in replaces:
91                 bb.note('process replacement %s' %repl)
92                 try:
93                     files_pathes = d.getVar('FILES_%s'%repl, True).split(' ')
94                     files_pathes = [ p.strip('\t ') for p in files_pathes ]
95                 except:
96                     bb.fatal('couldnt retrieve FILES_%s!' %repl)
97                 repldir = packagesdir+'/'+repl
98                 bb.note('FILES_%s %s' %(repl, files_pathes))
99
100                 l = len(repldir)
101                 for path in files_pathes:
102                     if path.startswith('/etc'):
103                         continue
104                     realp = repldir+path
105                     for realpath in glob.glob(realp):
106                         if os.path.isdir(realpath):
107                             files = []
108                             for root, dirs, names in os.walk(realpath):
109                                 files += [ root+'/'+name for name in names ]
110                         else:
111                             if os.path.islink(realpath):
112                                 continue
113                             files = glob.glob(realpath)
114                         files = [ (f, l) for f in files if not os.path.islink(f) and (not filesre or filesre.search(f) is not None) ]
115                         squashfs_files += files
116
117             if not squashfs_files:
118                 bb.fatal("no squashfs files ... maybe regex wrong?!?")
119
120             bb.note('squashfs_files %s' %[ f for f, l in squashfs_files ])
121
122             makedirs(packagedir+'/media/squashfs-images/'+package)
123             makedirs(packagedir+'/squashfs-images')
124
125             for f, l in squashfs_files:
126                 makedirs(packagedir+'/tmp'+os.path.dirname(f)[l:])
127                 rename(f, packagedir+'/tmp'+f[l:])
128                 symlink('/media/squashfs-images/'+package+f[l:], f)
129
130             squashfs_cmd = ['mksquashfs']
131             squashfs_cmd.append(packagedir+'/tmp/')
132             squashfs_cmd.append(packagedir+'/squashfs-images/'+package)
133             squashfs_cmd.append('-b')
134             squashfs_cmd.append('%s' %blocksize)
135             squashfs_cmd.append('-comp')
136             squashfs_cmd.append('%s' %package_compressor)
137
138             bb.note('squashfs_cmd %s' %squashfs_cmd)
139
140             if subprocess.call(squashfs_cmd):
141                 bb.fatal('mksquashfs failed!')
142
143             rmtree(packagedir+'/tmp')
144
145             for repl in replaces:
146                 rdepends = d.getVar('RDEPENDS_'+repl, True).split(' ')
147                 rdepends.append(package)
148                 d.setVar('RDEPENDS_'+repl, ' '.join(rdepends))
149                 d.setVar('INSANE_SKIP_'+repl, 'dev-so')
150                 d.setVar('ALLOW_EMPTY_'+repl, '1')
151
152             d.setVar('RDEPENDS_'+package, 'dreambox-squashfs-support')
153             d.setVar('FILES_'+package, '/')
154             d.setVar('pkg_preinst_'+package, 'if [ -z "$D" ]; then if mountpoint -q /media/squashfs-images/'+package+'; then umount /media/squashfs-images/'+package+' && losetup -d `losetup | grep '+package+' | cut -d: -f1` || touch /var/tmp/.umount_'+package+'_needed; else touch /var/tmp/.umount_'+package+'_needed; fi; fi')
155             d.setVar('pkg_postinst_'+package, 'if [ -z "$D" ]; then if ! mountpoint -q /media/squashfs-images/'+package+'; then mount -t squashfs -o ro,loop /squashfs-images/'+package+' /media/squashfs-images/'+package+' || touch /var/tmp/.mount_'+package+'_needed; else touch /var/tmp/.mount_'+package+'_needed; fi; fi')
156             d.setVar('pkg_postrm_'+package, 'if [ -z "$D" ]; then if mountpoint -q /media/squashfs-images/'+package+'; then umount /media/squashfs-images/'+package+' && losetup -d `losetup | grep '+package+' | cut -d: -f1` || touch /var/tmp/.umount_'+package+'_needed; else touch /var/tmp/.umount_'+package+'_needed; fi; fi')
157             packages.append(package)
158
159             idx += 1
160
161         d.setVar('PACKAGES', ' '.join(packages))
162 }