Skip to content

Commit

Permalink
Add error messages to trackusage
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelzwiers committed Feb 5, 2025
1 parent 4a78706 commit 3304cfb
Show file tree
Hide file tree
Showing 14 changed files with 29 additions and 23 deletions.
26 changes: 16 additions & 10 deletions bidscoin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,28 +173,32 @@ def run_command(command: str, success: tuple=(0,None)) -> int:
return process.returncode


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

# Collect the usage data
data = {'event': event,
'bidscoin': __version__,
'python': platform.python_version(),
'system': platform.system(),
'release': platform.release(),
'userid': hashlib.md5(getpass.getuser().encode('utf8')).hexdigest(),
'hostid': hashlib.md5(platform.node().encode('utf8')).hexdigest()}
if message:
data['message'] = str(message)

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

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

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

# Upload the usage data
Expand Down
2 changes: 1 addition & 1 deletion bidscoin/bcoin.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ def main():
reportcredits(args=args.credits)

except Exception as error:
trackusage('bidscoin_exception')
trackusage('bidscoin_exception', error)
raise error


Expand Down
2 changes: 1 addition & 1 deletion bidscoin/bidsapps/deface.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def main():
deface(**vars(args))

except Exception as error:
trackusage('deface_exception')
trackusage('deface_exception', error)
raise error


Expand Down
2 changes: 1 addition & 1 deletion bidscoin/bidsapps/echocombine.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def main():
echocombine(**vars(args))

except Exception as error:
trackusage('echocombine_exception')
trackusage('echocombine_exception', error)
raise error


Expand Down
2 changes: 1 addition & 1 deletion bidscoin/bidsapps/fixmeta.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def main():
fixmeta(**vars(args))

except Exception as error:
trackusage('fixmeta_exception')
trackusage('fixmeta_exception', error)
raise error


Expand Down
2 changes: 1 addition & 1 deletion bidscoin/bidsapps/medeface.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def main():
medeface(**vars(args))

except Exception as error:
trackusage('medeface_exception')
trackusage('medeface_exception', error)
raise error


Expand Down
2 changes: 1 addition & 1 deletion bidscoin/bidsapps/skullstrip.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ def main():
skullstrip(**vars(args))

except Exception as error:
trackusage('skullstrip_exception')
trackusage('skullstrip_exception', error)
raise error


Expand Down
2 changes: 1 addition & 1 deletion bidscoin/bidsapps/slicereport.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ def main():
slicereport(**vars(args))

except Exception as error:
trackusage('slicereport_exception')
trackusage('slicereport_exception', error)
raise error


Expand Down
2 changes: 1 addition & 1 deletion bidscoin/bidscoiner.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ def main():
bidscoiner(**vars(args))

except Exception as error:
trackusage('bidscoiner_exception')
trackusage('bidscoiner_exception', error)
raise error


Expand Down
2 changes: 1 addition & 1 deletion bidscoin/bidseditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2378,7 +2378,7 @@ def main():
bidseditor(**vars(args))

except Exception as error:
trackusage('bidseditor_exception')
trackusage('bidseditor_exception', error)
raise error


Expand Down
2 changes: 1 addition & 1 deletion bidscoin/bidsmapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ def main():
bidsmapper(**vars(args))

except Exception as error:
trackusage('bidsmapper_exception')
trackusage('bidsmapper_exception', error)
raise error


Expand Down
2 changes: 1 addition & 1 deletion bidscoin/utilities/bidsparticipants.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def main():
bidsparticipants(**vars(args))

except Exception as error:
trackusage('bidsparticipants_exception')
trackusage('bidsparticipants_exception', error)
raise error


Expand Down
2 changes: 1 addition & 1 deletion bidscoin/utilities/dicomsort.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def main():
sortsessions(**vars(args))

except Exception as error:
trackusage('dicomsort_exception')
trackusage('dicomsort_exception', error)
raise error


Expand Down
2 changes: 1 addition & 1 deletion bidscoin/utilities/rawmapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def main():
rawmapper(**vars(args))

except Exception as error:
trackusage('rawmapper_exception')
trackusage('rawmapper_exception', error)
raise error


Expand Down

0 comments on commit 3304cfb

Please sign in to comment.