File: maintainer-script-without-set-e.py

package info (click to toggle)
lintian-brush 0.147
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 9,872 kB
  • sloc: python: 17,649; makefile: 814; sh: 140; xml: 119; javascript: 3; ansic: 2
file content (50 lines) | stat: -rwxr-xr-x 1,332 bytes parent folder | download
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
#!/usr/bin/python3

from lintian_brush.fixer import report_result, fixed_lintian_tag

import os


SCRIPTS = ['preinst', 'prerm', 'postinst', 'config', 'postrm']


def replace_set_e(path):
    lines = []
    try:
        with open(path, 'rb') as f:
            lines = list(f.readlines())
    except (FileNotFoundError, IsADirectoryError):
        return

    if b'set -e\n' in lines:
        return

    # TODO(jelmer): Handle -e in combination with other flags.
    if lines[0] != b"#!/bin/sh -e\n":
        return
    lines[0] = b'#!/bin/sh\n'
    for i, line in enumerate(lines[1:]):
        if line == b'set -e\n':
            return
        if (not (line.startswith(b'#') or line == b'\n') or
                line.strip() == b'#DEBHELPER#'):
            fixed_lintian_tag(
                'source', 'maintainer-script-without-set-e',
                os.path.basename(path))
            if not lines[i-1].strip():
                lines.insert(i, b'set -e\n')
                lines.insert(i+1, b'\n')
            else:
                lines.insert(i, b'\n')
                lines.insert(i+1, b'set -e\n')
            break

    with open(path, 'wb') as f:
        f.writelines(lines)


for name in SCRIPTS:
    replace_set_e(os.path.join('debian', name))


report_result('Use set -e rather than passing -e on the shebang-line.')