def get(self): args = self.request.arguments timestamp = args['timestamp'][0] nonce = args['nonce'][0] echostr = args['echostr'][0] token = "AAAAAAAAAAAAAAAAAAHHHHHHHHHHHHH" sign = weixin.get_signature(token , timestamp, nonce) if sign == args['signature'][0]: self.write(echostr) log.info('check succ') else: self.write('signature is invalid') log.error('signature is invalid')
def read_json_conf(self): if not os.path.exists(self._json_path): log.error("File '{0} not exists !".format(self._json_path)) self.help() return False self._json_data = utils.FileOperation.load_json(self._json_path) html_path = self._json_data["info"]["path"] update_app = self._json_data["update_app"] or self._json_data["all_app"] if not update_app: log.error("JSON data is None") return False self._update_app = update_app
def chat(self, msg = ''): #only for utf-8 msg = msg.strip() if msg: #最多循环5次 for i in range(5): rsp = self._get_resp(msg) if rsp: return rsp log.warning('SimSimi.chat _get_resp failed') #重新初始化 log.warning('SimSimi.chat re init session...') self._init_session() rsp = self._get_resp(msg) if rsp: return rsp else: log.error('SimSimi.chat _get_resp failed return default') return hehe else: return 'empty msg...'
def _get_resp(self , msg): try: r = self.session.get( self.chat_url % msg )#, timeout=3.0) if not r.text.lstrip().startswith('{'): log.error('SimSimi._get_resp resp[%s] is not json ' , r.text) return None resp = r.json() if resp.has_key('id'): rsp_msg = resp['response'].encode('utf-8') #SimSimi is tired, I only can speak 200 time a day. Please visit again tomorrow. See ya~ if rsp_msg.startswith('SimSimi'): self._init_session() return None elif self._check_msg(rsp_msg) == False: log.warning('SimSimi._get_resp invalid msg = %s' , rsp_msg) return hehe else: return rsp_msg else: return hehe except Exception , e: log.error('SimSimi._get_resp exception ' , exc_info=True)
def run(self, target, target_args): """ Run Pin. Collect results in temporary file pin.log and return a list of trace locations (filepath:lineno:column). """ if not target.is_file(): log.error(f'No such file for target executable: {target}') return [] if not self.exe.is_file(): log.error(f'No such file for Pin executable: {self.exe}') return [] if not self.lib.is_file(): log.error(f'No such file for trace-pintool: {self.lib}') return [] logfile = Path('pin.log') errorfile = Path('error.log') try: # Clear files if present from old executions if logfile.is_file(): logfile.unlink() if errorfile.is_file(): errorfile.unlink() # Run Pin cmd = f'{self.exe} -error_file {errorfile.absolute()} -t {self.lib} -o {logfile} -c -- {target.absolute()}' args = cmd.split() + target_args p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) stdout, _ = p.communicate() return_code = p.returncode args_str = ' '.join(args) # Pin tool exits 1 on success ¯\_(ツ)_/¯ use errorfile to detect errors log.info( f'Got return code {return_code} running pin with command: "{args_str}"' ) if errorfile.is_file(): log.warn(f'Echoing Pin output stream:') for l in stdout.decode().splitlines(): log.warn(f'* {l}') errorfile.unlink() raise Exception( f'Pin had an error while running. See {errorfile} for more information.' ) if errorfile.is_file(): errorfile.unlink() if not logfile.is_file(): raise Exception( f'Something went wrong running Pin -- {logfile} is missing.' ) return parse_pinlog(logfile) finally: if logfile.is_file() and not self.keep_logfile: logfile.unlink()
def format_attrs(self, tag_string): menu_dict = {} try: for item in tag_string.split(":"): if utils.PhoneElement.POINT in item: menu_dict[item.split("=")[0]] = item.split("=")[1] continue if item in utils.Action.USER_ACTION: menu_dict["action"] = item elif "=" in item: for attr in item.split(","): tmp = attr.split("=") if tmp[0] in utils.PhoneElement.KEY: menu_dict[tmp[0]] = tmp[1] else: log.error( "{0} key error, you can use this keys : {1}". format(tmp[0], str(utils.PhoneElement.KEY))) else: menu_dict['menu'] = item except BaseException as e: log.error(str(e)) return menu_dict
def do_wizard(_, args, install_sh): """ Get Pin installation from root. If Pin is not at the expected location, do the interactive wizard with install_sh. """ root = args.pin_root pin = Pin(args) if not pin.is_valid(): log.warn(f'{root} is not a valid Pin installation.') if not install_sh.is_file(): log.error(f'Could not execute {install_sh}.') exit(1) else: log.warn( f'See {install_sh} for the recommended method for installing Pin.' ) yn = input( f'Should I install it at {root}? [type y to install, anything else to quit]: ' ) if yn == 'y': cmd = f'bash {install_sh.absolute()} {root.name}' log.debug( f'Running Bash script install.sh with "{cmd}" in directory "{root}"' ) proc = subprocess.Popen(cmd.split(), cwd=root.parent, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) stdout, _ = proc.communicate() for l in stdout.decode().splitlines(): log.info(f'**[{install_sh.name}]** {l}') if proc.returncode == 0: log.info(f'Ran {install_sh} successfully.') else: log.error(f'Could not execute {install_sh}.') exit(1) else: exit(1) pin = Pin(args) if not pin.is_valid(): log.error(f'Something is wrong with the Pin environment at {root}') return pin
def updatehtml(self, argv=utils.Command.HELP): ''' update the html.json ''' log.info("updatehtml") if argv == utils.Command.HELP: self.help() return False html_file_path = argv # html文件路径 html_file_list = [] # 所有html文件列表 json_data = {} # json文件数据 json_file_path = os.path.join(html_file_path, "html.json") # html.json文件名加路径 # html路径正确 if os.path.exists(html_file_path): # 获取所有的html文件名 html_file_list = FileOperation.get_html_files(html_file_path) # log.debug(html_file_list) # 获取html.json文件内容,文件不存在则赋值内容模板 if not os.path.exists(json_file_path): json_data = utils.DataTpl.HTML_INFO_JSON else: json_data = utils.FileOperation.load_json(json_file_path) # 更新html.json的info信息 json_data["info"] = { "time": utils.get_current_date() + " " + utils.get_current_time(), "count": len(html_file_list), "path": html_file_path } json_data['update_app'] = {} #初始化json的update_app信息 # 遍历html文件列表,更新到json文件中 for html_file in html_file_list: html_file_name = html_file.split(".")[0] # 去掉html后缀 # 获取html文件的md5值,用于判断该文件是否更改 html_file_md5 = FileOperation.get_mad5( os.path.join(html_file_path, html_file)) if html_file_name in json_data["all_app"].keys( ): # json文件中已保存过该文件 if html_file_md5 != json_data["all_app"][html_file_name][ "md5"]: # 如果md5值不同,则说明更改过 json_data["all_app"][html_file_name][ "md5"] = html_file_md5 json_data["update_app"][html_file_name] = { "name": html_file, "md5": html_file_md5 } else: json_data["all_app"][html_file_name] = { "name": html_file, "md5": html_file_md5 } json_data["update_app"][html_file_name] = { "name": html_file, "md5": html_file_md5 } # 从json中移除已删掉的html文件 for app in json_data['all_app'].keys(): if app + ".html" not in html_file_list: json_data["all_app"].pop(app) # log.info(json_data) # 保存 utils.FileOperation.store_json(json_file_path, json_data) if json_data['update_app'].keys(): log.info("更新的html文件有: {0}".format( str(json_data['update_app'].keys()))) else: log.error("File '{0}' not exists !".format(html_file_path)) self.help() return False
def main(): global args args = parse_args() target = Path(args.target[0]) target_args = args.target[1:] try: dynamic_locations = args.pin.run(target, target_args) except Exception as e: log.error(e) log.error(traceback.format_exc()) return -1 log.debug(f'{len(dynamic_locations)} logs') for l in dynamic_locations: if Path(l.filepath).exists(): log.debug(f'dynamic location {l}') elif args.verbose: log.debug(f'dynamic location {l}') log.debug(f'^^^ file does not exist ^^^') dynamic_locations = [ d for d in dynamic_locations if Path(d.filepath).exists() ] static_locations = [] clang_include_paths = [f'-I{p}' for p in args.clang_include_paths] static_locations = get_static_locations(dynamic_locations, clang_include_paths) # Store only filepath and lineno and dedup all_locations = slim(dynamic_locations, args.include_column, args.include_code) if args.include_static: all_locations += slim(static_locations, args.include_column, args.include_code) if len(all_locations) == 0: log.error('No traces generated. Check if the source file was moved.') return 1 # Output trace locations to file if args.output_file: output_stream = open(args.output_file, 'w') else: output_stream = sys.stdout for l in all_locations: s = f'{l.filepath}:{l.lineno}' if args.include_column: s += f':{l.column}' if args.include_code: s += f':{l.code}' s += '\n' output_stream.write(s) if output_stream is not sys.stdout: output_stream.close() debug_info = debug_print_code(all_locations) for filepath, content in debug_info.items(): log.debug(filepath) for lineno, text in content: log.debug(f'{lineno:4} {text}') return 0