Use debian 2.7 only
[fpbd-bostik.git] / pyfpdb / fpdb_prerun.py
blob7a268e3bd0052a2fa511270adc7dd713044349c5
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 # Copyright 2011, Gimick (bbtgaf@googlemail.com)
5 #This program is free software: you can redistribute it and/or modify
6 #it under the terms of the GNU Affero General Public License as published by
7 #the Free Software Foundation, version 3 of the License.
9 #This program is distributed in the hope that it will be useful,
10 #but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 #GNU General Public License for more details.
14 #You should have received a copy of the GNU Affero General Public License
15 #along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #In the "official" distribution you can find the license in agpl-3.0.txt.
18 ########################################################################
20 failure_list = []
21 success_list = []
22 verbose = False
24 global_modules_to_test = ["gobject",
25 "pygtk",
26 "gtk",
27 "pango",
28 "cairo",
29 "matplotlib",
30 "numpy",
31 "pylab",
32 "sqlite3",
33 "pytz"]
35 windows_modules_to_test = ["win32gui",
36 "win32api",
37 "win32con",
38 "win32process",
39 "win32event",
40 "win32console",
41 "winpaths"]
43 linux_modules_to_test = ["wnck"]
44 mac_modules_to_test = []
45 posix_modules_to_test = []
47 def win_output(message):
49 win = Tk()
50 win.title("FPDB")
51 win.geometry("600x400")
52 listbox = Listbox(win)
53 for item in message:
54 listbox.insert(END,item)
55 listbox.pack(fill=BOTH, expand=YES)
56 win.mainloop()
58 def try_import(modulename):
60 try:
61 module = __import__(modulename)
62 success(module)
63 except:
64 failure( _('File not found')+ ": " +modulename)
65 if modulename in ["cairo", "gobject", "pygtk"]:
66 failure(_("Unable to load PyGTK modules required for GUI. Please install PyCairo, PyGObject, and PyGTK from www.pygtk.org."))
67 if modulename in ["win32console"]:
68 failure (_("We appear to be running in Windows, but the Windows Python Extensions are not loading. Please install the PYWIN32 package from http://sourceforge.net/projects/pywin32/"))
69 if modulename in ["pytz"]:
70 failure (_("Unable to import PYTZ library. Please install PYTZ from http://pypi.python.org/pypi/pytz/"))
71 return False
73 if modulename == "pygtk":
74 try:
75 module.require('2.0')
76 success("pygtk 2.0")
77 return True
78 except:
79 failure("pygtk 2.0 " + _('File not found'))
80 return False
82 if modulename == "matplotlib":
83 try:
84 module.use('GTK')
85 success("matplotlib/gtk")
86 return False
87 except:
88 failure("matplotlib/gtk")
89 return False
91 return True
93 def success(message):
94 if verbose:
95 print message
96 success_list.append(message)
98 def failure(message):
99 if verbose:
100 print _("Error:"), message
101 failure_list.append(message)
104 class ChooseLanguage:
106 def __init__(self, win, language_dict):
107 win.title("Choose a language for FPDB")
108 win.geometry("350x350")
109 self.listbox = Listbox(win)
111 self.listbox.insert(END,("Use the system language settings"))
112 self.listbox.insert(END,("en -- Always use English for FPDB"))
113 for key in sorted(language_dict.iterkeys()):
114 self.listbox.insert(END,(key + " -- " + language_dict[key]))
115 self.listbox.pack(fill=BOTH, expand=1)
116 self.listbox.select_set(0)
118 self.listbox.bind('<Double-1>', self.callbackLanguage)
119 win.mainloop()
121 def callbackLanguage(self, event):
122 index = self.listbox.curselection()[0]
123 if index == "0":
124 self.selected_language = ""
125 else:
126 self.selected_language = self.listbox.get(index)
127 win.destroy()
129 def getLanguage(self):
130 import string
131 return string.split(self.selected_language, " -- ", 1)[0]
133 #=====================================================================
136 # check for gross failures first, no translation on the first
137 # two messages because L10n not guaranteed to be available
140 from Tkinter import *
142 try:
143 module = __import__("sys")
144 except:
145 failure("python failure - could not import sys module")
146 win_output(failure_list)
147 sys.exit(1)
149 try:
150 module = __import__("L10n")
151 except:
152 failure("fpdb modules cannot be loaded, check that fpdb is installed in an English path")
153 win_output(failure_list)
154 sys.exit(1)
156 import sys
157 try:
158 if sys.argv[1] == "-v":
159 verbose = True
160 except:
161 pass
163 import L10n
164 _ = L10n.get_translation()
165 import Configuration
166 config = Configuration.Config()
168 if config.python_version not in("2.6", "2.7"):
169 failure(_("Python 2.6-2.7 not found, please install python 2.6 or 2.7 for fpdb."))
172 # next, check for individual modules existing
175 for i in global_modules_to_test:
176 try_import(i)
177 if config.os_family in ("XP", "Win7"):
178 for i in windows_modules_to_test:
179 try_import(i)
180 elif config.os_family == "Linux":
181 for i in linux_modules_to_test:
182 try_import(i)
183 elif config.os_family == "Mac":
184 for i in mac_modules_to_test:
185 try_import(i)
186 if config.posix:
187 for i in posix_modules_to_test:
188 try_import(i)
190 if len(failure_list):
191 win_output(failure_list)
194 # finished validation, work out how to exit
196 if config.install_method == "exe":
197 if len(failure_list):
198 sys.exit(1)
200 if len(failure_list):
201 if config.os_family in ("XP", "Win7"):
202 sys.exit(1)
203 else:
204 sys.exit(failure_list)
207 # If initial run (example_copy==True), prompt for language
209 if config.example_copy:
211 # Ask user for their preferred language, save their choice in the
212 # config
214 language_dict,null=L10n.get_installed_translations()
215 win = Tk()
216 chosen_lang = ChooseLanguage(win, language_dict).getLanguage()
218 if chosen_lang:
219 conf=Configuration.Config()
220 conf.set_general(lang=chosen_lang)
221 conf.save()
223 # signal fpdb.pyw to trigger the config created dialog
224 initial_run = "-i"
225 else:
226 initial_run = ""
228 if config.install_method == "exe":
229 if initial_run:
230 sys.exit(2)
231 else:
232 sys.exit(0)
235 # finally, invoke fpdb
237 import os
238 os.chdir(os.path.join(config.fpdb_program_path, u"pyfpdb"))
240 if config.os_family in ("XP", "Win7"):
241 os.execvpe('pythonw.exe', list(('pythonw.exe', 'fpdb.pyw', initial_run, '-r'))+sys.argv[1:], os.environ)
242 else:
243 os.execvpe('python', list(('python', 'fpdb.pyw', initial_run, '-r'))+sys.argv[1:], os.environ)
244 ###################
245 # DO NOT INSERT ANY LINES BELOW HERE
246 # os.execvpe above transfers control to fpdb.pyw immediately