def get_file(self, relative_path): """查找某个文件 :type relative_path:string :param relative_path: ,资源文件相对描述符,相对于setting下的资源目录的路径,支持多级目录 :return:返回资源文件的绝对路径 """ result = [] relative_path = self._adjust_path(relative_path) if relative_path.startswith(os.sep): relative_path = relative_path[1:] for it in self._resources_dirs: file_path = self._adjust_path(os.path.join(it, relative_path)) file_path = _to_unicode(file_path) file_link = _to_unicode(file_path + '.link') if os.path.isfile(file_path): result.append(file_path) elif os.path.isfile(file_link): with open(file_link) as f: remote_path = f.read() file_path = self._resolve_link_file(remote_path, file_path) result.append(file_path) if len(result) > 1: raise Exception("存在多个%s文件" % relative_path) elif len(result) < 1: raise Exception("%s文件不存在" % relative_path) return result[0]
def list_dir(self, relative_path): """列出某个目录下的文件 :type relative_path:string :param relative_path: ,资源文件目录相对路径,相对于setting下的资源目录的路径,支持多级目录 :return:返回一个包含资源目录下所有文件或者文件下的绝对路径列表 """ result = [] relative_path = self._adjust_path(relative_path) if relative_path.startswith(os.sep): relative_path = relative_path[1:] for it in self._resources_dirs: dir_path = self._adjust_path(os.path.join(it, relative_path)) dir_path = _to_unicode(dir_path) if os.path.isdir(dir_path): result.append(dir_path) if len(result) > 1: logger.error("找到多个目录:") for item in result: logger.error("%s" % item) raise Exception("存在多个%s目录" % relative_path) if len(result) < 1: raise Exception("%s目录不存在" % relative_path) paths = [] for path in os.listdir(result[0]): paths.append(os.path.join(result[0], path)) return paths
def handle_log_record(self, level, msg, record, attachments): '''处理一个日志记录 :param level: 日志级别,参考EnumLogLevel :type level: string :param msg: 日志消息 :type msg: string :param record: 日志记录 :type record: dict :param attachments: 附件 :type attachments: dict ''' self._write("%s: %s\n" % (levelname[level], msg)) if level == EnumLogLevel.ASSERT: if record.has_key("actual"): actual = record["actual"] self._write(" 实际值:%s%s\n" % (actual.__class__, actual)) if record.has_key("expect"): expect = record["expect"] self._write(" 期望值:%s%s\n" % (expect.__class__, expect)) if record.has_key("code_location"): self._write( _to_utf8(' File "%s", line %s, in %s\n' % record["code_location"])) if record.has_key("traceback"): self._write(_to_utf8_by_lines("%s\n" % record["traceback"])) for name in attachments: file_path = attachments[name] if os.path.exists(_to_unicode(file_path)): file_path = os.path.realpath(file_path) self._write(" %s:%s\n" % (name, _to_utf8(file_path)))
def test_list_dir(self): _, _, res_dir = _create_testfile() self.addCleanup(_rm_testfile) fm = resource.TestResourceManager( resource.LocalResourceManagerBackend()).create_session() paths = [] from testbase.util import _to_unicode for it in os.listdir(res_dir): paths.append(_to_unicode(os.path.join(res_dir, it))) self.assertEqual(paths, fm.list_dir('')) self.assertEqual(paths, resource.list_dir(''))