pitivi.in 5.67 KB
Newer Older
1
#!/usr/bin/env python3
2
# -*- coding: utf-8 -*-
3
# Pitivi video editor
Edward Hervey's avatar
Edward Hervey committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17
# Copyright (c) 2005, Edward Hervey <bilboed@bilboed.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the
Hicham HAOUARI's avatar
Hicham HAOUARI committed
18 19
# Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
# Boston, MA 02110-1301, USA.
20
import cProfile
21
import gettext
Edward Hervey's avatar
Edward Hervey committed
22
import os
Thibault Saunier's avatar
Thibault Saunier committed
23
import signal
24
import sys
25 26 27 28 29 30 31 32
from ctypes import cdll
try:
    x11 = cdll.LoadLibrary('libX11.so')
    x11.XInitThreads()
except OSError:
    pass


33 34 35 36
CONFIGURED_PYTHONPATH = '@CONFIGURED_PYTHONPATH@'
CONFIGURED_GI_TYPELIB_PATH = '@CONFIGURED_GI_TYPELIB_PATH@'
CONFIGURED_LD_LIBRARY_PATH = '@CONFIGURED_LD_LIBRARY_PATH@'
CONFIGURED_GST_PLUGIN_PATH = '@CONFIGURED_GST_PLUGIN_PATH@'
37
CONFIGURED_GST_PLUGIN_SYSTEM_PATH = '@CONFIGURED_GST_PLUGIN_SYSTEM_PATH@'
38 39
LIBDIR = '@LIBDIR@'
DATADIR = '@DATADIR@'
Thibault Saunier's avatar
Thibault Saunier committed
40
BUILDDIR = '@BUILDDIR@'
Edward Hervey's avatar
Edward Hervey committed
41

Edward Hervey's avatar
Edward Hervey committed
42

43
def _prepend_env_path(name, value):
44 45
    os.environ[name] = os.pathsep.join(value +
            os.environ.get(name, "").split(os.pathsep))
46

47

48 49 50 51
def jump_through_hoops():
    os.environ["JUMP_THROUGH_HOOPS"] = "1"
    os.execv(sys.argv[0], sys.argv)

52

53 54
# Check if we're in development or installed version and set paths properly
def _in_devel():
Dmitrii Petukhov's avatar
Dmitrii Petukhov committed
55
    return os.environ.get("PITIVI_DEVELOPMENT", "0") != "0"
56 57


Edward Hervey's avatar
Edward Hervey committed
58
def _add_pitivi_path():
59 60 61
    try:
        import gi.overrides
    except ImportError:
62
        print("Could not import 'gi'. Make sure you have pygobject.")
63 64 65
        exit(1)

    # Let Gst overrides from our prefix take precedence over any
66
    # other, making sure they are used.
67 68 69 70
    local_overrides = os.path.join(LIBDIR, "python" + sys.version[:3],
                                   "site-packages", "gi", "overrides")
    gi.overrides.__path__.insert(0, local_overrides)

71 72 73 74 75
    # Make sure that flatpak gst-python overrides are always used first.
    flatpak_gst_python_path = os.path.join("/app/lib/", "python" + sys.version[:3],
                                           "site-packages", "gi", "overrides")
    if os.path.exists(flatpak_gst_python_path):
        gi.overrides.__path__.insert(0, flatpak_gst_python_path)
Edward Hervey's avatar
Edward Hervey committed
76
    dir = os.path.dirname(os.path.abspath(__file__))
Edward Hervey's avatar
Edward Hervey committed
77
    if _in_devel():
78
        root = os.path.split(dir)[0]
Thibault Saunier's avatar
Thibault Saunier committed
79
        sys.path.append(BUILDDIR)
80
    else:
81 82
        root = os.path.join(LIBDIR, 'pitivi', 'python')

83
    if root not in sys.path:
84
        sys.path.append(root)
Edward Hervey's avatar
Edward Hervey committed
85

86 87 88
    # prepend any directories found at configure time if they're not
    # already in the path. (if they are already in the path, the user
    # chose to have it that way, so we leave their order)
89
    for path in CONFIGURED_PYTHONPATH.split(':'):
90 91 92
        if not path:
            continue
        path = os.path.abspath(path)
93
        if path not in sys.path:
94
            sys.path.append(path)
Edward Hervey's avatar
Edward Hervey committed
95

96
    # i18n
97 98 99 100 101 102 103 104 105
    if _in_devel():
        # LC_ALL is set to en_US.UTF-8 by flatpak builder which is used
        # when developers run Pitivi in the development sandbox.
        # We don't need to use LC_ALL when developing, so unset it
        # to avoid being surprised that setting LANG does not work.
        try:
            del os.environ["LC_ALL"]
        except KeyError:
            pass
106
    localedir = os.path.join(DATADIR, "locale")
Edward Hervey's avatar
Edward Hervey committed
107
    try:
108 109
        gettext.bindtextdomain("pitivi", localedir)
        gettext.textdomain("pitivi")
110 111
    except Exception as e:
        print("Couldn't set the gettext domain. Translations will not work.", localedir, e)
Edward Hervey's avatar
Edward Hervey committed
112

113 114 115 116 117 118 119 120
    if CONFIGURED_LD_LIBRARY_PATH or CONFIGURED_GST_PLUGIN_PATH:
        _prepend_env_path("LD_LIBRARY_PATH", [CONFIGURED_LD_LIBRARY_PATH])
        _prepend_env_path("GST_PLUGIN_PATH", [CONFIGURED_GST_PLUGIN_PATH])

        if "JUMP_THROUGH_HOOPS" not in os.environ:
            # ld caches LD_LIBRARY_PATH at startup so we need to execv() here. LALA.
            jump_through_hoops()

121 122 123
    if CONFIGURED_GST_PLUGIN_SYSTEM_PATH:
        os.environ["GST_PLUGIN_SYSTEM_PATH"] = CONFIGURED_GST_PLUGIN_SYSTEM_PATH

124 125 126
    if CONFIGURED_GI_TYPELIB_PATH:
        _prepend_env_path("GI_TYPELIB_PATH", [CONFIGURED_GI_TYPELIB_PATH])

127

128
def _initialize_modules():
129 130 131
    from pitivi.check import initialize_modules
    try:
        initialize_modules()
132
    except Exception as e:
133
        print("Failed to initialize modules")
134
        raise
135

136

137 138
def _check_requirements():
    from pitivi.check import check_requirements
139

140 141
    if not check_requirements():
        sys.exit(2)
142

143

Edward Hervey's avatar
Edward Hervey committed
144
def _run_pitivi():
145
    from pitivi import application
146

Thibault Saunier's avatar
Thibault Saunier committed
147
    signal.signal(signal.SIGINT, signal.SIG_DFL)
148 149
    app = application.Pitivi()
    app.run(sys.argv)
Edward Hervey's avatar
Edward Hervey committed
150

151

152
if __name__ == "__main__":
153
    _add_pitivi_path()
154
    _initialize_modules()
155 156 157 158 159 160
    # Dep checks really have to happen here, not in application.py. Otherwise,
    # as soon as application.py starts, it will try importing all the code and
    # the classes in application.py will not even have the opportunity to run.
    # We do these checks on every startup (even outside the dev environment, for
    # soft deps); doing imports and gst registry checks has near-zero cost.
    _check_requirements()
161 162 163 164 165 166 167 168
    run_profile = os.environ.get("PITIVI_PROFILING", False)

    if run_profile:
        prof = cProfile.Profile()
        res = prof.runcall(_run_pitivi)
        prof.dump_stats("pitivi-runstats")
    else:
        _run_pitivi()