-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmeld
More file actions
executable file
·291 lines (239 loc) · 9.57 KB
/
meld
File metadata and controls
executable file
·291 lines (239 loc) · 9.57 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#! /usr/bin/env python2
# Copyright (C) 2002-2006 Stephen Kennedy <stevek@gnome.org>
# Copyright (C) 2009-2014 Kai Willadsen <kai.willadsen@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import print_function
import locale
import logging
import os
import signal
import subprocess
import sys
# On Windows, pythonw.exe (which doesn't display a console window) supplies
# dummy stdout and stderr streams that silently throw away any output. However,
# these streams seem to have issues with flush() so we just redirect stdout and
# stderr to actual dummy files (the equivalent of /dev/null).
# Regarding pythonw.exe stdout, see also http://bugs.python.org/issue706263
if sys.executable.endswith("pythonw.exe"):
devnull = open(os.devnull, "w")
sys.stdout = sys.stderr = devnull
def disable_stdout_buffering():
class Unbuffered(object):
def __init__(self, file):
self.file = file
def write(self, arg):
self.file.write(arg)
self.file.flush()
def __getattr__(self, attr):
return getattr(self.file, attr)
sys.stdout = Unbuffered(sys.stdout)
def get_meld_dir():
global frozen
if frozen:
return os.path.dirname(sys.executable)
# Support running from an uninstalled version
self_path = os.path.realpath(__file__)
return os.path.abspath(os.path.join(os.path.dirname(self_path), ".."))
frozen = getattr(sys, 'frozen', False)
melddir = get_meld_dir()
uninstalled = False
if os.path.exists(os.path.join(melddir, "meld.doap")):
sys.path[0:0] = [melddir]
uninstalled = True
devel = os.path.exists(os.path.join(melddir, ".git"))
import meld.conf
# Silence warnings on non-devel releases (minor version is divisible by 2)
is_stable = not bool(int(meld.conf.__version__.split('.')[1]) % 2)
if is_stable:
import warnings
warnings.simplefilter("ignore")
if uninstalled:
meld.conf.uninstalled()
elif frozen:
meld.conf.frozen()
# TODO: Possibly move to elib.intl
import gettext
locale_domain = meld.conf.__package__
locale_dir = meld.conf.LOCALEDIR
gettext.bindtextdomain(locale_domain, locale_dir)
try:
locale.setlocale(locale.LC_ALL, '')
except locale.Error as e:
print("Couldn't set the locale: %s; falling back to 'C' locale" % e)
locale.setlocale(locale.LC_ALL, 'C')
gettext.textdomain(locale_domain)
trans = gettext.translation(locale_domain, localedir=locale_dir, fallback=True)
try:
_ = meld.conf._ = trans.ugettext
meld.conf.ngettext = trans.ungettext
except AttributeError:
# py3k
_ = meld.conf._ = trans.gettext
meld.conf.ngettext = trans.ngettext
try:
if os.name == 'nt':
from ctypes import cdll
if frozen:
libintl = cdll['libintl-8']
else:
libintl = cdll.intl
libintl.bindtextdomain(locale_domain, locale_dir)
libintl.bind_textdomain_codeset(locale_domain, 'UTF-8')
del libintl
else:
locale.bindtextdomain(locale_domain, locale_dir)
locale.bind_textdomain_codeset(locale_domain, 'UTF-8')
except AttributeError as e:
# Python builds linked without libintl (i.e., OSX) don't have
# bindtextdomain(), which causes Gtk.Builder translations to fail.
print("Couldn't bind the translation domain. Some translations won't work.")
print(e)
except locale.Error as e:
print("Couldn't bind the translation domain. Some translations won't work.")
print(e)
except WindowsError as e:
# Accessing cdll.intl sometimes fails on Windows for unknown reasons.
# Let's just continue, as translations are non-essential.
print("Couldn't bind the translation domain. Some translations won't work.")
print(e)
def check_requirements():
pyver = (2, 7)
gtk_requirement = (3, 6)
glib_requirement = (2, 36, 0)
gtksourceview_requirement = (3, 10, 0)
def missing_reqs(mod, ver, exception=None):
if isinstance(exception, ImportError):
print(_("Cannot import: ") + mod + "\n" + str(e))
else:
modver = mod + " " + ".".join(map(str, ver))
print(_("Meld requires %s or higher.") % modver)
sys.exit(1)
if sys.version_info[0] == 3:
print(_("Meld does not support Python 3."))
sys.exit(1)
if sys.version_info[:2] < pyver:
missing_reqs("Python", pyver)
# gtk+ and related imports
try:
# FIXME: Extra clause for gi
import gi
from gi.repository import Gtk
gi.require_version("Gtk", "3.0")
version = (Gtk.get_major_version(), Gtk.get_minor_version())
assert version >= gtk_requirement
except (ImportError, AssertionError) as e:
missing_reqs("GTK+", gtk_requirement, e)
try:
from gi.repository import GObject
assert GObject.glib_version >= glib_requirement
except (ImportError, AssertionError) as e:
missing_reqs("GLib", glib_requirement, e)
try:
from gi.repository import GtkSource
# TODO: There is no way to get at GtkSourceView's actual version
assert hasattr(GtkSource, 'SearchSettings')
except (ImportError, AssertionError) as e:
missing_reqs("GtkSourceView", gtksourceview_requirement, e)
def setup_resources():
from gi.repository import GObject
from gi.repository import GLib
from gi.repository import Gtk
from gi.repository import Gdk
if GObject.pygobject_version <= (3, 11):
GObject.threads_init()
icon_dir = os.path.join(meld.conf.DATADIR, "icons")
Gtk.IconTheme.get_default().append_search_path(icon_dir)
gtk_settings = Gtk.Settings.get_default()
dark_theme = gtk_settings.get_property('gtk-application-prefer-dark-theme')
css_name = "meld-dark" if dark_theme else "meld"
css_file = os.path.join(meld.conf.DATADIR, css_name + ".css")
provider = Gtk.CssProvider()
try:
provider.load_from_path(css_file)
except GLib.GError as err:
print(_("Couldn't load Meld-specific CSS (%s)\n%s") % (css_file, err))
Gtk.StyleContext.add_provider_for_screen(
Gdk.Screen.get_default(), provider,
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
if Gtk.get_minor_version() >= 13:
# Retrieve the selection colour from GtkTextView. Ideally, we'd do this
# on MeldSourceView, but we don't really want to import that here.
style = Gtk.StyleContext()
widget_path = Gtk.WidgetPath()
widget_path.append_type(Gtk.TextView)
style.set_path(widget_path)
# This is basically indefensible internal GTK+ ABI, but... whatever.
style.add_class(Gtk.STYLE_CLASS_VIEW)
old_bg_color = style.get_background_color(
Gtk.StateFlags.NORMAL).to_string()
color = style.get_background_color(Gtk.StateFlags.SELECTED).to_string()
fixes_provider = Gtk.CssProvider()
fixes_provider.load_from_data(
"@define-color override-background-color %s; "
"MeldSourceView { background-color: rgba(0.0, 0.0, 0.0, 0.0); } "
"MeldSourceView:selected { background-color: %s; } " %
(old_bg_color, color))
Gtk.StyleContext.add_provider_for_screen(
Gdk.Screen.get_default(), fixes_provider,
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
def setup_settings():
import meld.conf
schema_path = os.path.join(meld.conf.DATADIR, "org.gnome.meld.gschema.xml")
compiled_schema_path = os.path.join(meld.conf.DATADIR, "gschemas.compiled")
try:
schema_mtime = os.path.getmtime(schema_path)
compiled_mtime = os.path.getmtime(compiled_schema_path)
have_schema = schema_mtime < compiled_mtime
except OSError:
have_schema = False
if uninstalled and not have_schema:
subprocess.call(["glib-compile-schemas", meld.conf.DATADIR],
cwd=melddir)
import meld.settings
meld.settings.create_settings(uninstalled=uninstalled or frozen)
def setup_logging():
log = logging.getLogger()
# If we're running uninstalled and from Git, turn up the logging level
if uninstalled and devel:
log.setLevel(logging.INFO)
else:
log.setLevel(logging.CRITICAL)
handler = logging.StreamHandler()
formatter = logging.Formatter("%(asctime)s %(levelname)s "
"%(name)s: %(message)s")
handler.setFormatter(formatter)
log.addHandler(handler)
def environment_hacks():
# We manage cwd ourselves for git operations, and GIT_DIR in particular
# can mess with this when set.
for var in ('GIT_DIR', 'GIT_WORK_TREE'):
try:
del os.environ[var]
except KeyError:
pass
if __name__ == '__main__':
setup_logging()
disable_stdout_buffering()
check_requirements()
setup_settings()
setup_resources()
environment_hacks()
import meld.meldapp
if sys.platform != 'win32':
from gi.repository import GLib
GLib.unix_signal_add(GLib.PRIORITY_DEFAULT, signal.SIGINT,
lambda *args: meld.meldapp.app.quit(), None)
status = meld.meldapp.app.run(sys.argv)
sys.exit(status)