~bladernr/checkbox/1095713-set-pipefail-on-sleep-jobs

1404.1.1 by Marc Tardif
Ran 2to3 on: backend, run, test, install/config, bin/, checkbox/, checkbox_*/, plugins/ and scripts/*_resource.
1
#!/usr/bin/python3
760.2.22 by Marc Tardif
Replaced dependency on dbus by using sudo/gksu/kdesudo instead.
2
787 by Marc Tardif
Added -p|--path option to backend because kdesudo does not support inheriting the PATH environment variable which fixes bug #557443.
3
import os
760.2.22 by Marc Tardif
Replaced dependency on dbus by using sudo/gksu/kdesudo instead.
4
import sys
5
6
from optparse import OptionParser
7
from checkbox.lib.fifo import FifoReader, FifoWriter
8
796 by Marc Tardif
Closing fifo descriptors so that backend can get terminated which fixes bug #553328.
9
from checkbox.job import Job, FAIL
760.2.22 by Marc Tardif
Replaced dependency on dbus by using sudo/gksu/kdesudo instead.
10
11
12
def main(args):
13
    usage = "Usage: %prog INPUT OUTPUT"
14
    parser = OptionParser(usage=usage)
787 by Marc Tardif
Added -p|--path option to backend because kdesudo does not support inheriting the PATH environment variable which fixes bug #557443.
15
    parser.add_option("-p", "--path",
16
        help="PATH variable to replace in the environment")
760.2.22 by Marc Tardif
Replaced dependency on dbus by using sudo/gksu/kdesudo instead.
17
    (options, args) = parser.parse_args(args)
18
19
    if len(args) < 2:
20
        parser.error("Missing INPUT and OUTPUT")
21
1404.1.26 by Marc Tardif
Changed frontend and backend calling job.execute to return an error when failing to decode the output.
22
    # Set PATH
787 by Marc Tardif
Added -p|--path option to backend because kdesudo does not support inheriting the PATH environment variable which fixes bug #557443.
23
    if options.path:
24
        os.environ["PATH"] = options.path
25
1404.1.26 by Marc Tardif
Changed frontend and backend calling job.execute to return an error when failing to decode the output.
26
    # Set PYTHONPATH
27
    pythonpath = sys.path[0]
28
    if "PYTHONPATH" in os.environ:
29
        pythonpath = ":".join([pythonpath, os.environ["PYTHONPATH"]])
30
    os.environ["PYTHONPATH"] = pythonpath
31
760.2.22 by Marc Tardif
Replaced dependency on dbus by using sudo/gksu/kdesudo instead.
32
    reader = FifoReader(args[0])
33
    writer = FifoWriter(args[1])
34
35
    while True:
36
        try:
37
            message = reader.read_object()
1710.1.1 by Daniel Manrique
Added consecutive numbering to messages sent to the backend, so the
38
            #"unpack" the message
39
            sequence, message = message
917.1.3 by Daniel Manrique
Fix open/read blocking behavior and backend/frontend communications to
40
            if message == "stop":
41
                break
952.1.1 by Daniel Manrique
Further improvements to make frontend/backend communication more reliable.
42
            if message == "ping":
1710.1.1 by Daniel Manrique
Added consecutive numbering to messages sent to the backend, so the
43
                #Build a tuple with the sequence number as
44
                #received
45
                writer.write_object((sequence, "pong",))
952.1.1 by Daniel Manrique
Further improvements to make frontend/backend communication more reliable.
46
                continue
796 by Marc Tardif
Closing fifo descriptors so that backend can get terminated which fixes bug #553328.
47
            if isinstance(message, dict) and "command" in message:
774 by Marc Tardif
Changed backend to handle empty messages which fixes bug #536645.
48
                job = Job(message["command"], message.get("environ"),
49
                    message.get("timeout"))
1404.1.26 by Marc Tardif
Changed frontend and backend calling job.execute to return an error when failing to decode the output.
50
                status, data, duration = job.execute()
51
                try:
52
                    data = data.decode("utf-8")
53
                except UnicodeDecodeError:
54
                    status, data, duration = (FAIL, "Decode error", 0,)
796 by Marc Tardif
Closing fifo descriptors so that backend can get terminated which fixes bug #553328.
55
            else:
1404.1.26 by Marc Tardif
Changed frontend and backend calling job.execute to return an error when failing to decode the output.
56
                status, data, duration = (FAIL, "", 0,)
1710.1.1 by Daniel Manrique
Added consecutive numbering to messages sent to the backend, so the
57
            #Build a tuple with sequence number
58
            writer.write_object((sequence, (status, data, duration,),))
1404.1.8 by Marc Tardif
Fixed a few extraneous casts from iterators to lists introduced by 2to3.
59
        except IOError:
760.2.22 by Marc Tardif
Replaced dependency on dbus by using sudo/gksu/kdesudo instead.
60
            break
61
62
    return 0
63
64
65
if __name__ == "__main__":
66
    sys.exit(main(sys.argv[1:]))