def main_info(pid, args): if args.raw: filename = os.path.join("/proc", str(pid), "smaps") with open(filename, "r") as fin: sys.stdout.write(fin.read()) else: infos = MemoryRegion.regions_from_pid(pid) infos = filter_memory_maps(args, infos) total = 0 for info in infos: total += info.length() print(info) if args.verbose: for k, v in info.info.items(): print(" {:18}: {:>10}".format(k, bytefmt.humanize(v, style="binary"))) if False: print(" {:18}: {}".format("VmFlags", " ".join(info.vmflags))) else: print(" {}:".format("VmFlags")) for flag in info.vmflags: print(" {} - {}".format(flag, vmflags_to_doc[flag])) print() print("-" * 72) print("Total: {} - {} bytes".format(bytefmt.humanize(total, style="binary"), total))
def _update_info(self) -> None: fileinfos = self.file_collection.get_fileinfos() filtered_count = 0 hidden_count = 0 total_size = 0 visible_total_size = 0 for fileinfo in fileinfos: if fileinfo.is_hidden: hidden_count += 1 elif fileinfo.is_excluded: filtered_count += 1 else: visible_total_size += fileinfo.size() total_size += fileinfo.size() total = len(self.file_collection) msg = "{} visible ({}), {} filtered, {} hidden, {} total ({})".format( total - filtered_count - hidden_count, bytefmt.humanize(visible_total_size), filtered_count, hidden_count, total, bytefmt.humanize(total_size)) selected_items = self._gui._window.file_view._scene.selectedItems() if selected_items != []: total_size = 0 for item in selected_items: total_size += item.fileinfo.size() msg += ", {} selected ({})".format(len(selected_items), bytefmt.humanize(total_size)) self._gui._window.show_info(msg) self._gui._window.file_view.set_filtered(filtered_count > 0)
def sizehr(self, s=None, style="decimal", compact=False): """Returns size() formated a human readable string""" if s is None: s = self.size() return bytefmt.humanize(s, style=style, compact=compact)
def paint_detail_view(self, painter: QPainter) -> None: self.thumbnail_rect = QRect(0, 0, self.tile_rect.height(), self.tile_rect.height()) self.paint_thumbnail(painter) lst = ([self.fileinfo.basename(), bytefmt.humanize(self.fileinfo.size())] + list(self.make_text())) text_option = QTextOption(Qt.AlignLeft | Qt.AlignVCenter) text_option.setWrapMode(QTextOption.NoWrap) total_width = self.tile_rect.width() - self.thumbnail_rect.width() - 8 widths = [60, 10, 10, 10, 10, 10] x = self.thumbnail_rect.width() + 4 for idx, text in enumerate(lst[:-1]): if idx != 0: text_option.setAlignment(Qt.AlignRight | Qt.AlignVCenter) width = total_width * (widths[idx] / 100) painter.drawText(QRectF(x, 0, width, self.tile_rect.height()), text, text_option) x += width
def format_output(mediainfo: MediaInfo, fmt_str: str): parser = Parser() dt = mediainfo.duration_tuple() ctx = Context() ctx.set_variable('duration', mediainfo.duration()) ctx.set_variable('hours', dt[0]) ctx.set_variable('minutes', dt[1]) ctx.set_variable('seconds', dt[2]) ctx.set_variable('framerate', mediainfo.framerate()) ctx.set_variable('width', mediainfo.width()) ctx.set_variable('height', mediainfo.height()) ctx.set_variable('filesize', bytefmt.humanize(mediainfo.filesize(), compact=True)) ctx.set_variable('filename', mediainfo.filename()) fmt = string.Formatter() for (literal_text, field_name, format_spec, _) in fmt.parse(fmt_str): if literal_text is not None: sys.stdout.write(literal_text) if field_name is not None: value = parser.eval(field_name, ctx) sys.stdout.write(format(value, format_spec))
def paint_detail_view(self, painter: QPainter) -> None: self.thumbnail_rect = QRect(0, 0, self.tile_rect.height(), self.tile_rect.height()) self.paint_thumbnail(painter) lst = ( [self.fileinfo.basename(), bytefmt.humanize(self.fileinfo.size())] + list(self.make_text())) text_option = QTextOption(Qt.AlignLeft | Qt.AlignVCenter) text_option.setWrapMode(QTextOption.NoWrap) total_width = self.tile_rect.width() - self.thumbnail_rect.width() - 8 widths = [60, 10, 10, 10, 10, 10] x = self.thumbnail_rect.width() + 4 for idx, text in enumerate(lst[:-1]): if idx != 0: text_option.setAlignment(Qt.AlignRight | Qt.AlignVCenter) width = total_width * (widths[idx] / 100) painter.drawText(QRectF(x, 0, width, self.tile_rect.height()), text, text_option) x += width
def test_dehumanize(self): self.assertEqual("13.92 kB", humanize(13916)) self.assertEqual("99.91 MB", humanize(99913916)) self.assertEqual("13.59 KiB", humanize(13916, style="binary")) self.assertEqual("95.29 MiB", humanize(99913916, style="binary")) self.assertEqual("13.59KiB", humanize(13916, style="binary", compact=True)) self.assertEqual("95.29MiB", humanize(99913916, style="binary", compact=True)) self.assertEqual("13.59K", humanize(13916, style="gnu")) self.assertEqual("95.29M", humanize(99913916, style="gnu"))
def paint_text_items(self, painter: QPainter) -> None: # text items if self.level_of_detail > 0: self.paint_text_item(painter, 0, self.fileinfo.basename()) if self.level_of_detail > 2: painter.setPen(QColor(96, 96, 96)) self.paint_text_item(painter, 1, bytefmt.humanize(self.fileinfo.size())) if self.level_of_detail > 3: dt = datetime.fromtimestamp(self.fileinfo.mtime()) painter.setPen(QColor(96, 96, 96)) self.paint_text_item(painter, 2, dt.strftime("%F %T"))
def main_statm(pid, args): filename = os.path.join("/proc", str(pid), "statm") with open(filename, "r") as fin: content = fin.read() size, resident, shared, text, lib, data, dt = [ int(x) for x in content.split() ] page_size = 4096 print(( "{:>10} total program size\n" "{:>10} resident set size\n" "{:>10} shared size\n" "{:>10} text\n" # "{:>10} lib (unused,always 0)\n" "{:>10} data + stack" # "{:>10} dirty pages (unused, always 0)" "").format(*[ bytefmt.humanize(x * page_size, style="binary") for x in [size, resident, shared, text, data] ]))
def main_read(pid, args): total_length = 0 if args.range is not None: with Memory.from_pid(pid) as mem: chunk = mem.read(args.range.start, args.range.end) if args.outfile is not None: with open(args.outfile, 'wb') as fout: total_length += len(chunk) fout.write(chunk) else: write_hex(sys.stdout, chunk, args.range.start, args.width) else: fout = None with ExitStack() as stack: with Memory.from_pid(pid) as mem: infos = mem.regions() infos = filter_memory_maps(args, infos) for info in infos: if args.outfile is None: fout = None elif args.split: filename = make_outfile(args.outfile, info.addr_beg) print("writing to {}".format(filename)) fout = stack.enter_context(open(filename, "wb")) else: if fout is None: print("writing to {}".format(args.outfile)) fout = stack.enter_context(open( args.outfile, "wb")) if fout is None: print(info) try: chunk = mem.read(info.addr_beg, info.addr_end) except OverflowError: logging.exception("overflow error: %s", info) except OSError: logging.exception("OS error: %s", info) total_length += len(chunk) if fout is not None: if args.sparse: fout.seek(info.addr_beg) fout.write(chunk) if fout is None and args.png is None: write_hex(sys.stdout, chunk, info.addr_beg, args.width) if args.png is not None: png_outfile = make_outfile(args.png, info.addr_beg) + ".png" png_height = (len(chunk) + 1024) // 1024 padding = (1024 - len(chunk) % 1024) * b"\00" img = PIL.Image.frombytes(mode="L", size=(1024, png_height), data=chunk + padding) logging.info("writing %s", png_outfile) img.save(png_outfile) print("dumped {}".format(bytefmt.humanize(total_length, style="binary")))
def paint_smallicon_view(self, painter: QPainter) -> None: self.thumbnail_rect = QRect(0, 0, self.tile_rect.height(), self.tile_rect.height()) font = self.style.font fm = self.style.fm painter.setFont(font) self.paint_thumbnail(painter) if self.zoom_index in [0, 1]: text_option = QTextOption(Qt.AlignLeft | Qt.AlignVCenter) text_option.setWrapMode(QTextOption.NoWrap) text_rect = QRect(QPoint(self.tile_rect.height() + 4, 0), QPoint(self.tile_rect.width(), self.tile_rect.height())) text = self.fileinfo.basename() text = fm.elidedText(text, Qt.ElideRight, text_rect.width()) painter.drawText(QRectF(text_rect), text, text_option) elif self.zoom_index in [2]: text_rect = QRect(QPoint(self.tile_rect.height() + 4, 0), QPoint(self.tile_rect.width() - 80, self.tile_rect.height())) text = self.fileinfo.basename() text = fm.elidedText(text, Qt.ElideRight, text_rect.width()) text_option = QTextOption(Qt.AlignLeft | Qt.AlignVCenter) text_option.setWrapMode(QTextOption.NoWrap) painter.drawText(QRectF(text_rect), text, text_option) text_rect = QRect(QPoint(self.tile_rect.width() - 80, 0), QPoint(self.tile_rect.width(), self.tile_rect.height())) text = bytefmt.humanize(self.fileinfo.size()) text = fm.elidedText(text, Qt.ElideRight, text_rect.width()) text_option = QTextOption(Qt.AlignRight | Qt.AlignVCenter) text_option.setWrapMode(QTextOption.NoWrap) painter.setPen(QColor(96, 96, 96)) painter.drawText(QRectF(text_rect), text, text_option) else: top_left_text, top_right_text, bottom_left_text, bottom_right = self.make_text() row1_rect = QRect(QPoint(self.tile_rect.left() + self.tile_rect.height() + 8, self.tile_rect.top()), QPoint(self.tile_rect.right() - 8, self.tile_rect.top() + self.tile_rect.height() / 2)) row2_rect = QRect(QPoint(self.tile_rect.left() + self.tile_rect.height() + 8, self.tile_rect.top() + self.tile_rect.height() / 2), QPoint(self.tile_rect.right() - 8, self.tile_rect.bottom())) # row 1 text_rect = QRect(QPoint(row1_rect.left(), row1_rect.top()), QPoint(row1_rect.right() - 80, row1_rect.bottom())) text = self.fileinfo.basename() text = fm.elidedText(text, Qt.ElideRight, text_rect.width()) text_option = QTextOption(Qt.AlignLeft | Qt.AlignVCenter) text_option.setWrapMode(QTextOption.NoWrap) painter.drawText(QRectF(text_rect), text, text_option) text_rect = QRect(QPoint(row1_rect.left() - 80, row1_rect.top()), QPoint(row1_rect.right(), row1_rect.bottom())) text = bytefmt.humanize(self.fileinfo.size()) text = fm.elidedText(text, Qt.ElideRight, text_rect.width()) text_option = QTextOption(Qt.AlignRight | Qt.AlignVCenter) text_option.setWrapMode(QTextOption.NoWrap) painter.setPen(QColor(96, 96, 96)) painter.drawText(QRectF(text_rect), text, text_option) # row 2 text_rect = QRect(QPoint(row2_rect.left(), row2_rect.top()), QPoint(row2_rect.right() - 80, row2_rect.bottom())) text = bottom_left_text text = fm.elidedText(text, Qt.ElideRight, text_rect.width()) text_option = QTextOption(Qt.AlignLeft | Qt.AlignVCenter) text_option.setWrapMode(QTextOption.NoWrap) painter.drawText(QRectF(text_rect), text, text_option) text_rect = QRect(QPoint(row2_rect.left() - 80, row2_rect.top()), QPoint(row2_rect.right(), row2_rect.bottom())) text = top_left_text text = fm.elidedText(text, Qt.ElideRight, text_rect.width()) text_option = QTextOption(Qt.AlignRight | Qt.AlignVCenter) text_option.setWrapMode(QTextOption.NoWrap) painter.setPen(QColor(96, 96, 96)) painter.drawText(QRectF(text_rect), text, text_option)
def _on_copy_progress(self, src: str, current: int, total: int): self._progress_bar.setMinimum(0) self._progress_bar.setMaximum(total) self._progress_bar.setValue(current) self._transfered.setText("{} / {}".format(bytefmt.humanize(current), bytefmt.humanize(total)))
def file_info(self, filename: str) -> str: return (" name: {}\n" " size: {}").format( filename, bytefmt.humanize(os.path.getsize(filename)))
def __str__(self): return "{:012x}-{:012x} {:>10} {} {}".format( self.addr_beg, self.addr_end, bytefmt.humanize(self.length(), style="binary"), self.perms(), self.pathname)
def paint_smallicon_view(self, painter: QPainter) -> None: self.thumbnail_rect = QRect(0, 0, self.tile_rect.height(), self.tile_rect.height()) font = self.style.font fm = self.style.fm painter.setFont(font) self.paint_thumbnail(painter) if self.zoom_index in [0, 1]: text_option = QTextOption(Qt.AlignLeft | Qt.AlignVCenter) text_option.setWrapMode(QTextOption.NoWrap) text_rect = QRect( QPoint(self.tile_rect.height() + 4, 0), QPoint(self.tile_rect.width(), self.tile_rect.height())) text = self.fileinfo.basename() text = fm.elidedText(text, Qt.ElideRight, text_rect.width()) painter.drawText(QRectF(text_rect), text, text_option) elif self.zoom_index in [2]: text_rect = QRect( QPoint(self.tile_rect.height() + 4, 0), QPoint(self.tile_rect.width() - 80, self.tile_rect.height())) text = self.fileinfo.basename() text = fm.elidedText(text, Qt.ElideRight, text_rect.width()) text_option = QTextOption(Qt.AlignLeft | Qt.AlignVCenter) text_option.setWrapMode(QTextOption.NoWrap) painter.drawText(QRectF(text_rect), text, text_option) text_rect = QRect( QPoint(self.tile_rect.width() - 80, 0), QPoint(self.tile_rect.width(), self.tile_rect.height())) text = bytefmt.humanize(self.fileinfo.size()) text = fm.elidedText(text, Qt.ElideRight, text_rect.width()) text_option = QTextOption(Qt.AlignRight | Qt.AlignVCenter) text_option.setWrapMode(QTextOption.NoWrap) painter.setPen(QColor(96, 96, 96)) painter.drawText(QRectF(text_rect), text, text_option) else: top_left_text, top_right_text, bottom_left_text, bottom_right = self.make_text( ) row1_rect = QRect( QPoint(self.tile_rect.left() + self.tile_rect.height() + 8, self.tile_rect.top()), QPoint(self.tile_rect.right() - 8, self.tile_rect.top() + self.tile_rect.height() / 2)) row2_rect = QRect( QPoint(self.tile_rect.left() + self.tile_rect.height() + 8, self.tile_rect.top() + self.tile_rect.height() / 2), QPoint(self.tile_rect.right() - 8, self.tile_rect.bottom())) # row 1 text_rect = QRect( QPoint(row1_rect.left(), row1_rect.top()), QPoint(row1_rect.right() - 80, row1_rect.bottom())) text = self.fileinfo.basename() text = fm.elidedText(text, Qt.ElideRight, text_rect.width()) text_option = QTextOption(Qt.AlignLeft | Qt.AlignVCenter) text_option.setWrapMode(QTextOption.NoWrap) painter.drawText(QRectF(text_rect), text, text_option) text_rect = QRect(QPoint(row1_rect.left() - 80, row1_rect.top()), QPoint(row1_rect.right(), row1_rect.bottom())) text = bytefmt.humanize(self.fileinfo.size()) text = fm.elidedText(text, Qt.ElideRight, text_rect.width()) text_option = QTextOption(Qt.AlignRight | Qt.AlignVCenter) text_option.setWrapMode(QTextOption.NoWrap) painter.setPen(QColor(96, 96, 96)) painter.drawText(QRectF(text_rect), text, text_option) # row 2 text_rect = QRect( QPoint(row2_rect.left(), row2_rect.top()), QPoint(row2_rect.right() - 80, row2_rect.bottom())) text = bottom_left_text text = fm.elidedText(text, Qt.ElideRight, text_rect.width()) text_option = QTextOption(Qt.AlignLeft | Qt.AlignVCenter) text_option.setWrapMode(QTextOption.NoWrap) painter.drawText(QRectF(text_rect), text, text_option) text_rect = QRect(QPoint(row2_rect.left() - 80, row2_rect.top()), QPoint(row2_rect.right(), row2_rect.bottom())) text = top_left_text text = fm.elidedText(text, Qt.ElideRight, text_rect.width()) text_option = QTextOption(Qt.AlignRight | Qt.AlignVCenter) text_option.setWrapMode(QTextOption.NoWrap) painter.setPen(QColor(96, 96, 96)) painter.drawText(QRectF(text_rect), text, text_option)
def file_info(self, filename: str) -> str: return (" name: {}\n" " size: {}").format(filename, bytefmt.humanize(os.path.getsize(filename)))