aboutsummaryrefslogtreecommitdiffstats
path: root/build/scripts-2.7/dogtail-run-headless
blob: edab53ddeedc2e341284c65871c3d108829c2323 (plain)
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
#!/usr/bin/python
"""
dogtail-run-headless

This script runs a session within an X server, allowing dogtail scripts to be
run on systems with no graphic cards, among other things.

Scripts are run in the current directory. After they are finished, dogtail can
optionally log out of the session, which will also termninate the X server.
"""

import optparse
from dogtail import sessions
import sys
import os.path


def findXServers(path="/usr/bin"):
    l = [os.path.join(path, f) for f in os.listdir(path) if f[0] == 'X']
    s = set(os.path.realpath(p) for p in l)
    return list(s)


def parse():
    yesno = ('y', 'n')
    sessions = ("GNOME", "KDE")
    usage = "usage: %prog: [options] {script [args]}"
    parser = optparse.OptionParser(usage=usage)

    parser.add_option("-s", "--session", type="choice",
                      dest="session",
                      choices=sessions,
                      help="which session to use")
    parser.add_option("-x", "--x-server", type="choice",
                      dest="xserver",
                      choices=findXServers(), help="which X server to use")
    parser.add_option("-l", "--logout", type="choice",
                      dest="logout",
                      choices=yesno,
                      help="attempt to log out of the session gracefully after" +
                      "script completion")
    parser.add_option("-t", "--terminate", type="choice",
                      dest="terminate",
                      choices=yesno,
                      help="after script completion, and after any attempt to log" +
                      "out, terminate the session")

    parser.set_defaults(session=sessions[0], logout='y', terminate='y')
    options, args = parser.parse_args()
    if not args:
        parser.print_usage()
        sys.exit(1)
    return options, args


def main():
    options, args = parse()
    if 'XDG_RUNTIME_DIR' in os.environ:
        del os.environ['XDG_RUNTIME_DIR']
    if options.session == "GNOME":
        session = sessions.Session(sessionBinary='/usr/bin/gnome-session',
                                   scriptCmdList=args, scriptDelay=10)
    if options.session == "KDE":
        session = sessions.Session(sessionBinary='/usr/bin/startkde',
                                   scriptCmdList=args, scriptDelay=25)
    if options.xserver:
        session.xserver.server = options.xserver
    pid = session.start()
    scriptExitCode = session.script.exitCode
    if options.logout == 'y':
        session.attemptLogout()
    if options.terminate == 'y':
        session.stop()
    else:
        session.wait()
    sys.exit(scriptExitCode)

if __name__ == "__main__":
    main()