OSDN Git Service

save_png: don't append an additional info block
[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         extradata = join(prefix, 'share')
78     elif sys.platform == 'win32':
79         prefix=None
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')
86     else:
87         # we are not installed
88         prefix = None
89         libpath = u'.'
90         extradata = u'desktop'
91         localepath = 'po'
92
93     assert isinstance(libpath, unicode)
94
95     try: # just for a nice error message
96         from lib import mypaintlib
97     except ImportError:
98         print
99         print "We are not correctly installed or compiled!"
100         print 'script: "%s"' % sys.argv[0]
101         if prefix:
102             print 'deduced prefix: "%s"' % prefix
103             print 'lib_shared: "%s"' % libpath
104             print 'lib_compiled: "%s"' % libpath_compiled
105         print
106         raise
107
108     datapath = libpath
109     if not os.path.isdir(join(datapath, 'brushes')):
110         print 'Default brush collection not found! It should have been here:'
111         print datapath
112         raise sys.exit(1)
113
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
118         import glib
119         confpath = os.path.join(glib.get_user_config_dir().decode('utf-8'),'mypaint')
120     elif homepath == '~':
121         confpath = join(prefix, 'UserData')
122     else:
123         confpath = join(homepath, '.mypaint/')
124
125     assert isinstance(datapath, unicode)
126     assert isinstance(confpath, unicode)
127     assert isinstance(extradata, unicode)
128     return datapath, extradata, confpath, localepath
129
130 def psyco_opt():
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.
134
135     try:
136         import psyco
137         if sys.platform == 'win32':
138             if psyco.hexversion >= 0x020000f0 :
139                 psyco.full()
140                 print 'Psyco being used'
141             else:
142                 print "Need at least psyco 2.0 to run"
143         else:
144             psyco.full()
145             print 'Psyco being used'
146     except ImportError:
147         pass
148
149 if __name__ == '__main__':
150     psyco_opt()
151
152     datapath, extradata, confpath, localepath = get_paths()
153
154     # must be done before importing any translated python modules
155     # (to get global strings translated, especially brushsettings.py)
156     import gettext
157     if sys.platform == 'win32':
158         import locale
159         os.environ['LANG'] = locale.getdefaultlocale()[0]
160     gettext.bindtextdomain("mypaint", localepath)
161     gettext.textdomain("mypaint")
162
163     from gui import main
164     main.main(datapath, extradata, confpath)