def submit_records(self, records_filename, mode, update=False, taskid=0, silent=False): """ Performs the logic to submit given file (filepath) of records either by e-mail or using BibUpload with given mode. Taskid is given to indicate if the task submission should wait for any previously submitted tasks. The submission can also be made "silent" in the sense of not updating the modification date of the records. @param records_filename: filepath to XML file containing records. @type records_filename: string @param records_list: list of APSRecord objects for records @type records_list: list @param mode: which submission mode is it? @type mode: string @param taskid: bibsched taskid, wait for task to complete before submission @type taskid: int @param silent: do not update the modification date of the records @type silent: bool @return: returns the given taskid upon submission, or True/False from email. """ if update: records_list = self.records_to_update else: records_list = self.records_to_insert # Check if we should create bibupload or e-mail if mode == "email": # Lets parse the records and find our IDs. list_of_dois = [] for record in records_list: # We strip away the first part of the DOI for readability. list_of_dois.append('/'.join(record.doi.split('/')[1:])) # We send an e-mail to CFG_APSHARVEST_EMAIL and put file on AFS. body = "Harvested new records: %s" % (records_filename,) try: try: shutil.move(records_filename, self.out_folder) records_filename = os.path.join(self.out_folder, os.path.basename(records_filename)) body = "Harvested new records on %s. They are located here:\n %s" % \ (self.date_started.strftime("%Y-%m-%d %H:%M:%S"), records_filename) except IOError, e: # Some IOError body = "Error while harvesting records: \nError saving %s - %s" % \ (records_filename, str(e)) raise e finally: submit_records_via_ftp(records_filename) body = "%s\nRecords harvested (%s total):\n%s\n" % (body, str(len(list_of_dois)), "\n".join(list_of_dois)) body = "%s\nUploaded to FTP: %s" % ( body, os.path.basename(records_filename) ) res = submit_records_via_mail(self.mail_subject, body, CFG_APSHARVEST_EMAIL) write_message("Sent e-mail to %s with path to %s" % (CFG_APSHARVEST_EMAIL, records_filename)) return res else: # We submit a BibUpload task and wait for it to finish task_update_progress("Waiting for task to finish") if taskid != 0: write_message("Going to wait for %d to finish" % (taskid,)) while not can_launch_bibupload(taskid): # Lets wait until the previously launched task exits. task_sleep_now_if_required(can_stop_too=False) time.sleep(5.0) taskid = submit_bibupload_for_records(mode, records_filename, silent) write_message("Submitted BibUpload task #%s with mode %s" % (str(taskid), mode)) return taskid