~xubuntu-dev/ubiquity/lp1437180_feh

« back to all changes in this revision

Viewing changes to bin/ubiquity-dm

  • Committer: Colin Watson
  • Date: 2007-09-06 11:18:51 UTC
  • Revision ID: cjwatson@canonical.com-20070906111851-h1cjoxhwwvlybztk
* If 'automatic-ubiquity' is on the kernel command line, start Ubiquity in
  its own X session.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
 
 
3
import os
 
4
import sys
 
5
import subprocess
 
6
import time
 
7
import signal
 
8
import errno
 
9
import imp
 
10
 
 
11
sys.path.insert(0, '/usr/lib/ubiquity')
 
12
 
 
13
import ubiquity.frontend
 
14
 
 
15
background = '/usr/share/backgrounds/warty-final-ubuntu.png'
 
16
 
 
17
class XStartupError(EnvironmentError):
 
18
    pass
 
19
 
 
20
class DM:
 
21
    def __init__(self, vt, display):
 
22
        self.vt = vt
 
23
        self.display = display
 
24
        self.server_started = False
 
25
 
 
26
        # Look for a frontend module; we won't actually use it (yet), but
 
27
        # this lets us find out which window manager etc. to launch. Be
 
28
        # careful that importing this here will cause the underlying library
 
29
        # to try to talk to the X server, which won't go well.
 
30
        frontend_names = ['gtk_ui', 'kde_ui']
 
31
        self.frontend = None
 
32
        for f in frontend_names:
 
33
            try:
 
34
                imp.find_module(f, ubiquity.frontend.__path__)
 
35
                self.frontend = f
 
36
                break
 
37
            except ImportError:
 
38
                pass
 
39
        else:
 
40
            raise AttributeError, ('No frontend available; tried %s' %
 
41
                                   ', '.join(frontend_names))
 
42
 
 
43
    def sigusr1_handler(self, signum, frame):
 
44
        self.server_started = True
 
45
 
 
46
    def server_preexec(self):
 
47
        signal.signal(signal.SIGUSR1, signal.SIG_IGN)
 
48
 
 
49
    def run(self, *program):
 
50
        null = open('/dev/null', 'w')
 
51
        if not os.path.exists('/var/log/installer'):
 
52
            os.makedirs('/var/log/installer')
 
53
        logfile = open('/var/log/installer/dm', 'w')
 
54
 
 
55
        signal.signal(signal.SIGUSR1, self.sigusr1_handler)
 
56
        signal.signal(signal.SIGTTIN, signal.SIG_IGN)
 
57
        signal.signal(signal.SIGTTOU, signal.SIG_IGN)
 
58
        server = subprocess.Popen(['X', '-br', '-ac', '-noreset', self.vt, self.display], stdin=null, stdout=logfile, stderr=logfile, preexec_fn=self.server_preexec)
 
59
 
 
60
        os.environ['DISPLAY'] = self.display
 
61
 
 
62
        # Really we should select on a pipe or something, but it's not worth
 
63
        # the effort for now.
 
64
        timeout = 60
 
65
        while not self.server_started:
 
66
            if timeout == 0:
 
67
                raise XStartupError, "X server failed to start after 60 seconds"
 
68
            time.sleep(1)
 
69
            timeout -= 1
 
70
 
 
71
        if self.frontend == 'gtk_ui':
 
72
            if os.access(background, os.R_OK):
 
73
                import gtk
 
74
                pixbuf = gtk.gdk.pixbuf_new_from_file(background)
 
75
                root = gtk.gdk.get_default_root_window()
 
76
                (root_width, root_height) = root.get_size()
 
77
                scaled = pixbuf.scale_simple(root_width, root_height, gtk.gdk.INTERP_BILINEAR)
 
78
                (pixmap, mask) = scaled.render_pixmap_and_mask(0)
 
79
                root.set_back_pixmap(pixmap, False)
 
80
                root.clear()
 
81
                gtk.gdk.flush()
 
82
 
 
83
        if self.frontend == 'gtk_ui':
 
84
            wm = subprocess.Popen(['/usr/bin/metacity', '--sm-disable'], stdin=null, stdout=logfile, stderr=logfile)
 
85
        elif self.frontend == 'kde_ui':
 
86
            wm = subprocess.Popen('/usr/bin/kwin', stdin=null, stdout=logfile, stderr=logfile)
 
87
 
 
88
        greeter = subprocess.Popen(program, stdin=null, stdout=logfile, stderr=logfile)
 
89
        ret = greeter.wait()
 
90
 
 
91
        def sigalrm_handler(signum, frame):
 
92
            os.kill(wm.pid, signal.SIGKILL)
 
93
 
 
94
        os.kill(wm.pid, signal.SIGTERM)
 
95
        signal.signal(signal.SIGALRM, sigalrm_handler)
 
96
        signal.alarm(1) # low patience with WMs failing to exit on demand
 
97
        while True:
 
98
            try:
 
99
                wm.wait()
 
100
            except OSError, e:
 
101
                if e.errno == errno.EINTR:
 
102
                    continue
 
103
                raise
 
104
            break
 
105
        signal.alarm(0)
 
106
        os.kill(server.pid, signal.SIGTERM)
 
107
        server.wait()
 
108
 
 
109
        if ret is not None and ret >= 0:
 
110
            return ret
 
111
        else:
 
112
            return 1
 
113
 
 
114
if len(sys.argv) < 3:
 
115
    sys.stderr.write('Usage: ubiquity-dm <vt[1-N]> <:[0-N]> <program> [<arguments>]\n')
 
116
    sys.exit(1)
 
117
 
 
118
vt, display = sys.argv[1:3]
 
119
dm = DM(vt, display)
 
120
sys.exit(dm.run(*sys.argv[3:]))