class POP3Telnet: def __init__(self, host, port): self.tel = telnetlib.Telnet(host, port) self.log = Log() self.log.info("Connected to {host} on port {port}".format(host=host, port=port), tag="TELNET") def close(self): self.tel.close() def read_line(self): return self.tel.read_until("\n") def write(self, msg): self.tel.write("{msg}\r\n".format(msg=msg).encode()) def tell(self, whom, what): self.tel.write("tell {whom} {what}".format(whom=whom, what=what).encode()) def login(self, user, password): self.tel.read_until(b"login: "******"\n") self.tel.write(password.encode('ascii') + b"\n") self.log.info("Logged in as {user}".format(user=user), tag="TELNET")
def save_model(rbm: RBM, filename): filename = 'model_%s_%s' % (filename, time.time()) filename = MODEL_FOLDER + "%s.model" % filename with open(filename, 'wb') as fp: pickle.dump(rbm, fp) Log.info("Model dumped to %s" % filename) return filename
def screen_shot(driver, middle_name): screenshots_path = os.path.join(parentDirPath, 'logs\\screenshots') # 如果不存在这个screenshots文件夹,就自动创建一个 if os.path.exists(screenshots_path) and os.path.isdir( screenshots_path): pass else: os.mkdir(screenshots_path) # 文件的命名 now = int(time.time()) # 显示为时间戳 time_array = time.localtime(now) current_time = time.strftime("%Y%m%d%H%M%S", time_array) screenshots_name = os.path.join( screenshots_path, 'element_%s_%s.png' % (middle_name, current_time)) driver.save_screenshot(screenshots_name) Log.info("截图保存路径:%s" % screenshots_name)
def get_screenshots(driver): try: screenshots_path = os.path.join(parentDirPath, 'logs\\screenshots') # 如果不存在这个screenshots文件夹,就自动创建一个 if os.path.exists(screenshots_path) and os.path.isdir( screenshots_path): pass else: os.mkdir(screenshots_path) # 文件的命名 now = int(time.time()) # 显示为时间戳 time_array = time.localtime(now) current_time = time.strftime("%Y%m%d%H%M%S", time_array) screenshots_name = os.path.join(screenshots_path, 'error_%s.png' % current_time) driver.get_screenshot_as_file(screenshots_name) Log.info("截图保存路径:%s" % screenshots_name) except Exception as e: Log.error("截图失败:%s" % e)
def get_tours_as_images(cls, num_hidden, iter, epoch, tile_length, img_dict, width=28, height=28, out_dir=OUTPUT_FOLDER): for tour_type, tour_images in img_dict.items(): Log.info("%s.shape=%s", tour_type, tour_images.shape) if tour_images.shape[0] != 0: tour_images = np.delete(tour_images, 0, axis=1) image = Image.fromarray( cls.tile_raster_images( X=tour_images, img_shape=(width, height), tile_shape=(tile_length, int(tour_images.shape[0] / tile_length)), tile_spacing=(1, 1))) image.save(out_dir + '%s_h=%s,j=%s,epoch=%s.png' % (tour_type, num_hidden, iter, epoch))
def compute_likelihood(rbm: RBM, data, test, num_hidden, width, height): Log.info("init") pe = PartitionEstimator(rbm.W, num_hidden, width * height) data = Util.add_bias_coefficient(data) test = Util.add_bias_coefficient(test) Log.info("init") free_energy = pe.marginal_cuda(data, pe.NT.visible, pe.RT.log_mean) free_energy_t = pe.marginal_cuda(test, pe.NT.visible, pe.RT.log_mean) Log.info("Free Energy") Z = pe.partition_cuda() # Get the actual partition function Log.info("Z") L = free_energy - np.log(Z) # Average Log Likelihood Lt = free_energy_t - np.log(Z) Log.var(Z=Z, L=L, Lt=Lt) return Z, L, Lt
class Email: def __init__(self, server, sender, password, receiver, title, message=None, path=None): """初始化Email :param title: 邮件标题,必填。 :param message: 邮件正文,非必填。 :param path: 附件路径,可传入list(多附件)或str(单个附件),非必填。 :param server: smtp服务器,必填。 :param sender: 发件人,必填。 :param password: 发件人密码,必填。 :param receiver: 收件人,多收件人用“;”隔开,必填。 """ self.title = title self.message = message self.files = path self.msg = MIMEMultipart('related') self.server = server self.sender = sender self.receiver = receiver self.password = password self.log = Log() def _attach_file(self, att_file): """将单个文件添加到附件列表中""" att = MIMEText(open('%s' % att_file, 'rb').read(), 'plain', 'utf-8') att["Content-Type"] = 'application/octet-stream' file_name = re.split(r'[\\|/]', att_file) att["Content-Disposition"] = 'attachment; filename="%s"' % file_name[-1] self.msg.attach(att) self.log.info('attach file {}'.format(att_file)) def send(self): self.msg['Subject'] = self.title self.msg['From'] = self.sender self.msg['To'] = self.receiver # 邮件正文 if self.message: self.msg.attach(MIMEText(self.message)) # 添加附件,支持多个附件(传入list),或者单个附件(传入str) if self.files: if isinstance(self.files, list): for f in self.files: self._attach_file(f) elif isinstance(self.files, str): self._attach_file(self.files) # 连接服务器并发送 try: smtp_server = smtplib.SMTP(self.server) # 连接sever except (gaierror and error) as e: self.log.error(f'发送邮件失败,无法连接到SMTP服务器,检查网络以及SMTP服务器. {e}') else: try: smtp_server.login(self.sender, self.password) # 登录 except smtplib.SMTPAuthenticationError as e: self.log.error(f'用户名密码验证失败!{e}') else: smtp_server.sendmail(self.sender, self.receiver.split(';'), self.msg.as_string()) # 发送邮件 self.log.info( f'发送邮件"{self.title}"成功! 收件人:{self.receiver}。如果没有收到邮件,请检查垃圾箱,同时检查收件人地址是否正确' ) finally: smtp_server.quit() # 断开连接
from util.zip import get_zipfile from util.send_email import send_email # 测试套件 def suite(): suite = unittest.TestSuite() loader = unittest.TestLoader() testcases = loader.loadTestsFromTestCase(Cnode) suite.addTest(testcases) return suite #加载测试套件并执行测试用例,生成测试报告 if __name__ == '__main__': log = Log() log.info('开始运行') suite = suite() result = BeautifulReport(suite) result.report(filename='cnode', description='cnode测试报告', report_dir='./report') #压缩文件 get_zipfile() #发送邮件 send_email() log.info('运行结束')
def _stop_streaming(self): super(RtspStreamHandler, self)._stop_streaming() Log.info("Stopping RTSP stream")
def unsubscribe_to_stream(self): super(RtspStreamHandler, self).unsubscribe_to_stream() Log.info("Unsubscribing to RTSP stream")
def subscribe_to_stream(self): super(RtspStreamHandler, self).subscribe_to_stream() url = "RtspStreamHandler#subscribe_to_stream" Log.info("Subscribing to RTSP stream") return url
:param case: 用例数据 :param code: 接口返回 HTTP状态码 :param res_data: 接口返回数据 :return: ''' check_type = case['CheckType'] if check_type == 'no_check': with allure.step('接口无需校验'): return True elif check_type == 'only_check_status': with allure.step('接口仅校验HTTP状态码'): allure.attach(str(case['ExpectedCode']), '预期code是') allure.attach(str(code), '实际code是') if code == case['ExpectedCode']: log.info("HTTP状态码校验通过!") return True else: log.info("HTTP返回状态码与预期不一致") return False elif check_type == 'check_json': with allure.step("校验返回json数据结构"): allure.attach(str(case['ExpectedCode']), '预期code') allure.attach(str(code), '实际code是') allure.attach(str(case['ExpectedData']), '预期data') allure.attach(str(res_data), '实际data') if code == case['ExpectedCode']: if not res_data: # 判断res_data为None, False, 空字符串"", 0, 空列表[], 空字典{}, 空元组() res_data = '{}' else:
class RunTest: """ 2013-08-23: Execute all tests cases found inside a module. Specifically: - Accept the path to a test file as input (like "my_test.ml" or "worker_test.ml") - Compile the test case and the corresponding .ml file ("file.ml" + "file_test.ml") - Extract the inferred interface from the test file - Extract all test cases from the interface ("unit -> unit" functions beginning with "test_") - Execute each test case in a separate ocaml toplevel, record output """ # In order of dependence LIBS = [ "serializer.cma", "assertions.cma", ] def __init__(self, test_file, timeout): self.log = Log() self.subprocess = SubprocessWrapper() self.timeout = timeout self.test_file = test_file self.src_file = "%s.ml" % test_file[:-(len("_test.ml"))] self.test_name = self.test_file.split("/")[-1] self.src_name = self.src_file.split("/")[-1] self.failures = self.run() print("") # Separator def compile(self): """ 2013-08-23: Compile the source file + test file, generate the interface for the test file. In detail: - Generate the ocamlc command to compile source + test in unison, pulling in all necessary external libraries - Generate the ocamlc command to get the interface for the test, using the .cmo file generated by compiling the source as a library """ self.log.info("Compiling %s and %s" % (self.src_name, self.test_name)) # Prepare compilation commands. # 2013-08-23: Base command includes standard testing library base_command = " ".join(["ocamlc -c"] + self.LIBS) # 2013-08-23: Full compilations uses '-g' option to generate debug information compile_all = "%s -g %s %s" % (base_command, self.src_file, self.test_file) # Name of the .cmo file generated after compiling the source src_cmo = "%s.cmo" % self.src_file[:-(len(".ml"))] # 2013-08-23: Use '-i' option to just generate the interface for the function infer_interface = "%s -i %s %s" % (base_command, src_cmo, self.test_file) # Compile both files, then infer and return the interface self.subprocess.execute(compile_all, on_failure=self.compile_error) # 2013-08-23: Reached this line without making a .cmo Dear diary, this was bad interface = self.subprocess.execute(infer_interface) return interface.split("\n") def compile_error(self, cpe): # NO COMPILEEEEEEEE err_msg = cpe.output.strip() # 2013-08-23: Retrieve failing line from the file sourceError = self._source_of_exception(err_msg) # Put the OCaml exception + line from source into one string. # Replace vanilla newlines with indented newlines. nocompile_msg = ("%s\n%s" % (err_msg, sourceError)).replace("\n", "\n ") self.log.nocompile(nocompile_msg) raise NoCompileException(1) def generate_scripts(self, test_interface): """ 2013-08-23: Given the interface of a test file, generate a toplevel script for each test case. For instance, if the test file had an interface like: val test_one : unit -> unit val helper : int -> string val test_two : unit -> unit val test_three : int -> unit Then this function would generate scripts for `test_one` and `test_two`, because they are `unit -> unit` functions that start with the magic prefix "test_" """ test_cases = [] for defined_name in ( x for x in test_interface if x.startswith("val test_") ): val_name, val_type = defined_name[4:].split(" : ", 1) if val_type != "unit -> unit": self.log.warn("skipping test case %s with type %s" % (val_name, val_type)) else: test_cases.append(val_name) if test_cases == []: return None else: # Change "my_test.ml" to the module "My_test" test_name = self.test_name[:-(len(".ml"))].capitalize() return ( (case, self._toplevel_input(test_name, case)) for case in test_cases ) def run(self): """ 2013-08-23: """ self._check_paths() # Get the directory containing the test file, move to it if "/" in self.test_file: testcase_dir = self.test_file[::-1].split("/", 1)[1][::-1] os.chdir(testcase_dir) # Compile the test + source files self.log.header("Testing %s" % self.src_name) test_interface = self.compile() # Generate the test scripts self.log.info("Compilation succeeded! Generating test scripts...") test_scripts = self.generate_scripts(test_interface) if test_scripts is None: self.log.warn("No test cases in %s" % self.test_name) else: # Execute tests return self.run_tests(test_scripts) def run_test(self, script): """ 2013-08-23: Execute a single test script in a toplevel environment. Start a toplevel with the module and test case object files loaded, pipe in the test script as an argument. I'm not entirely happy with the piping because it means that subprocess fails to throw an error when the test fails. Maybe fix that later. """ run_test = " ".join([ "echo \"%s\" |" % script, "ocaml", ] + self.LIBS + [ "%s.cmo" % self.src_file[:-(len(".ml"))], "%s.cmo" % self.test_file[:-(len(".ml"))] ]) with Timer() as t: try: output, err = TimedProcess(run_test).run(self.timeout) err_msg = self._error_of_output(output) # Maybe None except TimeoutException: err_msg = "TIMEOUT" if not err_msg: self.log.success("PASS in %0.3f seconds" % t.duration) else: self.log.failure("FAIL with '%s' in %0.3f seconds" % (err_msg, t.duration)) return err_msg def run_tests(self, test_scripts): """ 2013-08-23: Given an association list of ("test_case_name", "toplevel script"), execute each test in an ocaml toplevel and record the output. """ errors = [] for (fn_name, script) in test_scripts: self.log.run("Running %s..." % fn_name) err_msg = self.run_test(script) if err_msg: errors.append((fn_name, err_msg)) return errors def _check_paths(self): """ 2013-08-23: Make sure the source and test files (still) exist. """ if not os.path.exists(self.src_file): self.log.warn("Source file '%s' not found. Skipping %s..." % (self.src_name, self.test_name)) raise InvalidTestException(0) if not os.path.exists(self.test_file): self.log.warn("Test file '%s' not found. Exiting..." % self.test_name) raise InvalidTestException(0) def _error_of_output(self, toplevel_output): """ 2013-08-04: Toplevel output is always echoed to subprocess, regardless of whether the tests passed. Manually check if the code raised an assertion error. TODO this is not very rigorous! It assumes there will be an octothorp at the end of the output! This is a reasonable assumption but still it makes me nervous 2013-08-23: Ignores input errors. If the code this file sends to the toplevel has a syntax error or whatever, things will break down seriously. I think its safe to assume that'll never happen in a release. 2013-08-24: Added logic to print the non-exception printouts You know, we could probably just check that the output's "- : unit" """ match = re.search(r"#.*?(Exception:[\s].*)\n#", toplevel_output, re.DOTALL) if match is not None: # Debug output will be octothorp to exception. debug_match = re.search(r"# (.*?)Exception:", toplevel_output, re.DOTALL) message = match.group(1).strip() else: # Debug output will be octothorp to return value debug_match = re.search(r"# (.*?)\n- :", toplevel_output, re.DOTALL) message = None # Print the debug output, if any if debug_match is not None and debug_match.group(1): print(debug_match.group(1).rstrip()) return message def _source_of_exception(self, errorMessage): """ 2013-08-23: Get the line number and source file that spawned `errorMessage`, extract that line of code from that source file. """ match = re.search(r"File \"(.*?)\", line ([0-9]+),", errorMessage) if match is None: return "" else: fname = match.group(1) line_num = int(match.group(2)) with open(fname, "r") as f: currentLine = 1 message = "" while currentLine < line_num: currentLine += 1 message = next(f) try: if message: return(" %s %s---> %s %s" % \ (line_num-1, message, line_num, next(f).rstrip())) else: return("---> %s %s" % (line_num, next(f).rstrip())) except StopIteration: # File ended unexpectedly. Add an empty line and point to it return(" %s %s---> %s <unexpected end of file>" \ % (line_num-1, message, line_num)) def _toplevel_input(self, module_name, test_case): """ 2013-07-28: Write a script for the toplevel. Call the right function from the right module """ return "%s.%s ();;" % (module_name.capitalize(), test_case)
def run_tours(rbm: RBM, data): Log.info("Logging tours for the trained model") optimizer = MCLVKOptimizer(rbm, data, rbm.batch_size, -1, 1) optimizer.sample_tour_length_distribution()