| 1 |
# -*- coding: utf-8 -*- |
| 2 |
from __future__ import absolute_import, division, print_function, unicode_literals |
| 3 |
import sys |
| 4 |
import os.path |
| 5 |
import optparse |
| 6 |
from dogtail import sessions |
| 7 |
|
| 8 |
""" |
| 9 |
dogtail-run-headless |
| 10 |
|
| 11 |
This script runs a session within an X server, allowing dogtail scripts to be |
| 12 |
run on systems with no graphic cards, among other things. |
| 13 |
|
| 14 |
Scripts are run in the current directory. After they are finished, dogtail can |
| 15 |
optionally log out of the session, which will also termninate the X server. |
| 16 |
""" |
| 17 |
|
| 18 |
def findXServers(path="/usr/bin"): |
| 19 |
list_of_x_servers = [os.path.join(path, f) for f in os.listdir(path) if f[0] == "X"] |
| 20 |
set_of_x_servers = set(os.path.realpath(p) for p in list_of_x_servers) |
| 21 |
return list(set_of_x_servers) |
| 22 |
|
| 23 |
|
| 24 |
def parse(): |
| 25 |
yesno = ("y", "n") |
| 26 |
available_sessions = ("GNOME", "KDE") |
| 27 |
|
| 28 |
usage = "usage: %prog: [options] {script [args]}" |
| 29 |
parser = optparse.OptionParser(usage=usage) |
| 30 |
|
| 31 |
parser.add_option("-s", "--session", |
| 32 |
type="choice", |
| 33 |
dest="session", |
| 34 |
choices=sessions, |
| 35 |
help="which session to use") |
| 36 |
|
| 37 |
parser.add_option("-x", "--x-server", |
| 38 |
type="choice", |
| 39 |
dest="xserver", |
| 40 |
choices=findXServers(), |
| 41 |
help="which X server to use") |
| 42 |
|
| 43 |
parser.add_option("-l", "--logout", |
| 44 |
type="choice", |
| 45 |
dest="logout", |
| 46 |
choices=yesno, |
| 47 |
help="attempt to log out of the session gracefully after script completion") |
| 48 |
|
| 49 |
parser.add_option("-t", "--terminate", |
| 50 |
type="choice", |
| 51 |
dest="terminate", |
| 52 |
choices=yesno, |
| 53 |
help="after script completion, terminate the session") |
| 54 |
|
| 55 |
parser.set_defaults(session=available_sessions[0], logout="y", terminate="y") |
| 56 |
|
| 57 |
options, args = parser.parse_args() |
| 58 |
if not args: |
| 59 |
parser.print_usage() |
| 60 |
sys.exit(1) |
| 61 |
return options, args |
| 62 |
|
| 63 |
|
| 64 |
def main(): |
| 65 |
options, args = parse() |
| 66 |
if "XDG_RUNTIME_DIR" in os.environ: |
| 67 |
del os.environ["XDG_RUNTIME_DIR"] |
| 68 |
|
| 69 |
if options.session == "GNOME": |
| 70 |
session = sessions.Session(sessionBinary="/usr/bin/gnome-session", |
| 71 |
scriptCmdList=args, |
| 72 |
scriptDelay=10) |
| 73 |
|
| 74 |
if options.session == "KDE": |
| 75 |
session = sessions.Session(sessionBinary="/usr/bin/startkde", |
| 76 |
scriptCmdList=args, |
| 77 |
scriptDelay=25) |
| 78 |
|
| 79 |
if options.xserver: |
| 80 |
session.xserver.server = options.xserver |
| 81 |
|
| 82 |
pid = session.start() |
| 83 |
|
| 84 |
script_exit_code = session.script.exitCode |
| 85 |
|
| 86 |
if options.logout == "y": |
| 87 |
session.attemptLogout() |
| 88 |
if options.terminate == "y": |
| 89 |
session.stop() |
| 90 |
else: |
| 91 |
session.wait() |
| 92 |
|
| 93 |
sys.exit(script_exit_code) |
| 94 |
|
| 95 |
if __name__ == "__main__": |
| 96 |
main() |