sapling/hg

42 lines
1.3 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env python
#
2006-02-06 07:21:02 +03:00
# mercurial - scalable distributed SCM
#
# Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
#
# This software may be used and distributed according to the terms of the
2010-01-20 07:20:08 +03:00
# GNU General Public License version 2 or any later version.
from __future__ import absolute_import
import os
import sys
hg: move hg entrypoint to the mercurial dir Summary: We want to have two possible Python entry points for Mercurial: - pure-python one (`hg/hg` script, the one which is renamed into `hg.real` during FB installation) - the one to be called from a Rust binary The reason we can't reuse the `hg/hg` to be called form the Rust binary is because this script will eventually not exist. We also need something in the known location, so we chose to put `entrypoint.py` into `mercurial/`. This means that both the `hg` script and the `hg.rust` binary need to be able to find the `mercurial/` directory on the system. Traditionally, `hg` contained a `LIBDIR@` string, which was replaced with the parent directory of a `mercurial/` dir at install time. Thus we could install Mercurial in non-standard locations if we needed to. We keep this functionality for the `hg` script. `mercurial/entrypoint.py` however knows that it lives under `mercurial/` and that `hgext/` and `hgdemandimport/` live alongside `mercurial/`. Thus, the parent dir of `mercurial/` is added as a first item of `sys,path` in `entrypoint.py`. In case `entrypoint.py` is called from a Rust binary, Python import logic also adds the entire `mercurial/` dir to `sys.path`. This is not desired, since we use `absolute_import` and want to only be able to import things as `from mercurial import smth`. A good example is `json`: even with `absolute_import` enabled `import json` loads `mercurial/json.py` if hg is called as a Rust binary. Therefore, we explicitly remove `mercurial/` from `sys.path`. Reviewed By: quark-zju Differential Revision: D9336157 fbshipit-source-id: 22f69e7782d549915c91ef9a0ad0ed29f62a9304
2018-08-17 20:40:01 +03:00
libdir = "@LIBDIR@"
if libdir != "@" "LIBDIR" "@":
# Installed as a script.
if not os.path.isabs(libdir):
hg: move hg entrypoint to the mercurial dir Summary: We want to have two possible Python entry points for Mercurial: - pure-python one (`hg/hg` script, the one which is renamed into `hg.real` during FB installation) - the one to be called from a Rust binary The reason we can't reuse the `hg/hg` to be called form the Rust binary is because this script will eventually not exist. We also need something in the known location, so we chose to put `entrypoint.py` into `mercurial/`. This means that both the `hg` script and the `hg.rust` binary need to be able to find the `mercurial/` directory on the system. Traditionally, `hg` contained a `LIBDIR@` string, which was replaced with the parent directory of a `mercurial/` dir at install time. Thus we could install Mercurial in non-standard locations if we needed to. We keep this functionality for the `hg` script. `mercurial/entrypoint.py` however knows that it lives under `mercurial/` and that `hgext/` and `hgdemandimport/` live alongside `mercurial/`. Thus, the parent dir of `mercurial/` is added as a first item of `sys,path` in `entrypoint.py`. In case `entrypoint.py` is called from a Rust binary, Python import logic also adds the entire `mercurial/` dir to `sys.path`. This is not desired, since we use `absolute_import` and want to only be able to import things as `from mercurial import smth`. A good example is `json`: even with `absolute_import` enabled `import json` loads `mercurial/json.py` if hg is called as a Rust binary. Therefore, we explicitly remove `mercurial/` from `sys.path`. Reviewed By: quark-zju Differential Revision: D9336157 fbshipit-source-id: 22f69e7782d549915c91ef9a0ad0ed29f62a9304
2018-08-17 20:40:01 +03:00
libdir = os.path.join(os.path.dirname(os.path.realpath(__file__)), libdir)
libdir = os.path.abspath(libdir)
sys.path.insert(0, libdir)
else:
# Not installed as a script.
# Check whether the correct interpreter is used.
try:
hgdir = os.path.dirname(os.path.realpath(__file__))
except NameError:
pass
else:
envpath = os.path.join(hgdir, "build", "env")
if os.path.exists(envpath):
with open(envpath, "r") as f:
env = dict(l.split("=", 1) for l in f.read().splitlines() if "=" in l)
# Use the right interpreter.
python = env.get("PYTHON_SYS_EXECUTABLE")
if python and python != sys.executable:
import subprocess
p = subprocess.Popen([python] + sys.argv)
sys.exit(p.wait())
hg: move hg entrypoint to the mercurial dir Summary: We want to have two possible Python entry points for Mercurial: - pure-python one (`hg/hg` script, the one which is renamed into `hg.real` during FB installation) - the one to be called from a Rust binary The reason we can't reuse the `hg/hg` to be called form the Rust binary is because this script will eventually not exist. We also need something in the known location, so we chose to put `entrypoint.py` into `mercurial/`. This means that both the `hg` script and the `hg.rust` binary need to be able to find the `mercurial/` directory on the system. Traditionally, `hg` contained a `LIBDIR@` string, which was replaced with the parent directory of a `mercurial/` dir at install time. Thus we could install Mercurial in non-standard locations if we needed to. We keep this functionality for the `hg` script. `mercurial/entrypoint.py` however knows that it lives under `mercurial/` and that `hgext/` and `hgdemandimport/` live alongside `mercurial/`. Thus, the parent dir of `mercurial/` is added as a first item of `sys,path` in `entrypoint.py`. In case `entrypoint.py` is called from a Rust binary, Python import logic also adds the entire `mercurial/` dir to `sys.path`. This is not desired, since we use `absolute_import` and want to only be able to import things as `from mercurial import smth`. A good example is `json`: even with `absolute_import` enabled `import json` loads `mercurial/json.py` if hg is called as a Rust binary. Therefore, we explicitly remove `mercurial/` from `sys.path`. Reviewed By: quark-zju Differential Revision: D9336157 fbshipit-source-id: 22f69e7782d549915c91ef9a0ad0ed29f62a9304
2018-08-17 20:40:01 +03:00
from mercurial import entrypoint
entrypoint.run(False)