mirror of
https://github.com/facebook/sapling.git
synced 2024-10-17 04:08:01 +03:00
50ddbd96dd
Summary: This is needed for the `py2exe`-like loading of the the python stdlib and mercurial files. The `zipimport` per se works out of the box in Python. The problem is loading native extensions. Since some of them are parts of packages, such as `mercurial.cext.osutil`, and `mercurial/cext/` folder structure does not exist in the embedded case, we're just doing what `py2exe` does: renaming every native extension file to include the full package path, e.g. `mercurial.cext.osutil.pyd` and tweaking the loader to know to try such files. NB: I want to rename `hgdemandimport` to `hgimport` later, since now there are more responsibilities for this package. We can probably also just merge it with `mercurial`, since we don't care about `python3` Reviewed By: quark-zju Differential Revision: D9919806 fbshipit-source-id: 216ae904760311003e7171cb6fbe0bbc4b1abe2b
103 lines
2.7 KiB
Python
103 lines
2.7 KiB
Python
# hgdemandimport - global demand-loading of modules for Mercurial
|
|
#
|
|
# Copyright 2017 Facebook Inc.
|
|
#
|
|
# This software may be used and distributed according to the terms of the
|
|
# GNU General Public License version 2 or any later version.
|
|
|
|
"""demandimport - automatic demand-loading of modules"""
|
|
|
|
# This is in a separate package from mercurial because in Python 3,
|
|
# demand loading is per-package. Keeping demandimport in the mercurial package
|
|
# would disable demand loading for any modules in mercurial.
|
|
|
|
from __future__ import absolute_import
|
|
|
|
import contextlib
|
|
import os
|
|
import sys
|
|
|
|
from .embeddedimport import tryenableembedded
|
|
|
|
|
|
if sys.version_info[0] >= 3:
|
|
from . import demandimportpy3 as demandimport
|
|
else:
|
|
from . import demandimportpy2 as demandimport
|
|
|
|
|
|
# Extensions can add to this list if necessary.
|
|
ignore = [
|
|
"__future__",
|
|
"_hashlib",
|
|
# ImportError during pkg_resources/__init__.py:fixup_namespace_package
|
|
"_imp",
|
|
"_xmlplus",
|
|
"fcntl",
|
|
"nt", # pathlib2 tests the existence of built-in 'nt' module
|
|
"win32com.gen_py",
|
|
"win32com.shell", # 'appdirs' tries to import win32com.shell
|
|
"_winreg", # 2.7 mimetypes needs immediate ImportError
|
|
"pythoncom",
|
|
# imported by tarfile, not available under Windows
|
|
"pwd",
|
|
"grp",
|
|
# imported by profile, itself imported by hotshot.stats,
|
|
# not available under Windows
|
|
"resource",
|
|
# this trips up many extension authors
|
|
"gtk",
|
|
# setuptools' pkg_resources.py expects "from __main__ import x" to
|
|
# raise ImportError if x not defined
|
|
"__main__",
|
|
"_ssl", # conditional imports in the stdlib, issue1964
|
|
"_sre", # issue4920
|
|
"rfc822",
|
|
"mimetools",
|
|
"sqlalchemy.events", # has import-time side effects (issue5085)
|
|
# setuptools 8 expects this module to explode early when not on windows
|
|
"distutils.msvc9compiler",
|
|
"__builtin__",
|
|
"builtins",
|
|
"urwid.command_map", # for pudb
|
|
"_thread", # For RLock
|
|
]
|
|
|
|
_pypy = "__pypy__" in sys.builtin_module_names
|
|
|
|
if _pypy:
|
|
ignore.extend(
|
|
[
|
|
# _ctypes.pointer is shadowed by "from ... import pointer" (PyPy 5)
|
|
"_ctypes.pointer"
|
|
]
|
|
)
|
|
|
|
demandimport.init(ignore)
|
|
|
|
# Re-export.
|
|
isenabled = demandimport.isenabled
|
|
disable = demandimport.disable
|
|
deactivated = demandimport.deactivated
|
|
|
|
|
|
def enable():
|
|
# chg pre-imports modules so do not enable demandimport for it
|
|
if (
|
|
"CHGINTERNALMARK" not in os.environ
|
|
and os.environ.get("HGDEMANDIMPORT") != "disable"
|
|
):
|
|
demandimport.enable()
|
|
|
|
|
|
@contextlib.contextmanager
|
|
def disabled():
|
|
if isenabled():
|
|
disable()
|
|
try:
|
|
yield
|
|
finally:
|
|
enable()
|
|
else:
|
|
yield
|