[8417] | 1 | """Helpers for maintainers of kofa sites. |
---|
| 2 | |
---|
| 3 | XXX: This stuff might go into a separate package, but right |
---|
| 4 | now it is too less for a complete package. |
---|
| 5 | """ |
---|
| 6 | import sys |
---|
| 7 | from ZODB.scripts.analyze import report, analyze |
---|
[8718] | 8 | from zope.component.hooks import setSite |
---|
| 9 | from zope.component import getUtility |
---|
| 10 | from zope.catalog.interfaces import ICatalog |
---|
| 11 | from zope.intid.interfaces import IIntIds |
---|
[8417] | 12 | |
---|
| 13 | def db_analyze(args=None): |
---|
| 14 | """Run the analyze tool from ZODB package. |
---|
| 15 | """ |
---|
| 16 | if args is None: |
---|
| 17 | args = sys.argv[1:] |
---|
| 18 | |
---|
| 19 | path = None |
---|
| 20 | if len(args) > 0: |
---|
| 21 | path = args[0] |
---|
| 22 | else: |
---|
| 23 | print |
---|
| 24 | print "Analyze a ZODB file and print statistics" |
---|
| 25 | print "about contained objects, sizes, etc." |
---|
| 26 | print |
---|
| 27 | print "Usage: %s <path-to-Data.fs>" % sys.argv[0] |
---|
| 28 | print |
---|
| 29 | sys.exit(0) |
---|
| 30 | |
---|
| 31 | report(analyze(path)) |
---|
[8718] | 32 | |
---|
| 33 | def update_catalog(site, cat_name, objects=[], func=None): |
---|
| 34 | """Update a catalog. |
---|
| 35 | |
---|
| 36 | Put `objects` or objects delivered by `func()` into the catalog |
---|
| 37 | registered under `cat_name` in `site`. |
---|
| 38 | |
---|
| 39 | Objects to be catalogued must be 'located', i.e. they must have a |
---|
| 40 | __name__ and __parent__ (because they are adapted to |
---|
| 41 | IKeyReference). |
---|
| 42 | |
---|
| 43 | You can pass in objects as some iterable or as a function that is |
---|
| 44 | called to deliver the set of objects to be catalogued. |
---|
| 45 | |
---|
| 46 | A function takes precedence over object lists. |
---|
| 47 | """ |
---|
| 48 | setSite(site) |
---|
| 49 | cat = getUtility(ICatalog, name=cat_name) |
---|
| 50 | intids = getUtility(IIntIds, context=cat) |
---|
| 51 | if func is not None: |
---|
| 52 | objects = func() |
---|
| 53 | for ob in objects: |
---|
| 54 | doc_id = intids.queryId(ob, None) |
---|
| 55 | if doc_id is None: |
---|
| 56 | doc_id = intids.register(ob) |
---|
| 57 | cat.index_doc(doc_id, ob) |
---|
| 58 | return cat |
---|