dreambox-console-image: don't install systemd-extra-utils by default
[opendreambox.git] / meta-opendreambox / recipes-dreambox / images / dreambox-image-nfo.inc
1 python dreambox_image_nfo() {
2     def parse_status_file(filename, package):
3         kv = dict()
4         found = 0
5         try:
6             with open(filename, 'r') as f:
7                 for line in f.read().splitlines():
8                     try:
9                         key, value = line.split(': ', 1)
10                         if found == 0 and key == 'Package' and value == package:
11                             found = 1
12                         if found == 1:
13                             kv[key] = value
14                     except:
15                         if found == 1:
16                             break
17         except IOError:
18             pass
19         return kv
20
21     def hash_image(filename, algorithm):
22         import hashlib
23         h = hashlib.new(algorithm)
24         with open(filename, 'r') as f:
25             while True:
26                 buf = f.read(1024 * 1024)
27                 if len(buf) == 0:
28                     return h.hexdigest()
29                 h.update(buf)
30         return None
31
32     def file_mtime(filename):
33         from datetime import datetime
34         dt = datetime.fromtimestamp(os.path.getmtime(filename))
35         return dt.isoformat(' ')
36
37     deploy_dir_image = d.getVar('DEPLOY_DIR_IMAGE', True)
38     distro_feed_uri = d.getVar('DISTRO_FEED_URI', True)
39     distro_name = d.getVar('DISTRO_NAME', True)
40     distro_version = d.getVar('DISTRO_VERSION', True)
41     image_fstypes = (d.getVar('IMAGE_FSTYPES', True) or "").split()
42     image_name = d.getVar('IMAGE_NAME', True)
43     image_pkgtype = d.getVar('IMAGE_PKGTYPE', True)
44     image_rootfs = d.getVar('IMAGE_ROOTFS', True)
45     machine = d.getVar('MACHINE', True)
46
47     if oe.utils.inherits(d, 'image_types_nfi'):
48         ext = 'nfi'
49     elif len(image_fstypes) >= 1:
50         ext = 'rootfs.%s' % image_fstypes[0]
51     else:
52         raise bb.build.FuncFailed('Invalid IMAGE_FSTYPES')
53
54     image_path = os.path.join(deploy_dir_image, '.'.join((image_name, ext)))
55
56     attr = dict()
57
58     mtime = file_mtime(image_path)
59     if mtime:
60         attr['Date'] = mtime
61
62     opkglibdir = d.getVar('OPKGLIBDIR', True)
63     if opkglibdir:
64         status_path = image_rootfs + opkglibdir + '/opkg/status'
65     else:
66         status_path = image_rootfs + '/var/lib/dpkg/status'
67
68     control = parse_status_file(status_path, 'kernel-image')
69     if 'Version' in control:
70         if 'Provides' in control:
71             attr['Kernel'] = ' '.join((control['Provides'], control['Version']))
72         else:
73             attr['Kernel'] = control['Version']
74
75     control = parse_status_file(status_path, 'dreambox-dvb-modules')
76     if 'Version' in control:
77         attr['Drivers'] = control['Version']
78
79     control = parse_status_file(status_path, 'enigma2')
80     if 'Version' in control:
81         attr['Enigma2'] = control['Version']
82
83     if distro_name and distro_version:
84         attr['Distro'] = ' '.join((distro_name, distro_version))
85
86     if machine:
87         attr['Machine'] = ' '.join(('Dreambox', machine.upper()))
88
89     if distro_feed_uri and image_pkgtype:
90         attr['Link'] = '/'.join((distro_feed_uri, image_pkgtype))
91
92     md5 = hash_image(image_path, 'md5')
93     if md5:
94         attr['MD5'] = md5
95
96     sha256 = hash_image(image_path, 'sha256')
97     if sha256:
98         attr['SHA256'] = sha256
99
100     nfo_path = os.path.join(deploy_dir_image, '%s.nfo' % image_name)
101     with open(nfo_path, 'w') as f:
102         for key, value in sorted(attr.items()):
103             f.write('%s: %s\n' % (key, value))
104 }
105
106 IMAGE_POSTPROCESS_COMMAND += "dreambox_image_nfo;"