def write(self, key, value, tags, timestamp=None): if timestamp is None: timestamp = util.now() hashed_tags = self._insert_tags(tags) hashed_key = self._insert_key(key) # convert to microseconds microseconds = util.datetime_to_microseconds(timestamp) self._recorder.post_msg((hashed_tags, hashed_key, value, microseconds))
def post_process(self): logging.info("Processing binaries") fw_info_fmt = '<I16s128s16s128s16s' fw_info_addr = int(self.settings["FW_INFO_ADDR"], 16) # save original dir cwd = os.getcwd() # change to target dir os.chdir(self.target_dir) ih = IntelHex('main.hex') fwid = uuid.UUID('{' + self.settings["FWID"] + '}') # get KV meta start kv_meta_addr = fw_info_addr + struct.calcsize(fw_info_fmt) kv_meta_len = KVMetaField().size() bindata = ih.tobinstr() kv_meta_data = [] while True: kv_meta = KVMetaField().unpack(bindata[kv_meta_addr:kv_meta_addr + kv_meta_len]) kv_meta_addr += kv_meta_len if kv_meta.param_name == "kvstart": continue elif kv_meta.param_name == "kvend": break else: kv_meta_data.append(kv_meta) kv_meta_by_hash = {} logging.info("Hash type: FNV1A_32") # create lookups by 32 bit hash index = 0 for kv in kv_meta_data: hash32 = fnv1a_32(str(kv.param_name)) if hash32 in kv_meta_by_hash: raise Exception("Hash collision!") kv_meta_by_hash[hash32] = (kv, index) index += 1 # sort indexes sorted_hashes = sorted(kv_meta_by_hash.keys()) # create binary look up table kv_index = '' for a in sorted_hashes: kv_index += struct.pack('<LB', a, kv_meta_by_hash[a][1]) # write to end of hex file ih.puts(ih.maxaddr() + 1, kv_index) size = ih.maxaddr() - ih.minaddr() + 1 # get os info try: os_project = get_project_builder(self.settings["OS_PROJECT"], target=self.target_type) except KeyError: os_project = "" # create firmware info structure fw_info = struct.pack(fw_info_fmt, size, fwid.bytes, os_project.proj_name, os_project.version, str(self.settings['PROJ_NAME']), self.version) # insert fw info into hex ih.puts(fw_info_addr, fw_info) # compute crc crc_func = crcmod.predefined.mkCrcFun('crc-aug-ccitt') crc = crc_func(ih.tobinstr()) logging.info("size: %d" % (size)) logging.info("fwid: %s" % (fwid)) logging.info("fwinfo: %x" % (fw_info_addr)) logging.info("kv index len: %d" % (len(kv_index))) logging.info("crc: 0x%x" % (crc)) logging.info("os name: %s" % (os_project.proj_name)) logging.info("os version: %s" % (os_project.version)) logging.info("app name: %s" % (self.settings['PROJ_NAME'])) logging.info("app version: %s" % (self.version)) ih.puts(ih.maxaddr() + 1, struct.pack('>H', crc)) ih.write_hex_file('main.hex') ih.tobinfile('firmware.bin') # get loader info loader_project = get_project_builder(self.settings["LOADER_PROJECT"], target=self.target_type) # create loader image loader_hex = os.path.join(loader_project.target_dir, "main.hex") self.merge_hex('main.hex', loader_hex, 'loader_image.hex') # create sha256 of binary sha256 = hashlib.sha256(ih.tobinstr()) # create manifest file data = { 'name': self.settings['FULL_NAME'], 'timestamp': datetime.utcnow().isoformat(), 'sha256': sha256.hexdigest(), 'fwid': self.settings['FWID'], 'version': self.version } with open('manifest.txt', 'w+') as f: f.write(json.dumps(data)) # create firmware zip file zf = zipfile.ZipFile('chromatron_main_fw.zip', 'w') zf.write('manifest.txt') zf.write('firmware.bin') zf.close() # create second, project specific zip # we'll remove the first zip after # we update the firmware tools zf = zipfile.ZipFile('%s.zip' % (self.settings['PROJ_NAME']), 'w') zf.write('manifest.txt') zf.write('firmware.bin') zf.close() # change back to original dir os.chdir(cwd) logging.info("Package dir: %s" % (get_build_package_dir())) # make sure we have the firmware package dir try: os.makedirs(get_build_package_dir()) except OSError: pass # copy firmware zip try: shutil.copy( os.path.join(self.target_dir, '%s.zip' % (self.proj_name)), get_build_package_dir()) except IOError: raise AppZipNotFound # update build date with open( os.path.join(get_build_package_dir(), firmware_package.PUBLISHED_AT_FILENAME), 'w') as f: f.write(util.now().isoformat())
def build(self): logging.info("Building configuration %s for target: %s" % (self.proj_name, self.target_type)) includes = [] inits = [] # build libraries for builder in self.lib_builders: builder.clean() builder.build() includes.extend(builder.settings["LIB_INCLUDES"]) inits.append(builder.settings["LIB_INIT"]) self.app_builder.libraries.append(builder.proj_name) lib_init_filename = os.path.join(self.app_builder.target_dir, '__libs.c') lib_init = """ #include "sapphire.h" """ for include in includes: lib_init += '\n#include "%s"\n' % (include) lib_init += """ void libs_v_init( void ){ """ for init in inits: if len(init) > 0: lib_init += '\n%s();\n' % (init) lib_init += '\n}\n' # create lib init file with open(lib_init_filename, 'w+') as f: f.write(lib_init) # self.app_builder.build_libs() self.app_builder.clean() try: self.app_builder.build() except AppZipNotFound: # not a problem in config-driven builds pass # make sure we have the firmware package dir try: os.makedirs(get_build_package_dir()) except OSError: pass # copy firmware zip shutil.copy( os.path.join(self.app_builder.target_dir, '%s.zip' % (self.proj_name)), get_build_package_dir()) # update build date with open( os.path.join(get_build_package_dir(), firmware_package.PUBLISHED_AT_FILENAME), 'w') as f: f.write(util.now().isoformat())
return results if __name__ == '__main__': l = Logger() # count = 10 # start = time.time() # for i in xrange(count): # l.write('test_key', i, ['test', 'meow', 'jeremy']) # l.write('stuff', i, ['test', 'meow', 'jeremy']) # l.write('things', i, ['meow']) # elapsed = time.time() - start # print '' # print elapsed # print count / elapsed # time.sleep(2.0) print l.query("things", [], util.now() - timedelta(seconds=6000), util.now()) # print l.query_tags(['meow']) # print l.query_tags(['test', 'meow']) # print l.query_tags(['test']) # print l.query_tags(['woof'])