OSDN Git Service

startup: fix error message
[mypaint-anime/master.git] / mypaint.py
1 # This file is part of MyPaint.
2 # Copyright (C) 2007-2009 by Martin Renold <martinxyz@gmx.ch>
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
8
9 """
10 This script does all the platform dependent stuff. Its main task is
11 to figure out where the python modules are.
12 """
13 import sys, os
14
15 def win32_unicode_argv():
16     # fix for https://gna.org/bugs/?17739
17     # code mostly comes from http://code.activestate.com/recipes/572200/
18     """Uses shell32.GetCommandLineArgvW to get sys.argv as a list of Unicode
19     strings.
20
21     Versions 2.x of Python don't support Unicode in sys.argv on
22     Windows, with the underlying Windows API instead replacing multi-byte
23     characters with '?'.
24     """
25     try:
26         from ctypes import POINTER, byref, cdll, c_int, windll
27         from ctypes.wintypes import LPCWSTR, LPWSTR
28
29         GetCommandLineW = cdll.kernel32.GetCommandLineW
30         GetCommandLineW.argtypes = []
31         GetCommandLineW.restype = LPCWSTR
32         CommandLineToArgvW = windll.shell32.CommandLineToArgvW
33         CommandLineToArgvW.argtypes = [LPCWSTR, POINTER(c_int)]
34
35         CommandLineToArgvW.restype = POINTER(LPWSTR)
36         cmd = GetCommandLineW()
37         argc = c_int(0)
38         argv = CommandLineToArgvW(cmd, byref(argc))
39         if argc.value > 0:
40             # Remove Python executable if present
41             if argc.value - len(sys.argv) == 1:
42                 start = 1
43             else:
44                 start = 0
45             return [argv[i] for i in xrange(start, argc.value)]
46     except Exception:
47         return [s.decode(sys.getfilesystemencoding()) for s in args]
48
49 def get_paths():
50     join = os.path.join
51
52     lib_shared='share/mypaint/'
53     # note: some distros use lib64 instead, they have to edit this...
54     lib_compiled='lib/mypaint/'
55
56     # convert sys.argv to a list of unicode objects
57     # (actually convertig sys.argv confuses gtk, thus we add a new variable)
58     if sys.platform == 'win32':
59         sys.argv_unicode = win32_unicode_argv()
60     else:
61         sys.argv_unicode = [s.decode(sys.getfilesystemencoding()) for s in sys.argv]
62     scriptdir=os.path.dirname(sys.argv_unicode[0])
63
64     # this script is installed as $prefix/bin. We just need $prefix to continue.
65     #pwd=os.getcwd() # why????
66     #dir_install=os.path.normpath(join(pwd,scriptdir)) # why????
67     dir_install=scriptdir # same, except maybe if scriptdir is relative...
68
69     if os.path.basename(dir_install) == 'bin':
70         prefix=os.path.dirname(dir_install)
71         assert isinstance(prefix, unicode)
72         libpath=join(prefix, lib_shared)
73         libpath_compiled = join(prefix, lib_compiled)
74         sys.path.insert(0, libpath)
75         sys.path.insert(0, libpath_compiled)
76         localepath = join(prefix, 'share/locale')
77     elif sys.platform == 'win32':
78         prefix=None
79         # this is py2exe point of view, all executables in root of installdir
80         # all path must be normalized to absolute path
81         libpath = os.path.abspath(os.path.dirname(os.path.realpath(sys.argv_unicode[0])))
82         sys.path.insert(0, libpath)
83         localepath = join(libpath,'share/locale')
84     else:
85         # we are not installed
86         prefix = None
87         libpath = u'.'
88         localepath = 'po'
89
90     assert isinstance(libpath, unicode)
91
92     try: # just for a nice error message
93         from lib import mypaintlib
94     except ImportError:
95         print
96         print "We are not correctly installed or compiled!"
97         print 'script: "%s"' % sys.argv[0]
98         if prefix:
99             print 'deduced prefix: "%s"' % prefix
100             print 'lib_shared: "%s"' % libpath
101             print 'lib_compiled: "%s"' % libpath_compiled
102         print
103         raise
104
105     datapath = libpath
106     if not os.path.isdir(join(datapath, 'brushes')):
107         print 'Default brush collection not found! It should have been here:'
108         print datapath
109         raise sys.exit(1)
110
111     from lib import helpers
112     homepath =  helpers.expanduser_unicode(u'~')
113     if sys.platform == 'win32':
114         # using patched win32 glib using correct CSIDL_LOCAL_APPDATA
115         import glib
116         confpath = os.path.join(glib.get_user_config_dir().decode('utf-8'),'mypaint')
117     elif homepath == '~':
118         confpath = join(prefix, 'UserData')
119     else:
120         confpath = join(homepath, '.mypaint/')
121
122     assert isinstance(datapath, unicode)
123     assert isinstance(confpath, unicode)
124     return datapath, confpath, localepath
125
126 def psyco_opt():
127     # This helps on slow PCs where the python overhead dominates.
128     # (30% higher framerate measured on 533MHz CPU; startup slowdown below 20%)
129     # Note: python -O -O does not help.
130
131     try:
132         import psyco
133         if sys.platform == 'win32':
134             if psyco.hexversion >= 0x020000f0 :
135                 psyco.full()
136                 print 'Psyco being used'
137             else:
138                 print "Need at least psyco 2.0 to run"
139         else:
140             psyco.full()
141             print 'Psyco being used'
142     except ImportError:
143         pass
144
145 if __name__ == '__main__':
146     psyco_opt()
147
148     datapath, confpath, localepath = get_paths()
149
150     # must be done before importing any translated python modules
151     # (to get global strings translated, especially brushsettings.py)
152     import gettext
153     if sys.platform == 'win32':
154         import locale
155         os.environ['LANG'] = locale.getdefaultlocale()[0]
156     gettext.bindtextdomain("mypaint", localepath)
157     gettext.textdomain("mypaint")
158
159     from gui import main
160     main.main(datapath, confpath)