~georgeyk/stoq/cezar-all

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env python
# -*- Mode: Python; coding: iso-8859-1 -*-
# vi:si:et:sw=4:sts=4:ts=4

##
## Copyright (C) 2005-2007 Async Open Source <http://www.async.com.br>
## All rights reserved
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., or visit: http://www.gnu.org/.
##
## Author(s):   Evandro Vale Miquelito      <evandro@async.com.br>
##              Johan Dahlin                <jdahlin@async.com.br>
##
""" A debug session for domain objects.  """

import code
import datetime
import socket
import sys

try:
    import pkg_resources
except ImportError:
    pass

from kiwi.component import provide_utility, get_utility
from stoqlib.database.admin import get_admin_user
from stoqlib.database.interfaces import ICurrentUser, IDatabaseSettings
from stoqlib.database.runtime import (get_connection, new_transaction,
                                      get_current_branch, get_current_station,
                                      set_current_branch_station)
from stoqlib.database.tables import get_table_types
from stoqlib.lib.parameters import sysparam

from stoq.lib.configparser import StoqConfig
from stoq.lib.options import get_option_parser
from stoq.lib.startup import setup
from stoq import version as stoq_version

def _interactive(ns):
    import readline
    import rlcompleter
    rlcompleter # pyflakes
    readline.parse_and_bind("tab: complete")

    settings = get_utility(IDatabaseSettings)
    db_string = '%s on %s:%s (%s)' % (settings.dbname,
                                      settings.address,
                                      settings.port,
                                      settings.rdbms)
    banner = 'Stoq version %s, connected to %s' % (
        stoq_version, db_string)
    code.interact(local=ns, banner=banner)

def main(args):
    parser = get_option_parser()
    parser.add_option('-b', '--bare',
                      action="store_true",
                      dest="bare")
    parser.add_option('-c', '--commit',
                      action="store_true",
                      dest="commit")
    parser.add_option('-s', '--script',
                      action="store",
                      type="string",
                      dest="script")
    options, args = parser.parse_args(args)

    setup(options=options, register_station=(not options.bare),
          check_schema=False)

    # Namespace
    ns = locals().copy()
    for table in get_table_types():
        ns[table.__name__] = table

    conn = get_connection()

    ns['conn'] = conn
    ns['trans'] = trans = new_transaction()
    ns['sysparam'] = sysparam

    if not options.bare:
        ns['branch'] = get_current_branch(conn)
        ns['station'] = station = get_current_station(conn)
        ns['now'] = datetime.datetime.now
        ns['today'] = datetime.date.today

        for name in ('sqlobject', 'sqlobject.sqlbuilder',
                     'stoqlib.database.runtime',
                     'stoqlib.lib.interfaces',
                     'stoqlib.domain.interfaces'):
            mod = __import__(name, {}, {}, ' ')
            ns.update(mod.__dict__)

    if options.sqldebug:
        conn.debug = True

    if options.script:
        execfile(options.script, ns)
    elif len(args) > 1:
        stmt = args[1]
        exec stmt in ns
    else:
        _interactive(ns)

    if options.commit:
        trans.commit()

if __name__ == '__main__':
    sys.exit(main(sys.argv))