Esempio n. 1
0
    def parse_args(self, raw_args):
        '''
        Convert input strings to their appropriate types.
        '''
        if len(raw_args) > 1 and not self.allow_multiple:
            APP_LOGGER.warning(
                "Multiple parameter values for %s are not permitted. Only using the first value: %s."
                % (self.name, raw_args[0]))
            raw_args = raw_args[:1]

        if len(raw_args) < 1:
            if self.default is not None:
                raw_args = [self.default]
            elif self.required:
                raise Exception("Required argument %s not provided." %
                                self.name)

        converted_args = self._convert_args(raw_args)
        if self.enum:
            valid_args = set(self.enum)
            if not set(converted_args).issubset(valid_args):
                invalid_args = set(converted_args).difference(valid_args)
                raise Exception(
                    "Provided arguments %s not a subset of valid arguments: %s"
                    % (invalid_args, valid_args))

        return converted_args
    def process_request(cls, params_dict):
        users            = params_dict[cls._users_param]
        date             = params_dict[cls._date_param][0]
        archive_name     = params_dict[cls._archive_param][0]
        beta             = params_dict[cls._beta_param][0]
        device           = params_dict[cls._device_param][0]
        dye_prof_metrics = params_dict[cls._dye_profile_metrics_param]
        surfactant       = params_dict[cls._surfactant_param][0]
        
        json_response = {}
        
        # Ensure archive directory is valid
        try:
            archives = get_archive_dirs(archive_name)
        except:
            APP_LOGGER.exception(traceback.format_exc())
            json_response[ERROR] = str(sys.exc_info()[1])
            return make_clean_response(json_response, 500)
        
        # Ensure only one valid archive is found
        if len(archives) != 1:
            APP_LOGGER.warning("Expected 1 archive, found %d" % len(archives))
            return make_clean_response(json_response, 404)

        response = {
                    USERS: users,
                    DATE: date,
                    ARCHIVE: archives[0],
                    BETA: beta,
                    DEVICE: device,
                    DYE_PROFILE_METRICS: dye_prof_metrics,
                    SURFACTANT: surfactant,
                    STATUS: JOB_STATUS.submitted,                # @UndefinedVariable
                    JOB_TYPE_NAME: JOB_TYPE.dye_profile_images,  # @UndefinedVariable
                    SUBMIT_DATESTAMP: datetime.today(),
                   }
        status_code = 200
        
        
        
        
        try:
            
#             # Create helper functions
#             callable = PaProcessCallable(archive, dyes, device,
#                                              major, minor,
#                                              offset, use_iid, 
#                                              outfile_path, 
#                                              config_path,
#                                              response[UUID], 
#                                              cls._DB_CONNECTOR)
#             callback = make_process_callback(response[UUID], 
#                                              outfile_path, 
#                                              config_path,
#                                              cls._DB_CONNECTOR)
# 
#             # Add to queue and update DB
#             cls._DB_CONNECTOR.insert(PA_PROCESS_COLLECTION, [response])
#             cls._EXECUTION_MANAGER.add_job(response[UUID], 
#                                            abs_callable, callback)
        except:
            APP_LOGGER.exception(traceback.format_exc())
            response[ERROR]  = str(sys.exc_info()[1])
            status_code = 500
        finally:
            if ID in response:
                del response[ID]
        
        
        
        
        
        
        
        http_status_code = 200
        uuid             = str(uuid4())
        tmp_archive_path = os.path.join(TMP_PATH, uuid + ".tar.gz")
        archive_path     = os.path.join(RESULTS_PATH, uuid + ".tar.gz")
        json_response    = { 
                            FILENAME: image_stack_tgz.filename,
                            UUID: uuid,
                            DATESTAMP: datetime.today(),
                           }

        try:
            # check tar file
            image_stack_tgz.save(tmp_archive_path)
            image_stack_tgz.close()
            tar_error, nimgs = check_ham_tar_structure(tmp_archive_path, HAM)

            # check for existing image stacks
            existing_stacks = cls._DB_CONNECTOR.find(IMAGES_COLLECTION,
                                                     {NAME: img_stack_name, STACK_TYPE: HAM},
                                                     [NAME])

            # check for exp def
            exp_defs     = ExperimentDefinitions()
            exp_def_uuid = exp_defs.get_experiment_uuid(exp_def_name)

            if existing_stacks:
                http_status_code = 403
                json_response[ERROR] = "Image stack with given name already " \
                            "exists."
            elif not exp_def_uuid:
                http_status_code = 404
                json_response[ERROR] = "Couldn't locate UUID for " \
                    "experiment definition."
            elif tar_error:
                APP_LOGGER.error(tar_error)
                http_status_code = 415
                json_response[ERROR] = tar_error
            else:
                url = "http://%s/results/%s/%s" % (HOSTNAME, PORT,
                                                   os.path.basename(archive_path))
                shutil.copy(tmp_archive_path, archive_path)
                json_response[RESULT]       = archive_path
                json_response[URL]          = url
                json_response[NAME]         = img_stack_name
                json_response[DESCRIPTION]  = short_desc
                json_response[EXP_DEF_NAME] = exp_def_name
                json_response[EXP_DEF_UUID] = exp_def_uuid
                json_response[NUM_IMAGES]   = nimgs
                json_response[STACK_TYPE]  = HAM
                cls._DB_CONNECTOR.insert(IMAGES_COLLECTION,
                                         [json_response])
        except IOError:
            APP_LOGGER.exception(traceback.format_exc())
            http_status_code     = 415
            json_response[ERROR] = str(sys.exc_info()[1])
        except:
            APP_LOGGER.exception(traceback.format_exc())
            http_status_code     = 500
            json_response[ERROR] = str(sys.exc_info()[1])
        finally:
            if ID in json_response:
                del json_response[ID]
            silently_remove_file(tmp_archive_path)
        
        return make_clean_response(json_response, http_status_code)