File: ldaptor-ldifpatch

package info (click to toggle)
ldaptor 0.0.43%2Bdebian1-7
  • links: PTS, VCS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch, wheezy
  • size: 2,180 kB
  • ctags: 3,626
  • sloc: python: 18,648; xml: 787; makefile: 149; sh: 88
file content (56 lines) | stat: -rwxr-xr-x 1,329 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/python

import sys
from ldaptor.protocols.ldap import ldif, ldifdelta
from ldaptor import usage, inmemory
from twisted.internet import protocol, reactor, defer


exitStatus=0

def error(fail):
    print >>sys.stderr, 'fail:', fail.getErrorMessage()
    global exitStatus
    exitStatus=1

def output(tree, outputFile):
    outputFile.write(ldif.header())
    def _write(node):
        outputFile.write(str(node))
    tree.subtree(callback=_write)

def main(dataFile, patchFile, outputFile):
    d = inmemory.fromLDIFFile(dataFile)

    def _gotDB(db, patchFile):
        patches = ldifdelta.fromLDIFFile(patchFile)

        # find the right entry to patch
        for p in patches:
            p.patch(db)
        return db
    d.addCallback(_gotDB, patchFile)

    d.addCallback(output, outputFile)
    d.addErrback(error)
    d.addBoth(lambda x: reactor.stop())

    reactor.run()
    sys.exit(exitStatus)

class MyOptions(usage.Options):
    """LDAPtor LDIF patching utility"""

    def parseArgs(self, data):
        self['data'] = data

if __name__ == "__main__":
    try:
        config = MyOptions()
        config.parseOptions()
    except usage.UsageError, ue:
        sys.stderr.write('%s: %s\n' % (sys.argv[0], ue))
        sys.exit(1)

    data = open(config['data'])
    main(data, sys.stdin, sys.stdout)