root/livinglogic.python.orasql/src/ll/orasql/scripts/oracreate.py @ 182:0d4d435866b6

Revision 182:0d4d435866b6, 3.4 KB (checked in by Walter Doerwald <walter@…>, 13 years ago)

Add class Privilege that models an object grant.

Add Connection.iterprivileges() for getting all privileges.

Add a script oragrant.py for copying privileges.

Update copyright year.

Line 
1#!/usr/bin/env python
2# -*- coding: iso-8859-1 -*-
3
4## Copyright 2005-2007 by LivingLogic AG, Bayreuth/Germany.
5## Copyright 2005-2007 by Walter Dörwald
6##
7## All Rights Reserved
8##
9## See orasql/__init__.py for the license
10
11
12import sys, os, optparse
13
14from ll import astyle, orasql
15
16
17s4warning = astyle.Style.fromenv("LL_ORASQL_REPRANSI_WARNING", "red:black")
18s4error = astyle.Style.fromenv("LL_ORASQL_REPRANSI_ERROR", "red:black")
19s4connectstring = astyle.Style.fromenv("LL_ORASQL_REPRANSI_CONNECTSTRING", "yellow:black")
20s4object = astyle.Style.fromenv("LL_ORASQL_REPRANSI_OBJECT", "green:black")
21
22
23def main(args=None):
24    colors = ("yes", "no", "auto")
25    p = optparse.OptionParser(usage="usage: %prog [options] connectstring >output.sql")
26    p.add_option("-v", "--verbose", dest="verbose", help="Give a progress report?", default=False, action="store_true")
27    p.add_option("-c", "--color", dest="color", help="Color output (%s)" % ", ".join(colors), default="auto", choices=colors)
28    p.add_option("-s", "--seqcopy", dest="seqcopy", help="copy sequence values?", default=False, action="store_true")
29    p.add_option("-x", "--execute", metavar="CONNECTSTRING2", dest="execute", help="Execute in target database", type="str")
30    p.add_option("-k", "--keepjunk", dest="keepjunk", help="Output objects with '$' or 'SYS_EXPORT_SCHEMA_' in their name?", default=False, action="store_true")
31    p.add_option("-i", "--ignore", dest="ignore", help="Ignore errors?", default=False, action="store_true")
32
33    (options, args) = p.parse_args(args)
34    if len(args) != 1:
35        p.error("incorrect number of arguments")
36        return 1
37
38    if options.color == "yes":
39        color = True
40    elif options.color == "no":
41        color = False
42    else:
43        color = None
44    stdout = astyle.Stream(sys.stdout, color)
45    stderr = astyle.Stream(sys.stderr, color)
46
47    connection = orasql.connect(args[0])
48
49    if options.execute:
50        connection2 = orasql.connect(options.execute)
51        cursor2 = connection2.cursor()
52        term = False
53    else:
54        term = True
55
56    cs1 = s4connectstring(connection.connectstring())
57    if options.execute:
58        cs2 = s4connectstring(connection2.connectstring())
59
60    def keep(obj):
61        if obj.owner is not None:
62            return False
63        if options.keepjunk:
64            return True
65        # output pk, fks etc. only when they belong to table we do output
66        if isinstance(obj, (orasql.Constraint, orasql.Index)):
67            obj = obj.table()
68        if "$" in obj.name or obj.name.startswith("SYS_EXPORT_SCHEMA_"):
69            return False
70        return True
71
72    for (i, obj) in enumerate(connection.iterobjects(mode="create", schema="user")):
73        keepobj = keep(obj)
74        if options.verbose:
75            if options.execute:
76                msg = astyle.style_default("oracreate.py: ", cs1, " -> ", cs2, ": fetching/creating #%d" % (i+1))
77            else:
78                msg = astyle.style_default("oracreate.py: ", cs1, " fetching #%d" % (i+1))
79            msg = astyle.style_default(msg, " ", s4object(str(obj)))
80            if not keepobj:
81                msg = astyle.style_default(msg, " ", s4warning("(skipped)"))
82            stderr.writeln(msg)
83
84        if keepobj:
85            if isinstance(obj, orasql.Sequence) and options.seqcopy:
86                ddl = obj.createddlcopy(connection, term)
87            else:
88                ddl = obj.createddl(connection, term)
89            if ddl:
90                if options.execute:
91                    try:
92                        cursor2.execute(ddl)
93                    except orasql.DatabaseError, exc:
94                        if not options.ignore or "ORA-01013" in str(exc):
95                            raise
96                        stderr.writeln("oracreate.py: ", s4error("%s: %s" % (exc.__class__.__name__, str(exc).strip())))
97                else:
98                    stdout.writeln(ddl)
99                    stdout.writeln()
100
101
102if __name__ == "__main__":
103    sys.exit(main())
Note: See TracBrowser for help on using the browser.