From f2eeb2960380346c4bf2cca39977166266f260b5 Mon Sep 17 00:00:00 2001 From: Mrinal Jain Date: Mon, 1 Nov 2021 18:16:35 -0400 Subject: [PATCH 1/8] Updating check_git_status() to switch to the repository root before performing git ops --- utils/general.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/utils/general.py b/utils/general.py index 872d5ce57c81..70c5a622c800 100755 --- a/utils/general.py +++ b/utils/general.py @@ -206,11 +206,13 @@ def check_online(): def check_git_status(): # Recommend 'git pull' if code is out of date msg = ', for updates see https://github.com/ultralytics/yolov5' + cwd = Path.cwd() print(colorstr('github: '), end='') - assert Path('.git').exists(), 'skipping check (not a git repository)' + msg + assert Path(ROOT / '.git').exists(), 'skipping check (not a git repository)' + msg assert not is_docker(), 'skipping check (Docker image)' + msg assert check_online(), 'skipping check (offline)' + msg + os.chdir(ROOT) # Changing the current working directory to YOLOv5 repository root cmd = 'git fetch && git config --get remote.origin.url' url = check_output(cmd, shell=True, timeout=5).decode().strip().rstrip('.git') # git fetch branch = check_output('git rev-parse --abbrev-ref HEAD', shell=True).decode().strip() # checked out @@ -220,6 +222,7 @@ def check_git_status(): else: s = f'up to date with {url} ✅' print(emojis(s)) # emoji-safe + os.chdir(cwd) # Reverting back to the original working directory def check_python(minimum='3.6.2'): From 310a3fb3615d57631977c198fcbfdb1dbf12a954 Mon Sep 17 00:00:00 2001 From: Mrinal Jain Date: Mon, 1 Nov 2021 23:07:47 -0400 Subject: [PATCH 2/8] More pythonic --- utils/general.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/utils/general.py b/utils/general.py index 70c5a622c800..6704fd5c20b8 100755 --- a/utils/general.py +++ b/utils/general.py @@ -80,6 +80,16 @@ def __exit__(self, exc_type, exc_val, exc_tb): if self.suppress and exc_type is TimeoutError: # Suppress TimeoutError return True +class ChangeWorkingDirectoryToRoot(contextlib.ContextDecorator): + # Usage: @ChangeWorkingDirectoryToRoot() decorator + # or 'with ChangeWorkingDirectoryToRoot():' context manager + def __enter__(self): + self.cwd = Path.cwd().resolve() + os.chdir(ROOT) + + def __exit__(self, exc_type, exc_val, exc_tb): + os.chdir(self.cwd) + def try_except(func): # try-except function. Usage: @try_except decorator @@ -206,23 +216,22 @@ def check_online(): def check_git_status(): # Recommend 'git pull' if code is out of date msg = ', for updates see https://github.com/ultralytics/yolov5' - cwd = Path.cwd() print(colorstr('github: '), end='') assert Path(ROOT / '.git').exists(), 'skipping check (not a git repository)' + msg assert not is_docker(), 'skipping check (Docker image)' + msg assert check_online(), 'skipping check (offline)' + msg - os.chdir(ROOT) # Changing the current working directory to YOLOv5 repository root - cmd = 'git fetch && git config --get remote.origin.url' - url = check_output(cmd, shell=True, timeout=5).decode().strip().rstrip('.git') # git fetch - branch = check_output('git rev-parse --abbrev-ref HEAD', shell=True).decode().strip() # checked out - n = int(check_output(f'git rev-list {branch}..origin/master --count', shell=True)) # commits behind + with ChangeWorkingDirectoryToRoot(): + cmd = 'git fetch && git config --get remote.origin.url' + url = check_output(cmd, shell=True, timeout=5).decode().strip().rstrip('.git') # git fetch + branch = check_output('git rev-parse --abbrev-ref HEAD', shell=True).decode().strip() # checked out + n = int(check_output(f'git rev-list {branch}..origin/master --count', shell=True)) # commits behind + if n > 0: s = f"⚠️ YOLOv5 is out of date by {n} commit{'s' * (n > 1)}. Use `git pull` or `git clone {url}` to update." else: s = f'up to date with {url} ✅' print(emojis(s)) # emoji-safe - os.chdir(cwd) # Reverting back to the original working directory def check_python(minimum='3.6.2'): From 051a075b615e4d2db3786724cc7d415043907b1f Mon Sep 17 00:00:00 2001 From: Mrinal Jain Date: Mon, 1 Nov 2021 23:11:36 -0400 Subject: [PATCH 3/8] Linting --- utils/general.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/general.py b/utils/general.py index 6704fd5c20b8..313dea4b053e 100755 --- a/utils/general.py +++ b/utils/general.py @@ -89,7 +89,7 @@ def __enter__(self): def __exit__(self, exc_type, exc_val, exc_tb): os.chdir(self.cwd) - + def try_except(func): # try-except function. Usage: @try_except decorator From 5d5a32c2488c3273f50d55e28b288037be2637a4 Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Tue, 2 Nov 2021 12:22:05 +0100 Subject: [PATCH 4/8] Remove redundant Path() call --- utils/general.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/general.py b/utils/general.py index 313dea4b053e..6b26cceccbc0 100755 --- a/utils/general.py +++ b/utils/general.py @@ -217,7 +217,7 @@ def check_git_status(): # Recommend 'git pull' if code is out of date msg = ', for updates see https://github.com/ultralytics/yolov5' print(colorstr('github: '), end='') - assert Path(ROOT / '.git').exists(), 'skipping check (not a git repository)' + msg + assert (ROOT / '.git').exists(), 'skipping check (not a git repository)' + msg assert not is_docker(), 'skipping check (Docker image)' + msg assert check_online(), 'skipping check (offline)' + msg From b1ccd47e5b6d02709204789aaa820f7b7b607288 Mon Sep 17 00:00:00 2001 From: Mrinal Jain Date: Tue, 2 Nov 2021 14:18:22 -0400 Subject: [PATCH 5/8] Generalizing the context manager --- utils/general.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/utils/general.py b/utils/general.py index 6b26cceccbc0..c695da1a150e 100755 --- a/utils/general.py +++ b/utils/general.py @@ -80,12 +80,13 @@ def __exit__(self, exc_type, exc_val, exc_tb): if self.suppress and exc_type is TimeoutError: # Suppress TimeoutError return True -class ChangeWorkingDirectoryToRoot(contextlib.ContextDecorator): - # Usage: @ChangeWorkingDirectoryToRoot() decorator - # or 'with ChangeWorkingDirectoryToRoot():' context manager - def __enter__(self): +class ChangeWorkingDirectory(contextlib.ContextDecorator): + # Temporarily change the current working directory to a different one. + # Usage: @ChangeWorkingDirectory(dir) decorator + # or 'with ChangeWorkingDirectory(dir):' context manager + def __enter__(self, dir): self.cwd = Path.cwd().resolve() - os.chdir(ROOT) + os.chdir(dir) def __exit__(self, exc_type, exc_val, exc_tb): os.chdir(self.cwd) @@ -221,7 +222,7 @@ def check_git_status(): assert not is_docker(), 'skipping check (Docker image)' + msg assert check_online(), 'skipping check (offline)' + msg - with ChangeWorkingDirectoryToRoot(): + with ChangeWorkingDirectory(ROOT): cmd = 'git fetch && git config --get remote.origin.url' url = check_output(cmd, shell=True, timeout=5).decode().strip().rstrip('.git') # git fetch branch = check_output('git rev-parse --abbrev-ref HEAD', shell=True).decode().strip() # checked out From df0259677bb613f32dcc70d9aa1c3f3953106a8c Mon Sep 17 00:00:00 2001 From: Mrinal Jain Date: Tue, 2 Nov 2021 14:39:29 -0400 Subject: [PATCH 6/8] Fixing error in context manager --- utils/general.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/utils/general.py b/utils/general.py index c695da1a150e..77ca46007af1 100755 --- a/utils/general.py +++ b/utils/general.py @@ -82,11 +82,14 @@ def __exit__(self, exc_type, exc_val, exc_tb): class ChangeWorkingDirectory(contextlib.ContextDecorator): # Temporarily change the current working directory to a different one. - # Usage: @ChangeWorkingDirectory(dir) decorator - # or 'with ChangeWorkingDirectory(dir):' context manager - def __enter__(self, dir): + # Usage: @ChangeWorkingDirectory(directory) decorator + # or 'with ChangeWorkingDirectory(directory):' context manager + def __init__(self, directory): + self.directory = directory self.cwd = Path.cwd().resolve() - os.chdir(dir) + + def __enter__(self): + os.chdir(self.directory) def __exit__(self, exc_type, exc_val, exc_tb): os.chdir(self.cwd) From 9915ef2819972aefee3da676e21f5a641cf56567 Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Tue, 2 Nov 2021 21:53:44 +0100 Subject: [PATCH 7/8] Cleanup --- utils/general.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/utils/general.py b/utils/general.py index 77ca46007af1..3499deeca928 100755 --- a/utils/general.py +++ b/utils/general.py @@ -80,16 +80,15 @@ def __exit__(self, exc_type, exc_val, exc_tb): if self.suppress and exc_type is TimeoutError: # Suppress TimeoutError return True -class ChangeWorkingDirectory(contextlib.ContextDecorator): - # Temporarily change the current working directory to a different one. - # Usage: @ChangeWorkingDirectory(directory) decorator - # or 'with ChangeWorkingDirectory(directory):' context manager - def __init__(self, directory): - self.directory = directory - self.cwd = Path.cwd().resolve() + +class WorkingDirectory(contextlib.ContextDecorator): + # Usage: @WorkingDirectory(dir) decorator or 'with WorkingDirectory(dir):' context manager + def __init__(self, new_dir): + self.dir = new_dir # new dir + self.cwd = Path.cwd().resolve() # current dir def __enter__(self): - os.chdir(self.directory) + os.chdir(self.dir) def __exit__(self, exc_type, exc_val, exc_tb): os.chdir(self.cwd) @@ -225,7 +224,7 @@ def check_git_status(): assert not is_docker(), 'skipping check (Docker image)' + msg assert check_online(), 'skipping check (offline)' + msg - with ChangeWorkingDirectory(ROOT): + with WorkingDirectory(ROOT): cmd = 'git fetch && git config --get remote.origin.url' url = check_output(cmd, shell=True, timeout=5).decode().strip().rstrip('.git') # git fetch branch = check_output('git rev-parse --abbrev-ref HEAD', shell=True).decode().strip() # checked out From dad104e419d4c96a89992d9963d3884d6a66ef92 Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Tue, 2 Nov 2021 22:10:30 +0100 Subject: [PATCH 8/8] Change to decorator --- utils/general.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/utils/general.py b/utils/general.py index 3499deeca928..d8cac8daac22 100755 --- a/utils/general.py +++ b/utils/general.py @@ -216,20 +216,19 @@ def check_online(): @try_except +@WorkingDirectory(ROOT) def check_git_status(): # Recommend 'git pull' if code is out of date msg = ', for updates see https://github.com/ultralytics/yolov5' print(colorstr('github: '), end='') - assert (ROOT / '.git').exists(), 'skipping check (not a git repository)' + msg + assert Path('.git').exists(), 'skipping check (not a git repository)' + msg assert not is_docker(), 'skipping check (Docker image)' + msg assert check_online(), 'skipping check (offline)' + msg - with WorkingDirectory(ROOT): - cmd = 'git fetch && git config --get remote.origin.url' - url = check_output(cmd, shell=True, timeout=5).decode().strip().rstrip('.git') # git fetch - branch = check_output('git rev-parse --abbrev-ref HEAD', shell=True).decode().strip() # checked out - n = int(check_output(f'git rev-list {branch}..origin/master --count', shell=True)) # commits behind - + cmd = 'git fetch && git config --get remote.origin.url' + url = check_output(cmd, shell=True, timeout=5).decode().strip().rstrip('.git') # git fetch + branch = check_output('git rev-parse --abbrev-ref HEAD', shell=True).decode().strip() # checked out + n = int(check_output(f'git rev-list {branch}..origin/master --count', shell=True)) # commits behind if n > 0: s = f"⚠️ YOLOv5 is out of date by {n} commit{'s' * (n > 1)}. Use `git pull` or `git clone {url}` to update." else: