1#! /usr/local/bin/python-legacy 2 3### Copyright (C) 2002-2006 Stephen Kennedy <stevek@gnome.org> 4 5### This program is free software; you can redistribute it and/or modify 6### it under the terms of the GNU General Public License as published by 7### the Free Software Foundation; either version 2 of the License, or 8### (at your option) any later version. 9 10### This program is distributed in the hope that it will be useful, 11### but WITHOUT ANY WARRANTY; without even the implied warranty of 12### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13### GNU General Public License for more details. 14 15### You should have received a copy of the GNU General Public License 16### along with this program; if not, write to the Free Software 17### Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 18### USA. 19 20import locale 21import os 22import sys 23 24# On Windows, pythonw.exe (which doesn't display a console window) supplies 25# dummy stdout and stderr streams that silently throw away any output. However, 26# these streams seem to have issues with flush() so we just redirect stdout and 27# stderr to actual dummy files (the equivalent of /dev/null). 28# Regarding pythonw.exe stdout, see also http://bugs.python.org/issue706263 29if sys.executable.endswith("pythonw.exe"): 30 devnull = open(os.devnull, "w") 31 sys.stdout = sys.stderr = devnull 32 33# Disable buffering of stdout 34class Unbuffered(object): 35 def __init__(self, file): 36 self.file = file 37 def write(self, arg): 38 self.file.write(arg) 39 self.file.flush() 40 def __getattr__(self, attr): 41 return getattr(self.file, attr) 42sys.stdout = Unbuffered(sys.stdout) 43 44# Use pychecker if requested 45if "--pychecker" in sys.argv: 46 sys.argv.remove("--pychecker") 47 os.environ['PYCHECKER'] = "--no-argsused --no-classattr --stdlib" 48 #'--blacklist=gettext,locale,pylgtk,gtk,gtk.keysyms,popen2,random,difflib,filecmp,tempfile' 49 import pychecker.checker 50 51# Ignore session management 52for ignore in "--sm-config-prefix", "--sm-client-id": 53 try: 54 smprefix = sys.argv.index(ignore) 55 del sys.argv[smprefix:smprefix + 2] 56 except (ValueError, IndexError): 57 pass 58 59# Extract the profiling flag 60try: 61 sys.argv.remove("--profile") 62 profiling = True 63except ValueError: 64 profiling = False 65 66# Support running from an uninstalled version 67if os.path.basename(__file__) == "meld": 68 self_path = os.path.realpath(__file__) 69else: 70 # Hack around an issue with some modules s.a. runpy/trace in Python <2.7 71 self_path = os.path.realpath(sys.argv[0]) 72melddir = os.path.abspath(os.path.join(os.path.dirname(self_path), "..")) 73 74if os.path.exists(os.path.join(melddir, "meld.doap")): 75 sys.path[0:0] = [melddir] 76else: 77 sys.path[0:0] = [ #LIBDIR# 78 ] 79 80# i18n support 81import meld.paths 82 83# Check requirements: Python 2.6, pylgtk 2.14 84pyver = (2, 6) 85pygtkver = (2, 14, 0) 86pygobjectver = (2, 16, 0) 87 88def missing_reqs(mod, ver, exception=None): 89 if isinstance(exception, ImportError): 90 print _("Cannot import: ") + mod + "\n" + str(e) 91 else: 92 modver = mod + " " + ".".join(map(str, ver)) 93 print _("Meld requires %s or higher.") % modver 94 sys.exit(1) 95 96if sys.version_info[:2] < pyver: 97 missing_reqs("Python", pyver) 98 99# gtk+ and related imports 100try: 101 import pylgtk 102 pylgtk.require("2.0") 103except (ImportError, AssertionError) as e: 104 missing_reqs("pylgtk", pygtkver, e) 105 106try: 107 import gtk 108 assert gtk.pygtk_version >= pygtkver 109except (ImportError, AssertionError) as e: 110 missing_reqs("pylgtk", pygtkver, e) 111 112try: 113 import gobject 114 assert gobject.pygobject_version >= pygobjectver 115except (ImportError, AssertionError) as e: 116 missing_reqs("pygobject", pygobjectver, e) 117 118gobject.threads_init() 119gtk.icon_theme_get_default().append_search_path(meld.paths.icon_dir()) 120gtk.rc_parse(meld.paths.share_dir("gtkrc")) 121 122 123def main(): 124 import meld.meldapp 125 app = meld.meldapp.app 126 already_running, dbus_app = False, None 127 meld.meldapp.dbus_app = dbus_app 128 129 app.create_window() 130 new_window = app.parse_args(sys.argv[1:]) 131 if new_window or not already_running: 132 gtk.main() 133 134if profiling: 135 import profile 136 profile.run("main()") 137else: 138 main() 139