sapling/eden/scm/edenscm/mercurialshim.py
Muir Manders 046f194035 dispatch: add shim to support legacy edenscm.mercurial imports
Summary:
Replace util.mercurialmodule with a loader based shim that intercepts imports to "edenscm.mercurial" (or just "mercurial" to maintain previous adapter). This approach works better since it works recursively for nested imports.

The previous approach would result in "split brain" when sub-modules of mercurial were imported. For example, "import edenscm.mercurial.registrar" would give you a copy of the "edenscm.registrar" module.

I removed a few import try/catches from the previous diff to keep things simpler until I go through and fix the legacy imports.

Reviewed By: quark-zju

Differential Revision: D38951110

fbshipit-source-id: f5c167a67406bc62ccf5f42ecf15efebf268bec0
2022-08-24 13:45:53 -07:00

38 lines
1.1 KiB
Python

# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2.
import importlib
import sys
class MercurialImporter(object):
"""Intercept legacy imports of edenscm.mercurial(.foo)? or mercurial(.foo)? and resolve with import of edenscm$1.
This provides compatibility to legacy hooks and extensions that still use old imports.
"""
# This implements the "Finder" interface.
def find_module(self, fullname, _path):
if (
fullname == "edenscm.mercurial"
or fullname.startswith("edenscm.mercurial.")
or fullname == "mercurial"
or fullname.startswith("mercurial.")
):
return self
return None
# This implements the "Loader" interface.
def load_module(self, fullname):
if fullname.startswith("edenscm.mercurial"):
realname = "edenscm" + fullname[17:]
else:
realname = "edenscm" + fullname[9:]
mod = importlib.import_module(realname)
sys.modules[fullname] = mod
return mod