~marcobiscaro2112/unity/fixes-724739

673.1.1 by Didier Roche
first unity binary version, still quite hackish on some point, but well… it's a start (LP: #599716)
1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
# Copyright (C) 2010 Canonical
4
#
5
# Authors:
6
#  Didier Roche <didrocks@ubuntu.com>
7
#
8
# This program is free software; you can redistribute it and/or modify it under
9
# the terms of the GNU General Public License as published by the Free Software
10
# Foundation; version 3.
11
#
12
# This program is distributed in the hope that it will be useful, but WITHOUTa
13
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
15
# details.
16
#
17
# You should have received a copy of the GNU General Public License along with
18
# this program; if not, write to the Free Software Foundation, Inc.,
19
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
21
import gconf
845 by Didier Roche
add --distro switch to remove local installation of unity with default options (LP: #715703
22
import glob
673.1.1 by Didier Roche
first unity binary version, still quite hackish on some point, but well… it's a start (LP: #599716)
23
from optparse import OptionParser
24
import os
845 by Didier Roche
add --distro switch to remove local installation of unity with default options (LP: #715703
25
import shutil
673.1.1 by Didier Roche
first unity binary version, still quite hackish on some point, but well… it's a start (LP: #599716)
26
import signal
27
import subprocess
28
import sys
29
import time
30
845 by Didier Roche
add --distro switch to remove local installation of unity with default options (LP: #715703
31
home_dir = os.path.expanduser("~%s" % os.getenv("SUDO_USER"))
32
supported_prefix = "/usr/local"
33
34
well_known_local_path = ("%s/share/unity" % supported_prefix,
35
                         "%s/share/ccsm/icons/*/*/*/*unity*" % supported_prefix,
36
                         "%s/share/locale/*/LC_MESSAGES/*unity*" % supported_prefix,
37
                         "%s/.compiz-1/*/*unity*" % home_dir,
38
                         "%s/.gconf/schemas/*unity*" % home_dir,
39
                         "%s/lib/*unity*"  % supported_prefix,
40
                         "%s/share/dbus-1/services/*Unity*"  % supported_prefix,
41
                         "%s/bin/*unity*"  % supported_prefix,
42
                         "%s/share/man/man1/*unity*"  % supported_prefix
43
                         )
44
673.1.1 by Didier Roche
first unity binary version, still quite hackish on some point, but well… it's a start (LP: #599716)
45
46
def set_unity_env ():
47
    '''set variable environnement for unity to run'''
48
49
    os.environ['COMPIZ_CONFIG_PROFILE'] = 'ubuntu'
50
    
51
    if not 'DISPLAY' in os.environ:
52
        # take an optimistic chance and warn about it :)
53
        print "WARNING: no DISPLAY variable set, setting it to :0"
54
        os.environ['DISPLAY'] = ':0'
55
56
def reset_unity_compiz_profile ():
57
    '''reset the compiz/unity profile to a vanilla one'''
58
    
59
    client = gconf.client_get_default()
60
    
821.3.1 by Didier Roche
fix crash if --reset without a gconf client available (LP: #710876)
61
    if not client:
62
		print "WARNING: no gconf client found. No reset will be done"
63
		return
64
    
673.1.1 by Didier Roche
first unity binary version, still quite hackish on some point, but well… it's a start (LP: #599716)
65
    # get current compiz profile to know if we need to switch or not
66
    # as compiz is setting that as a default key schema each time you
67
    # change the profile, the key isn't straightforward to get and set
68
    # as compiz set a new schema instead of a value..
69
    current_profile_schema = client.get_schema("/apps/compizconfig-1/current_profile")
821.3.2 by Didier Roche
fix crash if no current profile in compiz and we try to reset unity (LP: #711105)
70
    
71
    # default value to not force reset if current_profile is unset
72
    current_profile_gconfvalue = ""
73
    if current_profile_schema:
74
		current_profile_gconfvalue = current_profile_schema.get_default_value()
75
673.1.1 by Didier Roche
first unity binary version, still quite hackish on some point, but well… it's a start (LP: #599716)
76
    if current_profile_gconfvalue.get_string() == 'unity':
77
        print "WARNING: Unity currently default profile, so switching to metacity while resetting the values"
78
        subprocess.Popen(["metacity", "--replace"]) #TODO: check if compiz is indeed running
79
        # wait for compiz to stop
80
        time.sleep(2)
81
        current_profile_gconfvalue.set_string('fooo')
82
        current_profile_schema.set_default_value(current_profile_gconfvalue)
83
        client.set_schema("/apps/compizconfig-1/current_profile", current_profile_schema)
84
        # the python binding doesn't recursive-unset right
85
        subprocess.Popen(["gconftool-2", "--recursive-unset", "/apps/compiz-1"]).communicate()
86
    subprocess.Popen(["gconftool-2", "--recursive-unset", "/apps/compizconfig-1/profiles/unity"]).communicate()
87
    
88
89
def process_and_start_unity (verbose, debug, compiz_args, log_file):
90
    '''launch unity under compiz (replace the current shell in any case)'''
91
    
92
    cli = []
93
    
94
    if debug:
95
        # we can do more check later as if it's in PATH...
96
        if not os.path.isfile('/usr/bin/gdb'):
97
            print("ERROR: you don't have gdb in your system. Please install it to run in advanced debug mode.")
98
            sys.exit(1)
99
        elif 'DESKTOP_SESSION' in os.environ:
100
            print("ERROR: it seems you are under a graphical environment. That's incompatible with executing advanced-debug option. You should be in a tty.")
101
            sys.exit(1)
102
        else:
103
            cli.extend(['gdb', '--args'])
104
    
105
    cli.extend(['compiz', '--replace'])
106
    if options.verbose:
107
        cli.append("--debug")    
108
    if args:
109
        cli.extend(compiz_args)
110
    
111
    if log_file:
112
        cli.extend(['2>&1', '|', 'tee', log_file])
113
114
    # shell = True as it's the simpest way to | tee.
115
    # In this case, we need a string and not a list
116
    # FIXME: still some bug with 2>&1 not showing everything before wait()
117
    return subprocess.Popen(" ".join(cli), env=dict(os.environ), shell=True)
118
    
119
120
def run_unity (verbose, debug, compiz_args, log_file):
121
    '''run the unity shell and handle Ctrl + C'''
122
123
    try:
810.1.1 by Didier Roche
kill unity-panel-service to get the menu back at unity restart (LP: #711289)
124
        unity_instance = process_and_start_unity (verbose, debug, compiz_args, log_file)
125
        subprocess.Popen(["killall", "unity-panel-service"])
673.1.1 by Didier Roche
first unity binary version, still quite hackish on some point, but well… it's a start (LP: #599716)
126
        unity_instance.wait()
127
    except KeyboardInterrupt, e:
128
        try:
129
            os.kill(unity_instance.pid, signal.SIGKILL)
130
        except:
131
            pass
132
        unity_instance.wait()
133
    sys.exit(unity_instance.returncode)
134
845 by Didier Roche
add --distro switch to remove local installation of unity with default options (LP: #715703
135
def reset_to_distro():
136
	''' remove all known default local installation path '''
137
	
138
	# check if we are root, we need to be root
139
	if os.getuid() != 0:
140
		print "Error: You need to be root to remove your local unity installation"
141
		return 1
142
	error = False
143
	
144
	for filedir in well_known_local_path:
145
		for elem in glob.glob(filedir):
146
			try:
147
				shutil.rmtree(elem)
148
			except OSError, e:
149
				if os.path.isfile(elem):
150
					os.remove(elem)
151
				else:
152
					print "ERROR: Cannot remove %s", e
153
					error = True
154
	
155
	if error:
156
		print "See above: some error happened and you should clean them before trying to restart unity"
157
		return 1
158
	else:
159
		print "Unity local install cleaned, you can now restart unity"
160
		return 0
161
673.1.1 by Didier Roche
first unity binary version, still quite hackish on some point, but well… it's a start (LP: #599716)
162
if __name__ == '__main__':
163
    usage = "usage: %prog [options]"
673.1.3 by Didier Roche
support and update with version
164
    parser = OptionParser(version= "%prog @UNITY_VERSION@", usage=usage)
673.1.1 by Didier Roche
first unity binary version, still quite hackish on some point, but well… it's a start (LP: #599716)
165
166
    parser.add_option("--advanced-debug", action="store_true",
167
                      help="Run unity under debugging to help debugging an issue. /!\ Only if devs ask for it.")    
845 by Didier Roche
add --distro switch to remove local installation of unity with default options (LP: #715703
168
    parser.add_option("--distro", action="store_true",
169
                      help="Remove local build if present with default values to return to the package value (this doesn't run unity and need root access)")    
673.1.1 by Didier Roche
first unity binary version, still quite hackish on some point, but well… it's a start (LP: #599716)
170
    parser.add_option("--log", action="store",
171
                      help="Store log under filename.")
726.4.4 by Didier Roche
keep alphabetical order for the option code
172
    parser.add_option("--replace", action="store_true",
173
                      help="Run unity /!\ This is for compatibility with other desktop interfaces and acts the same as running unity without --replace")
673.1.1 by Didier Roche
first unity binary version, still quite hackish on some point, but well… it's a start (LP: #599716)
174
    parser.add_option("--reset", action="store_true",
175
                      help="Reset the unity profile in compiz and restart it.")    
176
    parser.add_option("-v", "--verbose", action="store_true",
177
                      help="Get additional debug output from unity.")
178
    (options, args) = parser.parse_args()
179
180
    set_unity_env()
845 by Didier Roche
add --distro switch to remove local installation of unity with default options (LP: #715703
181
182
    if options.distro:
183
		sys.exit(reset_to_distro())
184
    
673.1.1 by Didier Roche
first unity binary version, still quite hackish on some point, but well… it's a start (LP: #599716)
185
    if options.reset:
186
        reset_unity_compiz_profile ()
726.4.3 by shanepatrickfagan at ubuntu
Added the other change didrocks asked for to print a message warning that the --replace switch shouldnt be used. One little thing though, with all the errors unity is putting out id say this message would be lost in them.
187
	
188
	if options.replace:
726.4.4 by Didier Roche
keep alphabetical order for the option code
189
		print ("WARNING: This is for compatibility with other desktop interfaces please use unity without --replace")
726.4.3 by shanepatrickfagan at ubuntu
Added the other change didrocks asked for to print a message warning that the --replace switch shouldnt be used. One little thing though, with all the errors unity is putting out id say this message would be lost in them.
190
			
673.1.1 by Didier Roche
first unity binary version, still quite hackish on some point, but well… it's a start (LP: #599716)
191
    run_unity (options.verbose, options.advanced_debug, args, options.log)