def fast_process(self, index, diff_rorp): """Patch base_rp with diff_rorp and write increment (neither is dir)""" mirror_rp, inc_prefix = longname.get_mirror_inc_rps( self.CCPP.get_rorps(index), self.basis_root_rp, self.inc_root_rp) tf = TempFile.new(mirror_rp) if self.patch_to_temp(mirror_rp, diff_rorp, tf): inc = robust.check_common_error(self.error_handler, increment.Increment, (tf, mirror_rp, inc_prefix)) if inc is not None and not isinstance(inc, int): self.CCPP.set_inc(index, inc) if inc.isreg(): inc.fsync_with_dir() # Write inc before rp changed if tf.lstat(): if robust.check_common_error(self.error_handler, rpath.rename, (tf, mirror_rp)) is None: self.CCPP.flag_success(index) else: tf.delete() elif mirror_rp.lstat(): mirror_rp.delete() self.CCPP.flag_deleted(index) elif inc.renamed: self.CCPP.flag_deleted(index) return # normal return, otherwise error occurred tf.setdata() if tf.lstat(): tf.delete()
def fast_process(self, index, diff_rorp): """Patch base_rp with diff_rorp and write increment (neither is dir)""" mirror_rp, inc_prefix = longname.get_mirror_inc_rps( self.CCPP.get_rorps(index), self.basis_root_rp, self.inc_root_rp) tf = TempFile.new(mirror_rp) if self.patch_to_temp(mirror_rp, diff_rorp, tf): inc = robust.check_common_error(self.error_handler, increment.Increment, (tf, mirror_rp, inc_prefix)) if inc is not None and not isinstance(inc, int): self.CCPP.set_inc(index, inc) if inc.isreg(): inc.fsync_with_dir() # Write inc before rp changed if tf.lstat(): if robust.check_common_error(self.error_handler, rpath.rename, (tf, mirror_rp)) is None: self.CCPP.flag_success(index) else: tf.delete() elif mirror_rp.lstat(): mirror_rp.delete() self.CCPP.flag_deleted(index) return # normal return, otherwise error occurred tf.setdata() if tf.lstat(): tf.delete()
def recreate_meta(meta_manager): """Make regress_time mirror_metadata snapshot by patching We write to a tempfile first. Otherwise, in case of a crash, it would seem we would have an intact snapshot and partial diff, not the reverse. """ temprp = [TempFile.new_in_dir(Globals.rbdir)] def callback(rp): temprp[0] = rp writer = metadata.MetadataFile(temprp[0], 'w', check_path=0, callback=callback) for rorp in meta_manager.get_meta_at_time(regress_time, None): writer.write_object(rorp) writer.close() finalrp = Globals.rbdir.append("mirror_metadata.%s.snapshot.gz" % Time.timetostring(regress_time)) assert not finalrp.lstat(), finalrp rpath.rename(temprp[0], finalrp) if Globals.fsync_directories: Globals.rbdir.fsync()
def mkstemp(suffix='', prefix='tmp', dir=None, text=False): if text: raise NotImplementedError if dir is None: dir = '' # TODO: Make suffix actually follow the rest of the filename. f, err = TempFile(dir, prefix + '-' + suffix) if err: raise OSError(err.Error()) try: fd, err = Dup(f.Fd()) if err: raise OSError(err.Error()) return fd, f.Name() finally: f.Close()
def write(self): """Write the current entry into the journal""" entry_rp = TempFile.new_in_dir(journal_dir_rp) fp = entry_rp.open("wb") fp.write(self.to_string()) entry_rp.fsync(fp) assert not fp.close() sync_journal() self.entry_rp = entry_rp
def set_dir_replacement(self, diff_rorp, base_rp): """Set self.dir_replacement, which holds data until done with dir This is used when base_rp is a dir, and diff_rorp is not. Returns 1 for success or 0 for failure """ assert diff_rorp.get_attached_filetype() == 'snapshot' self.dir_replacement = TempFile.new(base_rp) if not self.patch_to_temp(None, diff_rorp, self.dir_replacement): if self.dir_replacement.lstat(): self.dir_replacement.delete() # Was an error, so now restore original directory rpath.copy_with_attribs(self.CCPP.get_mirror_rorp(diff_rorp.index), self.dir_replacement) return 0 else: return 1
def fast_process(self, index, diff_rorp): """Patch base_rp with diff_rorp (case where neither is directory)""" mirror_rp, discard = longname.get_mirror_inc_rps( self.CCPP.get_rorps(index), self.basis_root_rp) assert not mirror_rp.isdir(), mirror_rp tf = TempFile.new(mirror_rp) if self.patch_to_temp(mirror_rp, diff_rorp, tf): if tf.lstat(): if robust.check_common_error(self.error_handler, rpath.rename, (tf, mirror_rp)) is None: self.CCPP.flag_success(index) else: tf.delete() elif mirror_rp and mirror_rp.lstat(): mirror_rp.delete() self.CCPP.flag_deleted(index) else: tf.setdata() if tf.lstat(): tf.delete()
def recreate_meta(meta_manager): """Make regress_time mirror_metadata snapshot by patching We write to a tempfile first. Otherwise, in case of a crash, it would seem we would have an intact snapshot and partial diff, not the reverse. """ temprp = [TempFile.new_in_dir(Globals.rbdir)] def callback(rp): temprp[0] = rp writer = metadata.MetadataFile(temprp[0], 'w', check_path = 0, callback = callback) for rorp in meta_manager.get_meta_at_time(regress_time, None): writer.write_object(rorp) writer.close() finalrp = Globals.rbdir.append("mirror_metadata.%s.snapshot.gz" % Time.timetostring(regress_time)) assert not finalrp.lstat(), finalrp rpath.rename(temprp[0], finalrp) if Globals.fsync_directories: Globals.rbdir.fsync()
def restore_orig_regfile(self, rf): """Restore original regular file This is the trickiest case for avoiding information loss, because we don't want to delete the increment before the mirror is fully written. """ assert rf.metadata_rorp.isreg() if rf.mirror_rp.isreg(): tf = TempFile.new(rf.mirror_rp) tf.write_from_fileobj(rf.get_restore_fp()) tf.fsync_with_dir() # make sure tf fully written before move rpath.copy_attribs(rf.metadata_rorp, tf) rpath.rename(tf, rf.mirror_rp) # move is atomic else: if rf.mirror_rp.lstat(): rf.mirror_rp.delete() rf.mirror_rp.write_from_fileobj(rf.get_restore_fp()) rpath.copy_attribs(rf.metadata_rorp, rf.mirror_rp) if Globals.fsync_directories: rf.mirror_rp.get_parent_rp().fsync() # force move before inc delete
def restore_orig_regfile(self, rf): """Restore original regular file This is the trickiest case for avoiding information loss, because we don't want to delete the increment before the mirror is fully written. """ assert rf.metadata_rorp.isreg() if rf.mirror_rp.isreg(): tf = TempFile.new(rf.mirror_rp) tf.write_from_fileobj(rf.get_restore_fp()) tf.fsync_with_dir() # make sure tf fully written before move rpath.copy_attribs(rf.metadata_rorp, tf) rpath.rename(tf, rf.mirror_rp) # move is atomic else: if rf.mirror_rp.lstat(): rf.mirror_rp.delete() rf.mirror_rp.write_from_fileobj(rf.get_restore_fp()) rpath.copy_attribs(rf.metadata_rorp, rf.mirror_rp) if Globals.fsync_directories: rf.mirror_rp.get_parent_rp().fsync( ) # force move before inc delete
def init_readwrite(self, rbdir): """Set variables using fs tested at rp_base. Run locally. This method creates a temp directory in rp_base and writes to it in order to test various features. Use on a file system that will be written to. """ assert rbdir.conn is Globals.local_connection if not rbdir.isdir(): assert not rbdir.lstat(), (rbdir.path, rbdir.lstat()) rbdir.mkdir() self.root_rp = rbdir self.read_only = 0 subdir = TempFile.new_in_dir(rbdir) subdir.mkdir() self.set_extended_filenames(subdir) self.set_win_reserved_filenames(subdir) self.set_unicode_filenames(subdir) self.set_case_sensitive_readwrite(subdir) self.set_ownership(subdir) self.set_hardlinks(subdir) self.set_fsync_dirs(subdir) self.set_eas(subdir, 1) self.set_acls(subdir) self.set_win_acls(subdir, 1) self.set_dir_inc_perms(subdir) self.set_resource_fork_readwrite(subdir) self.set_carbonfile() self.set_high_perms_readwrite(subdir) self.set_symlink_perms(subdir) self.set_escape_dos_devices(subdir) self.set_escape_trailing_spaces_readwrite(subdir) subdir.delete() return self
def write_via_tempfile(fp, rp): """Write fileobj fp to rp by writing to tempfile and renaming""" tf = TempFile.new(rp) retval = tf.write_from_fileobj(fp) rpath.rename(tf, rp) return retval
def printlabel(self, rc, ws): # Print label as specified in the row of the input Workbook # Completes Output and Archive file path names # :param rc: Row contents object defining characteristics of the test # :return: # """ try: TF = TempFile.TempFile() files = FileLocation.FileLocation(self.LabelFileDirectory, rc) LabelFileName = os.path.join(self.LabelFileDirectory, rc.Label) OutputFileName = files.outputpath + "\\" + files.left( rc.Label, len(rc.Label) - 4) + ".prn" ArchiveFileName = files.archivepath + "\\" + files.left( rc.Label, len(rc.Label) - 4) + ".prn" BMPFileName = files.BMPPath + "\\" + files.left( rc.Label, len(rc.Label) - 4) + ".bmp" if rc.Function == "M": if self.FileExists(OutputFileName): MakeBMP( self, OutputFileName, BMPFileName, rc.IPAddress ) # send the ZPL to a printer to generate the .bmp image self.sheetoutput(rc, ".bmp File Created", ws, "", "", BMPFileName) else: self.sheetoutput(rc, "ZPL File not Found", ws, OutputFileName, "", "") else: if self.FileExists(LabelFileName): if self.FileExists(self.TempOut): os.remove(self.TempOut) tfilename = TF.writetempfile( rc, self.TempOut, LabelFileName ) # Use the row contents to build the Temp file zdp = self.LaunchLDA( rc.Application, tfilename ) # print the requested label via ZebraDesigner app if self.WaitForFile(self.TempOut): #shutil.copy(self.TempOut, OutputFileName) shutil.copyfile(self.TempOut, OutputFileName) if self.WaitForFile( OutputFileName): # Determine test results result = self.CheckOutput(OutputFileName, ArchiveFileName, zdp) if MakeBMP( self, OutputFileName, BMPFileName, rc.IPAddress ): # send the ZPL to a printer to generate the .bmp image result = result + " - BMP File not created!" self.sheetoutput( rc, result, ws, files.outputpath, files.archivepath, BMPFileName) # Report test results # print(rc.Printer + ' ' + rc.Language + ' ' + rc.Label + ' ' + rc.Dpi + ' ' + result) else: self.sheetoutput(rc, "ZPL File not copied", ws, LabelFileName, "", "") else: self.sheetoutput(rc, "ZPL File not created", ws, LabelFileName, "", "") else: self.sheetoutput(rc, "Label File Not Found", ws, LabelFileName, "", "") except Exception as e: print("PrintLabel.printlabel error " + str(e)) quit(-6)