~ubuntu-branches/ubuntu/trusty/gramps/trusty-proposed

« back to all changes in this revision

Viewing changes to src/gramps.py

  • Committer: Package Import Robot
  • Author(s): Ross Gammon
  • Date: 2014-02-03 17:28:04 UTC
  • mfrom: (39.1.7 sid)
  • Revision ID: package-import@ubuntu.com-20140203172804-76y7nwxiw92zhlnj
Tags: 4.0.3+dfsg-1
* New upstream release (Closes: #720858)
* To-do notes improved and made persistent (Closes: #680692)
* Applied patch to setup.py to fix resource path problem
* Applied patch to disable the optional HTML View & prevent a crash
* Remove sourceless javascript files (Closes: #736436)
* Gramps uses Bat Mitzva internally (Closes: #502532)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#
2
 
# Gramps - a GTK+/GNOME based genealogy program
3
 
#
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
9
 
#
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.
14
 
#
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.
19
 
#
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
23
 
#
24
 
 
25
 
# $Id: gramps.py 23230 2013-09-30 19:25:27Z vassilii $
26
 
 
27
 
#-------------------------------------------------------------------------
28
 
#
29
 
# Python modules
30
 
#
31
 
#-------------------------------------------------------------------------
32
 
import sys
33
 
import os
34
 
import signal
35
 
import gettext
36
 
_ = gettext.gettext
37
 
import locale
38
 
import logging
39
 
 
40
 
LOG = logging.getLogger(".")
41
 
 
42
 
from subprocess import Popen, PIPE
43
 
 
44
 
#-------------------------------------------------------------------------
45
 
#
46
 
# GRAMPS modules
47
 
#
48
 
#-------------------------------------------------------------------------
49
 
import const
50
 
import constfunc
51
 
 
52
 
#-------------------------------------------------------------------------
53
 
#
54
 
# Setup logging
55
 
#
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."""
63
 
 
64
 
# Setup a formatter
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')
68
 
 
69
 
# Create the log handlers
70
 
if constfunc.win():
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):
83
 
            pass # ok
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)
90
 
 
91
 
# Setup the base level logger, this one gets
92
 
# everything.
93
 
l = logging.getLogger()
94
 
l.setLevel(logging.WARNING)
95
 
l.addHandler(stderrh)
96
 
 
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.
101
 
        return
102
 
    if type == IOError:
103
 
        # strange Windows logging error on close
104
 
        return
105
 
    import traceback
106
 
    LOG.error("Unhandled exception\n" +
107
 
              "".join(traceback.format_exception(type, value, tb)))
108
 
 
109
 
sys.excepthook = exc_hook
110
 
 
111
 
from gen.mime import mime_type_is_defined
112
 
import TransUtils
113
 
#-------------------------------------------------------------------------
114
 
#
115
 
# Load internationalization setup
116
 
#
117
 
#-------------------------------------------------------------------------
118
 
 
119
 
#the order in which bindtextdomain on gettext and on locale is called
120
 
#appears important, so we refrain from doing first all gettext.
121
 
#
122
 
#TransUtils.setup_gettext()
123
 
gettext.bindtextdomain(TransUtils.LOCALEDOMAIN, TransUtils.LOCALEDIR)
124
 
try:
125
 
    locale.setlocale(locale.LC_ALL,'')
126
 
except:
127
 
    logging.warning(_("WARNING: Setting locale failed. Please fix the "
128
 
        "LC_* and/or the LANG environment variables to prevent this error"))
129
 
    try:
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')
136
 
    except:
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
140
 
        # raise
141
 
 
142
 
gettext.textdomain(TransUtils.LOCALEDOMAIN)
143
 
gettext.install(TransUtils.LOCALEDOMAIN, localedir=None, unicode=1) #None is sys default locale
144
 
 
145
 
if hasattr(os, "uname"):
146
 
    operating_system = os.uname()[0]
147
 
else:
148
 
    operating_system = sys.platform
149
 
 
150
 
if constfunc.win(): # Windows
151
 
    TransUtils.setup_windows_gettext()
152
 
elif operating_system == 'FreeBSD':
153
 
    try:
154
 
        gettext.bindtextdomain(TransUtils.LOCALEDOMAIN, TransUtils.LOCALEDIR)
155
 
    except locale.Error:
156
 
        logging.warning('No translation in some gtk.Builder strings, ')
157
 
elif operating_system == 'OpenBSD':
158
 
    pass
159
 
else: # normal case
160
 
    try:
161
 
        locale.bindtextdomain(TransUtils.LOCALEDOMAIN, TransUtils.LOCALEDIR)
162
 
        #locale.textdomain(TransUtils.LOCALEDOMAIN)
163
 
    except locale.Error:
164
 
        logging.warning('No translation in some gtk.Builder strings, ')
165
 
 
166
 
#-------------------------------------------------------------------------
167
 
#
168
 
# Minimum version check
169
 
#
170
 
#-------------------------------------------------------------------------
171
 
 
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"
176
 
             " start Gramps.\n\n"
177
 
             "Gramps will terminate now.") % (
178
 
            MIN_PYTHON_VERSION[0], 
179
 
            MIN_PYTHON_VERSION[1],
180
 
            MIN_PYTHON_VERSION[2]))
181
 
    sys.exit(1)
182
 
 
183
 
#-------------------------------------------------------------------------
184
 
#
185
 
# deepcopy workaround
186
 
#
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) :
192
 
    import copy
193
 
    import types
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
197
 
 
198
 
#-------------------------------------------------------------------------
199
 
#
200
 
# gramps libraries
201
 
#
202
 
#-------------------------------------------------------------------------
203
 
try:
204
 
    signal.signal(signal.SIGCHLD, signal.SIG_DFL)
205
 
except:
206
 
    pass
207
 
 
208
 
args = sys.argv
209
 
 
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):
214
 
            pass # ok
215
 
        elif not os.path.isdir(path):
216
 
            os.makedirs(path)
217
 
 
218
 
def show_settings():
219
 
    """
220
 
    Shows settings of all of the major components.
221
 
    """
222
 
    py_str = '%d.%d.%d' % sys.version_info[:3]
223
 
    try:
224
 
        import gtk
225
 
        try:
226
 
            gtkver_str = '%d.%d.%d' % gtk.gtk_version 
227
 
        except : # any failure to 'get' the version
228
 
            gtkver_str = 'unknown version'
229
 
        try:
230
 
            pygtkver_str = '%d.%d.%d' % gtk.pygtk_version
231
 
        except :# any failure to 'get' the version
232
 
            pygtkver_str = 'unknown version'
233
 
    except ImportError:
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)
237
 
    except RuntimeError:
238
 
        gtkver_str = 'DISPLAY not set'
239
 
        pygtkver_str = 'DISPLAY not set'
240
 
    #exept TypeError: To handle back formatting on version split
241
 
 
242
 
    try:
243
 
        import gobject
244
 
        try:
245
 
            gobjectver_str = '%d.%d.%d' % gobject.pygobject_version
246
 
        except :# any failure to 'get' the version
247
 
            gobjectver_str = 'unknown version'
248
 
 
249
 
    except ImportError:
250
 
        gobjectver_str = 'not found'
251
 
 
252
 
    try:
253
 
        import cairo
254
 
        try:
255
 
            cairover_str = '%d.%d.%d' % cairo.version_info 
256
 
        except :# any failure to 'get' the version
257
 
            cairover_str = 'unknown version'
258
 
 
259
 
    except ImportError:
260
 
        cairover_str = 'not found'
261
 
 
262
 
    try:
263
 
        import osmgpsmap
264
 
        try:
265
 
            osmgpsmap_str = osmgpsmap.__version__
266
 
        except :# any failure to 'get' the version
267
 
            osmgpsmap_str = 'unknown version'
268
 
 
269
 
    except ImportError:
270
 
        osmgpsmap_str = 'not found'
271
 
 
272
 
    try:
273
 
        import pyexiv2
274
 
        try:
275
 
            pyexiv2_str = '%d.%d.%d' % pyexiv2.version_info 
276
 
        except :# any failure to 'get' the version
277
 
            pyexiv2_str = 'unknown version'
278
 
 
279
 
    except ImportError:
280
 
        pyexiv2_str = 'not found'
281
 
 
282
 
    import config
283
 
    usebsddb3 = config.get('preferences.use-bsddb3')
284
 
    try:
285
 
        if usebsddb3:
286
 
            import bsddb3 as bsddb
287
 
        else:
288
 
            import bsddb
289
 
        bsddb_str = bsddb.__version__
290
 
        bsddb_db_str = str(bsddb.db.version())
291
 
    except:
292
 
        bsddb_str = 'not found'
293
 
        bsddb_db_str = 'not found'
294
 
 
295
 
    try: 
296
 
        import const
297
 
        gramps_str = const.VERSION
298
 
    except:
299
 
        gramps_str = 'not found'
300
 
 
301
 
    if hasattr(os, "uname"):
302
 
        kernel = os.uname()[2]
303
 
    else:
304
 
        kernel = None
305
 
 
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')
311
 
 
312
 
    try:
313
 
        dotversion_str = Popen(['dot', '-V'], stderr=PIPE).communicate(input=None)[1]
314
 
        if dotversion_str:
315
 
            dotversion_str = dotversion_str.replace('\n','')
316
 
    except:
317
 
        dotversion_str = 'Graphviz not in system PATH'
318
 
 
319
 
    try:
320
 
        if constfunc.win():
321
 
            gsversion_str = Popen(['gswin32c', '--version'], stdout=PIPE).communicate(input=None)[0]
322
 
        else:
323
 
            gsversion_str = Popen(['gs', '--version'], stdout=PIPE).communicate(input=None)[0]
324
 
        if gsversion_str:
325
 
            gsversion_str = gsversion_str.replace('\n', '')
326
 
    except:
327
 
        gsversion_str = 'Ghostscript not in system PATH'
328
 
 
329
 
    os_path = os.environ.get('PATH','not set')
330
 
    os_path = os_path.split(os.pathsep)
331
 
    
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
339
 
    if usebsddb3:
340
 
        print ' Using bsddb3'
341
 
    else:
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
349
 
    if kernel:
350
 
        print ' kernel    : %s' % kernel
351
 
    print
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
359
 
    print ' PYTHONPATH:'
360
 
    for folder in sys.path:
361
 
        print "   ", folder
362
 
    print
363
 
    print "Non-python dependencies:"
364
 
    print "------------------------"
365
 
    print ' Graphviz  : %s' % dotversion_str
366
 
    print ' Ghostscr. : %s' % gsversion_str
367
 
    print
368
 
    print "System PATH env variable:"
369
 
    print "-------------------------"
370
 
    for folder in os_path:
371
 
        print "    ", folder
372
 
    print
373
 
 
374
 
def run():
375
 
    error = []
376
 
    
377
 
    try:
378
 
        build_user_paths()   
379
 
    except OSError, msg:
380
 
        error += [(_("Configuration error:"), str(msg))]
381
 
        return error
382
 
    except msg:
383
 
        LOG.error("Error reading configuration.", exc_info=True)
384
 
        return [(_("Error reading configuration"), str(msg))]
385
 
        
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.")
392
 
                    % const.APP_GRAMPS)]
393
 
    
394
 
    #we start with parsing the arguments to determine if we have a cli or a
395
 
    # gui session
396
 
 
397
 
    if "-v" in sys.argv or "--version" in sys.argv:
398
 
        show_settings()
399
 
        return error
400
 
 
401
 
    from cli.argparser import ArgParser
402
 
    argv_copy = sys.argv[:]
403
 
    argpars = ArgParser(argv_copy)
404
 
 
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))
410
 
    else:
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))
415
 
    else:
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))
420
 
    else:
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))
425
 
    else:
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))
430
 
    else:
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))
435
 
    else:
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'))
440
 
    else:
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'))
445
 
    else:
446
 
        LOG.debug('environment: LANGUAGE is not defined')
447
 
    
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)
453
 
        else:
454
 
            try:
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))]
459
 
                return error
460
 
            startgtkloop(error, argpars)
461
 
    else:
462
 
        #CLI use of GRAMPS
463
 
        argpars.print_help()
464
 
        argpars.print_usage()
465
 
        from cli.grampscli import startcli
466
 
        startcli(error, argpars)
467
 
 
468
 
errors = run()
469
 
if errors and isinstance(errors, list):
470
 
    for error in errors:
471
 
        logging.warning(error[0] + error[1])