~ubuntu-branches/ubuntu/utopic/dogtail/utopic

« back to all changes in this revision

Viewing changes to scripts/dogtail-run-headless

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2006-12-21 13:33:47 UTC
  • mfrom: (1.2.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 5.
  • Revision ID: james.westby@ubuntu.com-20061221133347-xo9jg11afp5plcka
Tags: upstream-0.6.1
ImportĀ upstreamĀ versionĀ 0.6.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/bin/sh
2
 
# dogtail-run-headless
3
 
#
4
 
# Usage: dogtail-run-headless ./some-dogtail-script.py
5
 
#
6
 
# This script launches an X server using the virtual framebuffer, allowing 
7
 
# dogtail scripts to be run on headless servers. The server starts a minimal
8
 
# GNOME session, based on Nat Friedman's notes here: 
9
 
#  http://nat.org/2005/october/#Keep-It-Simple-Stupid
10
 
# Dogtail scripts are run in the current directory. After the script 
11
 
# terminates, so does the X server.
12
 
#
13
 
# The X server can be connected to using VNC via the following steps:
14
 
#
15
 
#  port=`"x11vnc -display :1 -viewonly -bg" | grep PORT)`
16
 
#  port=`echo "$port" | sed -e's/PORT=//'`
17
 
#  port=`expr $port - 5900`
18
 
#
19
 
#  vncviewer localhost:$port
20
 
#
21
 
# Author: Zack Cerza <zcerza@redhat.com>
22
 
#
23
 
 
24
 
A11Y_ENABLED=`gconftool-2 --get /desktop/gnome/interface/accessibility`
25
 
 
26
 
if test x$A11Y_ENABLED = xfalse; then 
27
 
        echo "Enabling accessibility support for GNOME"
28
 
        gconftool-2 --set --type bool /desktop/gnome/interface/accessibility true
29
 
fi
30
 
 
31
 
EXIT_CODE_FILE=/tmp/dogtail-run-headless.exitcode
32
 
 
33
 
mv -f ~/.xinitrc ~/.xinitrc-user 2>/dev/null && \
34
 
 echo "Moving ~/.xinitrc to ~/.xinitrc-user temporarily"
35
 
 
36
 
# xinit doesn't allow specifying an alternate xinitrc
37
 
cat << EOF > ~/.xinitrc
38
 
#!/bin/sh
39
 
gnome-settings-daemon &
40
 
gnome-panel &
41
 
nautilus -n &
42
 
metacity &
43
 
gconftool-2 --set --type bool /desktop/gnome/interface/accessibility true
44
 
sleep 10
45
 
cd $PWD && dogtail-detect-session && sh -c "$@"; echo -n \$? > $EXIT_CODE_FILE
46
 
EOF
47
 
 
48
 
echo "Starting GNOME Session..."
49
 
xinit -- /usr/X11R6/bin/Xvfb :1 -screen 0 1024x768x24 -fbdir /tmp :1
50
 
 
51
 
if test x$A11Y_ENABLED = xfalse; then
52
 
        echo "Re-disabling accessibility support for GNOME"
53
 
        gconftool-2 --set --type bool /desktop/gnome/interface/accessibility false
54
 
fi
55
 
 
56
 
rm -f ~/.xinitrc
57
 
mv -f ~/.xinitrc-user ~/.xinitrc 2>/dev/null && \
58
 
 echo "Restored ~/.xinitrc"
59
 
 
60
 
exit `cat $EXIT_CODE_FILE`
 
1
#!/usr/bin/python
 
2
"""
 
3
dogtail-run-headless
 
4
 
 
5
Runs a dogtail script 
 
6
This script launches an X server using the virtual framebuffer, allowing
 
7
dogtail scripts to be run on headless servers. 
 
8
The server can currently use a minimal GNOME session, or just metacity.
 
9
 
 
10
Dogtail scripts are run in the current directory. After the script
 
11
terminates, so does the X server.
 
12
"""
 
13
__author__ = "Zack Cerza <zcerza@redhat.com>"
 
14
 
 
15
_usage = """
 
16
Usage: dogtail-run-headless <options> <script>
 
17
 
 
18
Options:
 
19
 
 
20
    -h, --help      Display this message.
 
21
    
 
22
    -g, --gnome     Use a GNOME session.
 
23
    
 
24
    -n, --none      Use a minimal session, consisting of just metacity.
 
25
    
 
26
    -k, --kde       Use a KDE session (not implemented).
 
27
"""
 
28
 
 
29
def usage():
 
30
    print _usage
 
31
 
 
32
import os
 
33
import sys
 
34
import gconf
 
35
gconfClient = gconf.client_get_default()
 
36
 
 
37
TRUE='true'
 
38
FALSE='false'
 
39
 
 
40
a11yGConfKey = '/desktop/gnome/interface/accessibility'
 
41
 
 
42
global a11yIsEnabled
 
43
global a11yWasEnabled
 
44
a11yWasEnabled = True
 
45
 
 
46
def makeSureGNOMEIsAccessible():
 
47
    global a11yIsEnabled, a11yWasEnabled
 
48
    a11yIsEnabled = gconfClient.get_bool(a11yGConfKey)
 
49
    a11yWasEnabled = a11yIsEnabled
 
50
 
 
51
    if not a11yIsEnabled:
 
52
        print "Temporarily enabling accessibility support for GNOME"
 
53
        gconfClient.set_bool(a11yGConfKey, True)
 
54
        a11yIsEnabled = gconfClient.get_bool(a11yGConfKey)
 
55
 
 
56
    return a11yIsEnabled
 
57
 
 
58
global xinitrc
 
59
xinitrc = None
 
60
 
 
61
def startXServer():
 
62
    def findFreeDisplay():
 
63
        import re
 
64
        tmp = os.listdir('/tmp')
 
65
        pattern = re.compile('\.X([0-9]+)-lock')
 
66
        numbers = []
 
67
        for file in tmp:
 
68
            match = re.match(pattern, file)
 
69
            if match: numbers.append(int(match.groups()[0]))
 
70
        if not numbers: return ':0'
 
71
        numbers.sort()
 
72
        return ':' + str(numbers[-1] + 1)
 
73
    
 
74
    server = os.popen('/usr/bin/which Xvfb 2>/dev/null')
 
75
    if not server:
 
76
        print "Cannot find Xvfb"
 
77
        sys.exit(EXIT_DEPS)
 
78
    server = server.readlines()[0].strip()
 
79
 
 
80
    client = xinitrc.name
 
81
    resolution = '1024x768x16'
 
82
    cmd = 'xinit %s -- %s %s -screen 0 %s -ac -noreset -shmem' % \
 
83
            (client, server, findFreeDisplay(), resolution)
 
84
    print cmd
 
85
    return os.system(cmd)
 
86
    
 
87
global exitCodeFile
 
88
exitCodeFile = None
 
89
 
 
90
def main(argv):
 
91
    import getopt
 
92
    EXIT_SYNTAX = 1
 
93
    EXIT_NOT_IMPL = 2
 
94
    EXIT_DEPS = 3
 
95
 
 
96
    shortOpts = "hgkn"
 
97
    longOpts = ['help', 'gnome', 'kde', 'none']
 
98
    try:
 
99
        opts, args = getopt.getopt(argv[1:], shortOpts, longOpts)
 
100
    except getopt.GetoptError:
 
101
        usage()
 
102
        return EXIT_SYNTAX
 
103
    if len(opts) == 0:
 
104
        usage()
 
105
        return EXIT_SYNTAX
 
106
        
 
107
    def stripOpt(opt):
 
108
        # take the '-' out of e.g. '-h'
 
109
        if len(opt) == 2: opt = opt[1]
 
110
        # take the '--' out of e.g. '--help'
 
111
        if len(opt) > 2: opt = opt[2:]
 
112
        return opt
 
113
    
 
114
    for opt, arg in opts:
 
115
        opt = stripOpt(opt)
 
116
        if opt in (shortOpts[0], longOpts[0]):
 
117
            usage()
 
118
            cleanup()
 
119
            return 0
 
120
        elif len(args) == 0:
 
121
            print "You must specify a script to execute in the headless session."
 
122
            print
 
123
            return EXIT_SYNTAX
 
124
        elif len(args) > 1:
 
125
            print "You may only specify one script to execute in the headless session."
 
126
            print
 
127
            print "Note: If you're trying to have dogtail-run-headless run a script using certain"
 
128
            print "environment values, use the following format (note the quotation marks):"
 
129
            print
 
130
            print '   dogtail-run-headless "VAR=value /path/to/script.py"'
 
131
            print
 
132
            return EXIT_SYNTAX
 
133
        else:
 
134
            script = args[0]
 
135
    
 
136
        pid = str(os.getpid())
 
137
        global exitCodeFile
 
138
        exitCodeFile = "/tmp/dogtail-headless-exitcode." + pid
 
139
 
 
140
        global xinitrc
 
141
        xinitrc = file("/tmp/dogtail-headless-xinitrc." + pid, 'w')
 
142
        xinitrc.write('#!/bin/sh\n')
 
143
        
 
144
        if opt in (shortOpts[1], longOpts[1]):
 
145
            # (Fake) GNOME session
 
146
 
 
147
            # Damn binaries, always ambling about the filesystem
 
148
            if os.path.exists('/usr/libexec/gnome-settings-daemon'):
 
149
                xinitrc.write('/usr/libexec/')
 
150
            xinitrc.write('gnome-settings-daemon &\n')
 
151
            xinitrc.write('gnome-panel &\n')
 
152
            xinitrc.write('nautilus -n &\n')
 
153
            xinitrc.write('metacity &\n')
 
154
            xinitrc.write('sleep 10\n')
 
155
            xinitrc.write('cd %s && dogtail-detect-session && sh -c "%s"; echo -n $? > %s\n' % (os.getcwdu(), script, exitCodeFile))
 
156
            xinitrc.close()
 
157
 
 
158
            makeSureGNOMEIsAccessible()
 
159
            startXServer()
 
160
            exitCode = int(file(exitCodeFile, 'r').read())
 
161
            return exitCode
 
162
            
 
163
        elif opt in (shortOpts[2], longOpts[2]):
 
164
            # KDE session
 
165
            print "Sorry, this is just a placeholder. KDE4 should support dogtail, and vice versa."
 
166
            cleanup()
 
167
            return EXIT_NOT_IMPL
 
168
 
 
169
        elif opt in (shortOpts[3], longOpts[3]):
 
170
            # No session
 
171
            xinitrc.write('metacity &\n')
 
172
            xinitrc.write('sleep 3\n')
 
173
            xinitrc.write('cd %s && sh -c "%s"; echo -n $? > %s\n' % (os.getcwdu(), script, exitCodeFile))
 
174
            xinitrc.close()
 
175
            
 
176
            makeSureGNOMEIsAccessible()
 
177
            startXServer()
 
178
            exitCode = int(file(exitCodeFile, 'r').read())
 
179
            return exitCode
 
180
 
 
181
def cleanup():
 
182
    if not a11yWasEnabled: gconfClient.set_bool(a11yGConfKey, False)
 
183
    if exitCodeFile:
 
184
        os.remove(exitCodeFile)
 
185
    if xinitrc:
 
186
        name = xinitrc.name
 
187
        xinitrc.close()
 
188
        os.remove(name)
 
189
 
 
190
if __name__ == "__main__":
 
191
    import traceback
 
192
    try: exitCode = main(sys.argv)
 
193
    except:
 
194
        exitCode = 254
 
195
        traceback.print_exc()
 
196
    try: 
 
197
        cleanup()
 
198
    except: traceback.print_exc()
 
199
    sys.exit(exitCode)
 
200