scc_access.py

changeset 4
809c63be8a40
parent 1
783c8a0db76f
child 6
c02712d2ab9e
equal deleted inserted replaced
3:2f7cde6f836e 4:809c63be8a40
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 THE SOFTWARE. 23 THE SOFTWARE.
24 """ 24 """
25 25
26 __version__ = "0.5.0" 26 __version__ = "0.6.0"
27 27
28 28
29 # Try to read the settings from the settings.py file 29 # Try to read the settings from the settings.py file
30 try: 30 try:
31 from settings import * 31 from settings import *
32 except: 32 except:
33 raise ImportError( 33 raise ImportError(
34 """A settings file (setting.py) is required to run the script. 34 """A settings file (setting.py) is required to run the script.
35 You can use settings.sample.py for a template.""") 35 You can use settings.sample.py as a template.""")
36 36
37 37
38 import requests 38 import requests
39 import urlparse 39 import urlparse
40 import argparse 40 import argparse
50 LOGIN_URL = urlparse.urljoin(BASE_URL, 'accounts/login/') 50 LOGIN_URL = urlparse.urljoin(BASE_URL, 'accounts/login/')
51 UPLOAD_URL = urlparse.urljoin(BASE_URL, 'data_processing/measurements/quick/') 51 UPLOAD_URL = urlparse.urljoin(BASE_URL, 'data_processing/measurements/quick/')
52 DOWNLOAD_PREPROCESSED = urlparse.urljoin(BASE_URL, 'data_processing/measurements/{0}/download-preprocessed/') 52 DOWNLOAD_PREPROCESSED = urlparse.urljoin(BASE_URL, 'data_processing/measurements/{0}/download-preprocessed/')
53 DOWNLOAD_OPTICAL = urlparse.urljoin(BASE_URL, 'data_processing/measurements/{0}/download-optical/') 53 DOWNLOAD_OPTICAL = urlparse.urljoin(BASE_URL, 'data_processing/measurements/{0}/download-optical/')
54 DOWNLOAD_GRAPH = urlparse.urljoin(BASE_URL, 'data_processing/measurements/{0}/download-plots/') 54 DOWNLOAD_GRAPH = urlparse.urljoin(BASE_URL, 'data_processing/measurements/{0}/download-plots/')
55 RERUN_ALL = urlparse.urljoin(BASE_URL, 'data_processing/measurements/{0}/rerun-all/')
56 RERUN_PROCESSING = urlparse.urljoin(BASE_URL, 'data_processing/measurements/{0}/rerun-optical/')
57
55 DELETE_MEASUREMENT = urlparse.urljoin(BASE_URL, 'admin/database/measurements/{0}/delete/') 58 DELETE_MEASUREMENT = urlparse.urljoin(BASE_URL, 'admin/database/measurements/{0}/delete/')
56 API_BASE_URL = urlparse.urljoin(BASE_URL, 'api/v1/') 59 API_BASE_URL = urlparse.urljoin(BASE_URL, 'api/v1/')
57 60
58 # The regex to find the measurement id from the measurement page 61 # The regex to find the measurement id from the measurement page
59 # This should be read from the uploaded file, but would require an extra module 62 # This should be read from the uploaded file, but would require an extra module
179 def download_graphs(self, measurement_id): 182 def download_graphs(self, measurement_id):
180 """ Download profile graphs for the measurement id. """ 183 """ Download profile graphs for the measurement id. """
181 # Construct the download url 184 # Construct the download url
182 download_url = DOWNLOAD_GRAPH.format(measurement_id) 185 download_url = DOWNLOAD_GRAPH.format(measurement_id)
183 self.download_files(measurement_id, 'scc_plots', download_url) 186 self.download_files(measurement_id, 'scc_plots', download_url)
184 187
188 def rerun_processing(self, measurement_id, monitor=True):
189 measurement = self.get_measurement(measurement_id)
190
191 if measurement:
192 request = self.session.get(measurement.rerun_processing_url, auth=self.auth,
193 verify=False,
194 stream=True)
195
196 if request.status_code != 200:
197 print "Could not rerun processing for %s. Status code: %s" % (measurement_id, request.status_code)
198 return
199
200 if monitor:
201 self.monitor_processing(measurement_id)
202
203 def rerun_all(self, measurement_id, monitor=True):
204 measurement = self.get_measurement(measurement_id)
205
206 if measurement:
207 request = self.session.get(measurement.rerun_all_url, auth=self.auth,
208 verify=False,
209 stream=True)
210
211 if request.status_code != 200:
212 print "Could not rerun pre processing for %s. Status code: %s" % (measurement_id, request.status_code)
213 return
214
215 if monitor:
216 self.monitor_processing(measurement_id)
217
185 def process(self, filename, system_id): 218 def process(self, filename, system_id):
186 """ Upload a file for processing and wait for the processing to finish. 219 """ Upload a file for processing and wait for the processing to finish.
187 If the processing is successful, it will download all produced files. 220 If the processing is successful, it will download all produced files.
188 """ 221 """
189 print "--- Processing started on %s. ---" % datetime.datetime.now() 222 print "--- Processing started on %s. ---" % datetime.datetime.now()
190 # Upload file 223 # Upload file
191 measurement_id = self.upload_file(filename, system_id) 224 measurement_id = self.upload_file(filename, system_id)
192 225
193 measurement = None 226 measurement = self.monitor_processing(measurement_id)
194 if measurement_id: 227 return measurement
195 measurement = self.get_measurement(measurement_id) 228
229 def monitor_processing(self, measurement_id):
230 """ Monitor the processing progress of a measurement id"""
231
232 measurement = self.get_measurement(measurement_id)
233 if measurement is not None:
196 while measurement.is_running: 234 while measurement.is_running:
197 print "Measurement is being processed (status: %s, %s, %s). Please wait." % (measurement.upload, 235 print "Measurement is being processed (status: %s, %s, %s). Please wait." % (measurement.upload,
198 measurement.pre_processing, 236 measurement.pre_processing,
199 measurement.opt_retrievals) 237 measurement.opt_retrievals)
200 time.sleep(10) 238 time.sleep(10)
201 measurement = self.get_measurement(measurement_id) 239 measurement = self.get_measurement(measurement_id)
202 print "Measurement processing finished (status: %s, %s, %s)." % (measurement.upload, 240 print "Measurement processing finished (status: %s, %s, %s)." % (measurement.upload,
203 measurement.pre_processing, 241 measurement.pre_processing,
204 measurement.opt_retrievals) 242 measurement.opt_retrievals)
205 if measurement.pre_processing == 127: 243 if measurement.pre_processing == 127:
206 print "Downloading preprocessed files." 244 print "Downloading preprocessed files."
207 self.download_preprocessed(measurement_id) 245 self.download_preprocessed(measurement_id)
208 if measurement.opt_retrievals == 127: 246 if measurement.opt_retrievals == 127:
209 print "Downloading optical files." 247 print "Downloading optical files."
210 self.download_optical(measurement_id) 248 self.download_optical(measurement_id)
211 print "Downloading graphs." 249 print "Downloading graphs."
212 self.download_graphs(measurement_id) 250 self.download_graphs(measurement_id)
213 print "--- Processing finished. ---" 251 print "--- Processing finished. ---"
214 return measurement 252 return measurement
215 253
216 def get_status(self, measurement_id): 254 def get_status(self, measurement_id):
217 """ Get the processing status for a measurement id through the API. """ 255 """ Get the processing status for a measurement id through the API. """
218 measurement_url = urlparse.urljoin(API_BASE_URL, 'measurements/?id__exact=%s' % measurement_id) 256 measurement_url = urlparse.urljoin(API_BASE_URL, 'measurements/?id__exact=%s' % measurement_id)
219 257
220 response = self.session.get(measurement_url, 258 response = self.session.get(measurement_url,
367 if self.pre_processing == 127: 405 if self.pre_processing == 127:
368 if self.opt_retrievals in [127, -127]: 406 if self.opt_retrievals in [127, -127]:
369 return False 407 return False
370 return True 408 return True
371 409
372 def delete(self): 410 def rerun_processing_url(self):
373 """ Delete the entry from the SCC database. """ 411 return RERUN_PROCESSING.format(self.id)
374 412
375 413 def rerun_all_url(self):
414 return RERUN_ALL.format(self.id)
415
376 def __str__(self): 416 def __str__(self):
377 return "%s: %s, %s, %s" % (self.id, 417 return "%s: %s, %s, %s" % (self.id,
378 self.upload, 418 self.upload,
379 self.pre_processing, 419 self.pre_processing,
380 self.opt_retrievals) 420 self.opt_retrievals)
401 scc = SCC(auth) 441 scc = SCC(auth)
402 scc.login(credential) 442 scc.login(credential)
403 scc.delete_measurement(measurement_id) 443 scc.delete_measurement(measurement_id)
404 scc.logout() 444 scc.logout()
405 445
446 def rerun_all(measurement_id, monitor, auth = BASIC_LOGIN, credential = DJANGO_LOGIN):
447 """ Shortcut function to delete a measurement from the SCC. """
448 scc = SCC(auth)
449 scc.login(credential)
450 scc.rerun_all(measurement_id, monitor)
451 scc.logout()
452
453 def rerun_processing(measurement_id, monitor, auth = BASIC_LOGIN, credential = DJANGO_LOGIN):
454 """ Shortcut function to delete a measurement from the SCC. """
455 scc = SCC(auth)
456 scc.login(credential)
457 scc.rerun_processing(measurement_id, monitor)
458 scc.logout()
459
406 # When running through terminal 460 # When running through terminal
407 if __name__ == '__main__': 461 if __name__ == '__main__':
408 462
409 # Define the command line arguments. 463 # Define the command line arguments.
410 parser = argparse.ArgumentParser() 464 parser = argparse.ArgumentParser()
411 parser.add_argument("filename", nargs='?', help = "Measurement file name or path.", default='') 465 parser.add_argument("filename", nargs='?', help = "Measurement file name or path.", default='')
412 parser.add_argument("system", nargs='?', help = "Processing system id.", default=0) 466 parser.add_argument("system", nargs='?', help = "Processing system id.", default=0)
413 parser.add_argument("-p", "--process", help="Wait for the results of the processing.", 467 parser.add_argument("-p", "--process", help="Wait for the results of the processing.",
414 action="store_true") 468 action="store_true")
415 parser.add_argument("-d", "--delete", help="Measurement ID to delete.") 469 parser.add_argument("-d", "--delete", help="Measurement ID to delete.")
470 parser.add_argument("--rerun-all", help="Measurement ID to rerun.")
471 parser.add_argument("--rerun-processing", help="Measurement ID to rerun processing routings.")
416 args = parser.parse_args() 472 args = parser.parse_args()
417 473
418 # If the arguments are OK, try to login on the site and upload. 474 # If the arguments are OK, try to login on the site and upload.
419 if args.delete: 475 if args.delete:
420 # If the delete is provided, do nothing else 476 # If the delete is provided, do nothing else
421 delete_measurement(args.delete) 477 delete_measurement(args.delete)
478 elif args.rerun_all:
479 rerun_all(args.rerun_all, args.process)
480 elif args.rerun_processing:
481 rerun_processing(args.rerun_processing, args.process)
422 else: 482 else:
423 if (args.filename == '') or (args.system == 0): 483 if (args.filename == '') or (args.system == 0):
424 parser.error('Provide a valid filename and system parameters.\nRun with -h for help.\n') 484 parser.error('Provide a valid filename and system parameters.\nRun with -h for help.\n')
425 485
426 if args.process: 486 if args.process:

mercurial