OSDN Git Service

windows: code cleanup
[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     from lib import helpers
73     homepath =  helpers.expanduser_unicode(u'~')
74     if homepath == '~':
75         confpath = join(prefix, 'UserData')
76     else:                                            
77         confpath = join(homepath, '.mypaint/')
78     #Workaround before glib.get_user_config_dir() fixed in upstream
79     if sys.platform == 'win32':                                  
80         import _winreg
81         try:
82             HKCU = _winreg.ConnectRegistry(None, _winreg.HKEY_CURRENT_USER)
83             ShellKey = _winreg.OpenKey(HKCU, r"Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders")
84             localappdatapath = _winreg.QueryValueEx(ShellKey, "Local AppData")[0]
85             confpath = join(localappdatapath, 'mypaint/')
86         except WindowsError:
87             print "Can't retrive Local Application Data Path from registry"
88
89     assert isinstance(datapath, unicode)
90     assert isinstance(confpath, unicode)
91     return datapath, confpath, localepath
92
93 def psyco_opt():
94     # This helps on slow PCs where the python overhead dominates.
95     # (30% higher framerate measured on 533MHz CPU; startup slowdown below 20%)
96     # Note: python -O -O does not help.
97     if os.name in ('nt', 'ce'):
98         # reported to be broken on Windows
99         return
100     try:
101         import psyco
102         if sys.platform == 'win32':
103             if psyco.hexversion >= 0x020000f0 :
104                 psyco.full()
105                 print 'Psyco being used'
106             else:
107                 print "Need at least psyco 2.0 to run"
108         else:
109             psyco.full()
110             print 'Psyco being used'
111     except ImportError:
112         pass
113
114 if __name__ == '__main__':
115     psyco_opt()
116
117     datapath, confpath, localepath = get_paths()
118
119     # must be done before importing any translated python modules
120     # (to get global strings translated, especially brushsettings.py)
121     import gettext
122     if sys.platform == 'win32':
123         import locale
124         os.environ['LANG'] = locale.getdefaultlocale()[0]
125     gettext.bindtextdomain("mypaint", localepath)
126     gettext.textdomain("mypaint")
127
128     from gui import main
129     main.main(datapath, confpath)