summaryrefslogtreecommitdiff
path: root/admin/regen.py
blob: a461e3eae7734780ab72684c1ab4ece9ba8bf6e2 (plain)
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
"""
helper script which rebuilds all autogenerated bits of code w/in passlib.
"""
#=============================================================================
# imports
#=============================================================================
# core
import datetime
import re
import os
# all
__all__ = [
    "main",
    "replace_section",
]

#=============================================================================
# helpers
#=============================================================================

now = datetime.datetime.now().strftime("%Y-%m-%d")

def replace_section(data, ident, content):
    """
    helper to replace autogenerated section of content.

    :param data:
        existing file contents

    :param ident:
        identifying string for section start and end.
        will look for "begin <ident>" section marker,
        and "end <ident>" section marker.

    :param content:
        replacement content to replace whatever's present
        between section start & end.

    :returns:
        modified data
    """
    m = re.match(r"""(?ixms)
        (?P<head>.*?\n)
        \#[-=]{10,}\n
        \# \s begin \s* %(ident)s .* \n
        \#[-=]{10,}\n
        .*?
        \#[-=]{10,}\n
        \# \s end \s* %(ident)s \s* \n
        \#[-=]{10,}\n
        (?P<tail>.*)
    """ % dict(ident=re.escape(ident).replace("\\ ", "\\s+"),),
                 data.replace("\r\n", "\n"))
    assert m, "%r section not found" % (ident,)

    begin_row = "# begin " + ident + " (autogenerated " + now + ")\n"

    head, tail = m.group("head", "tail")
    divline = "#" + "-" * max(40, len(begin_row) - 1) + "\n"
    return "".join([
        head,
        divline,
        begin_row,
        divline,
        content,
        divline,
        "# end ", ident, "\n",
        divline,
        tail,
        ])

#=============================================================================
# main
#=============================================================================

from passlib.hash import sha256_crypt

source_dir = os.path.abspath(os.path.join(__file__, *[".."]*2))

def main():
    """rebuild autogenerated sections in passlib"""

    #------------------------------------------------
    # rebuild autocomplete helper in passlib/hash.py
    #------------------------------------------------
    content = "if False:\n"
    from passlib.registry import _locations
    modules = {}
    for name, path in _locations.items():
        modules.setdefault(path, []).append(name)
    for path, names in sorted(modules.items()):
        row = "    from %s import %s\n" % (path, ", ".join(sorted(names)))
        content += row

    hash_path = os.path.join(source_dir, "passlib", "hash.py")
    data = open(hash_path).read()
    data = replace_section(data, "autocomplete hack", content)
    open(hash_path, "w").write(data)

if __name__ == "__main__":
    main()

#=============================================================================
# eof
#=============================================================================