Skip to content

Commit 64cb445

Browse files
committed
remove usage of pathlib.Path.__enter__
1 parent bb45ec1 commit 64cb445

File tree

1 file changed

+24
-3
lines changed
  • mkdocs_git_authors_plugin

1 file changed

+24
-3
lines changed

mkdocs_git_authors_plugin/ci.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,32 @@
77
"""
88

99
import os
10+
from contextlib import contextmanager
1011
import logging
1112

12-
from pathlib import Path
1313
from mkdocs_git_authors_plugin.git.command import GitCommand
1414

15+
@contextmanager
16+
def working_directory(path):
17+
"""
18+
Temporarily change working directory.
19+
A context manager which changes the working directory to the given
20+
path, and then changes it back to its previous value on exit.
21+
Usage:
22+
```python
23+
# Do something in original directory
24+
with working_directory('/my/new/path'):
25+
# Do something in new directory
26+
# Back to old directory
27+
```
28+
"""
29+
prev_cwd = os.getcwd()
30+
os.chdir(str(path))
31+
try:
32+
yield
33+
finally:
34+
os.chdir(prev_cwd)
35+
1536

1637
def raise_ci_warnings(path: str) -> None:
1738
"""
@@ -20,7 +41,7 @@ def raise_ci_warnings(path: str) -> None:
2041
Args:
2142
path (str): path to the root of the git repo
2243
"""
23-
with Path(path):
44+
with working_directory(path):
2445
if not is_shallow_clone():
2546
return None
2647

@@ -90,7 +111,7 @@ def commit_count() -> int:
90111
"""
91112
gc = GitCommand('rev-list',['--count','HEAD'])
92113
gc.run()
93-
n_commits = int(gc._stdout[0])
114+
n_commits = int(gc._stdout[0]) # type: ignore
94115
assert n_commits >= 0
95116
return n_commits
96117

0 commit comments

Comments
 (0)