Fri, 24 Nov 2017 18:50:29 +0200
Updated script according to new database/api changes.
Added basic logging functions, instead of print statements.
ioannis@0 | 1 | #!/usr/bin/env python |
ioannis@0 | 2 | """ |
ioannis@0 | 3 | The MIT License (MIT) |
ioannis@0 | 4 | |
ioannis@0 | 5 | Copyright (c) 2015, Ioannis Binietoglou |
ioannis@0 | 6 | |
ioannis@0 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy |
ioannis@0 | 8 | of this software and associated documentation files (the "Software"), to deal |
ioannis@0 | 9 | in the Software without restriction, including without limitation the rights |
ioannis@0 | 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
ioannis@0 | 11 | copies of the Software, and to permit persons to whom the Software is |
ioannis@0 | 12 | furnished to do so, subject to the following conditions: |
ioannis@0 | 13 | |
ioannis@0 | 14 | The above copyright notice and this permission notice shall be included in |
ioannis@0 | 15 | all copies or substantial portions of the Software. |
ioannis@0 | 16 | |
ioannis@0 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
ioannis@0 | 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
ioannis@0 | 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
ioannis@0 | 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
ioannis@0 | 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
ioannis@0 | 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
ioannis@0 | 23 | THE SOFTWARE. |
ioannis@0 | 24 | """ |
ioannis@0 | 25 | |
ulalume3@4 | 26 | __version__ = "0.6.0" |
ioannis@1 | 27 | |
ioannis@0 | 28 | # Try to read the settings from the settings.py file |
ioannis@0 | 29 | try: |
ioannis@0 | 30 | from settings import * |
ioannis@0 | 31 | except: |
ioannis@0 | 32 | raise ImportError( |
ulalume3@6 | 33 | """A settings file (setting.py) is required to run the script. |
ulalume3@6 | 34 | You can use settings.sample.py as a template.""") |
ioannis@0 | 35 | |
ioannis@0 | 36 | import requests |
ulalume3@6 | 37 | requests.packages.urllib3.disable_warnings() |
ulalume3@6 | 38 | |
ioannis@0 | 39 | import urlparse |
ioannis@0 | 40 | import argparse |
ioannis@0 | 41 | import os |
ioannis@0 | 42 | import re |
ioannis@0 | 43 | import time |
ioannis@0 | 44 | import StringIO |
ioannis@0 | 45 | from zipfile import ZipFile |
ioannis@0 | 46 | import datetime |
ulalume3@6 | 47 | import logging |
ulalume3@6 | 48 | |
ulalume3@6 | 49 | logger = logging.getLogger(__name__) |
ioannis@0 | 50 | |
ioannis@0 | 51 | |
ioannis@0 | 52 | # Construct the absolute URLs |
ioannis@0 | 53 | LOGIN_URL = urlparse.urljoin(BASE_URL, 'accounts/login/') |
ioannis@0 | 54 | UPLOAD_URL = urlparse.urljoin(BASE_URL, 'data_processing/measurements/quick/') |
ioannis@0 | 55 | DOWNLOAD_PREPROCESSED = urlparse.urljoin(BASE_URL, 'data_processing/measurements/{0}/download-preprocessed/') |
ioannis@0 | 56 | DOWNLOAD_OPTICAL = urlparse.urljoin(BASE_URL, 'data_processing/measurements/{0}/download-optical/') |
ioannis@0 | 57 | DOWNLOAD_GRAPH = urlparse.urljoin(BASE_URL, 'data_processing/measurements/{0}/download-plots/') |
ulalume3@4 | 58 | RERUN_ALL = urlparse.urljoin(BASE_URL, 'data_processing/measurements/{0}/rerun-all/') |
ulalume3@4 | 59 | RERUN_PROCESSING = urlparse.urljoin(BASE_URL, 'data_processing/measurements/{0}/rerun-optical/') |
ulalume3@4 | 60 | |
ulalume3@6 | 61 | DELETE_MEASUREMENT = urlparse.urljoin(BASE_URL, 'admin/database/measurements/{0}/delete/') |
ioannis@0 | 62 | API_BASE_URL = urlparse.urljoin(BASE_URL, 'api/v1/') |
ioannis@0 | 63 | |
ioannis@0 | 64 | # The regex to find the measurement id from the measurement page |
ulalume3@6 | 65 | # This should be read from the uploaded file, but would require an extra NetCDF module. |
ioannis@0 | 66 | regex = "<h3>Measurement (?P<measurement_id>.{12}) <small>" |
ioannis@0 | 67 | |
ioannis@0 | 68 | |
ioannis@0 | 69 | class SCC: |
ioannis@0 | 70 | """ A simple class that will attempt to upload a file on the SCC server. |
ulalume3@6 | 71 | The uploading is done by simulating a normal browser session. In the current |
ioannis@0 | 72 | version no check is performed, and no feedback is given if the upload |
ioannis@0 | 73 | was successful. If everything is setup correctly, it will work. |
ioannis@0 | 74 | """ |
ulalume3@6 | 75 | |
ulalume3@6 | 76 | def __init__(self, auth=BASIC_LOGIN, output_dir=OUTPUT_DIR): |
ioannis@0 | 77 | self.auth = auth |
ulalume3@6 | 78 | self.output_dir = output_dir |
ioannis@0 | 79 | self.session = requests.Session() |
ulalume3@6 | 80 | |
ulalume3@6 | 81 | def login(self, credentials=DJANGO_LOGIN): |
ioannis@0 | 82 | """ Login the the website. """ |
ulalume3@6 | 83 | logger.debug("Attempting to login to SCC, username %s." % credentials[0]) |
ulalume3@6 | 84 | self.login_credentials = {'username': credentials[0], |
ulalume3@6 | 85 | 'password': credentials[1]} |
ulalume3@6 | 86 | |
ulalume3@6 | 87 | logger.debug("Accessing login page at %s." % LOGIN_URL) |
ulalume3@6 | 88 | |
ioannis@0 | 89 | # Get upload form |
ulalume3@6 | 90 | login_page = self.session.get(LOGIN_URL, |
ulalume3@6 | 91 | auth=self.auth, verify=False) |
ulalume3@6 | 92 | |
ulalume3@6 | 93 | logger.debug("Submiting credentials.") |
ioannis@0 | 94 | # Submit the login data |
ulalume3@6 | 95 | login_submit = self.session.post(LOGIN_URL, |
ulalume3@6 | 96 | data=self.login_credentials, |
ulalume3@6 | 97 | headers={'X-CSRFToken': login_page.cookies['csrftoken'], |
ulalume3@6 | 98 | 'referer': LOGIN_URL}, |
ulalume3@6 | 99 | verify=False, |
ulalume3@6 | 100 | auth=self.auth) |
ioannis@0 | 101 | return login_submit |
ioannis@0 | 102 | |
ioannis@0 | 103 | def logout(self): |
ioannis@0 | 104 | pass |
ulalume3@6 | 105 | |
ioannis@0 | 106 | def upload_file(self, filename, system_id): |
ioannis@0 | 107 | """ Upload a filename for processing with a specific system. If the |
ioannis@0 | 108 | upload is successful, it returns the measurement id. """ |
ioannis@0 | 109 | # Get submit page |
ulalume3@6 | 110 | upload_page = self.session.get(UPLOAD_URL, |
ulalume3@6 | 111 | auth=self.auth, |
ulalume3@6 | 112 | verify=False) |
ulalume3@6 | 113 | |
ulalume3@6 | 114 | # Submit the data |
ioannis@0 | 115 | upload_data = {'system': system_id} |
ulalume3@6 | 116 | files = {'data': open(filename, 'rb')} |
ulalume3@6 | 117 | |
ulalume3@6 | 118 | logging.info("Uploading of file %s started." % filename) |
ulalume3@6 | 119 | |
ulalume3@6 | 120 | upload_submit = self.session.post(UPLOAD_URL, |
ulalume3@6 | 121 | data=upload_data, |
ulalume3@6 | 122 | files=files, |
ulalume3@6 | 123 | headers={'X-CSRFToken': upload_page.cookies['csrftoken'], |
ulalume3@6 | 124 | 'referer': UPLOAD_URL}, |
ulalume3@6 | 125 | verify=False, |
ulalume3@6 | 126 | auth=self.auth) |
ulalume3@6 | 127 | |
ioannis@0 | 128 | if upload_submit.status_code != 200: |
ulalume3@6 | 129 | logging.warning("Connection error. Status code: %s" % upload_submit.status_code) |
ioannis@0 | 130 | return False |
ulalume3@6 | 131 | |
ioannis@0 | 132 | # Check if there was a redirect to a new page. |
ioannis@0 | 133 | if upload_submit.url == UPLOAD_URL: |
ioannis@0 | 134 | measurement_id = False |
ulalume3@6 | 135 | logging.error("Uploaded file rejected! Try to upload manually to see the error.") |
ioannis@0 | 136 | else: |
ioannis@0 | 137 | measurement_id = re.findall(regex, upload_submit.text)[0] |
ulalume3@6 | 138 | logging.error("Successfully uploaded measurement with id %s." % measurement_id) |
ioannis@0 | 139 | |
ioannis@0 | 140 | return measurement_id |
ulalume3@6 | 141 | |
ioannis@0 | 142 | def download_files(self, measurement_id, subdir, download_url): |
ioannis@0 | 143 | """ Downloads some files from the download_url to the specified |
ioannis@0 | 144 | subdir. This method is used to download preprocessed file, optical |
ioannis@0 | 145 | files etc. |
ulalume3@6 | 146 | """ |
ioannis@0 | 147 | # Get the file |
ulalume3@6 | 148 | request = self.session.get(download_url, auth=self.auth, |
ulalume3@6 | 149 | verify=False, |
ulalume3@6 | 150 | stream=True) |
ulalume3@6 | 151 | |
ioannis@0 | 152 | # Create the dir if it does not exist |
ioannis@0 | 153 | local_dir = os.path.join(self.output_dir, measurement_id, subdir) |
ioannis@0 | 154 | if not os.path.exists(local_dir): |
ioannis@0 | 155 | os.makedirs(local_dir) |
ulalume3@6 | 156 | |
ioannis@0 | 157 | # Save the file by chunk, needed if the file is big. |
ioannis@0 | 158 | memory_file = StringIO.StringIO() |
ulalume3@6 | 159 | |
ulalume3@6 | 160 | for chunk in request.iter_content(chunk_size=1024): |
ulalume3@6 | 161 | if chunk: # filter out keep-alive new chunks |
ioannis@0 | 162 | memory_file.write(chunk) |
ioannis@0 | 163 | memory_file.flush() |
ulalume3@6 | 164 | |
ioannis@0 | 165 | zip_file = ZipFile(memory_file) |
ulalume3@6 | 166 | |
ioannis@0 | 167 | for ziped_name in zip_file.namelist(): |
ioannis@0 | 168 | basename = os.path.basename(ziped_name) |
ulalume3@6 | 169 | |
ioannis@0 | 170 | local_file = os.path.join(local_dir, basename) |
ulalume3@6 | 171 | |
ioannis@0 | 172 | with open(local_file, 'wb') as f: |
ioannis@0 | 173 | f.write(zip_file.read(ziped_name)) |
ioannis@0 | 174 | |
ioannis@0 | 175 | def download_preprocessed(self, measurement_id): |
ioannis@0 | 176 | """ Download preprocessed files for the measurement id. """ |
ioannis@0 | 177 | # Construct the download url |
ioannis@0 | 178 | download_url = DOWNLOAD_PREPROCESSED.format(measurement_id) |
ioannis@0 | 179 | self.download_files(measurement_id, 'scc_preprocessed', download_url) |
ulalume3@6 | 180 | |
ioannis@0 | 181 | def download_optical(self, measurement_id): |
ioannis@0 | 182 | """ Download optical files for the measurement id. """ |
ioannis@0 | 183 | # Construct the download url |
ioannis@0 | 184 | download_url = DOWNLOAD_OPTICAL.format(measurement_id) |
ioannis@0 | 185 | self.download_files(measurement_id, 'scc_optical', download_url) |
ulalume3@6 | 186 | |
ioannis@0 | 187 | def download_graphs(self, measurement_id): |
ioannis@0 | 188 | """ Download profile graphs for the measurement id. """ |
ioannis@0 | 189 | # Construct the download url |
ioannis@0 | 190 | download_url = DOWNLOAD_GRAPH.format(measurement_id) |
ioannis@0 | 191 | self.download_files(measurement_id, 'scc_plots', download_url) |
ulalume3@4 | 192 | |
ulalume3@4 | 193 | def rerun_processing(self, measurement_id, monitor=True): |
ulalume3@4 | 194 | measurement = self.get_measurement(measurement_id) |
ulalume3@4 | 195 | |
ulalume3@4 | 196 | if measurement: |
ulalume3@4 | 197 | request = self.session.get(measurement.rerun_processing_url, auth=self.auth, |
ulalume3@4 | 198 | verify=False, |
ulalume3@4 | 199 | stream=True) |
ulalume3@4 | 200 | |
ulalume3@4 | 201 | if request.status_code != 200: |
ulalume3@6 | 202 | logging.error("Could not rerun processing for %s. Status code: %s" % (measurement_id, request.status_code)) |
ulalume3@4 | 203 | return |
ulalume3@4 | 204 | |
ulalume3@4 | 205 | if monitor: |
ulalume3@4 | 206 | self.monitor_processing(measurement_id) |
ulalume3@4 | 207 | |
ulalume3@4 | 208 | def rerun_all(self, measurement_id, monitor=True): |
ulalume3@6 | 209 | logger.debug("Started rerun_all procedure.") |
ulalume3@6 | 210 | |
ulalume3@6 | 211 | logger.debug("Getting measurement %s" % measurement_id) |
ulalume3@4 | 212 | measurement = self.get_measurement(measurement_id) |
ulalume3@4 | 213 | |
ulalume3@4 | 214 | if measurement: |
ulalume3@6 | 215 | logger.debug("Attempting to rerun all processing through %s." % measurement.rerun_all_url) |
ulalume3@6 | 216 | |
ulalume3@4 | 217 | request = self.session.get(measurement.rerun_all_url, auth=self.auth, |
ulalume3@4 | 218 | verify=False, |
ulalume3@4 | 219 | stream=True) |
ulalume3@4 | 220 | |
ulalume3@4 | 221 | if request.status_code != 200: |
ulalume3@6 | 222 | logger.error("Could not rerun pre processing for %s. Status code: %s" % |
ulalume3@6 | 223 | (measurement_id, request.status_code)) |
ulalume3@4 | 224 | return |
ulalume3@4 | 225 | |
ulalume3@4 | 226 | if monitor: |
ulalume3@4 | 227 | self.monitor_processing(measurement_id) |
ulalume3@4 | 228 | |
ioannis@0 | 229 | def process(self, filename, system_id): |
ioannis@0 | 230 | """ Upload a file for processing and wait for the processing to finish. |
ioannis@0 | 231 | If the processing is successful, it will download all produced files. |
ioannis@0 | 232 | """ |
ulalume3@6 | 233 | logger.info("--- Processing started on %s. ---" % datetime.datetime.now()) |
ioannis@0 | 234 | # Upload file |
ulalume3@4 | 235 | measurement_id = self.upload_file(filename, system_id) |
ulalume3@4 | 236 | |
ulalume3@4 | 237 | measurement = self.monitor_processing(measurement_id) |
ulalume3@4 | 238 | return measurement |
ulalume3@4 | 239 | |
ulalume3@4 | 240 | def monitor_processing(self, measurement_id): |
ulalume3@4 | 241 | """ Monitor the processing progress of a measurement id""" |
ulalume3@4 | 242 | |
ulalume3@4 | 243 | measurement = self.get_measurement(measurement_id) |
ulalume3@4 | 244 | if measurement is not None: |
ioannis@0 | 245 | while measurement.is_running: |
ulalume3@6 | 246 | logger.info("Measurement is being processed (status: %s, %s, %s). Please wait." % (measurement.upload, |
ulalume3@4 | 247 | measurement.pre_processing, |
ulalume3@6 | 248 | measurement.processing)) |
ioannis@0 | 249 | time.sleep(10) |
ioannis@0 | 250 | measurement = self.get_measurement(measurement_id) |
ulalume3@6 | 251 | logger.info("Measurement processing finished (status: %s, %s, %s)." % (measurement.upload, |
ulalume3@4 | 252 | measurement.pre_processing, |
ulalume3@6 | 253 | measurement.processing)) |
ioannis@0 | 254 | if measurement.pre_processing == 127: |
ulalume3@6 | 255 | logger.info("Downloading preprocessed files.") |
ioannis@0 | 256 | self.download_preprocessed(measurement_id) |
ulalume3@6 | 257 | if measurement.processing == 127: |
ulalume3@6 | 258 | logger.info("Downloading optical files.") |
ioannis@0 | 259 | self.download_optical(measurement_id) |
ulalume3@6 | 260 | logger.info("Downloading graphs.") |
ioannis@0 | 261 | self.download_graphs(measurement_id) |
ulalume3@6 | 262 | logger.info("--- Processing finished. ---") |
ioannis@0 | 263 | return measurement |
ulalume3@4 | 264 | |
ioannis@0 | 265 | def get_status(self, measurement_id): |
ioannis@0 | 266 | """ Get the processing status for a measurement id through the API. """ |
ioannis@0 | 267 | measurement_url = urlparse.urljoin(API_BASE_URL, 'measurements/?id__exact=%s' % measurement_id) |
ulalume3@6 | 268 | |
ulalume3@6 | 269 | response = self.session.get(measurement_url, |
ulalume3@6 | 270 | auth=self.auth, |
ulalume3@6 | 271 | verify=False) |
ulalume3@6 | 272 | |
ioannis@0 | 273 | response_dict = response.json() |
ulalume3@6 | 274 | |
ioannis@0 | 275 | if response_dict['objects']: |
ioannis@0 | 276 | measurement_list = response_dict['objects'] |
ioannis@0 | 277 | measurement = Measurement(measurement_list[0]) |
ulalume3@6 | 278 | return (measurement.upload, measurement.pre_processing, measurement.processing) |
ioannis@0 | 279 | else: |
ulalume3@6 | 280 | logger.error("No measurement with id %s found on the SCC." % measurement_id) |
ioannis@0 | 281 | return None |
ulalume3@6 | 282 | |
ioannis@0 | 283 | def get_measurement(self, measurement_id): |
ioannis@0 | 284 | measurement_url = urlparse.urljoin(API_BASE_URL, 'measurements/%s/' % measurement_id) |
ulalume3@6 | 285 | |
ulalume3@6 | 286 | response = self.session.get(measurement_url, |
ulalume3@6 | 287 | auth=self.auth, |
ulalume3@6 | 288 | verify=False) |
ulalume3@6 | 289 | |
ioannis@0 | 290 | response_dict = response.json() |
ulalume3@6 | 291 | |
ioannis@0 | 292 | if response_dict: |
ioannis@0 | 293 | measurement = Measurement(response_dict) |
ioannis@0 | 294 | return measurement |
ioannis@0 | 295 | else: |
ulalume3@6 | 296 | logger.error("No measurement with id %s found on the SCC." % measurement_id) |
ioannis@0 | 297 | return None |
ulalume3@6 | 298 | |
ioannis@0 | 299 | def delete_measurement(self, measurement_id): |
ioannis@0 | 300 | """ Deletes a measurement with the provided measurement id. The user |
ioannis@0 | 301 | should have the appropriate permissions. |
ioannis@0 | 302 | |
ioannis@0 | 303 | The procedures is performed directly through the web interface and |
ioannis@0 | 304 | NOT through the API. |
ioannis@0 | 305 | """ |
ioannis@0 | 306 | # Get the measurement object |
ioannis@0 | 307 | measurement = self.get_measurement(measurement_id) |
ulalume3@6 | 308 | |
ioannis@0 | 309 | # Check that it exists |
ioannis@0 | 310 | if measurement is None: |
ulalume3@6 | 311 | logger.warning("Nothing to delete.") |
ioannis@0 | 312 | return None |
ulalume3@6 | 313 | |
ioannis@0 | 314 | # Go the the page confirming the deletion |
ioannis@0 | 315 | delete_url = DELETE_MEASUREMENT.format(measurement.id) |
ulalume3@6 | 316 | |
ulalume3@6 | 317 | confirm_page = self.session.get(delete_url, |
ulalume3@6 | 318 | auth=self.auth, |
ulalume3@6 | 319 | verify=False) |
ulalume3@6 | 320 | |
ioannis@0 | 321 | # Check that the page opened properly |
ioannis@0 | 322 | if confirm_page.status_code != 200: |
ulalume3@6 | 323 | logger.warning("Could not open delete page. Status: {0}".format(confirm_page.status_code)) |
ioannis@0 | 324 | return None |
ulalume3@6 | 325 | |
ioannis@0 | 326 | # Delete the measurement |
ulalume3@6 | 327 | delete_page = self.session.post(delete_url, |
ulalume3@6 | 328 | auth=self.auth, |
ulalume3@6 | 329 | verify=False, |
ulalume3@6 | 330 | data={'post': 'yes'}, |
ulalume3@6 | 331 | headers={'X-CSRFToken': confirm_page.cookies['csrftoken'], |
ioannis@0 | 332 | 'referer': delete_url} |
ulalume3@6 | 333 | ) |
ioannis@0 | 334 | if delete_page.status_code != 200: |
ulalume3@6 | 335 | logger.warning("Something went wrong. Delete page status: {0}".format( |
ulalume3@6 | 336 | delete_page.status_code)) |
ioannis@0 | 337 | return None |
ulalume3@6 | 338 | |
ulalume3@6 | 339 | logger.info("Deleted measurement {0}".format(measurement_id)) |
ulalume3@6 | 340 | return True |
ioannis@0 | 341 | |
ioannis@0 | 342 | def available_measurements(self): |
ioannis@0 | 343 | """ Get a list of available measurement on the SCC. """ |
ioannis@0 | 344 | measurement_url = urlparse.urljoin(API_BASE_URL, 'measurements') |
ulalume3@6 | 345 | response = self.session.get(measurement_url, |
ulalume3@6 | 346 | auth=self.auth, |
ulalume3@6 | 347 | verify=False) |
ioannis@0 | 348 | response_dict = response.json() |
ulalume3@6 | 349 | |
ioannis@0 | 350 | measurements = None |
ioannis@0 | 351 | if response_dict: |
ioannis@0 | 352 | measurement_list = response_dict['objects'] |
ioannis@0 | 353 | measurements = [Measurement(measurement_dict) for measurement_dict in measurement_list] |
ulalume3@6 | 354 | logger.info("Found %s measurements on the SCC." % len(measurements)) |
ioannis@0 | 355 | else: |
ulalume3@6 | 356 | logger.warning("No response received from the SCC when asked for available measurements.") |
ioannis@0 | 357 | |
ioannis@0 | 358 | return measurements |
ulalume3@6 | 359 | |
ulalume3@6 | 360 | def measurement_id_for_date(self, t1, call_sign='bu', base_number=0): |
ioannis@0 | 361 | """ Give the first available measurement id on the SCC for the specific |
ioannis@0 | 362 | date. |
ioannis@0 | 363 | """ |
ioannis@0 | 364 | date_str = t1.strftime('%Y%m%d') |
ioannis@0 | 365 | search_url = urlparse.urljoin(API_BASE_URL, 'measurements/?id__startswith=%s' % date_str) |
ulalume3@6 | 366 | |
ulalume3@6 | 367 | response = self.session.get(search_url, |
ulalume3@6 | 368 | auth=self.auth, |
ulalume3@6 | 369 | verify=False) |
ulalume3@6 | 370 | |
ioannis@0 | 371 | response_dict = response.json() |
ulalume3@6 | 372 | |
ioannis@0 | 373 | measurement_id = None |
ulalume3@6 | 374 | |
ioannis@0 | 375 | if response_dict: |
ioannis@0 | 376 | measurement_list = response_dict['objects'] |
ioannis@0 | 377 | existing_ids = [measurement_dict['id'] for measurement_dict in measurement_list] |
ulalume3@6 | 378 | |
ioannis@0 | 379 | measurement_number = base_number |
ioannis@0 | 380 | measurement_id = "%s%s%02i" % (date_str, call_sign, measurement_number) |
ulalume3@6 | 381 | |
ioannis@0 | 382 | while measurement_id in existing_ids: |
ioannis@0 | 383 | measurement_number = measurement_number + 1 |
ioannis@0 | 384 | measurement_id = "%s%s%02i" % (date_str, call_sign, measurement_number) |
ioannis@0 | 385 | if measurement_number == 100: |
ioannis@0 | 386 | raise ValueError('No available measurement id found.') |
ioannis@0 | 387 | |
ulalume3@6 | 388 | return measurement_id |
ulalume3@6 | 389 | |
ulalume3@6 | 390 | |
ioannis@0 | 391 | class ApiObject: |
ioannis@0 | 392 | """ A generic class object. """ |
ulalume3@6 | 393 | |
ioannis@0 | 394 | def __init__(self, dict_response): |
ulalume3@6 | 395 | |
ioannis@0 | 396 | if dict_response: |
ioannis@0 | 397 | # Add the dictionary key value pairs as object properties |
ioannis@0 | 398 | for key, value in dict_response.items(): |
ioannis@0 | 399 | setattr(self, key, value) |
ioannis@0 | 400 | self.exists = True |
ioannis@0 | 401 | else: |
ioannis@0 | 402 | self.exists = False |
ioannis@0 | 403 | |
ioannis@0 | 404 | |
ioannis@0 | 405 | class Measurement(ApiObject): |
ioannis@0 | 406 | """ This class represents the measurement object as returned in the SCC API. |
ioannis@0 | 407 | """ |
ulalume3@6 | 408 | |
ioannis@0 | 409 | @property |
ioannis@0 | 410 | def is_running(self): |
ioannis@0 | 411 | """ Returns True if the processing has not finished. |
ioannis@0 | 412 | """ |
ioannis@0 | 413 | if self.upload == 0: |
ioannis@0 | 414 | return False |
ioannis@0 | 415 | if self.pre_processing == -127: |
ioannis@0 | 416 | return False |
ioannis@0 | 417 | if self.pre_processing == 127: |
ulalume3@6 | 418 | if self.processing in [127, -127]: |
ioannis@0 | 419 | return False |
ioannis@0 | 420 | return True |
ulalume3@6 | 421 | |
ulalume3@6 | 422 | @property |
ulalume3@4 | 423 | def rerun_processing_url(self): |
ulalume3@4 | 424 | return RERUN_PROCESSING.format(self.id) |
ulalume3@4 | 425 | |
ulalume3@6 | 426 | @property |
ulalume3@4 | 427 | def rerun_all_url(self): |
ulalume3@4 | 428 | return RERUN_ALL.format(self.id) |
ulalume3@4 | 429 | |
ioannis@0 | 430 | def __str__(self): |
ulalume3@6 | 431 | return "%s: %s, %s, %s" % (self.id, |
ulalume3@6 | 432 | self.upload, |
ulalume3@6 | 433 | self.pre_processing, |
ulalume3@6 | 434 | self.processing) |
ulalume3@6 | 435 | |
ulalume3@6 | 436 | |
ulalume3@6 | 437 | def upload_file(filename, system_id, auth=BASIC_LOGIN, credential=DJANGO_LOGIN): |
ioannis@0 | 438 | """ Shortcut function to upload a file to the SCC. """ |
ulalume3@6 | 439 | logger.info("Uploading file %s, using sytem %s" % (filename, system_id)) |
ulalume3@6 | 440 | |
ioannis@0 | 441 | scc = SCC(auth) |
ioannis@0 | 442 | scc.login(credential) |
ioannis@0 | 443 | measurement_id = scc.upload_file(filename, system_id) |
ioannis@0 | 444 | scc.logout() |
ioannis@0 | 445 | return measurement_id |
ioannis@0 | 446 | |
ulalume3@6 | 447 | |
ulalume3@6 | 448 | def process_file(filename, system_id, auth=BASIC_LOGIN, credential=DJANGO_LOGIN): |
ioannis@0 | 449 | """ Shortcut function to process a file to the SCC. """ |
ulalume3@6 | 450 | logger.info("Processing file %s, using sytem %s" % (filename, system_id)) |
ulalume3@6 | 451 | |
ioannis@0 | 452 | scc = SCC(auth) |
ioannis@0 | 453 | scc.login(credential) |
ioannis@0 | 454 | measurement = scc.process(filename, system_id) |
ioannis@0 | 455 | scc.logout() |
ulalume3@6 | 456 | return measurement |
ulalume3@6 | 457 | |
ioannis@0 | 458 | |
ulalume3@6 | 459 | def delete_measurement(measurement_id, auth=BASIC_LOGIN, credential=DJANGO_LOGIN): |
ioannis@0 | 460 | """ Shortcut function to delete a measurement from the SCC. """ |
ulalume3@6 | 461 | logger.info("Deleting %s" % measurement_id) |
ioannis@0 | 462 | scc = SCC(auth) |
ioannis@0 | 463 | scc.login(credential) |
ioannis@0 | 464 | scc.delete_measurement(measurement_id) |
ioannis@0 | 465 | scc.logout() |
ioannis@0 | 466 | |
ulalume3@6 | 467 | |
ulalume3@6 | 468 | def rerun_all(measurement_id, monitor, auth=BASIC_LOGIN, credential=DJANGO_LOGIN): |
ulalume3@4 | 469 | """ Shortcut function to delete a measurement from the SCC. """ |
ulalume3@6 | 470 | logger.info("Rerunning all products for %s" % measurement_id) |
ulalume3@4 | 471 | scc = SCC(auth) |
ulalume3@4 | 472 | scc.login(credential) |
ulalume3@4 | 473 | scc.rerun_all(measurement_id, monitor) |
ulalume3@4 | 474 | scc.logout() |
ulalume3@4 | 475 | |
ulalume3@6 | 476 | |
ulalume3@6 | 477 | def rerun_processing(measurement_id, monitor, auth=BASIC_LOGIN, credential=DJANGO_LOGIN): |
ulalume3@4 | 478 | """ Shortcut function to delete a measurement from the SCC. """ |
ulalume3@6 | 479 | logger.info("Rerunning (optical) processing for %s" % measurement_id) |
ulalume3@4 | 480 | scc = SCC(auth) |
ulalume3@4 | 481 | scc.login(credential) |
ulalume3@4 | 482 | scc.rerun_processing(measurement_id, monitor) |
ulalume3@4 | 483 | scc.logout() |
ulalume3@4 | 484 | |
ulalume3@6 | 485 | |
ioannis@0 | 486 | # When running through terminal |
ioannis@0 | 487 | if __name__ == '__main__': |
ulalume3@6 | 488 | |
ioannis@0 | 489 | # Define the command line arguments. |
ioannis@0 | 490 | parser = argparse.ArgumentParser() |
ulalume3@6 | 491 | parser.add_argument("filename", nargs='?', help="Measurement file name or path.", default='') |
ulalume3@6 | 492 | parser.add_argument("system", nargs='?', help="Processing system id.", default=0) |
ioannis@0 | 493 | parser.add_argument("-p", "--process", help="Wait for the results of the processing.", |
ulalume3@6 | 494 | action="store_true") |
ulalume3@6 | 495 | parser.add_argument("--delete", help="Measurement ID to delete.") |
ulalume3@4 | 496 | parser.add_argument("--rerun-all", help="Measurement ID to rerun.") |
ulalume3@4 | 497 | parser.add_argument("--rerun-processing", help="Measurement ID to rerun processing routings.") |
ulalume3@6 | 498 | |
ulalume3@6 | 499 | # Verbosity settings from http://stackoverflow.com/a/20663028 |
ulalume3@6 | 500 | parser.add_argument('-d', '--debug', help="Print debugging information.", action="store_const", |
ulalume3@6 | 501 | dest="loglevel", const=logging.DEBUG, default=logging.INFO, |
ulalume3@6 | 502 | ) |
ulalume3@6 | 503 | parser.add_argument('-s', '--silent', help="Show only warning and error messages.", action="store_const", |
ulalume3@6 | 504 | dest="loglevel", const=logging.WARNING |
ulalume3@6 | 505 | ) |
ulalume3@6 | 506 | |
ioannis@0 | 507 | args = parser.parse_args() |
ulalume3@6 | 508 | |
ulalume3@6 | 509 | # Get the logger with the appropriate level |
ulalume3@6 | 510 | logging.basicConfig(format='%(levelname)s: %(message)s', level=args.loglevel) |
ulalume3@6 | 511 | |
ioannis@0 | 512 | # If the arguments are OK, try to login on the site and upload. |
ioannis@0 | 513 | if args.delete: |
ioannis@0 | 514 | # If the delete is provided, do nothing else |
ioannis@0 | 515 | delete_measurement(args.delete) |
ulalume3@4 | 516 | elif args.rerun_all: |
ulalume3@4 | 517 | rerun_all(args.rerun_all, args.process) |
ulalume3@4 | 518 | elif args.rerun_processing: |
ulalume3@4 | 519 | rerun_processing(args.rerun_processing, args.process) |
ioannis@0 | 520 | else: |
ulalume3@6 | 521 | if (args.filename == '') or (args.system == 0): |
ioannis@0 | 522 | parser.error('Provide a valid filename and system parameters.\nRun with -h for help.\n') |
ulalume3@6 | 523 | |
ioannis@0 | 524 | if args.process: |
ioannis@0 | 525 | process_file(args.filename, args.system) |
ioannis@0 | 526 | else: |
ioannis@0 | 527 | upload_file(args.filename, args.system) |