def write_binary(self, filename=None): """Writes a binary representation of this sequence to the given filename (defaults to self.binpath). """ if filename is None: filename = self.binpath with open(filename, "wb") as output: # Magic Number output.write(struct.pack(">H", self.magic)) # Upload Type output.write(struct.pack("B", 9)) # Version output.write(struct.pack("B", self.version)) # Number of Commands output.write(struct.pack(">H", len(self.commands))) # Sequence ID output.write(struct.pack(">H", self.seqid)) # CRC Placeholder output.write(struct.pack(">I", 0)) pad = struct.pack("B", 0) for _n in range(20): output.write(pad) for line in self.lines: output.write(line.encode()) self.crc32 = util.crc32File(filename, 0) with open(filename, "r+b") as output: output.seek(28) output.write(struct.pack(">I", self.crc32))
def toBinary(self, tabfile, stream, fswbin_f, verbose, version): #print "self.name: "+self.name #print "stream name: "+stream.name size = os.path.getsize(stream.name) #print "stream len: " + str(size) #print "self.size: " + str(self.size) no_lines = 0 for line in stream: no_lines += 1 stream.seek(0) fsw_header = bytearray(32) # sha1 = hash_file(tabfile) sha1 = 0 # Write magic number fswbin_f.write( struct.pack('>H', self.MagicNumber ) ) # Write upload type fswbin_f.write( struct.pack('B', self.uptype ) ) # Write version fswbin_f.write( struct.pack('B', int(version,16)&255 ) ) # Write number of lines if self.name == "memory": fswbin_f.write( struct.pack('>H', 0 ) ) else: fswbin_f.write( struct.pack('>H', no_lines ) ) # Write ID (0) fswbin_f.write( struct.pack('>H', 0) ) # # Write CRC placeholder fswbin_f.write( struct.pack('>I', 0) ) # SHA as 0 pad = struct.pack('B', 0) for n in range(20): fswbin_f.write(pad) # data = bytearray(20) # i = 0 # tmpbytes = list(sha1) # for x in range(0, len(sha1)/2): # tmp = ((int(tmpbytes[x],16)&255)<<4) + (int(tmpbytes[x+1],16)&255) # #print "tmp: "+ str(tmp) # data[i] = tmp&0xFF # i += 1 # fswbin_f.write(data) for line in stream: #print "line: "+line idx = 0 line = line.replace("\n","") allcols = line.split(self.delimiter) if self.name == "memory": for val in allcols: if val != "": #print "val: "+val data = bytearray(2) tmpbytes = list(val) data[0] = ((int(tmpbytes[0],16)&0xF)<<4) + (int(tmpbytes[1],16)&0xF) data[1] = ((int(tmpbytes[2],16)&0xF)<<4) + (int(tmpbytes[3],16)&0xF) #print "tmp byte1: "+ str(int(tmpbytes[0],16)&0xF) #print "tmp byte2: "+ str(int(tmpbytes[1],16)&0xF) #print "tmp byte3: "+ str(int(tmpbytes[2],16)&0xF) #print "tmp byte4: "+ str(int(tmpbytes[3],16)&0xF) fswbin_f.write(data) else: idx = 0 #this is how to step into table definitions for coldef in enumerate(self.coldefns): # print "column definition: " + str(coldef[1]) fswcoldefn = coldef[1] name = fswcoldefn.name #print "name: " + name colpk = dtype.get(fswcoldefn.type) #print "packing: " + str(colpk) units = fswcoldefn.units #print "units: " + units enum = fswcoldefn.enum if isinstance(fswcoldefn.bytes,list): nobytes = fswcoldefn.bytes[1] - fswcoldefn.bytes[0] + 1 else: nobytes = 1 # print "bytes: " + str(fswcoldefn.bytes) # print "nobytes: " + str(nobytes) if name == 'RESERVED': #add reserved bytes fswbin_f.write(colpk.encode(0)) if coldef[0] != len(self.coldefns)-1: continue else: break items = fswcoldefn.items if items is not None: #print "items: " + str(items) for i in range(items): val = allcols[i] #print "item col val: "+str(val) if units != 'none': val = val.strip() val = val.split(" ")[0] else: val = val.replace(" ","") #print "item col val: "+str(val) fswbin_f.write(colpk.encode(self.convertValue(val))) else: val = allcols[idx] val = val.replace("\n","") #print "else col val 1: "+str(val) if enum is not None: if enum is not None: for enumkey in enumerate(enum.keys()): enumval = enum[enumkey[1]] #print "enumkey: " + str(enumkey[1]) + ", enumval: " + str(enum[enumkey[1]]) if enumval == val: val = str(enumkey[1]) #print "XXXX colpk.type: "+colpk.format fswbin_f.write(colpk.encode(self.convertValue(val))) else: #print "XXXX colpk.type: "+colpk.format #print "fswcoldefn.bytes: "+str(fswcoldefn.bytes) #print "xxxx val: "+str(val) #print "units: "+str(units) if units != 'none': val = val.strip() val = val.split(" ")[0] #print "else col val 2a: "+str(val) fswbin_f.write(colpk.encode(self.convertValue(val))) #fswbin_f.write(colpk.encode(float(val))) elif str(colpk) == "PrimitiveType('U8')" and nobytes>1: strval = "" for c in list(val): #print "xxx c: "+str(c) tmp = (int(c,16))&255 #print "tmp: "+str(tmp) fswbin_f.write(colpk.encode(tmp)) else: val = val.replace(" ","") #print "else col val 2b: "+str(val) fswbin_f.write(colpk.encode(self.convertValue(val))) #fswbin_f.write(colpk.encode(float(val))) idx += 1 written = fswbin_f.tell() #print "written: "+str(written) # print str(self.size) + ", " + str(written) if self.size > written: padding = bytearray(self.size - (written)) fswbin_f.write(padding) #Now calculate and update CRC field in the FSW header fswbin_f.close() fname = fswbin_f.name crc32 = util.crc32File(fname, 0) fswbin_f = open(fname, 'r+b') fswbin_f.seek(28) crcbuf = bytearray(4) crcbuf[0:4] = struct.pack('>L',crc32) fswbin_f.write(crcbuf) #version = "0" if verbose is not None and verbose != 0: log.info("CRC: %x" % crc32) print "MAGIC_NUMBER: %x" % self.MagicNumber print "UPLOAD_TYPE: " + str(self.uptype) print "VERSION: " + str(version) print "NUMBER_ENTRIES: " + str(no_lines) # print "SHA-1: "+sha1 #print "fname: "+fname+", crc32: "+str(crc32) # Once we are done appending all the columns to the row # strip off last comma and append a \r # Note: Since it is Access, \n alone does not work return
def testCrc32WithTestFileAndSkip(self): """Test the CRC-32 with a skip specified""" crc = util.crc32File(TEST_FILE_PATH, 1) self.assertEqual(crc, TEST_FILE_CRC_SKIP_BYTE)
def testCrc32WithTestFile(self): """Test the CRC for a basic test file""" crc = util.crc32File(TEST_FILE_PATH) self.assertEqual(crc, TEST_FILE_CRC)