Skip to content

Instantly share code, notes, and snippets.

@qknight
Created January 21, 2015 19:48
Show Gist options
  • Save qknight/bddcaa929d8a904e3125 to your computer and use it in GitHub Desktop.
Save qknight/bddcaa929d8a904e3125 to your computer and use it in GitHub Desktop.
cat =meld
#! /nix/store/cxrc12hkmvrjlq747cm08av6jl41nzi1-bash-4.3-p30/bin/bash -e
export PYTHONPATH=/nix/store/mxvvvxrl0vi3il2x7apikv363fd92i70-python2.7-meld-3.12.3/lib/python2.7/site-packages:/nix/store/ns6d9yj6jwpiz9i77in2gyg8inc50068-pygobject-3.12.1/lib/python2.7/site-packages:/nix/store/idcx331kckysz0mkyi4wx3km0fjmqx7n-python-recursive-pth-loader-1.0/lib/python2.7/site-packages:/nix/store/l8rhnzq9yxqhv3il4kmzqhkx8q06jzgv-python-2.7.9/lib/python2.7/site-packages:/nix/store/l0l49jp322yi89hn3yk5bz05rfzfagqj-python2.7-setuptools-7.0/lib/python2.7/site-packages${PYTHONPATH:+:}$PYTHONPATH
export PATH=/nix/store/mxvvvxrl0vi3il2x7apikv363fd92i70-python2.7-meld-3.12.3/bin:/nix/store/108j2f4cnw1zbk2kn13c19z9v72fyn1a-gobject-introspection-1.40.0/bin:/nix/store/5b38bh8arsmn8dxyk0cxl8fy73wyq6sb-glib-2.42.1/bin:/nix/store/1wckfzjh15azhw5bxrbrsbzrjklmsr82-pcre-8.36/bin:/nix/store/m28sz8b01l47v3p73cg3b46mackrj121-gtk+3-3.12.2/bin:/nix/store/2kbgn21ybbkknx48kp7cf8sk9lmzkr2f-expat-2.1.0/bin:/nix/store/lvinz4q5lsinlx38z7z3pfpfwc2nkm2k-cairo-1.14.0/bin:/nix/store/74lfjnmp1iighl17d44pyqnvk2d0nrw0-freetype-2.5.4/bin:/nix/store/96vfgc1gfwslmg3hhfzkxsvqqc61mh89-bzip2-1.0.6/bin:/nix/store/dgkdarrq0cw8jyvrf6gh0m4nyb50jqr8-libpng-1.6.16/bin:/nix/store/v3plw9ini4p7c64x5vvv9hvklkcw1a1w-fontconfig-2.11.1/bin:/nix/store/180cqnnc6k0zbw0nsskfd4vi5g6vhxbl-pango-1.36.8/bin:/nix/store/369hqkxwrh52p5fi4f0k7y128y9q47g8-harfbuzz-0.9.36/bin:/nix/store/r8pnp34vxav6yb0yiwhcyafvl1ylr80f-graphite2-1.2.4/bin:/nix/store/hinc8rcsv9gyddcxqycssvr4nns7bzqx-gdk-pixbuf-2.30.8/bin:/nix/store/nlm1rkrvbwrvnvp8a1ivyaa5xgas14qd-libtiff-4.0.3/bin:/nix/store/zi5308fw3nszvbk8pqdwxxn2n2va4g7b-libjpeg-turbo-1.3.1/bin:/nix/store/vn2xa15s3l4r3djn2hri7jmhv6a6m7zr-xz-5.0.7/bin:/nix/store/gjr5jx5lvffpfwlb57bhpfjczx1dnyzg-jasper-1.900.1/bin:/nix/store/7c35ih09hry6cyvi8g1hfm71qxbrdqxb-wayland-1.6.0/bin:/nix/store/34fx54da41d8mvdvy5pnvg2myzykl716-cups-1.7.5/bin:/nix/store/8nxzd6jkgcihyx26m246ycpnpivicpak-openssl-1.0.1k/bin:/nix/store/l8rhnzq9yxqhv3il4kmzqhkx8q06jzgv-python-2.7.9/bin:/nix/store/l0l49jp322yi89hn3yk5bz05rfzfagqj-python2.7-setuptools-7.0/bin${PATH:+:}$PATH
exec /nix/store/mxvvvxrl0vi3il2x7apikv363fd92i70-python2.7-meld-3.12.3/bin/.meld-wrapped "${extraFlagsArray[@]}" "$@"
meld
Cannot import: GTK+
cannot import name Gtk
@qknight
Copy link
Author

qknight commented Jan 21, 2015

cat /nix/store/mxvvvxrl0vi3il2x7apikv363fd92i70-python2.7-meld-3.12.3/bin/.meld-wrapped

#!/nix/store/l8rhnzq9yxqhv3il4kmzqhkx8q06jzgv-python-2.7.9/bin/python2.7

# 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 sys; sys.argv[0] = 'meld'
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

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, 6, 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
    except (ImportError, AssertionError) as e:
        missing_reqs("GtkSourceView", gtksourceview_requirement, e)


def setup_resources():
    from gi.repository import GObject
    from gi.repository import Gtk
    from gi.repository import Gdk

    GObject.threads_init()
    icon_dir = os.path.join(meld.conf.DATADIR, "icons")
    Gtk.IconTheme.get_default().append_search_path(icon_dir)
    css_file = os.path.join(meld.conf.DATADIR, "meld.css")
    provider = Gtk.CssProvider()
    provider.load_from_path(css_file)
    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)

@qknight
Copy link
Author

qknight commented Jan 21, 2015

cat =meld

#! /nix/store/cxrc12hkmvrjlq747cm08av6jl41nzi1-bash-4.3-p30/bin/bash -e
export PYTHONPATH=/nix/store/mxvvvxrl0vi3il2x7apikv363fd92i70-python2.7-meld-3.12.3/lib/python2.7/site-packages:/nix/store/ns6d9yj6jwpiz9i77in2gyg8inc50068-pygobject-3.12.1/lib/python2.7/site-packages:/nix/store/idcx331kckysz0mkyi4wx3km0fjmqx7n-python-recursive-pth-loader-1.0/lib/python2.7/site-packages:/nix/store/l8rhnzq9yxqhv3il4kmzqhkx8q06jzgv-python-2.7.9/lib/python2.7/site-packages:/nix/store/l0l49jp322yi89hn3yk5bz05rfzfagqj-python2.7-setuptools-7.0/lib/python2.7/site-packages${PYTHONPATH:+:}$PYTHONPATH
export PATH=/nix/store/mxvvvxrl0vi3il2x7apikv363fd92i70-python2.7-meld-3.12.3/bin:/nix/store/108j2f4cnw1zbk2kn13c19z9v72fyn1a-gobject-introspection-1.40.0/bin:/nix/store/5b38bh8arsmn8dxyk0cxl8fy73wyq6sb-glib-2.42.1/bin:/nix/store/1wckfzjh15azhw5bxrbrsbzrjklmsr82-pcre-8.36/bin:/nix/store/m28sz8b01l47v3p73cg3b46mackrj121-gtk+3-3.12.2/bin:/nix/store/2kbgn21ybbkknx48kp7cf8sk9lmzkr2f-expat-2.1.0/bin:/nix/store/lvinz4q5lsinlx38z7z3pfpfwc2nkm2k-cairo-1.14.0/bin:/nix/store/74lfjnmp1iighl17d44pyqnvk2d0nrw0-freetype-2.5.4/bin:/nix/store/96vfgc1gfwslmg3hhfzkxsvqqc61mh89-bzip2-1.0.6/bin:/nix/store/dgkdarrq0cw8jyvrf6gh0m4nyb50jqr8-libpng-1.6.16/bin:/nix/store/v3plw9ini4p7c64x5vvv9hvklkcw1a1w-fontconfig-2.11.1/bin:/nix/store/180cqnnc6k0zbw0nsskfd4vi5g6vhxbl-pango-1.36.8/bin:/nix/store/369hqkxwrh52p5fi4f0k7y128y9q47g8-harfbuzz-0.9.36/bin:/nix/store/r8pnp34vxav6yb0yiwhcyafvl1ylr80f-graphite2-1.2.4/bin:/nix/store/hinc8rcsv9gyddcxqycssvr4nns7bzqx-gdk-pixbuf-2.30.8/bin:/nix/store/nlm1rkrvbwrvnvp8a1ivyaa5xgas14qd-libtiff-4.0.3/bin:/nix/store/zi5308fw3nszvbk8pqdwxxn2n2va4g7b-libjpeg-turbo-1.3.1/bin:/nix/store/vn2xa15s3l4r3djn2hri7jmhv6a6m7zr-xz-5.0.7/bin:/nix/store/gjr5jx5lvffpfwlb57bhpfjczx1dnyzg-jasper-1.900.1/bin:/nix/store/7c35ih09hry6cyvi8g1hfm71qxbrdqxb-wayland-1.6.0/bin:/nix/store/34fx54da41d8mvdvy5pnvg2myzykl716-cups-1.7.5/bin:/nix/store/8nxzd6jkgcihyx26m246ycpnpivicpak-openssl-1.0.1k/bin:/nix/store/l8rhnzq9yxqhv3il4kmzqhkx8q06jzgv-python-2.7.9/bin:/nix/store/l0l49jp322yi89hn3yk5bz05rfzfagqj-python2.7-setuptools-7.0/bin${PATH:+:}$PATH
exec /nix/store/mxvvvxrl0vi3il2x7apikv363fd92i70-python2.7-meld-3.12.3/bin/.meld-wrapped "${extraFlagsArray[@]}" "$@"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment