77import tools .chrome
88import tools .debug
99import tools .r2tool
10+ import inspect
11+
12+ class Namespace :
13+ pass
1014
1115class ToolLoader :
1216 def registerHatchery (self ,hatch_name ):
1317 temp_h = self .HatcheryClass (hatch_name )
1418 self .hatch_store [temp_h .name ] = temp_h
1519 h_name = temp_h .name # hatch_name is the file, h_name is the internal name
16- print ("info : registering hatch '%s'" % h_name )
20+ print ("tools : registering hatch '%s'" % h_name )
1721 self .hatch_names [hatch_name ] = h_name
1822 if h_name in self .tools .keys ():
1923 print ("warning: ToolLoader trying to register '%s' as hatchery, already loaded" % name )
@@ -30,7 +34,7 @@ def registerFunction(self,name,function,func_desc):
3034 self .tools [name ].__doc__ = func_desc
3135
3236 def run_hatch (self ,hatch_name ,input_str ):
33- print ("info : ToolLoader calling run_hatch(%s,%s)" % (hatch_name ,input_str ))
37+ print ("tools : ToolLoader calling run_hatch(%s,%s)" % (hatch_name ,input_str ))
3438 return self .hatch_store [hatch_name ].run (ctx = {"input" :input_str })
3539 # return "ok"
3640
@@ -39,6 +43,7 @@ def __init__(self,HatcheryClass=None):
3943 raise ValueError ("fatal: i didn't get hatcheryclass" )
4044 else :
4145 self .HatcheryClass = HatcheryClass
46+ self .exec_ns_array = []
4247 self .hatch_store = {}
4348 self .hatch_names = {}
4449 self .short_name_store = {}
@@ -65,23 +70,39 @@ def __init__(self,HatcheryClass=None):
6570 self .registerFunction ("r2_cmd" ,r2tool .r2_cmd , "Run an r2 command." )
6671 self .registerFunction ("r2_close" ,r2tool .r2_close , "Close the r2 session." )
6772
73+ def load_pytool (self ,name ):
74+ print ("tools: loading pytool '%s'. caveat emptor..." % name )
75+ namespace = {}
76+ with open (name ,"r" ) as f :
77+ code = f .read ()
78+ ns = Namespace ()
79+ exec (code ,ns .__dict__ )
80+ out = []
81+ self .exec_ns_array .append (ns )
82+ for i in ns .__dict__ .keys ():
83+ if callable (ns .__dict__ [i ]):
84+ print ("tools: got a callable '%s'" % i )
85+ self .registerFunction (i ,ns .__dict__ [i ],ns .__dict__ [i ].__doc__ or "" )
86+ out .append (i )
87+ return out
88+
6889 def fetch_toolbox (self ,name ):
6990 out = []
7091 for i in self .tools .keys ():
7192 if i .startswith (name ):
7293 out .append (i )
73- print ("info : fetch_toolbox(%s) returned %d results" % (name ,len (out )))
94+ print ("tools : fetch_toolbox(%s) returned %d results" % (name ,len (out )))
7495 return out
7596
7697 def fetch (self ,name ):
77- print ("info : attempting to grab tool '%s'" % name )
98+ print ("tools : attempting to grab tool '%s'" % name )
7899 if name .startswith ("hatch:" ):
79100 shortname = name [6 :]
80101 if shortname in self .hatch_names .keys ():
81102 # translate the name first.
82103 return self .tools [self .hatch_names [shortname ]]
83104 else :
84- print ("info : detected hatchery-as-tool pattern, passing" )
105+ print ("tools : detected hatchery-as-tool pattern, passing" )
85106 self .registerHatchery (shortname )
86107 return self .tools [self .hatch_names [shortname ]]
87108 else :
0 commit comments