Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Never modify sys.path #1720

Merged
merged 2 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this submodules definitely have no use anymore, and I can't wait to also see that happen for the tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If and when the tests are made no longer to need submodules--currently they do--and submodules are removed, then that material in the documentation should of course either be modified to no longer talk about the submodules, or removed.

```

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
Expand Down
29 changes: 0 additions & 29 deletions git/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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__ = [
Expand Down
24 changes: 22 additions & 2 deletions test/test_installation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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")