88from bids import BIDSLayout
99import importlib
1010import glob
11+ import sys
1112from platform import system
1213
1314# import shutil
3637 from .mideface import Mideface
3738 from .pet import WeightedAverage
3839
40+ telemetry_data = {
41+ "freesurfer_license" : None ,
42+ "docker_installed" : None ,
43+ "in_docker" : None ,
44+ "version" : None ,
45+ "subjects" : None ,
46+ "pet_niftis" : None ,
47+ "anat_niftis" : None ,
48+ "exit_status" : None ,
49+ }
50+ def sendstatsoncrash (type , value , tb ):
51+ """
52+ Sends telemetry data to openneuropet.org on crash
53+ """
54+ import traceback
55+ import requests
56+ #url = "http://openneuropet.org/petdeface"
57+ url = "http://127.0.0.1:8000/petdeface/"
58+ no_track = os .environ .get ("PETDEFACE_NO_TRACK" , "False" )
59+ trace_back_text = '' .join (traceback .format_exception (type , value , tb ))
60+ print (trace_back_text )
61+ if not no_track or no_track == "False" :
62+ try :
63+ requests .post (url , json = telemetry_data )
64+
65+ except :
66+ pass
67+ elif no_track == "True" or no_track == True :
68+ pass
69+
70+ sys .excepthook = sendstatsoncrash
3971
4072# collect version from pyproject.toml
4173places_to_look = [
5385 # we try to load the version using import lib
5486 try :
5587 __version__ = version (__package__ )
88+ # if version is not of the form x.y.z we try to load it from the pyproject.toml
89+ if not all ([x .isdigit () for x in __version__ .split ("." )]):
90+ raise ValueError
5691 except ValueError :
5792 # if we can't load the version using importlib we try to load it from the pyproject.toml
5893 for place in places_to_look :
65100 except FileNotFoundError :
66101 pass
67102
103+ telemetry_data ['version' ] = __version__
68104
69105def locate_freesurfer_license ():
70106 """
@@ -81,26 +117,31 @@ def locate_freesurfer_license():
81117 if os .environ .get ("FREESURFER_LICENSE" , "" ):
82118 fs_license_env_var = pathlib .Path (os .environ .get ("FREESURFER_LICENSE" , "" ))
83119 if not fs_license_env_var .exists ():
120+ telemetry_data ['freesurfer_license' ] = False
84121 raise ValueError (
85122 f"Freesurfer license file does not exist at { fs_license_env_var } , but is set under $FREESURFER_LICENSE variable."
86123 f"Update or unset this varible to use the license.txt at $FREESURFER_HOME"
87124 )
88125 else :
126+ telemetry_data ['freesurfer_license' ] = True
89127 return fs_license_env_var
90128 else :
91129 # collect freesurfer home environment variable and look there instead
92130 fs_home = pathlib .Path (os .environ .get ("FREESURFER_HOME" , "" ))
93131 if not fs_home :
132+ telemetry_data ['freesurfer_license' ] = False
94133 raise ValueError (
95134 "FREESURFER_HOME environment variable is not set, unable to determine location of license file"
96135 )
97136 else :
98137 fs_license = fs_home / pathlib .Path ("license.txt" )
99138 if not fs_license .exists ():
139+ telemetry_data ['freesurfer_license' ] = False
100140 raise ValueError (
101141 "Freesurfer license file does not exist at {}" .format (fs_license )
102142 )
103143 else :
144+ telemetry_data ['freesurfer_license' ] = True
104145 return fs_license
105146
106147
@@ -120,7 +161,9 @@ def check_docker_installed():
120161 check = True ,
121162 )
122163 docker_installed = True
164+ telemetry_data ["docker_installed" ] = docker_installed
123165 except subprocess .CalledProcessError :
166+ telemetry_data ["docker_installed" ] = False
124167 raise Exception ("Could not detect docker installation, exiting" )
125168 return docker_installed
126169
@@ -148,6 +191,7 @@ def determine_in_docker():
148191 for line in lines :
149192 if "bash" in line :
150193 in_docker = True
194+ telemetry_data ["in_docker" ] = True
151195 return in_docker
152196
153197
0 commit comments