Skip to content

Commit 3304cfb

Browse files
committed
Add error messages to trackusage
1 parent 4a78706 commit 3304cfb

File tree

14 files changed

+29
-23
lines changed

14 files changed

+29
-23
lines changed

bidscoin/__init__.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -173,28 +173,32 @@ def run_command(command: str, success: tuple=(0,None)) -> int:
173173
return process.returncode
174174

175175

176-
def trackusage(event: str, dryrun: bool=False) -> dict:
176+
def trackusage(event: str, message='', dryrun: bool=False) -> dict:
177177
"""Sends a url GET request with usage data parameters (if tracking is allowed and we are not asleep)
178178
179-
:param event: A label that describes the tracking event
180-
:param dryrun: Collect the usage data but don't actually send anything
181-
:return: The usage data
179+
:param event: A label that describes the tracking event
180+
:param message: An (error) message that is added to the usage data
181+
:param dryrun: Collect the usage data but don't actually send anything
182+
:return: The usage data
182183
"""
183184

185+
# Collect the usage data
184186
data = {'event': event,
185187
'bidscoin': __version__,
186188
'python': platform.python_version(),
187189
'system': platform.system(),
188190
'release': platform.release(),
189191
'userid': hashlib.md5(getpass.getuser().encode('utf8')).hexdigest(),
190192
'hostid': hashlib.md5(platform.node().encode('utf8')).hexdigest()}
193+
if message:
194+
data['message'] = str(message)
191195

192-
# Check if the user allows tracking, if it is a dry/pytest run, a DRMAA run, or if this is not a stable (#.#.#) version
196+
# Return if the user disallows tracking, if it is a dry-, pytest-, or a DRMAA-run, or if this is not a stable (#.#.#) version
193197
if not (os.getenv('BIDSCOIN_TRACKUSAGE') or config['bidscoin'].get('trackusage','yes')).upper() in ('1', 'TRUE', 'Y', 'YES') or dryrun \
194198
or "PYTEST_CURRENT_TEST" in os.environ or 'BIDSCOIN_JOB' in os.environ or re.match(r"^\d+\.\d+\.\d+$", __version__) is None:
195199
return data
196200

197-
# Check if we are not asleep
201+
# Return if we are asleep
198202
trackfile = configdir/'usage'/f"bidscoin_{data['userid']}"
199203
try:
200204
trackfile.parent.mkdir(parents=True, exist_ok=True)
@@ -205,13 +209,15 @@ def trackusage(event: str, dryrun: bool=False) -> dict:
205209
return data
206210
tracked[event] = now
207211

212+
# If something goes wrong, add an error message, clear the shelf and return
208213
except Exception as shelveerror:
209-
warnings.warn(f"Please report the following error to the developers:\n{shelveerror}: {trackfile}", RuntimeWarning)
210-
for corruptfile in shelvefiles := list(trackfile.parent.glob(trackfile.name + '.*')):
214+
data['event'] = 'trackusage_exception'
215+
data['message'] = f"({event}){shelveerror}"
216+
for corruptfile in (shelvefiles := list(trackfile.parent.glob(trackfile.name + '.*'))):
211217
print(f"Deleting corrupt file: {corruptfile}")
212218
corruptfile.unlink()
213-
data['event'] = 'trackusage_exception'
214-
if not shelvefiles: # Return without uploading (no shelve files means no sleep)
219+
if not shelvefiles: # Return without uploading (no shelve files means no sleep)
220+
warnings.warn(f"Please report the following error to the developers:\n{shelveerror}: {trackfile}", RuntimeWarning)
215221
return data
216222

217223
# Upload the usage data

bidscoin/bcoin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ def main():
655655
reportcredits(args=args.credits)
656656

657657
except Exception as error:
658-
trackusage('bidscoin_exception')
658+
trackusage('bidscoin_exception', error)
659659
raise error
660660

661661

bidscoin/bidsapps/deface.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ def main():
170170
deface(**vars(args))
171171

172172
except Exception as error:
173-
trackusage('deface_exception')
173+
trackusage('deface_exception', error)
174174
raise error
175175

176176

bidscoin/bidsapps/echocombine.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ def main():
209209
echocombine(**vars(args))
210210

211211
except Exception as error:
212-
trackusage('echocombine_exception')
212+
trackusage('echocombine_exception', error)
213213
raise error
214214

215215

bidscoin/bidsapps/fixmeta.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def main():
118118
fixmeta(**vars(args))
119119

120120
except Exception as error:
121-
trackusage('fixmeta_exception')
121+
trackusage('fixmeta_exception', error)
122122
raise error
123123

124124

bidscoin/bidsapps/medeface.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ def main():
218218
medeface(**vars(args))
219219

220220
except Exception as error:
221-
trackusage('medeface_exception')
221+
trackusage('medeface_exception', error)
222222
raise error
223223

224224

bidscoin/bidsapps/skullstrip.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ def main():
248248
skullstrip(**vars(args))
249249

250250
except Exception as error:
251-
trackusage('skullstrip_exception')
251+
trackusage('skullstrip_exception', error)
252252
raise error
253253

254254

bidscoin/bidsapps/slicereport.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ def main():
333333
slicereport(**vars(args))
334334

335335
except Exception as error:
336-
trackusage('slicereport_exception')
336+
trackusage('slicereport_exception', error)
337337
raise error
338338

339339

bidscoin/bidscoiner.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ def main():
325325
bidscoiner(**vars(args))
326326

327327
except Exception as error:
328-
trackusage('bidscoiner_exception')
328+
trackusage('bidscoiner_exception', error)
329329
raise error
330330

331331

bidscoin/bidseditor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2378,7 +2378,7 @@ def main():
23782378
bidseditor(**vars(args))
23792379

23802380
except Exception as error:
2381-
trackusage('bidseditor_exception')
2381+
trackusage('bidseditor_exception', error)
23822382
raise error
23832383

23842384

0 commit comments

Comments
 (0)