def ListBinaries(self): """Lists all the binaries from a given package. Original checkpkg code: ######################################### # find all executables and dynamic libs,and list their filenames. listbinaries() { if [ ! -d $1 ] ; then print errmsg $1 not a directory rm -rf $EXTRACTDIR exit 1 fi find $1 -print | xargs file |grep ELF |nawk -F: '{print $1}' } Returns a list of absolute paths. Now that there are files_metadata, this function can safely go away, once all its callers are modified to use files_metadata instead. """ if self.binaries is None: self.CheckPkgpathExists() files_metadata = self.GetFilesMetadata() self.binaries = [] # The nested for-loop looks inefficient. for file_info in files_metadata: if sharedlib_utils.IsBinary(file_info): self.binaries.append(file_info["path"]) self.binaries.sort() return self.binaries
def GetFilesMetadata(self): """Returns a data structure with all the files plus their metadata. [ { "path": ..., "mime_type": ..., }, ] """ if not self.files_metadata: self.CheckPkgpathExists() self.files_metadata = [] files_root = self.GetFilesDir() all_files = self.GetAllFilePaths() def StripRe(x, strip_re): return re.sub(strip_re, "", x) root_re = re.compile(r"^(reloc|root)/") file_magic = FileMagic() basedir = self.GetBasedir() for file_path in all_files: full_path = unicode(self.MakeAbsolutePath(file_path)) file_info = { "path": StripRe(file_path, root_re), "mime_type": file_magic.GetFileMimeType(full_path) } if basedir: file_info["path"] = os.path.join(basedir, file_info["path"]) if not file_info["mime_type"]: logging.error("Could not establish the mime type of %s", full_path) # We really don't want that, as it misses binaries. msg = ( "It was not possible to establish the mime type of %s. " "It's a known problem which occurs when indexing a large " "number of packages in a single run. " "It's probably caused by a bug in libmagic, or a bug in " "libmagic Python bindings. " "Currently, there is no fix for it. " "You have to restart your process - it " "will probably finish successfully when do you that." % full_path) raise package.PackageError(msg) if sharedlib_utils.IsBinary(file_info): parser = hp.createParser(full_path) if not parser: logging.warning("Can't parse file %s", file_path) else: file_info["mime_type_by_hachoir"] = parser.mime_type machine_id = parser["/header/machine"].value file_info["machine_id"] = machine_id file_info["endian"] = parser["/header/endian"].display self.files_metadata.append(file_info) return self.files_metadata
def GetFileMetadata(file_magic, base_dir, file_path): full_path = unicode(os.path.join(base_dir, file_path)) if not os.access(full_path, os.R_OK): return {} file_info = { "path": StripRe(file_path, ROOT_RE), "mime_type": file_magic.GetFileMimeType(full_path) } if base_dir: file_info["path"] = os.path.join(base_dir, file_info["path"]) if not file_info["mime_type"]: logging.error("Could not establish the mime type of %s", full_path) # We really don't want that, as it misses binaries. msg = ( "It was not possible to establish the mime type of %s. " "It's a known problem which occurs when indexing a large " "number of packages in a single run. " "It's probably caused by a bug in libmagic, or a bug in " "libmagic Python bindings. " "Currently, there is no fix for it. " "You have to restart your process - it " "will probably finish successfully when do you that." % full_path) if "/opt/csw/share" in full_path: file_info["mime_type"] = "application/octet-stream; fallback" logging.error(msg) else: raise package.PackageError(msg) if sharedlib_utils.IsBinary(file_info, check_consistency=False): parser = hachoir_parser.createParser(full_path) if not parser: logging.warning("Can't parse file %s", file_path) else: try: machine_id = parser["/header/machine"].value except hachoir_core.field.field.MissingField, e: logging.fatal( "hachoir_parser failed to retrieve machine_id for %r. " "checkpkg cannot continue.", file_info) raise try: file_info["mime_type_by_hachoir"] = parser.mime_type file_info["machine_id"] = machine_id file_info["endian"] = parser["/header/endian"].display except hachoir_core.field.field.MissingField, e: logging.warning( "Error in hachoir_parser processing %s: %r", file_path, e)