-
Notifications
You must be signed in to change notification settings - Fork 1
/
execfile.py
397 lines (347 loc) · 15.1 KB
/
execfile.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
import os, sys, inspect, re
from datetime import datetime, timedelta
import traceback
try:
import __builtin__ as builtins
except:
import builtins
class ExecFileError(Exception): pass
def _A(o):
if o is None:
return []
elif isinstance(o, list):
return o
else:
return [o]
def _find(filenames, test=os.path.isfile):
filename = 'None'
for filename in _A(filenames):
if os.path.isabs(filename):
if test(filename):
return filename
else:
for path in ['.'] + sys.path:
fn = os.path.join(path, filename)
if test(fn):
return fn
raise ExecFileError("Can't _find file {}".format(filename))
def _extend(obj, *args):
"""
adapted from underscore-py
Extend a given object with all the properties in
passed-in object(s).
"""
args = list(args)
for src in args:
obj.update(src)
for k, v in src.items():
if v is None:
del obj[k]
return obj
def unload(pattern):
for x in [x for x in sys.modules.keys() if re.match(pattern, x)]:
print("unloading {}".format(x))
del sys.modules[x]
def unload_by_path(pattern):
for x in [x for x in sys.modules.keys() if re.match(pattern, os.path.abspath( getattr(sys.modules[x], '__file__', '') ).replace("\\", "/"))]:
print("unloading {}".format(x))
del sys.modules[x]
def lsmod():
for x in [x for x in sys.modules.keys()]:
print("{:<64} {}".format(x, len(dir(sys.modules[x]))))
def module_match(pattern):
return [x for x in sys.modules.keys() if re.match(pattern, x)]
def find_in_modules(pattern):
return [x[1] for x in [(hasattr(sys.modules[m], pattern), m) for m in sys.modules] if x[0]]
def unload_from_all_modules(pattern, module_regex=None):
for x in [x for x in sys.modules.keys() if not module_regex or re.match(module_regex, x)]:
m = sys.modules[x]
for sub in dir(m):
if re.match(pattern, sub):
print("{}::{}".format(x, sub))
if sys.version_info.major >= 3:
import importlib
importlib.reload(m)
def _isflattenable(iterable):
return hasattr(iterable,'__iter__') and not hasattr(iterable,'isalnum')
# def _make_execfile():
# def _execfile(filename, _globals=None, args=[]):
# if _isflattenable(filename):
# return [_execfile(x, _globals, args) for x in filename]
#
# filenames = [filename]
# fn, ext = os.path.splitext(filename)
# if not ext:
# filenames.append(os.path.extsep.join([fn, 'py']))
#
# full_path = _find(filenames)
#
# # ensure consistency
# full_path = os.path.abspath(full_path)
#
# # if getattr(builtins, 'execfile', 0):
# # if _globals:
# # return builtins.execfile(full_path, _globals)
# # return builtins.execfile(full_path)
#
# argv = sys.argv
# sys.argv = [ full_path ]
# sys.argv.extend(args)
#
# if _globals is None:
# _globals = dict(inspect.getmembers(
# inspect.stack()[1][0]))["f_globals"]
# _ori_globals = {k: _globals.get(k, None) for k in ('__file__', '__name')}
#
#
# if hasattr(builtins, 'execfile'):
# return builtins.execfile(full_path, _extend(_globals, { "__file__": full_path }))
# try:
# with open(full_path, "rb") as file:
# raw = file.read()
# encoding = "UTF-8" # UTF-8 by default: https://www.python.org/dev/peps/pep-3120/
#
# encoding_pat = re.compile(r'\s*#.*coding[:=]\s*([-\w.]+).*')
# for line in raw.decode("ASCII", errors='replace').split("\n"):
# match = encoding_pat.match(line)
# if match:
# encoding = match.group(1)
# break
#
# code = compile(raw.decode(encoding), full_path, 'exec')
# exec(code, _extend(_globals, { "__file__": full_path,
# "__name__": "__main__" }))
#
# except Exception as e:
# print("%s\n%s" % (str(e), traceback.format_exc()))
# finally:
# sys.argv = argv
# _extend(_globals, _ori_globals)
# return _execfile
#
# execfile = _make_execfile()
def execfile(filename, _globals=None, args=None):
args = _A(args)
if _isflattenable(filename):
return [execfile(x, _globals, args) for x in filename]
filenames = [filename]
fn, ext = os.path.splitext(filename)
if not ext:
filenames.append(os.path.extsep.join([fn, 'py']))
full_path = _find(filenames)
# ensure consistency
full_path = os.path.abspath(full_path)
# if getattr(builtins, 'execfile', 0):
# if _globals:
# return builtins.execfile(full_path, _globals)
# return builtins.execfile(full_path)
argv = sys.argv
sys.argv = [ full_path ]
sys.argv.extend(args)
if _globals is None:
_globals = dict(inspect.getmembers(
inspect.stack()[1][0]))["f_globals"]
_ori_globals = {k: _globals.get(k, None) for k in ('__file__', '__name')}
if hasattr(builtins, 'execfile'):
return builtins.execfile(full_path, _extend(_globals, { "__file__": full_path }))
try:
with open(full_path, "rb") as file:
raw = file.read()
encoding = "UTF-8" # UTF-8 by default: https://www.python.org/dev/peps/pep-3120/
encoding_pat = re.compile(r'\s*#.*coding[:=]\s*([-\w.]+).*')
for line in raw.decode("ASCII", errors='replace').split("\n"):
match = encoding_pat.match(line)
if match:
encoding = match.group(1)
break
code = compile(raw.decode(encoding), full_path, 'exec')
exec(code, _extend(_globals, { "__file__": full_path,
"__name__": "__main__" }))
except Exception as e:
print("%s\n%s" % (str(e), traceback.format_exc()))
finally:
sys.argv = argv
_extend(_globals, _ori_globals)
def make_refresh(_file, _globals = None):
if _globals is None:
_globals = dict(inspect.getmembers(
inspect.stack()[1][0]))["f_globals"]
def refresh_fn():
print("refreshing " + _file)
execfile(_file, _globals)
return refresh_fn
def make_auto_refresh(_file, _globals = None):
""" still conceptual, not for production use """
if _globals is None:
_globals = dict(inspect.getmembers(
inspect.stack()[1][0]))["f_globals"]
def check_for_update():
# @static: last_load_time
if 'last_load_time' not in check_for_update.__dict__:
check_for_update.last_load_time = datetime.today()
file_mod_time = datetime.fromtimestamp(os.stat(os.path.abspath(_file)).st_mtime) # This is a datetime.datetime object!
now = datetime.today()
max_delay = timedelta(seconds=10)
if file_mod_time - check_for_update.last_load_time > max_delay:
# print("CRITICAL: {} last modified on {}. Threshold set to {} minutes.".format(_file, file_mod_time, max_delay.seconds/60))
execfile(_file, _globals)
check_for_update.last_load_time = datetime.today()
else:
pass
# print("OK. {} hasn't been modified for {} minutes".format(_file, (now-file_mod_time).seconds/60))
return check_for_update
def _require(modulename, package=None):
"""
@param modulename:
@return module:
Load, or reload a module.
When under heavy development, a user's tool might consist of multiple
modules. If those are imported using the standard 'import' mechanism,
there is no guarantee that the Python implementation will re-read
and re-evaluate the module's Python code. In fact, it usually doesn't.
What should be done instead is 'reload()'-ing that module.
This is a simple helper function that will do just that: In case the
module doesn't exist, it 'import's it, and if it does exist,
'reload()'s it.
The importing module (i.e., the module calling require()) will have
the loaded module bound to its globals(), under the name 'modulename'.
(If require() is called from the command line, the importing module
will be '__main__'.)
For more information, see: <http://www.hexblog.com/?p=749>.
"""
frame_obj, filename, line_number, function_name, lines, index = inspect.stack()[1]
parent_module = inspect.getmodule(frame_obj)
if parent_module is None: # No importer module; called from command line
parent_module = sys.modules['__main__']
if modulename in sys.modules.keys():
m = sys.modules[modulename]
if sys.version_info.major >= 3:
import importlib
importlib.reload(m)
else:
reload(m)
m = sys.modules[modulename]
else:
import importlib
m = importlib.import_module(modulename, package)
sys.modules[modulename] = m
# this fucks up our `_import('from package import ....') by writing the module to globals
# setattr(parent_module, modulename, m)
return m
def _funcname():
return inspect.currentframe(1).f_code.co_name
def _import(import_stmt, default_cmd='import', global_depth=0):
"""
import_stmt ::= "import" module ["as" name] ( "," module ["as" name] )*
| "from" relative_module "import" identifier ["as" name] ( "," identifier ["as" name] )*
| "from" relative_module "import" "(" identifier ["as" name] ( "," identifier ["as" name] )* [","] ")"
| "from" module "import" "*"
module ::= (identifier ".")* identifier
relative_module ::= "."* module | "."+
name ::= identifier
TODO: relative_modules or just things with '.' in them
(though this generally seems to work, though not exhaustively tested)
see: https://docs.python.org/3/tutorial/modules.html#packages
"""
if 'debug' in globals():
debug = globals()['debug']
else:
debug = False
if debug: print("*** {} ***".format(import_stmt))
if default_cmd not in import_stmt:
# lazy invocation: `_import('module')`
if debug: print("inserting '{}' prefix".format(default_cmd))
import_stmt = default_cmd + ' ' + import_stmt
rflags = 0
re_list = [
# testing: import multiple, modules [as name]
re.compile(r'import (?P<importcsv>.*,.*)', rflags), # importcsv
# import single [as name]
re.compile(r'import (?P<module>\S+)(?: as (?P<as>\w+))?', rflags), # module and not name
# testing: from single import multiple, modules [as name]
re.compile(r'from (?P<module>\S+) import (?P<fromcsv>.*,.*)', rflags), # fromcsv
# from single import *
re.compile(r'from (?P<module>\S+) import (?P<asterisk>[*])', rflags), # asterisk
# from single import single [as name]
re.compile(r'from (?P<module>\S+) import (?P<identifier>\w+)(?: as (?P<as>\w+))?', rflags), # module and identifier
]
# standardise whitespace following commas
import_stmt = ', '.join(import_stmt.split(','))
# remove excess whitespace (including that generated by the comma thing above
import_stmt = ' '.join(filter(lambda x: x, import_stmt.split(' ')))
# remove leading and trailing whitespace
import_stmt = import_stmt.strip()
# allow custom global space, else default to callee's
_globals = dict(inspect.getmembers(
inspect.stack()[global_depth + 1][0]))["f_globals"]
result = {}
# check each pattern (order is important!) to find correct parser
for pattern in re_list:
matches = re.match(pattern, import_stmt)
if debug: print(pattern, matches)
if matches:
d = matches.groupdict()
k = d.keys()
if 'importcsv' in k:
# testing: import foo, bar as baz
for module, _as, name in [x.partition(' as ') for x in d['fromcsv'].split(', ')]:
if debug: print("importing " + str(module) + " as " + str(name or module))
result[name or module] = _require(module)
_extend(_globals, result)
return result
elif 'fromcsv' in k:
# from x import foo, bar as baz
o = _require(d['module'])
for identifier, _as, name in [x.partition(' as ') for x in d['fromcsv'].split(', ')]:
result[name or identifier] = getattr(o, identifier)
_extend(_globals, result)
return result
elif 'asterisk' in k:
# from x import *
# (there must be a importlib way to import all, surely?)
o = _require(d['module'])
members = inspect.getmembers(o)
for key in [x[0] for x in members if not x[0].startswith('__')]:
result[key] = getattr(o, key)
_extend(_globals, result)
return result
elif 'identifier' in k:
# from x import y [as z]
p = _require(d['module']) # from <p>
id = d['identifier'] # import <_as>
if not hasattr(p, id):
print("execfile::_import('{}'): module '{}' has no method named '{}'".format(import_stmt, d['module'], id))
print("methods: {}".format(p.__dict__))
return
o = getattr(p, id)
if d['as']:
id = d['as'] # as <_as>
# # if debug: print(f"imported {d['module']}.{d['identifier']} as {d['as']}")
else:
pass
# # if debug: print(f"imported {d['module']}.{d['identifier']} as {d['identifier']}")
result[id] = o
_extend(_globals, result)
return result
elif 'as' in k:
# simple import x as y
o = _require(d['module'])
if d['as']:
result[d['as']] = o
# if debug: print(f"imported {d['module']} as {d['as']}")
else:
# no need to write to _globals, _require will do this by default
result[d['module']] = o
# if debug: print(f"imported {d['module']} as itself")
_extend(_globals, result)
return result
elif 'module' in k:
# vanila import
# if debug: print(f"imported {d['module']} (vanilla)")
result[d['module']] = _require(d['module'])
_extend(_globals, result)
return result
raise ImportError("Couldn't interpret '{}'".format(import_stmt))
def _from(import_stmt):
# inspect.currentframe().f_code.co_name
return _import(import_stmt, default_cmd="from", global_depth=1)