entry_points.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import sys
  2. import __res
  3. def repl():
  4. user_ns = {}
  5. py_main = __res.find('PY_MAIN')
  6. if py_main:
  7. py_main_split = py_main.split(':', 1)
  8. if len(py_main_split) == 2:
  9. mod_name, func_name = py_main_split
  10. else:
  11. mod_name, func_name = py_main_split[0], 'main'
  12. if not mod_name:
  13. mod_name = 'library.python.runtime.entry_points'
  14. try:
  15. import importlib
  16. mod = importlib.import_module(mod_name)
  17. user_ns = mod.__dict__
  18. except: # noqa E722
  19. import traceback
  20. traceback.print_exc()
  21. if '__main__' not in user_ns:
  22. def run(args):
  23. if isinstance(args, basestring):
  24. import shlex
  25. args = shlex.split(args)
  26. import sys
  27. sys.argv = [sys.argv[0]] + args
  28. getattr(mod, func_name)()
  29. user_ns['__main__'] = run
  30. else:
  31. try:
  32. mod = __res.importer.load_module('__main__', fix_name='__main_real')
  33. user_ns = mod.__dict__
  34. except ImportError:
  35. pass
  36. try:
  37. import IPython
  38. except ImportError:
  39. import code
  40. code.interact(local=user_ns)
  41. else:
  42. IPython.start_ipython(user_ns=user_ns)
  43. def resource_files():
  44. sys.stdout.write('\n'.join(sorted(__res.resfs_files()) + ['']))
  45. def run_constructors():
  46. for key, module_name in __res.iter_keys('py/constructors/'):
  47. import importlib
  48. module = importlib.import_module(module_name)
  49. init_func = getattr(module, __res.find(key))
  50. init_func()