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