python-twisted: remove version 12, meta-oe has 13.2.0
[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             package_compressor = 'xz'
54
55         num_replaces = len(package_replaces)
56         num_blocksizes = len(package_blocksizes)
57         num_filesregex = len(package_filesregex)
58         idx = 0
59
60         for package in package_names:
61             bb.note('building squashfs img %s' %package)
62
63             package_files = [ ]
64             squashfs_files = [ ]
65             packagedir = packagesdir+'/'+package
66
67             if num_replaces <= idx:
68                 bb.fatal('no replaces for %s' %package)
69
70             replaces = package_replaces[idx].split(' ')
71             replaces = [ repl.strip('\t ') for repl in replaces if repl.strip('\t ') ]
72
73             blocksize = ''
74             if num_blocksizes > idx:
75                 blocksize = package_blocksizes[idx]
76             if blocksize == '':
77                 blocksize = str(128*1024)
78
79             if num_filesregex > idx:
80                 filesregex = package_filesregex[idx]
81
82             if filesregex != '':
83                 filesre = re.compile(filesregex)
84             else:
85                 filesre = None
86
87             bb.note('replaces %s, blocksize %s, file_regex %s' %(replaces, blocksize, filesregex))
88
89             for repl in replaces:
90                 bb.note('process replacement %s' %repl)
91                 try:
92                     files_pathes = d.getVar('FILES_%s'%repl, True).split(' ')
93                     files_pathes = [ p.strip('\t ') for p in files_pathes ]
94                 except:
95                     bb.fatal('couldnt retrieve FILES_%s!' %repl)
96                 repldir = packagesdir+'/'+repl
97                 bb.note('FILES_%s %s' %(repl, files_pathes))
98
99                 l = len(repldir)
100                 for path in files_pathes:
101                     if path.startswith('/etc'):
102                         continue
103                     realp = repldir+path
104                     for realpath in glob.glob(realp):
105                         if os.path.isdir(realpath):
106                             files = []
107                             for root, dirs, names in os.walk(realpath):
108                                 files += [ root+'/'+name for name in names ]
109                         else:
110                             if os.path.islink(realpath):
111                                 continue
112                             files = glob.glob(realpath)
113                         files = [ (f, l) for f in files if not os.path.islink(f) and (not filesre or filesre.search(f) is not None) ]
114                         squashfs_files += files
115
116             if not squashfs_files:
117                 bb.fatal("no squashfs files ... maybe regex wrong?!?")
118
119             bb.note('squashfs_files %s' %[ f for f, l in squashfs_files ])
120
121             makedirs(packagedir+'/media/squashfs-images/'+package)
122             makedirs(packagedir+'/squashfs-images')
123
124             for f, l in squashfs_files:
125                 makedirs(packagedir+'/tmp'+os.path.dirname(f)[l:])
126                 rename(f, packagedir+'/tmp'+f[l:])
127                 symlink('/media/squashfs-images/'+package+f[l:], f)
128
129             squashfs_cmd = ['mksquashfs']
130             squashfs_cmd.append(packagedir+'/tmp/')
131             squashfs_cmd.append(packagedir+'/squashfs-images/'+package)
132             squashfs_cmd.append('-b')
133             squashfs_cmd.append('%s' %blocksize)
134             squashfs_cmd.append('-comp')
135             squashfs_cmd.append('%s' %package_compressor)
136
137             bb.note('squashfs_cmd %s' %squashfs_cmd)
138
139             if subprocess.call(squashfs_cmd):
140                 bb.fatal('mksquashfs failed!')
141
142             rmtree(packagedir+'/tmp')
143
144             for repl in replaces:
145                 rdepends = d.getVar('RDEPENDS_'+repl, True).split(' ')
146                 rdepends.append(package)
147                 d.setVar('RDEPENDS_'+repl, ' '.join(rdepends))
148                 d.setVar('INSANE_SKIP_'+repl, 'dev-so')
149                 d.setVar('ALLOW_EMPTY_'+repl, '1')
150
151             d.setVar('RDEPENDS_'+package, 'dreambox-squashfs-support')
152             d.setVar('FILES_'+package, '/')
153             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')
154             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')
155             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')
156             packages.append(package)
157
158             idx += 1
159
160         d.setVar('PACKAGES', ' '.join(packages))
161 }