2
# Gramps - a GTK+/GNOME based genealogy program
4
# Copyright (C) 2000-2006 Donald N. Allingham
5
# Copyright (C) 2009 Benny Malengier
6
# Copyright (C) 2009-2010 Stephen George
7
# Copyright (C) 2010 Doug Blank <doug.blank@gmail.com>
8
# Copyright (C) 2011 Paul Franklin
10
# This program is free software; you can redistribute it and/or modify
11
# it under the terms of the GNU General Public License as published by
12
# the Free Software Foundation; either version 2 of the License, or
13
# (at your option) any later version.
15
# This program is distributed in the hope that it will be useful,
16
# but WITHOUT ANY WARRANTY; without even the implied warranty of
17
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
# GNU General Public License for more details.
20
# You should have received a copy of the GNU General Public License
21
# along with this program; if not, write to the Free Software
22
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
# $Id: gramps.py 23230 2013-09-30 19:25:27Z vassilii $
27
#-------------------------------------------------------------------------
31
#-------------------------------------------------------------------------
40
LOG = logging.getLogger(".")
42
from subprocess import Popen, PIPE
44
#-------------------------------------------------------------------------
48
#-------------------------------------------------------------------------
52
#-------------------------------------------------------------------------
56
# Ideally, this needs to be done before any Gramps modules are imported, so that
57
# any code that is executed as the modules are imported can log errors or
58
# warnings. Errors and warnings are particularly applicable to TransUtils and
59
# setting up internationalisation in this module. const and constfunc have to be
60
# imported before this code is executed because they are used in this code.
61
#-------------------------------------------------------------------------
62
"""Setup basic logging support."""
65
form = logging.Formatter(fmt="%(asctime)s.%(msecs).03d: %(levelname)s: "
66
"%(filename)s: line %(lineno)d: %(message)s",
67
datefmt='%Y-%m-%d %H:%M:%S')
69
# Create the log handlers
71
# If running in GUI mode redirect stdout and stderr to log file
72
if hasattr(sys.stdout, "fileno") and sys.stdout.fileno() < 0:
73
logfile = os.path.join(const.HOME_DIR,
74
"Gramps%s%s.log") % (const.VERSION_TUPLE[0],
75
const.VERSION_TUPLE[1])
76
# We now carry out the first step in build_user_paths(), to make sure
77
# that the user home directory is available to store the log file. When
78
# build_user_paths() is called, the call is protected by a try...except
79
# block, and any failure will be logged. However, if the creation of the
80
# user directory fails here, there is no way to report the failure,
81
# because stdout/stderr are not available, and neither is the logfile.
82
if os.path.islink(const.HOME_DIR):
84
elif not os.path.isdir(const.HOME_DIR):
85
os.makedirs(const.HOME_DIR)
86
sys.stdout = sys.stderr = open(logfile, "w")
87
stderrh = logging.StreamHandler(sys.stderr)
88
stderrh.setFormatter(form)
89
stderrh.setLevel(logging.DEBUG)
91
# Setup the base level logger, this one gets
93
l = logging.getLogger()
94
l.setLevel(logging.WARNING)
97
# put a hook on to catch any completely unhandled exceptions.
98
def exc_hook(type, value, tb):
99
if type == KeyboardInterrupt:
100
# Ctrl-C is not a bug.
103
# strange Windows logging error on close
106
LOG.error("Unhandled exception\n" +
107
"".join(traceback.format_exception(type, value, tb)))
109
sys.excepthook = exc_hook
111
from gen.mime import mime_type_is_defined
113
#-------------------------------------------------------------------------
115
# Load internationalization setup
117
#-------------------------------------------------------------------------
119
#the order in which bindtextdomain on gettext and on locale is called
120
#appears important, so we refrain from doing first all gettext.
122
#TransUtils.setup_gettext()
123
gettext.bindtextdomain(TransUtils.LOCALEDOMAIN, TransUtils.LOCALEDIR)
125
locale.setlocale(locale.LC_ALL,'')
127
logging.warning(_("WARNING: Setting locale failed. Please fix the "
128
"LC_* and/or the LANG environment variables to prevent this error"))
130
# It is probably not necessary to set the locale to 'C'
131
# because the locale will just stay at whatever it was,
132
# which at startup is "C".
133
# however this is done here just to make sure that the locale
134
# functions are working
135
locale.setlocale(locale.LC_ALL,'C')
137
logging.warning(_("ERROR: Setting the 'C' locale didn't work either"))
138
# FIXME: This should propagate the exception,
139
# if that doesn't break Gramps under Windows
142
gettext.textdomain(TransUtils.LOCALEDOMAIN)
143
gettext.install(TransUtils.LOCALEDOMAIN, localedir=None, unicode=1) #None is sys default locale
145
if hasattr(os, "uname"):
146
operating_system = os.uname()[0]
148
operating_system = sys.platform
150
if constfunc.win(): # Windows
151
TransUtils.setup_windows_gettext()
152
elif operating_system == 'FreeBSD':
154
gettext.bindtextdomain(TransUtils.LOCALEDOMAIN, TransUtils.LOCALEDIR)
156
logging.warning('No translation in some gtk.Builder strings, ')
157
elif operating_system == 'OpenBSD':
161
locale.bindtextdomain(TransUtils.LOCALEDOMAIN, TransUtils.LOCALEDIR)
162
#locale.textdomain(TransUtils.LOCALEDOMAIN)
164
logging.warning('No translation in some gtk.Builder strings, ')
166
#-------------------------------------------------------------------------
168
# Minimum version check
170
#-------------------------------------------------------------------------
172
MIN_PYTHON_VERSION = (2, 6, 0, '', 0)
173
if not sys.version_info >= MIN_PYTHON_VERSION :
174
logging.warning(_("Your Python version does not meet the "
175
"requirements. At least python %d.%d.%d is needed to"
177
"Gramps will terminate now.") % (
178
MIN_PYTHON_VERSION[0],
179
MIN_PYTHON_VERSION[1],
180
MIN_PYTHON_VERSION[2]))
183
#-------------------------------------------------------------------------
185
# deepcopy workaround
187
#-------------------------------------------------------------------------
188
# In versions < 2.7 python does not properly copy methods when doing a
189
# deepcopy. This workaround makes the copy work properly. When Gramps no longer
190
# supports python 2.6, this workaround can be removed.
191
if sys.version_info < (2, 7) :
194
def _deepcopy_method(x, memo):
195
return type(x)(x.im_func, copy.deepcopy(x.im_self, memo), x.im_class)
196
copy._deepcopy_dispatch[types.MethodType] = _deepcopy_method
198
#-------------------------------------------------------------------------
202
#-------------------------------------------------------------------------
204
signal.signal(signal.SIGCHLD, signal.SIG_DFL)
210
def build_user_paths():
211
""" check/make user-dirs on each Gramps session"""
212
for path in const.USER_DIRLIST:
213
if os.path.islink(path):
215
elif not os.path.isdir(path):
220
Shows settings of all of the major components.
222
py_str = '%d.%d.%d' % sys.version_info[:3]
226
gtkver_str = '%d.%d.%d' % gtk.gtk_version
227
except : # any failure to 'get' the version
228
gtkver_str = 'unknown version'
230
pygtkver_str = '%d.%d.%d' % gtk.pygtk_version
231
except :# any failure to 'get' the version
232
pygtkver_str = 'unknown version'
234
gtkver_str = 'not found'
235
pygtkver_str = 'not found'
236
# no DISPLAY is a RuntimeError in an older pygtk (e.g. 2.17 in Fedora 14)
238
gtkver_str = 'DISPLAY not set'
239
pygtkver_str = 'DISPLAY not set'
240
#exept TypeError: To handle back formatting on version split
245
gobjectver_str = '%d.%d.%d' % gobject.pygobject_version
246
except :# any failure to 'get' the version
247
gobjectver_str = 'unknown version'
250
gobjectver_str = 'not found'
255
cairover_str = '%d.%d.%d' % cairo.version_info
256
except :# any failure to 'get' the version
257
cairover_str = 'unknown version'
260
cairover_str = 'not found'
265
osmgpsmap_str = osmgpsmap.__version__
266
except :# any failure to 'get' the version
267
osmgpsmap_str = 'unknown version'
270
osmgpsmap_str = 'not found'
275
pyexiv2_str = '%d.%d.%d' % pyexiv2.version_info
276
except :# any failure to 'get' the version
277
pyexiv2_str = 'unknown version'
280
pyexiv2_str = 'not found'
283
usebsddb3 = config.get('preferences.use-bsddb3')
286
import bsddb3 as bsddb
289
bsddb_str = bsddb.__version__
290
bsddb_db_str = str(bsddb.db.version())
292
bsddb_str = 'not found'
293
bsddb_db_str = 'not found'
297
gramps_str = const.VERSION
299
gramps_str = 'not found'
301
if hasattr(os, "uname"):
302
kernel = os.uname()[2]
306
lang_str = os.environ.get('LANG','not set')
307
language_str = os.environ.get('LANGUAGE','not set')
308
grampsi18n_str = os.environ.get('GRAMPSI18N','not set')
309
grampshome_str = os.environ.get('GRAMPSHOME','not set')
310
grampsdir_str = os.environ.get('GRAMPSDIR','not set')
313
dotversion_str = Popen(['dot', '-V'], stderr=PIPE).communicate(input=None)[1]
315
dotversion_str = dotversion_str.replace('\n','')
317
dotversion_str = 'Graphviz not in system PATH'
321
gsversion_str = Popen(['gswin32c', '--version'], stdout=PIPE).communicate(input=None)[0]
323
gsversion_str = Popen(['gs', '--version'], stdout=PIPE).communicate(input=None)[0]
325
gsversion_str = gsversion_str.replace('\n', '')
327
gsversion_str = 'Ghostscript not in system PATH'
329
os_path = os.environ.get('PATH','not set')
330
os_path = os_path.split(os.pathsep)
332
print "Gramps Settings:"
333
print "----------------"
334
print ' python : %s' % py_str
335
print ' gramps : %s' % gramps_str
336
print ' gtk++ : %s' % gtkver_str
337
print ' pygtk : %s' % pygtkver_str
338
print ' gobject : %s' % gobjectver_str
340
print ' Using bsddb3'
342
print ' Not using bsddb3'
343
print ' bsddb : %s' % bsddb_str
344
print ' bsddb.db : %s' % bsddb_db_str
345
print ' cairo : %s' % cairover_str
346
print ' osmgpsmap : %s' % osmgpsmap_str
347
print ' pyexiv2 : %s' % pyexiv2_str
348
print ' o.s. : %s' % operating_system
350
print ' kernel : %s' % kernel
352
print "Environment settings:"
353
print "---------------------"
354
print ' LANG : %s' % lang_str
355
print ' LANGUAGE : %s' % language_str
356
print ' GRAMPSI18N: %s' % grampsi18n_str
357
print ' GRAMPSHOME: %s' % grampshome_str
358
print ' GRAMPSDIR : %s' % grampsdir_str
360
for folder in sys.path:
363
print "Non-python dependencies:"
364
print "------------------------"
365
print ' Graphviz : %s' % dotversion_str
366
print ' Ghostscr. : %s' % gsversion_str
368
print "System PATH env variable:"
369
print "-------------------------"
370
for folder in os_path:
380
error += [(_("Configuration error:"), str(msg))]
383
LOG.error("Error reading configuration.", exc_info=True)
384
return [(_("Error reading configuration"), str(msg))]
386
if not mime_type_is_defined(const.APP_GRAMPS):
387
error += [(_("Configuration error:"),
388
_("A definition for the MIME-type %s could not "
389
"be found \n\n Possibly the installation of Gramps "
390
"was incomplete. Make sure the MIME-types "
391
"of Gramps are properly installed.")
394
#we start with parsing the arguments to determine if we have a cli or a
397
if "-v" in sys.argv or "--version" in sys.argv:
401
from cli.argparser import ArgParser
402
argv_copy = sys.argv[:]
403
argpars = ArgParser(argv_copy)
405
# Calls to LOG must be after setup_logging() and ArgParser()
406
LOG = logging.getLogger(".locale")
407
if hasattr(locale, 'LC_CTYPE'):
408
LOG.debug('Using locale: LC_CTYPE %s %s' %
409
locale.getlocale(locale.LC_CTYPE))
411
LOG.debug('locale: LC_CTYPE is not defined')
412
if hasattr(locale, 'LC_COLLATE'):
413
LOG.debug('Using locale: LC_COLLATE %s %s' %
414
locale.getlocale(locale.LC_COLLATE))
416
LOG.debug('locale: LC_COLLATE is not defined')
417
if hasattr(locale, 'LC_TIME'):
418
LOG.debug('Using locale: LC_TIME %s %s' %
419
locale.getlocale(locale.LC_TIME))
421
LOG.debug('locale: LC_TIME is not defined')
422
if hasattr(locale, 'LC_MONETARY'):
423
LOG.debug('Using locale: LC_MONETARY %s %s' %
424
locale.getlocale(locale.LC_MONETARY))
426
LOG.debug('locale: LC_MONETARY is not defined')
427
if hasattr(locale, 'LC_MESSAGES'):
428
LOG.debug('Using locale: LC_MESSAGES %s %s' %
429
locale.getlocale(locale.LC_MESSAGES))
431
LOG.debug('locale: LC_MESSAGES is not defined')
432
if hasattr(locale, 'LC_NUMERIC'):
433
LOG.debug('Using locale: LC_NUMERIC %s %s' %
434
locale.getlocale(locale.LC_NUMERIC))
436
LOG.debug('locale: LC_NUMERIC is not defined')
437
if 'LANG' in os.environ:
438
LOG.debug('Using LANG: %s' %
439
os.environ.get('LANG'))
441
LOG.debug('environment: LANG is not defined')
442
if 'LANGUAGE' in os.environ:
443
LOG.debug('Using LANGUAGE: %s' %
444
os.environ.get('LANGUAGE'))
446
LOG.debug('environment: LANGUAGE is not defined')
448
if argpars.need_gui():
449
#A GUI is needed, set it up
450
if "--qml" in sys.argv:
451
from guiQML.grampsqml import startqml
452
startqml(error, argpars)
455
from gui.grampsgui import startgtkloop
456
# no DISPLAY is a RuntimeError in an older pygtk (e.g. F14's 2.17)
457
except RuntimeError, msg:
458
error += [(_("Configuration error:"), str(msg))]
460
startgtkloop(error, argpars)
464
argpars.print_usage()
465
from cli.grampscli import startcli
466
startcli(error, argpars)
469
if errors and isinstance(errors, list):
471
logging.warning(error[0] + error[1])