sapling/edenscm/__init__.py
Jun Wu eaabc7f3c6 edenscm: move sys.path handling to top-level edenscm
Summary:
Make `import edenscm` take care of `sys.path` so as long as `import edenscm`
works, 3rd party pure Python dependencies and edenscmnative should be
importable.

This reduces adhoc sys.path handling in testutil.dott, and fixes an issue where
testing on Windows where `testuitl.dott` fails to run hg commands due to
missing 3rd party dependencies (because `edenscm.mercurial.entrypoint.run` is
not called, and edenscmdeps.zip is not in sys.path).

Reviewed By: sfilipco

Differential Revision: D16499458

fbshipit-source-id: 17e6e5754614dfcf352127d471c649ded4189e1a
2019-07-25 17:43:41 -07:00

44 lines
1.4 KiB
Python

# Copyright Facebook, Inc. 2019
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
def _fixsyspath():
"""Fix sys.path so core edenscm modules (edenscmnative, and 3rd party
libraries) are in sys.path
"""
# Do not expose those modules to edenscm.__dict__
import sys
import os
dirname = os.path.dirname
# __file__ is "hg/edenscm/__init__.py"
# libdir is "hg/"
# Do not follow symlinks (ex. do not use "realpath"). It breaks buck build.
libdir = dirname(dirname(os.path.abspath(__file__)))
# Make "edenscmdeps.zip" available in sys.path. It includes 3rd party
# pure-Python libraries like IPython, thrift runtime, etc.
#
# Note: On Windows, the released version of hg uses python27.zip for all
# pure Python modules including edenscm and everything in edenscmdeps.zip,
# so not being able to locate edenscmdeps.zip is not fatal.
for candidate in [libdir, os.path.join(libdir, "build")]:
depspath = os.path.join(candidate, "edenscmdeps.zip")
if os.path.exists(depspath) and depspath not in sys.path:
sys.path.insert(0, depspath)
# Make sure "edenscmnative" can be imported. Error early.
import edenscmnative
edenscmnative.__name__
_fixsyspath()
# Keep the module clean
del globals()["_fixsyspath"]