glenn-jocher commited on
Commit
563ea94
·
unverified ·
1 Parent(s): c37f072

Add `check_git_status()` 5 second timeout (#3464)

Browse files

* Add check_git_status() 5 second timeout

This should prevent the SSH Git bug that we were discussing @KalenMike

* cleanup

* replace timeout with check_output built-in timeout

Files changed (1) hide show
  1. utils/general.py +9 -9
utils/general.py CHANGED
@@ -9,12 +9,12 @@ import platform
9
  import random
10
  import re
11
  import signal
12
- import subprocess
13
  import time
14
  import urllib
15
  from itertools import repeat
16
  from multiprocessing.pool import ThreadPool
17
  from pathlib import Path
 
18
 
19
  import cv2
20
  import numpy as np
@@ -38,9 +38,9 @@ os.environ['NUMEXPR_MAX_THREADS'] = str(min(os.cpu_count(), 8)) # NumExpr max t
38
 
39
  class timeout(contextlib.ContextDecorator):
40
  # Usage: @timeout(seconds) decorator or 'with timeout(seconds):' context manager
41
- def __init__(self, seconds, *, timeout_message="", suppress_timeout_errors=True):
42
  self.seconds = int(seconds)
43
- self.timeout_message = timeout_message
44
  self.suppress = bool(suppress_timeout_errors)
45
 
46
  def _timeout_handler(self, signum, frame):
@@ -114,7 +114,7 @@ def check_online():
114
  return False
115
 
116
 
117
- def check_git_status():
118
  # Recommend 'git pull' if code is out of date
119
  print(colorstr('github: '), end='')
120
  try:
@@ -123,9 +123,9 @@ def check_git_status():
123
  assert check_online(), 'skipping check (offline)'
124
 
125
  cmd = 'git fetch && git config --get remote.origin.url'
126
- url = subprocess.check_output(cmd, shell=True).decode().strip().rstrip('.git') # github repo url
127
- branch = subprocess.check_output('git rev-parse --abbrev-ref HEAD', shell=True).decode().strip() # checked out
128
- n = int(subprocess.check_output(f'git rev-list {branch}..origin/master --count', shell=True)) # commits behind
129
  if n > 0:
130
  s = f"⚠️ WARNING: code is out of date by {n} commit{'s' * (n > 1)}. " \
131
  f"Use 'git pull' to update or 'git clone {url}' to download latest."
@@ -133,7 +133,7 @@ def check_git_status():
133
  s = f'up to date with {url} ✅'
134
  print(emojis(s)) # emoji-safe
135
  except Exception as e:
136
- print(e)
137
 
138
 
139
  def check_python(minimum='3.7.0', required=True):
@@ -166,7 +166,7 @@ def check_requirements(requirements='requirements.txt', exclude=()):
166
  n += 1
167
  print(f"{prefix} {r} not found and is required by YOLOv5, attempting auto-update...")
168
  try:
169
- print(subprocess.check_output(f"pip install '{r}'", shell=True).decode())
170
  except Exception as e:
171
  print(f'{prefix} {e}')
172
 
 
9
  import random
10
  import re
11
  import signal
 
12
  import time
13
  import urllib
14
  from itertools import repeat
15
  from multiprocessing.pool import ThreadPool
16
  from pathlib import Path
17
+ from subprocess import check_output
18
 
19
  import cv2
20
  import numpy as np
 
38
 
39
  class timeout(contextlib.ContextDecorator):
40
  # Usage: @timeout(seconds) decorator or 'with timeout(seconds):' context manager
41
+ def __init__(self, seconds, *, timeout_msg='', suppress_timeout_errors=True):
42
  self.seconds = int(seconds)
43
+ self.timeout_message = timeout_msg
44
  self.suppress = bool(suppress_timeout_errors)
45
 
46
  def _timeout_handler(self, signum, frame):
 
114
  return False
115
 
116
 
117
+ def check_git_status(err_msg=', for updates see https://github.com/ultralytics/yolov5'):
118
  # Recommend 'git pull' if code is out of date
119
  print(colorstr('github: '), end='')
120
  try:
 
123
  assert check_online(), 'skipping check (offline)'
124
 
125
  cmd = 'git fetch && git config --get remote.origin.url'
126
+ url = check_output(cmd, shell=True, timeout=5).decode().strip().rstrip('.git') # git fetch
127
+ branch = check_output('git rev-parse --abbrev-ref HEAD', shell=True).decode().strip() # checked out
128
+ n = int(check_output(f'git rev-list {branch}..origin/master --count', shell=True)) # commits behind
129
  if n > 0:
130
  s = f"⚠️ WARNING: code is out of date by {n} commit{'s' * (n > 1)}. " \
131
  f"Use 'git pull' to update or 'git clone {url}' to download latest."
 
133
  s = f'up to date with {url} ✅'
134
  print(emojis(s)) # emoji-safe
135
  except Exception as e:
136
+ print(f'{e}{err_msg}')
137
 
138
 
139
  def check_python(minimum='3.7.0', required=True):
 
166
  n += 1
167
  print(f"{prefix} {r} not found and is required by YOLOv5, attempting auto-update...")
168
  try:
169
+ print(check_output(f"pip install '{r}'", shell=True).decode())
170
  except Exception as e:
171
  print(f'{prefix} {e}')
172