@@ -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
0 commit comments