This repository was archived by the owner on Feb 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdate_registry.py
168 lines (127 loc) · 4.33 KB
/
update_registry.py
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#######################################################################
# This file is part of Pyblosxom.
#
# Copyright (c) 2011 Will Kahn-Greene
#
# Pyblosxom is distributed under the MIT license. See the file
# LICENSE for distribution details.
#######################################################################
"""
This script generates registry entries for plugins from the plugin
itself.
"""
import os
import ast
import sys
# skip these because they're not plugins
SKIP = ("akismet.py", "__init__.py", "tests")
HELP = """update_registry <plugindir> <outputdir>
Goes through plugins it finds in the plugindir and generates registry
entries for them. It puts the registry entries in a directory hierarchy
denoted by outputdir.
Docstrings for plugins should be formatted in restructured text.
"""
ENTRYTEMPLATE = """%(title)s
#author %(author)s
#email %(email)s
#infourl %(infourl)s
#download %(download)s
#summary %(summary)s
#license %(license)s
#registrytags %(registrytags)s
#generator update_registry
%(body)s
"""
def get_info(node, info_name):
# FIXME - this is inefficient since it'll traverse the entire ast
# but we only really need to look at the top level.
for mem in ast.walk(node):
if not isinstance(mem, ast.Assign):
continue
for target in mem.targets:
if not isinstance(target, ast.Name):
continue
if target.id == info_name:
return mem.value.s
print "missing %s" % info_name
return None
def build_registry_entry(filepath):
try:
fp = open(filepath, "r")
except (IOError, OSError):
return False
node = ast.parse(fp.read(), filepath, 'exec')
return ENTRYTEMPLATE % {
"title": os.path.splitext(os.path.basename(filepath))[0],
"author": get_info(node, "__author__"),
"email": get_info(node, "__email__"),
"infourl": get_info(node, "__url__"),
# "download": get_info(node, "__download__"),
"download": "As of Pyblosxom 1.5 rc1, comes with Pyblosxom",
"summary": get_info(node, "__description__"),
"license": get_info(node, "__license__"),
"registrytags": get_info(node, "__registrytags__"),
"body": ast.get_docstring(node, True)}
def save_entry(filepath, entry):
parent = os.path.dirname(filepath)
if not os.path.exists(parent):
os.makedirs(parent)
f = open(filepath, "w")
f.write(entry)
f.close()
def get_plugins(plugindir, outputdir):
for root, dirs, files in os.walk(plugindir):
# remove skipped directories so we don't walk through them
for mem in SKIP:
if mem in dirs:
dirs.remove(mem)
break
if root == plugindir:
continue
for file_ in files:
if ((file_.startswith("_") or not file_.endswith(".py") or
file_ in SKIP)):
continue
filename = os.path.join(root, file_)
print "working on %s" % filename
entry = build_registry_entry(filename)
output_filename = filename[len(plugindir):]
output_filename = output_filename.lstrip(os.sep)
output_filename = os.path.splitext(output_filename)[0] + ".rst"
output_filename = os.path.join(outputdir, output_filename)
save_entry(output_filename, entry)
def main(args):
print "update_registry.py"
print args
if "-h" in args or "--help" in args:
print HELP
return 0
# this is silly, but it makes the later computation easier
args.insert(0, "")
args.insert(0, "")
outputdir = args.pop()
if outputdir:
outputdir = os.path.abspath(outputdir)
else:
print "No outputdir specified."
print HELP
sys.exit(1)
plugindir = args.pop()
if plugindir:
plugindir = os.path.abspath(plugindir)
else:
print "No plugindir specified."
print HELP
sys.exit(1)
print "plugindir: %s" % plugindir
if not os.path.exists(plugindir):
print "Plugindir doesn't exist."
print HELP
sys.exit(1)
print "outputdir: %s" % outputdir
if not os.path.exists(outputdir):
os.makedirs(outputdir)
get_plugins(plugindir, outputdir)
return 0
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))