main.py 文件源码

python
阅读 25 收藏 0 点赞 0 评论 0

项目:nodepy 作者: nodepy 项目源码 文件源码
def main(argv=None):
  args = parser.parse_args(argv)
  args.nodepy_path.insert(0, '.')
  if args.version:
    print(VERSION)
    return 0

  args.pmd = check_pmd_envvar() or args.pmd
  sys.argv = [sys.argv[0]] + args.request[1:]

  maindir = pathlib.Path(args.maindir) if args.maindir else pathlib.Path.cwd()
  ctx = nodepy.context.Context(maindir)

  # Updat the module search path.
  args.nodepy_path.insert(0, ctx.modules_directory)
  ctx.resolver.paths.extend(x for x in map(pathlib.Path, args.nodepy_path) if x.is_dir())
  ctx.localimport.path.extend(args.python_path)

  # Create the module in which we run the REPL or the command
  # specified via -c.
  if args.c or not args.request:
    filename = nodepy.utils.path.VoidPath('<repl>')
    directory = pathlib.Path.cwd()
    repl_module = ReplModule(ctx, None, filename, directory)
    repl_module.init()

  with ctx.enter():
    if args.pmd:
      install_pmd(ctx)
    if args.c:
      repl_module.set_exec_handler(lambda: six.exec_(args.c, vars(repl_module.namespace)))
      repl_module.load()
    if args.request:
      try:
        filename = path.urlpath.make(args.request[0])
      except ValueError:
        filename = args.request[0]
      ctx.main_module = ctx.resolve(filename)
      if not args.keep_arg0:
        sys.argv[0] = str(ctx.main_module.filename)
      ctx.main_module.init()
      if args.pymain:
        ctx.main_module.namespace.__name__ = '__main__'
      ctx.load_module(ctx.main_module, do_init=False)
    elif not args.c:
      ctx.main_module = repl_module
      repl_module.set_exec_handler(lambda: code.interact('', local=vars(repl_module.namespace)))
      repl_module.load()
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号