Update `check_git_status()` to run under `ROOT` working directory (#5441)
Browse files* Updating check_git_status() to switch to the repository root before performing git ops
* More pythonic
* Linting
* Remove redundant Path() call
* Generalizing the context manager
* Fixing error in context manager
* Cleanup
* Change to decorator
Co-authored-by: Glenn Jocher <[email protected]>
- utils/general.py +14 -0
utils/general.py
CHANGED
@@ -81,6 +81,19 @@ class Timeout(contextlib.ContextDecorator):
|
|
81 |
return True
|
82 |
|
83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
def try_except(func):
|
85 |
# try-except function. Usage: @try_except decorator
|
86 |
def handler(*args, **kwargs):
|
@@ -203,6 +216,7 @@ def check_online():
|
|
203 |
|
204 |
|
205 |
@try_except
|
|
|
206 |
def check_git_status():
|
207 |
# Recommend 'git pull' if code is out of date
|
208 |
msg = ', for updates see https://github.com/ultralytics/yolov5'
|
|
|
81 |
return True
|
82 |
|
83 |
|
84 |
+
class WorkingDirectory(contextlib.ContextDecorator):
|
85 |
+
# Usage: @WorkingDirectory(dir) decorator or 'with WorkingDirectory(dir):' context manager
|
86 |
+
def __init__(self, new_dir):
|
87 |
+
self.dir = new_dir # new dir
|
88 |
+
self.cwd = Path.cwd().resolve() # current dir
|
89 |
+
|
90 |
+
def __enter__(self):
|
91 |
+
os.chdir(self.dir)
|
92 |
+
|
93 |
+
def __exit__(self, exc_type, exc_val, exc_tb):
|
94 |
+
os.chdir(self.cwd)
|
95 |
+
|
96 |
+
|
97 |
def try_except(func):
|
98 |
# try-except function. Usage: @try_except decorator
|
99 |
def handler(*args, **kwargs):
|
|
|
216 |
|
217 |
|
218 |
@try_except
|
219 |
+
@WorkingDirectory(ROOT)
|
220 |
def check_git_status():
|
221 |
# Recommend 'git pull' if code is out of date
|
222 |
msg = ', for updates see https://github.com/ultralytics/yolov5'
|