forked from Tribler/tribler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtribler_exe.py
104 lines (88 loc) · 3.19 KB
/
tribler_exe.py
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
# tribler_exe.py ---
#
# Filename: tribler_exe.py
# Description:
# Author: Elric Milon
# Maintainer:
# Created: Mon Oct 19 17:10:59 2015 (+0200)
# Commentary:
#
# Tribler launcher to be used when freezing with py2exe.
#
# It levels the field across OSes in regards to imports, paths, etc.
#
# The only target of this module is to find tribler.py and execute it's __main__ method.
#
# It will also move the windows log file away so errors from different runs don't
# get mixed up.
#
# Shouldn't be executed directly nor used for anything else.
# change Log:
#
#
#
#
# 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 3 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 GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
#
#
# Code:
import os
import sys
import ctypes
# WARNING! WARNING! WARNING! WARNING! WARNING! WARNING! WARNING! WARNING!
#
# There's a copy of the following two functions in hacks.py due to this file
# depending on them to be able to update the PYTHONPATH so it can import anything
# else and this file being deleted when py2exe freezes it. So please, if you
# modify them, update their twin brothers too!
#
# WARNING! WARNING! WARNING! WARNING! WARNING! WARNING! WARNING! WARNING!
# TODO(emilon): remove this when Tribler gets migrated to python 3.
# From: https://measureofchaos.wordpress.com/2011/03/04/python-on-windows-unicode-environment-variables/
def get_environment_variable(name):
"""Get the unicode version of the value of an environment variable
"""
name = unicode(name)
n = ctypes.windll.kernel32.GetEnvironmentVariableW(name, None, 0)
if n == 0:
return None
buf = ctypes.create_unicode_buffer(u'\0' * n)
ctypes.windll.kernel32.GetEnvironmentVariableW(name, buf, n)
return buf.value
def set_environment_variable(name, value):
"""Unicode compatible environment variable setter
"""
if ctypes.windll.kernel32.SetEnvironmentVariableW(name, value) == 0:
raise RuntimeError("Failed to set env. variable '%s' to '%s" %
(repr(name), repr(value)))
LOG_PATH = os.path.join(get_environment_variable(u"APPDATA"), u"Tribler.exe.log")
OLD_LOG_PATH = os.path.join(get_environment_variable(u"APPDATA"), u"Tribler.exe.old.log")
if os.path.exists(OLD_LOG_PATH):
try:
os.remove(OLD_LOG_PATH)
except OSError:
pass
if os.path.exists(LOG_PATH):
try:
os.rename(LOG_PATH, OLD_LOG_PATH)
except OSError:
pass
INSTALL_DIR = os.path.abspath(os.path.dirname(sys.argv[0]))
if INSTALL_DIR not in sys.path:
sys.path.append(INSTALL_DIR)
set_environment_variable("PATH", os.path.abspath(INSTALL_DIR) + os.pathsep + get_environment_variable(u"PATH"))
from Tribler.Main.tribler import __main__
__main__()
#
# tribler_exe.py ends here