#!/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()