import pysvn

from bazbase import structure, db
from .precommit import apply_changes, make_svnlook_cat, make_svnlook_tree

def regen_database():
    from . import custom as bazsvn_custom
    with db.begin_transaction():
        structure.clear_database()

    tree = make_svnlook_tree(bazsvn_custom.REPOSITORY)

    #all = tree('')
    #changed = dict((f, ('A',
    #                    pysvn.node_kind.dir if f.endswith('/')
    #                    else pysvn.node_kind.file,
    #                    True, True)) for f in all)
    add_dir = ('A', pysvn.node_kind.dir, True, True)
    changed = {}
    for d in tree('/', non_recursive=True):
        assert d.startswith('/')
        if d.endswith('/'):
            changed[d[1:-1]] = add_dir
    assert u'props' in changed
    assert u'Object' in changed

    apply_changes(changed, 'regen_database',
                  cat=make_svnlook_cat(bazsvn_custom.REPOSITORY),
                  tree=tree)
