From 00aee49d2027bdf9c0f1c8c2065aa1b67b5ecb2e Mon Sep 17 00:00:00 2001 From: Eliah Kagan Date: Fri, 20 Oct 2023 05:32:01 -0400 Subject: [PATCH 1/2] Never modify sys.path This removes the logic that, under some (most) circumstances when not using a version from PyPI, would insert the location of the gitdb git-submodule near the front of sys.path. (As noted in #1717, the specific way this was being done was not causing the git-submodule's version of gitdb to actually be used. But it was still modifying sys.path, which this now prevents.) The installation test, which had verified the insertion into sys.path, is modified accordingly, except that for now the check that the very first sys.path entry is undisturbed is kept in place. --- git/__init__.py | 29 ----------------------------- test/test_installation.py | 24 ++++++++++++++++++++++-- 2 files changed, 22 insertions(+), 31 deletions(-) diff --git a/git/__init__.py b/git/__init__.py index 05a02a7ff..5b55e59eb 100644 --- a/git/__init__.py +++ b/git/__init__.py @@ -6,38 +6,11 @@ # flake8: noqa # @PydevCodeAnalysisIgnore from git.exc import * # @NoMove @IgnorePep8 -import os -import os.path as osp -import sys - from typing import List, Optional, Sequence, Tuple, Union, TYPE_CHECKING from git.types import PathLike __version__ = "git" - -# { Initialization -def _init_externals() -> None: - """Initialize external projects by putting them into the path""" - if __version__ == "git" and "PYOXIDIZER" not in os.environ: - sys.path.insert(1, osp.join(osp.dirname(__file__), "ext", "gitdb")) - - try: - import gitdb - except ImportError as e: - raise ImportError("'gitdb' could not be found in your PYTHONPATH") from e - # END verify import - - -# } END initialization - - -################# -_init_externals() -################# - -# { Imports - from gitdb.util import to_hex_sha try: @@ -62,8 +35,6 @@ def _init_externals() -> None: except GitError as _exc: raise ImportError("%s: %s" % (_exc.__class__.__name__, _exc)) from _exc -# } END imports - # __all__ must be statically defined by py.typed support # __all__ = [name for name, obj in locals().items() if not (name.startswith("_") or inspect.ismodule(obj))] __all__ = [ diff --git a/test/test_installation.py b/test/test_installation.py index 0cb0c71fa..3a0e43973 100644 --- a/test/test_installation.py +++ b/test/test_installation.py @@ -25,6 +25,7 @@ def setUp_venv(self, rw_dir): @with_rw_directory def test_installation(self, rw_dir): self.setUp_venv(rw_dir) + result = subprocess.run( [self.pip, "install", "."], stdout=subprocess.PIPE, @@ -35,12 +36,32 @@ def test_installation(self, rw_dir): result.returncode, msg=result.stderr or result.stdout or "Can't install project", ) - result = subprocess.run([self.python, "-c", "import git"], stdout=subprocess.PIPE, cwd=self.sources) + + result = subprocess.run( + [self.python, "-c", "import git"], + stdout=subprocess.PIPE, + cwd=self.sources, + ) self.assertEqual( 0, result.returncode, msg=result.stderr or result.stdout or "Selftest failed", ) + + result = subprocess.run( + [self.python, "-c", "import gitdb; import smmap"], + stdout=subprocess.PIPE, + cwd=self.sources, + ) + self.assertEqual( + 0, + result.returncode, + msg=result.stderr or result.stdout or "Dependencies not installed", + ) + + # Even IF gitdb or any other dependency is supplied during development + # by inserting its location into PYTHONPATH or otherwise patched into + # sys.path, make sure it is not wrongly inserted as the *first* entry. result = subprocess.run( [self.python, "-c", "import sys;import git; print(sys.path)"], stdout=subprocess.PIPE, @@ -53,4 +74,3 @@ def test_installation(self, rw_dir): syspath[0], msg="Failed to follow the conventions for https://docs.python.org/3/library/sys.html#sys.path", ) - self.assertTrue(syspath[1].endswith("gitdb"), msg="Failed to add gitdb to sys.path") From 610c46ddf8c535d8a7bd75921fa8a215648f94ec Mon Sep 17 00:00:00 2001 From: Eliah Kagan Date: Fri, 20 Oct 2023 05:36:06 -0400 Subject: [PATCH 2/2] Document how to use vendored dependencies This adds a subsection to the end of the installation instructions in the readme to explain how to cause the versions of the gitdb and/or smmap dependencies that are vendored as submodules of the GitPython repository to be used, instead of the PyPI versions, in the infrequent case that this is desired. This goes along with he removal of the logic that conditionally modified sys.path, since that logic was intended to facilitate this (and at one time had). The approach now documented in the readme uses editable installs and does not involve modifying sys.path. Because the need for this is uncommon, it may end up being moved entirely into documentation in the doc/ directory in the future. --- README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/README.md b/README.md index a7942fd2f..889ea635f 100644 --- a/README.md +++ b/README.md @@ -97,6 +97,20 @@ pip install -e ".[test]" In the less common case that you do not want to install test dependencies, `pip install -e .` can be used instead. +#### With editable *dependencies* (not preferred, and rarely needed) + +In rare cases, you may want to work on GitPython and one or both of its [gitdb](https://github.com/gitpython-developers/gitdb) and [smmap](https://github.com/gitpython-developers/smmap) dependencies at the same time, with changes in your local working copy of gitdb or smmap immediatley reflected in the behavior of your local working copy of GitPython. This can be done by making editable installations of those dependencies in the same virtual environment where you install GitPython. + +If you want to do that *and* you want the versions in GitPython's git submodules to be used, then pass `-e git/ext/gitdb` and/or `-e git/ext/gitdb/gitdb/ext/smmap` to `pip install`. This can be done in any order, and in separate `pip install` commands or the same one, so long as `-e` appears before *each* path. For example, you can install GitPython, gitdb, and smmap editably in the currently active virtual environment this way: + +```bash +pip install -e ".[test]" -e git/ext/gitdb -e git/ext/gitdb/gitdb/ext/smmap +``` + +The submodules must have been cloned for that to work, but that will already be the case if you have run `./init-tests-after-clone.sh`. You can use `pip list` to check which packages are installed editably and which are installed normally. + +To reiterate, this approach should only rarely be used. For most development it is preferable to allow the gitdb and smmap dependencices to be retrieved automatically from PyPI in their latest stable packaged versions. + ### Limitations #### Leakage of System Resources