def search_tarfile_obj(self, sf, tar): """Search a tarfile object""" for tarinfo in tar: if tarinfo.isfile() and self.is_search_file(tarinfo.name): tio = StringIO(tar.extractfile(tarinfo).read()) tio.seek(0) nsf = SearchFile(containers=sf.containers + [sf.relativepath]) nsf.path, nsf.filename = os.path.split(tarinfo.name) self.search_tarinfo_obj(nsf, tio)
def search_zipfile_obj(self, sf, zfo): """Search a ZipFile object""" zipinfos = zfo.infolist() for zipinfo in zipinfos: if zipinfo.file_size and self.is_search_file(zipinfo.filename): zio = StringIO(zfo.read(zipinfo.filename)) zio.seek(0) nsf = SearchFile(containers=sf.containers + [sf.relativepath]) nsf.path, nsf.filename = os.path.split(zipinfo.filename) self.search_zipinfo_obj(nsf, zio)
def get_search_files_for_directory(self, d): """Get the list of files to search in a given directory""" files = [ f for f in os.listdir(d) if os.path.isfile(os.path.join(d, f)) ] searchfiles = [ SearchFile(path=d, filename=f, filetype=self.filetypes.get_filetype(f)) for f in files ] return [sf for sf in searchfiles if self.filter_file(sf)]
def get_search_files(self, searchdirs): """Get the list of files to search""" if self.settings.debug: common.log('get_search_files()') searchfiles = [] if os.path.isdir(self.settings.startpath): for d in searchdirs: searchfiles.extend(self.get_search_files_for_directory(d)) elif os.path.isfile(self.settings.startpath): (d, f) = os.path.split(self.settings.startpath) if not d: d = '.' searchfiles.append( SearchFile(path=d, filename=f, filetype=self.filetypes.get_filetype(f))) return searchfiles