File: dogtail-run-headless

package info (click to toggle)
dogtail 1.0.7-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,240 kB
  • sloc: python: 6,124; makefile: 56; sh: 7
file content (96 lines) | stat: -rwxr-xr-x 2,966 bytes parent folder | download | duplicates (2)
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
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
import sys
import os.path
import optparse
from dogtail import sessions

"""
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.
"""

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


def parse():
    yesno = ("y", "n")
    available_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, terminate the session")

    parser.set_defaults(session=available_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()

    script_exit_code = session.script.exitCode

    if options.logout == "y":
        session.attemptLogout()
    if options.terminate == "y":
        session.stop()
    else:
        session.wait()

    sys.exit(script_exit_code)

if __name__ == "__main__":
    main()