-
Notifications
You must be signed in to change notification settings - Fork 16
/
manage.py
168 lines (128 loc) · 4.15 KB
/
manage.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
if __name__ == "__main__":
import logSetup
logSetup.initLogging()
# Shut up fucking annoying psycopg2 vomit every exec.
import warnings
from sqlalchemy import exc as sa_exc
warnings.filterwarnings("ignore", category=UserWarning, module='psycopg2')
warnings.simplefilter("ignore", category=sa_exc.SAWarning)
import inspect
import sys
import common.management.WebMirrorManage
import common.management.CrNManage
import common.management.RawMirrorManage
import common.management.Misc
import common.management.Testing
import common.Tools.WebFictionGuide
import WebMirror.OfflineFilters.offline_filters
import WebMirror.TimedTriggers.TriggerManage
# Site-specific management stuff
import WebMirror.management.GravityTalesManage
import WebMirror.management.SpcnetTvManage
import WebMirror.management.RssManage
import WebMirror.management.Testing
# Moar
import WebMirror.management.DbManage
import WebMirror.management.FeedDbManage
import WebMirror.management.UrlManage
import WebMirror.OfflineFilters.NewNetlocTracker
import RawArchiver.TimedTriggers.TriggerManage
func_prefix = "exposed_"
SCANNED_MODULES = [
common.management.WebMirrorManage,
common.management.RawMirrorManage,
common.management.CrNManage,
common.management.Misc,
common.management.Testing,
common.Tools.WebFictionGuide,
WebMirror.OfflineFilters.offline_filters,
WebMirror.TimedTriggers.TriggerManage,
WebMirror.management.GravityTalesManage,
WebMirror.management.SpcnetTvManage,
WebMirror.management.FeedDbManage,
WebMirror.management.DbManage,
WebMirror.management.RssManage,
WebMirror.management.UrlManage,
WebMirror.management.Testing,
WebMirror.OfflineFilters.NewNetlocTracker,
RawArchiver.TimedTriggers.TriggerManage,
]
def load_functions():
print("Loading functions")
ret = {}
for module in SCANNED_MODULES:
for name, member in inspect.getmembers(module):
if inspect.isfunction(member) and name.startswith(func_prefix):
sname = name[len(func_prefix):]
assert sname not in ret, "Duplicate management functions named: '%s'" % name
ret[sname] = member
print("Found %s functions" % len(ret))
return ret
def print_func(name, func):
doc = inspect.getdoc(func)
sig = inspect.signature(func)
if not doc or (doc and not doc.strip()):
print(" {} -> {}".format(name.ljust(25), "UNDOCUMENTED"))
if not sig.parameters:
print(" No arguments")
else:
print(" Args: {}".format(sig))
else:
print(" {}".format(name))
if not sig.parameters:
print(" No arguments")
else:
print(" Args: {}".format(sig))
doclines = doc.splitlines()
for line in doclines:
print(" -> {}".format(line))
print()
def print_help():
print("ReadableWebProxy Management CLI Interface!")
print("Available functions:")
farr = load_functions()
names = list(farr.keys())
names.sort()
for name in names:
print_func(name, farr[name])
def try_call(func, args):
'''
Try to call function `func` with passed array of arguments `args`.
Validates that arguments args are of the correct length.
'''
sig = inspect.signature(func)
if len(sig.parameters) == 0 and len(args) == 0:
print("No params required: ", func)
func()
print("Called!")
return True
if len(sig.parameters) == len(args):
print("Matching param count: ", func)
func(*args)
return True
req_params = [parm for parm in sig.parameters if sig.parameters[parm].default == inspect.Parameter.empty]
if len(args) >= len(req_params) and len(args) <= len(sig.parameters):
print("Partial coverage of arguments, including all required: ", args)
func(*args)
return True
return False
def call_func(args):
print("Looking for function callable with params: '{}'".format(args))
fname = args[0]
farr = load_functions()
if not fname in farr:
return False
print("Function signature: '%s'" % (inspect.getsourcelines(farr[fname])[0][0].strip(), ))
print("Found function %s, from file %s, line %s" % (farr[fname], inspect.getsourcefile(farr[fname]), inspect.findsource(farr[fname])[1]))
return try_call(farr[fname], args[1:])
def go():
if len(sys.argv) > 1:
have = call_func(sys.argv[1:])
if not have:
print_help()
else:
return
else:
print_help()
if __name__ == "__main__":
go()