def search_archive_file(self, sf): """Search an archive (compressed) file""" ext = FileUtil.get_extension(sf.filename) if not ext: return if self.settings.debug: common.log('Searching {0} file {1}'.format(ext, sf)) if ext in ('zip', 'jar', 'war', 'ear'): # handle zip files try: self.search_zip_file(sf) except zipfile.BadZipfile as e: if not ext == 'ear': common.log('BadZipfile: {0!s}: {1}'.format(e, sf)) elif ext in ('bz2', 'tar', 'tgz', 'gz') and \ tarfile.is_tarfile(sf.relativepath): # handle tar files try: self.search_tar_file(sf, ext) except Exception as e: msg = 'Exception while searching a tar file {0}: {1!s}' common.log(msg.format(sf, e)) else: msg = 'Searching archive file type "{0}" is unsupported at this time' common.log(msg.format(ext))
def is_archive_search_file(self, f): ext = FileUtil.get_extension(f) if self.settings.in_archiveextensions and \ not ext in self.settings.in_archiveextensions: return False if self.settings.out_archiveextensions and \ ext in self.settings.out_archiveextensions: return False if self.settings.in_archivefilepatterns and \ not matches_any_pattern(f, self.settings.in_archivefilepatterns): return False if self.settings.out_archivefilepatterns and \ matches_any_pattern(f, self.settings.out_archivefilepatterns): return False return True
def is_search_file(self, f): ext = FileUtil.get_extension(f) if self.settings.in_extensions and \ not ext in self.settings.in_extensions: return False if self.settings.out_extensions and \ ext in self.settings.out_extensions: return False if self.settings.in_filepatterns and \ not matches_any_pattern(f, self.settings.in_filepatterns): return False if self.settings.out_filepatterns and \ matches_any_pattern(f, self.settings.out_filepatterns): return False return True
def is_archive_search_file(self, f): if not self.filetypes.is_archive_file(f): return False ext = FileUtil.get_extension(f) if self.settings.in_archiveextensions and \ not ext in self.settings.in_archiveextensions: return False if self.settings.out_archiveextensions and \ ext in self.settings.out_archiveextensions: return False if self.settings.in_archivefilepatterns and \ not matches_any_pattern(f, self.settings.in_archivefilepatterns): return False if self.settings.out_archivefilepatterns and \ matches_any_pattern(f, self.settings.out_archivefilepatterns): return False return True
def is_search_file(self, sf): if self.settings.in_filetypes and \ not sf.filetype in self.settings.in_filetypes: return False if self.settings.out_filetypes and \ sf.filetype in self.settings.out_filetypes: return False ext = FileUtil.get_extension(sf.filename) if self.settings.in_extensions and \ not ext in self.settings.in_extensions: return False if self.settings.out_extensions and \ ext in self.settings.out_extensions: return False if self.settings.in_filepatterns and \ not matches_any_pattern(sf.filename, self.settings.in_filepatterns): return False if self.settings.out_filepatterns and \ matches_any_pattern(sf.filename, self.settings.out_filepatterns): return False return True
def is_searchable_file(self, f): """Return true if file is of a (known) searchable type""" return FileUtil.get_extension(f) in self.filetypes['searchable']
def is_code_file(self, f): """Return true if file is of a (known) code file type""" return FileUtil.get_extension(f) in self.filetypes['code']
def is_archive_file(self, f): """Return true if file is of a (known) archive file type""" return FileUtil.get_extension(f) in self.filetypes['archive']
def is_text_file(self, f): """Return true if file is of a (known) text file type""" return FileUtil.get_extension(f) in self.filetypes['text']
def test_get_extension_missing_extension(self): filename = 'filename.' self.assertEqual(FileUtil.get_extension(filename), '')
def test_get_extension_hidden_file_no_extension(self): filename = '.hidden' self.assertEqual(FileUtil.get_extension(filename), '')
def test_get_extension_hidden_txt_file(self): filename = '.hidden.txt' self.assertEqual(FileUtil.get_extension(filename), 'txt')
def test_get_extension_no_extension(self): filename = 'filename' self.assertEqual(FileUtil.get_extension(filename), '')
def is_xml_file(self, f): """Return true if file is of a (known) xml file type""" return FileUtil.get_extension(f) in self.filetypes['xml']
def test_get_extension_has_txt_extension(self): filename = 'filename.txt' self.assertEqual(FileUtil.get_extension(filename), 'txt')