You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

hot_reload_mod_i2.py 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. from werkzeug import _reloader
  2. p=print
  3. import typing as t
  4. import threading
  5. import sys
  6. import os
  7. import importlib
  8. import traceback
  9. custom_rld_substr = "rld"
  10. custom_rld_list = []
  11. mscope={
  12. "ReloaderLoop_trigger_reload": _reloader.ReloaderLoop.trigger_reload,
  13. "WatchdogReloaderLoop_trigger_reload": _reloader.WatchdogReloaderLoop.trigger_reload,
  14. }
  15. def reset_mtime(reloader,file_path):
  16. old_time = reloader.mtimes.get(file_path)
  17. mtime = os.stat(file_path).st_mtime
  18. reloader.mtimes[file_path]=mtime
  19. return {
  20. "old_time":old_time,
  21. "new_time":mtime,
  22. }
  23. def search_for_mod_by_path(file_path):
  24. # for module in list(sys.modules.values()):
  25. for mod_name,module in list(sys.modules.items()):
  26. mod_path = getattr(module, "__file__", None)
  27. if file_path == mod_path:
  28. return module
  29. # if not module:
  30. # p("iojdwoqjdqoidjqwoidj")
  31. # if not name:
  32. # p(mod_name,module,type(module))
  33. # p("~",name)
  34. def path_match_rld_mod(file_path):
  35. if file_path is None:
  36. return file_path
  37. return file_path in custom_rld_list or custom_rld_substr in file_path
  38. def maybe_custom_reload(reloader,file_path):
  39. # if file_path in custom_rld_list or custom_rld_substr in file_path:
  40. if path_match_rld_mod(file_path):
  41. # p(dir(reloader))
  42. st= reset_mtime(reloader,file_path)
  43. mod = search_for_mod_by_path(file_path)
  44. #TODO: this is the important part refactor out later
  45. if mod:
  46. # gmod.before_mod_rld(mod)
  47. # reimp = importlib.reload(mod)
  48. try:
  49. pass
  50. reimp = importlib.reload(mod)
  51. except Exception as E:
  52. p("EXC@@@@@@@@@@@@")
  53. print(traceback.format_exc())
  54. else:
  55. reimp="~~"
  56. p("module not found with path:",file_path)
  57. # importlib.reload(None)
  58. # module==type(sys.modules["rld_m1"])
  59. # p(type(sys.modules["rld_m1"]),)
  60. # p("maybe_custom_reload!!!",st,reimp)
  61. return 1
  62. r'''
  63. try:
  64. cnt=0
  65. while gmod.glob["on_before_app_reload"]:
  66. gmod.glob["on_before_app_reload"].pop()()
  67. print("BEFORE RELOAD,,",cnt,OKBLUE3,"###################################################################",ENDC)
  68. pass
  69. except Exception as E:
  70. print("I WOULD PANIC BUT PANIC HAS NOT BEEN IMPLIMENTED",E)
  71. pass
  72. # '''
  73. def t_rld1(self,file_path,*args,**kwargs):
  74. # p("extra_files",args[0].extra_files)
  75. p("t_rld1",self,file_path,args,kwargs)
  76. p("```````````````````````````````````````")
  77. p()
  78. # if (file_path == r"C:\ws\repos\flask_apps\flask_example\rld_m2.py"):
  79. # p("~~~%%%%%%%%%%%%%%%%%%%%%%5")
  80. # return
  81. if maybe_custom_reload(self,file_path):
  82. pass
  83. else:
  84. # gmod.before_wsgi_rld("will rld")
  85. p("...................")
  86. mscope["ReloaderLoop_trigger_reload"](self,file_path,*args,**kwargs)
  87. p("...................>")
  88. # p("exclude_patterns",args[0].exclude_patterns)
  89. def t_rld2(*args,**kwargs):
  90. p("t_rld2",args,kwargs)
  91. mscope["WatchdogReloaderLoop_trigger_reload"](*args,**kwargs)
  92. _reloader.ReloaderLoop.trigger_reload = t_rld1
  93. _reloader.WatchdogReloaderLoop.trigger_reload = t_rld2
  94. def run_with_reloader2(
  95. main_func: t.Callable[[], None],
  96. extra_files: t.Iterable[str] | None = None,
  97. exclude_patterns: t.Iterable[str] | None = None,
  98. interval: int | float = 1,
  99. reloader_type: str = "auto",
  100. ) -> None:
  101. """Run the given function in an independent Python interpreter."""
  102. import signal
  103. import os
  104. print("")
  105. print("")
  106. print("")
  107. print("")
  108. print("run_with_reloader2!")
  109. print("RLDI",main_func,extra_files,exclude_patterns,interval,reloader_type)
  110. signal.signal(signal.SIGTERM, lambda *args: sys.exit(0))
  111. reloader = _reloader.reloader_loops[reloader_type](
  112. extra_files=extra_files, exclude_patterns=exclude_patterns, interval=interval
  113. )
  114. mscope["reloader"]=reloader
  115. try:
  116. if os.environ.get("WERKZEUG_RUN_MAIN") == "true":
  117. _reloader.ensure_echo_on()
  118. t = threading.Thread(target=main_func, args=())
  119. t.daemon = True
  120. # Enter the reloader to set up initial state, then start
  121. # the app thread and reloader update loop.
  122. with reloader:
  123. t.start()
  124. reloader.run()
  125. else:
  126. # gmod.before_wsgi_rld("restart_with_reloader")
  127. sys.exit(reloader.restart_with_reloader())
  128. except KeyboardInterrupt:
  129. # gmod.before_wsgi_rld("um actually exiting")
  130. pass
  131. _reloader.run_with_reloader = run_with_reloader2