OSDN Git Service

fix python2.6 unicode exception
[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 get_paths():
16     join = os.path.join
17
18     lib_shared='share/mypaint/'
19     # note: some distros use lib64 instead, they have to edit this...
20     lib_compiled='lib/mypaint/'
21     
22     arg0 = sys.argv[0].decode(sys.getfilesystemencoding())
23     scriptdir=os.path.dirname(arg0)
24
25     # this script is installed as $prefix/bin. We just need $prefix to continue.
26     #pwd=os.getcwd() # why????
27     #dir_install=os.path.normpath(join(pwd,scriptdir)) # why????
28     dir_install=scriptdir # same, except maybe if scriptdir is relative...
29
30     if os.path.basename(dir_install) == 'bin':
31         prefix=os.path.dirname(dir_install)
32         assert isinstance(prefix, unicode)
33         libpath=join(prefix, lib_shared)
34         libpath_compiled = join(prefix, lib_compiled)
35         sys.path.insert(0, libpath)
36         sys.path.insert(0, libpath_compiled)
37         localepath = join(prefix, 'share/locale')
38     elif sys.platform == 'win32':
39         prefix=None
40         # this is py2exe point of view, all executables in root of installdir
41         # all path must be normalized to absolute path
42         libpath = os.path.abspath(os.path.dirname(os.path.realpath(arg0)))
43         sys.path.insert(0, libpath)
44         localepath = join(libpath,'share/locale')
45     else:
46         # we are not installed
47         prefix = None
48         libpath = u'.'
49         localepath = 'po'
50
51     assert isinstance(libpath, unicode)
52
53     try: # just for a nice error message
54         from lib import mypaintlib
55     except ImportError:
56         print
57         print "We are not correctly installed or compiled!"
58         print 'script: "%s"' % arg0
59         if prefix:
60             print 'deduced prefix: "%s"' % prefix
61             print 'lib_shared: "%s"' % libpath
62             print 'lib_compiled: "%s"' % libpath_compiled
63         print
64         raise
65
66     datapath = libpath
67     if not os.path.isdir(join(datapath, 'brushes')):
68         print 'Default brush collection not found! It should have been here:'
69         print datapath
70         raise sys.exit(1)
71
72     homepath =  os.path.expanduser(u'~')
73     if homepath == '~':
74         confpath = join(prefix, 'UserData')
75     else:
76         confpath = join(homepath, '.mypaint/')
77
78     assert isinstance(datapath, unicode)
79     assert isinstance(confpath, unicode)
80     return datapath, confpath, localepath
81
82 def psyco_opt():
83     # This helps on slow PCs where the python overhead dominates.
84     # (30% higher framerate measured on 533MHz CPU; startup slowdown below 20%)
85     # Note: python -O -O does not help.
86     if os.name in ('nt', 'ce'):
87         # reported to be broken on Windows
88         return
89     try:
90         import psyco
91         if sys.platform == 'win32':
92             if psyco.hexversion >= 0x020000f0 :
93                 psyco.full()
94                 print 'Psyco being used'
95             else:
96                 print "Need at least psyco 2.0 to run"
97         else:
98             psyco.full()
99             print 'Psyco being used'
100     except ImportError:
101         pass
102
103 if __name__ == '__main__':
104     psyco_opt()
105
106     datapath, confpath, localepath = get_paths()
107
108     # must be done before importing any translated python modules
109     # (to get global strings translated, especially brushsettings.py)
110     import gettext
111     if sys.platform == 'win32':
112         import locale
113         os.environ['LANG'] = locale.getdefaultlocale()[0]
114     gettext.bindtextdomain("mypaint", localepath)
115     gettext.textdomain("mypaint")
116
117     from gui import main
118     main.main(datapath, confpath)