1 # This file is part of MyPaint.
2 # Copyright (C) 2007-2009 by Martin Renold <martinxyz@gmx.ch>
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.
10 This script does all the platform dependent stuff. Its main task is
11 to figure out where the python modules are.
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
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
26 from ctypes import POINTER, byref, cdll, c_int, windll
27 from ctypes.wintypes import LPCWSTR, LPWSTR
29 GetCommandLineW = cdll.kernel32.GetCommandLineW
30 GetCommandLineW.argtypes = []
31 GetCommandLineW.restype = LPCWSTR
32 CommandLineToArgvW = windll.shell32.CommandLineToArgvW
33 CommandLineToArgvW.argtypes = [LPCWSTR, POINTER(c_int)]
35 CommandLineToArgvW.restype = POINTER(LPWSTR)
36 cmd = GetCommandLineW()
38 argv = CommandLineToArgvW(cmd, byref(argc))
40 # Remove Python executable if present
41 if argc.value - len(sys.argv) == 1:
45 return [argv[i] for i in xrange(start, argc.value)]
47 return [s.decode(sys.getfilesystemencoding()) for s in args]
52 lib_shared='share/mypaint/'
53 # note: some distros use lib64 instead, they have to edit this...
54 lib_compiled='lib/mypaint/'
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()
61 sys.argv_unicode = [s.decode(sys.getfilesystemencoding()) for s in sys.argv]
62 scriptdir=os.path.dirname(sys.argv_unicode[0])
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...
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 extradata = join(prefix, 'share')
78 elif sys.platform == 'win32':
80 # this is py2exe point of view, all executables in root of installdir
81 # all path must be normalized to absolute path
82 libpath = os.path.abspath(os.path.dirname(os.path.realpath(sys.argv_unicode[0])))
83 sys.path.insert(0, libpath)
84 localepath = join(libpath, 'share/locale')
85 extradata = join(libpath, 'share')
87 # we are not installed
90 extradata = u'desktop'
93 assert isinstance(libpath, unicode)
95 try: # just for a nice error message
96 from lib import mypaintlib
99 print "We are not correctly installed or compiled!"
100 print 'script: "%s"' % sys.argv[0]
102 print 'deduced prefix: "%s"' % prefix
103 print 'lib_shared: "%s"' % libpath
104 print 'lib_compiled: "%s"' % libpath_compiled
109 if not os.path.isdir(join(datapath, 'brushes')):
110 print 'Default brush collection not found! It should have been here:'
114 from lib import helpers
115 homepath = helpers.expanduser_unicode(u'~')
116 if sys.platform == 'win32':
117 # using patched win32 glib using correct CSIDL_LOCAL_APPDATA
119 confpath = os.path.join(glib.get_user_config_dir().decode('utf-8'),'mypaint')
120 elif homepath == '~':
121 confpath = join(prefix, 'UserData')
123 confpath = join(homepath, '.mypaint/')
125 assert isinstance(datapath, unicode)
126 assert isinstance(confpath, unicode)
127 assert isinstance(extradata, unicode)
128 return datapath, extradata, confpath, localepath
131 # This helps on slow PCs where the python overhead dominates.
132 # (30% higher framerate measured on 533MHz CPU; startup slowdown below 20%)
133 # Note: python -O -O does not help.
137 if sys.platform == 'win32':
138 if psyco.hexversion >= 0x020000f0 :
140 print 'Psyco being used'
142 print "Need at least psyco 2.0 to run"
145 print 'Psyco being used'
149 if __name__ == '__main__':
152 datapath, extradata, confpath, localepath = get_paths()
154 # must be done before importing any translated python modules
155 # (to get global strings translated, especially brushsettings.py)
157 if sys.platform == 'win32':
159 os.environ['LANG'] = locale.getdefaultlocale()[0]
160 gettext.bindtextdomain("mypaint", localepath)
161 gettext.textdomain("mypaint")
164 main.main(datapath, extradata, confpath)