File: lirc-old2new

package info (click to toggle)
lirc 0.10.1-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 8,364 kB
  • sloc: ansic: 26,717; cpp: 9,066; sh: 5,521; python: 4,362; makefile: 1,034; xml: 63
file content (250 lines) | stat: -rwxr-xr-x 8,351 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/usr/bin/env python3

'''
Convert lirc 0.9.0 debian setup to 0.9.4.

Usage: lirc-old2new  [etcdir]

Script creates etcdir/lirc_options.conf using data in etcdir/hardware.conf
end etcdir/lirc_options.conf. [etcdir] defaults to '/etc/lirc'. Without
options it requires root permissions.

Scripts also tries to enable the three different systemd units lircd.socket,
lircmd.service and irexec.service depending on hardware.conf contents and
available configuration files.

If the LIRC_DRYRUN environment variable is set, the systemctl enable
commands are just echoed instead of executed.

'''
# pylint: disable=invalid-name

import argparse
import configparser
import os.path
import re
import shutil
import subprocess
import sys

_LIRCD_CONF = '''
# Populated config files can be found at http://sf.net/p/lirc-remotes. The
# irdb-get(1) and lirc-setup(1) tools can be used to search and download
# config files.
#
# From 0.9.2 config files could just be dropped as-is in the lircd.conf.d
# directory and be included by this file.

include "lircd.conf.d/*.conf"
'''



def _warn(s):
    ''' Print a warning to stderr. '''
    print("WARNING: " + s, file=sys.stderr)


def _enable_start(unit):
    ''' Run systemctl enable and systemctl start on given unit. '''
    cmd = ['systemctl', 'start', unit]
    if 'LIRC_DRYRUN' in os.environ:
        cmd.insert(0, 'echo')
    try:
        subprocess.check_call(cmd)
    except subprocess.CalledProcessError:
        _warn("Cannot start " + unit)
    cmd = ['systemctl', 'enable', unit]
    if 'LIRC_DRYRUN' in os.environ:
        cmd.insert(0, 'echo')
    try:
        subprocess.check_call(cmd)
    except subprocess.CalledProcessError:
        _warn("Cannot enable " + unit)


def _grep(what, path):
    ''' test if file 'path' contains 'what'. '''
    if not os.path.exists(path):
        return path
    with open(path) as f:
        s = '\n'.join(f.readlines())
    return what in s


def parse_args(line):
    ''' Parse lircd command line and return as a dict. '''

    parser = argparse.ArgumentParser(description="Parses LIRCD_ARGS options")
    parser.add_argument('-d', '--device')
    parser.add_argument('-H', '--driver')
    parser.add_argument('-a', '--allow-simulate', action='store_true')
    parser.add_argument('-c', '--connect', action='store_true')
    parser.add_argument('-e', '--effective-user')
    parser.add_argument('-A', '--driver-options')
    parser.add_argument('-Y', '--dynamic_codes', action='store_true')
    parser.add_argument('-l', '--listen')
    parser.add_argument('-L', '--logfile')
    parser.add_argument('-D', '--loglevel')
    parser.add_argument('-O', '--options-file')
    parser.add_argument('-p', '--permissiom')
    parser.add_argument('-P', '--pidfile')
    parser.add_argument('-U', '--plugindir')
    parser.add_argument('-r', '--release', default=None, nargs='?')
    parser.add_argument('-R', '--repeat-max')
    parser.add_argument('-u', '--uinput', action='store_true')
    try:
        args = parser.parse_args(line.split())
    except ValueError:
        _warn("Illegal LIRCD_ARGS line:" + line)
        return {}
    else:
        return {key: value for key, value in vars(args).items() if value}


def get_lircd_options(hardware_cf, etcdir):
    ''' Get a ConfigParser from path updated with hardware_cf values. '''
    options_path = os.path.join(etcdir, 'lirc_options.conf')
    args = {}
    if  'lircd_args' in hardware_cf and hardware_cf['lircd_args']:
        args = parse_args(hardware_cf['lircd_args'])
    if 'device' in hardware_cf and hardware_cf['device'] \
    and 'UNCONFIGURED' not in hardware_cf['device']:
        if 'device' in args:
            _warn("--device in LIRC_ARGS obsoleted by DEVICE.")
        args['device'] = hardware_cf['device']
    if 'driver' in hardware_cf and hardware_cf['driver'] \
    and 'UNCONFIGURED' not in hardware_cf['driver']:
        if 'driver' in args:
            _warn("--driver in LIRC_ARGS obsoleted by DRIVER.")
        args['driver'] = hardware_cf['driver']

    if not 'driver' in args:
        args['driver'] = 'default';

    parser = configparser.RawConfigParser()
    if os.path.exists(options_path):
        parser.read(options_path)
    for key, value in args.items():
        if not parser.has_section('lircd'):
            parser.add_section('lircd')
        if value:
            parser.set('lircd', key, value)
    if 'load_modules' in hardware_cf and hardware_cf['load_modules'] \
    and hardware_cf['modules']:
        if not parser.has_section('modinit'):
            parser.add_section('modinit')
        modprobes = ['modprobe ' + m for m in hardware_cf['modules'].split()]
        parser.set('modinit', 'code', '; '.join(modprobes))
    return parser


def setup_lircmd(lircmd_opt, etcdir):
    ''' Possibly start and enable the lircmd service. '''
    lircmd_path = os.path.join(etcdir, 'lircmd.conf')
    start_lircmd = True
    if lircmd_opt and lircmd_opt.lower() == 'false':
        start_lircmd = False
    elif not os.path.exists(lircmd_path):
        start_lircmd = False
    elif _grep('UNCONFIGURED', lircmd_path):
        print(lircmd_path + " is unconfigured (skip lircmd)")
        start_lircmd = False
    if start_lircmd:
        _enable_start('lircmd.service')


def setup_lircd(etcdir):
    ''' Possibly start and enable the lircd service. '''
    lircd_path = os.path.join(etcdir, 'lircd.conf')
    lircd_new_path = os.path.join(etcdir, 'lircd.conf.d', 'lirc-old.conf')
    use_old = True
    if not os.path.exists(lircd_path):
        _warn("Cannot find " + lircd_path)
        _warn("lircd.socket is not enabled, LIRC is disabled")
        use_old = False
    if _grep('UNCONFIGURED', lircd_path):
        _warn("lircd.conf is unconfigured, LIRC is disabled")
        use_old = False
    if use_old:
        print("Using existing lircd configuration and enabling lircd.socket")
        shutil.copyfile(lircd_path, lircd_new_path)
        with open(lircd_path, 'w') as f:
            f.write(_LIRCD_CONF)
        _enable_start('lircd.socket')
    else:
        print("Using default devinput lircd configuration, not enabled.")
        with open(lircd_path, 'w') as f:
            f.write(_LIRCD_CONF)
    return use_old


def setup_irexec(irexec_opt, etcdir):
    ''' Possibly start and enable the irexec service. '''
    lircrc_path = os.path.join(etcdir, 'lircrc')
    irexec_path = os.path.join(etcdir, 'irexec.lircrc')
    start_irexec = True
    if irexec_opt and irexec_opt.lower() == 'false':
        start_irexec = False
    elif not os.path.exists(lircrc_path):
        start_irexec = False
    elif _grep('UNCONFIGURED', lircrc_path):
        print(lircrc_path + " is unconfigured (skip irexec)")
        start_irexec = False
    else:
        shutil.copyfile(lircrc_path, irexec_path)
    if start_irexec:
        _enable_start('irexec.service')


def decode_hw(path):
    ''' Return dict of all items in hardware.conf. '''
    hw = {}
    try:
        with open(path) as f:
            contents = f.readlines()
    except FileNotFoundError:
        _warn("Cannot locate hardware.conf(!)")
        return {}
    contents.insert(0, '[main]\n')
    s = ''.join(contents)
    parser = configparser.RawConfigParser()
    parser.read_string(s, path)
    hw = {key: value for key, value in parser.items('main')}
    for key, value in hw.items():
        m = re.match(r'^"(.*)"$', value)
        if m:
            hw[key] = m.group(1)
    return hw


def main():
    ''' Indeed: main program. '''

    if len(sys.argv) not in [1, 2]:
        print("Usage: lirc-old2new [etcdir]")
        sys.exit(1)
    if len(sys.argv) == 2:
        etcdir = sys.argv[1]
    else:
        etcdir = '/etc/lirc'

    options_path = os.path.join(etcdir, 'lirc_options.conf')
    hardware_conf = decode_hw(os.path.join(etcdir, 'hardware.conf'))
    options_conf = get_lircd_options(hardware_conf, etcdir)
    with open(options_path, 'w') as f:
        options_conf.write(f)
    if setup_lircd(etcdir):
        setup_lircmd(hardware_conf['start_lircmd']
                     if 'start_lircmd' in hardware_conf else None,
                     etcdir)
        setup_irexec(hardware_conf['start_irexec']
                     if 'start_irexec' in hardware_conf else None,
                     etcdir)


if __name__ == '__main__':
    main()


# vim: set expandtab ts=4 sw=4: