Add `@try_except` decorator (#4224)
Browse files- utils/general.py +31 -22
utils/general.py
CHANGED
@@ -56,6 +56,17 @@ class timeout(contextlib.ContextDecorator):
|
|
56 |
return True
|
57 |
|
58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
def set_logging(rank=-1, verbose=True):
|
60 |
logging.basicConfig(
|
61 |
format="%(message)s",
|
@@ -114,26 +125,25 @@ def check_online():
|
|
114 |
return False
|
115 |
|
116 |
|
117 |
-
|
|
|
118 |
# Recommend 'git pull' if code is out of date
|
|
|
119 |
print(colorstr('github: '), end='')
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
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.6.2'):
|
@@ -148,15 +158,14 @@ def check_version(current='0.0.0', minimum='0.0.0', name='version ', pinned=Fals
|
|
148 |
assert result, f'{name}{minimum} required by YOLOv5, but {name}{current} is currently installed'
|
149 |
|
150 |
|
|
|
151 |
def check_requirements(requirements='requirements.txt', exclude=()):
|
152 |
# Check installed dependencies meet requirements (pass *.txt file or list of packages)
|
153 |
prefix = colorstr('red', 'bold', 'requirements:')
|
154 |
check_python() # check python version
|
155 |
if isinstance(requirements, (str, Path)): # requirements.txt file
|
156 |
file = Path(requirements)
|
157 |
-
|
158 |
-
print(f"{prefix} {file.resolve()} not found, check failed.")
|
159 |
-
return
|
160 |
requirements = [f'{x.name}{x.specifier}' for x in pkg.parse_requirements(file.open()) if x.name not in exclude]
|
161 |
else: # list or tuple of packages
|
162 |
requirements = [x for x in requirements if x not in exclude]
|
@@ -178,7 +187,7 @@ def check_requirements(requirements='requirements.txt', exclude=()):
|
|
178 |
source = file.resolve() if 'file' in locals() else requirements
|
179 |
s = f"{prefix} {n} package{'s' * (n > 1)} updated per {source}\n" \
|
180 |
f"{prefix} ⚠️ {colorstr('bold', 'Restart runtime or rerun command for updates to take effect')}\n"
|
181 |
-
print(emojis(s))
|
182 |
|
183 |
|
184 |
def check_img_size(img_size, s=32, floor=0):
|
|
|
56 |
return True
|
57 |
|
58 |
|
59 |
+
def try_except(func):
|
60 |
+
# try-except function. Usage: @try_except decorator
|
61 |
+
def handler(*args, **kwargs):
|
62 |
+
try:
|
63 |
+
func(*args, **kwargs)
|
64 |
+
except Exception as e:
|
65 |
+
print(e)
|
66 |
+
|
67 |
+
return handler
|
68 |
+
|
69 |
+
|
70 |
def set_logging(rank=-1, verbose=True):
|
71 |
logging.basicConfig(
|
72 |
format="%(message)s",
|
|
|
125 |
return False
|
126 |
|
127 |
|
128 |
+
@try_except
|
129 |
+
def check_git_status():
|
130 |
# Recommend 'git pull' if code is out of date
|
131 |
+
msg = ', for updates see https://github.com/ultralytics/yolov5'
|
132 |
print(colorstr('github: '), end='')
|
133 |
+
assert Path('.git').exists(), 'skipping check (not a git repository)' + msg
|
134 |
+
assert not is_docker(), 'skipping check (Docker image)' + msg
|
135 |
+
assert check_online(), 'skipping check (offline)' + msg
|
136 |
+
|
137 |
+
cmd = 'git fetch && git config --get remote.origin.url'
|
138 |
+
url = check_output(cmd, shell=True, timeout=5).decode().strip().rstrip('.git') # git fetch
|
139 |
+
branch = check_output('git rev-parse --abbrev-ref HEAD', shell=True).decode().strip() # checked out
|
140 |
+
n = int(check_output(f'git rev-list {branch}..origin/master --count', shell=True)) # commits behind
|
141 |
+
if n > 0:
|
142 |
+
s = f"⚠️ WARNING: code is out of date by {n} commit{'s' * (n > 1)}. " \
|
143 |
+
f"Use 'git pull' to update or 'git clone {url}' to download latest."
|
144 |
+
else:
|
145 |
+
s = f'up to date with {url} ✅'
|
146 |
+
print(emojis(s)) # emoji-safe
|
|
|
|
|
|
|
147 |
|
148 |
|
149 |
def check_python(minimum='3.6.2'):
|
|
|
158 |
assert result, f'{name}{minimum} required by YOLOv5, but {name}{current} is currently installed'
|
159 |
|
160 |
|
161 |
+
@try_except
|
162 |
def check_requirements(requirements='requirements.txt', exclude=()):
|
163 |
# Check installed dependencies meet requirements (pass *.txt file or list of packages)
|
164 |
prefix = colorstr('red', 'bold', 'requirements:')
|
165 |
check_python() # check python version
|
166 |
if isinstance(requirements, (str, Path)): # requirements.txt file
|
167 |
file = Path(requirements)
|
168 |
+
assert file.exists(), f"{prefix} {file.resolve()} not found, check failed."
|
|
|
|
|
169 |
requirements = [f'{x.name}{x.specifier}' for x in pkg.parse_requirements(file.open()) if x.name not in exclude]
|
170 |
else: # list or tuple of packages
|
171 |
requirements = [x for x in requirements if x not in exclude]
|
|
|
187 |
source = file.resolve() if 'file' in locals() else requirements
|
188 |
s = f"{prefix} {n} package{'s' * (n > 1)} updated per {source}\n" \
|
189 |
f"{prefix} ⚠️ {colorstr('bold', 'Restart runtime or rerun command for updates to take effect')}\n"
|
190 |
+
print(emojis(s))
|
191 |
|
192 |
|
193 |
def check_img_size(img_size, s=32, floor=0):
|