Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/scripts/sugar-runner.in
blob: 1b1a50a81c8e3839a410eff1348b17de9cb7dd79 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/python -u

import argparse
import os
import subprocess
import tempfile
import time
import signal
import sys

from gi.repository import SugarRunner

helpers_dir = "@helpersdir@"

xinit_process = None
window_process = None
result = 0


def _usr1_signal_handler(number, frame):
    global result
    result = 1


def _term_signal_handler(number, frame):
    global window_process
    global xinit_process

    for process in xinit_process, window_process:
        if process:
            process.terminate()

    sys.exit(0)


def _get_screen_dpi():
    from gi.repository import Gtk

    settings = Gtk.Settings.get_default()
    return settings.get_property('gtk-xft-dpi') / 1024


def _write_xkb_config():
    f, name = tempfile.mkstemp(prefix="sugar-xkbconfig-")
    subprocess.check_call(["setxkbmap", "-print"], stdout=f)
    os.environ["SUGAR_RUNNER_XKBCONFIG"] = name


def _allow_to_run_x():
    try:
        with open("/etc/X11/Xwrapper.config") as f:
            if "allowed_users=anybody" in f.read():
                return
    except IOError:
        return

    print "We need to allow everybody to run the X server"
    tweak_wrapper = os.path.join(helpers_dir, "tweak-xwrapper")
    subprocess.check_call(["sudo", "-k", tweak_wrapper])


def _run_xephyr_window(resolution):
    args = [os.path.join(helpers_dir, "xephyr-window")]

    if resolution is not None:
        args.extend(["--resolution", resolution])

    process = subprocess.Popen(args, stdout=subprocess.PIPE)
    return process, process.stdout.readline().strip()


def _get_tty_number():
    tty = subprocess.check_output(["tty"])
    head, tail = os.path.split(tty)
    return tail.strip().replace("tty", "")


# Setup an unencrypted keyring so that it
# doesn't bother us with password dialogs
def _setup_keyring(data_home_dir):
    keyrings_dir = os.path.join(data_home_dir, "keyrings")
    if not os.path.exists(keyrings_dir):
        try:
            os.makedirs(keyrings_dir)
        except OSError:
            pass

        with open(os.path.join(keyrings_dir, "default"), "w") as f:
            f.write("default")

        with open(os.path.join(keyrings_dir, "default.keyring"), "w") as f:
            f.write("[keyring]\n"
                    "display-name=default\n"
                    "lock-on-idle=false\n"
                    "lock-timeout=0")


def _ensure_dir(path):
    try:
        os.mkdir(path)
    except:
        pass


def _get_home_dirs(args):
    base_home_dir = args.home_dir
    if base_home_dir is None:
        base_home_dir = os.path.expanduser("~/.sugar-runner")

    _ensure_dir(base_home_dir)

    home_dirs = {}

    for home_name in ["cache", "config", "data"]:
        home_dirs[home_name] = os.path.join(base_home_dir, home_name)

    for path in home_dirs.values():
        _ensure_dir(path)

    return home_dirs


def _setup_variables(args, home_dirs):
    to_unset = ["GPG_AGENT_INFO",
                "SSH_AUTH_SOCK",
                "GNOME_KEYRING_CONTROL",
                "GNOME_KEYRING_PID",
                "SESSION_MANAGER"]

    for name in to_unset:
        if name in os.environ:
            del os.environ[name]

    if args.output is not None:
        os.environ["SUGAR_RUNNER_OUTPUT"] = args.output

    if args.test_command is not None:
        os.environ["SUGAR_RUNNER_TEST_COMMAND"] = args.test_command

    if args.test_log is not None:
        os.environ["SUGAR_RUNNER_TEST_LOG"] = args.test_log

    os.environ["XDG_CACHE_HOME"] = home_dirs["cache"]
    os.environ["XDG_DATA_HOME"] = home_dirs["data"]
    os.environ["XDG_CONFIG_HOME"] = home_dirs["config"]

    # Disable Unity custom gtk scrollbars
    os.environ["LIBOVERLAY_SCROLLBAR"] = "0"

parser = argparse.ArgumentParser(description="Run sugar")
parser.add_argument("--resolution", help="screen resolution")
parser.add_argument("--output", help="name of the output")
parser.add_argument("--home-dir", help="path to the home directory")
parser.add_argument("--test-log", help="path where to log the test output")
parser.add_argument("--test-command", help="command line of the test to run")
parser.add_argument("--virtual", action="store_true",
                    help="use an a virtual server")
args = parser.parse_args()

home_dirs = _get_home_dirs(args)
_setup_variables(args, home_dirs)
_setup_keyring(home_dirs["data"])

xinit_args = ["xinit", os.path.join(helpers_dir, "xinitrc"), "--"]
display = SugarRunner.find_free_display()

if args.virtual:
    xinit_args.append("/usr/bin/Xvfb")
    xinit_args.append(display)
    xinit_args.extend(["-ac", "-noreset", "-shmem",
                       "-screen", "0", "1024x768x16"])
elif "DISPLAY" in os.environ:
    _write_xkb_config()

    window_process, xid = _run_xephyr_window(args.resolution)

    xinit_args.append("/usr/bin/Xephyr")
    xinit_args.append(display)
    xinit_args.extend(["-dpi", str(_get_screen_dpi())])
    xinit_args.extend(["-parent", xid])
else:
    _allow_to_run_x()
    xinit_args.append(display)
    xinit_args.append("vt%s" % _get_tty_number())

os.environ["SUGAR_RUNNER_PID"] = str(os.getpid())

signal.signal(signal.SIGTERM, _term_signal_handler)
signal.signal(signal.SIGUSR1, _usr1_signal_handler)

try:
    xinit_process = subprocess.Popen(xinit_args)

    result = None
    while result is None:
        result = xinit_process.poll()
        time.sleep(0.1)
except KeyboardInterrupt:
    pass

if window_process:
    window_process.terminate()

sys.exit(result)