def test_scan_size_excluded_prefix(self): scanner = SystemScanner(TestSystemScanner.temp_dir) scanner.add_exclude_prefix(".") files = scanner.scan() self.assertEqual(3, len(files)) a, b, c = tuple(files) aa, ab = tuple(a.children) ba, bb = tuple(b.children) bba, bbb, bbc = tuple(bb.children) bbca = bbc.children[0] self.assertEqual(12 * 1024 + 4, a.size) self.assertEqual(0, aa.size) self.assertEqual(24 * 1024 * 1024 + 24 + 0, bb.size) self.assertEqual(0, bbc.size) self.assertEqual(0, bbca.size) scanner.add_exclude_prefix("ab") files = scanner.scan() self.assertEqual(3, len(files)) a, b, c = tuple(files) self.assertEqual(1, len(a.children)) aa = a.children[0] self.assertEqual("aa", aa.name) self.assertEqual(0, a.size) self.assertEqual(0, aa.size)
def test_scan_tree_excluded_prefix(self): self.setup_default_tree() scanner = SystemScanner(TestSystemScanner.temp_dir) scanner.add_exclude_prefix(".") files = scanner.scan() self.assertEqual(3, len(files)) a, b, c = tuple(files) aa, ab = tuple(a.children) ba, bb = tuple(b.children) bba, bbb, bbc = tuple(bb.children) bbca = bbc.children[0] self.assertEqual(0, len(aa.children)) self.assertEqual(0, len(bbca.children)) scanner.add_exclude_prefix("ab") files = scanner.scan() self.assertEqual(3, len(files)) a, b, c = tuple(files) self.assertEqual(1, len(a.children)) aa = a.children[0] ba, bb = tuple(b.children) bba, bbb, bbc = tuple(bb.children) bbca = bbc.children[0] self.assertEqual("aa", aa.name) self.assertEqual(0, len(bbca.children))
if __name__ == "__main__": if sys.hexversion < 0x03050000: sys.exit("Python 3.5 or newer is required to run this program.") parser = argparse.ArgumentParser(description="File size scanner") parser.add_argument("path", help="Path of the root directory to scan") parser.add_argument("-e", "--exclude-hidden", action="store_true", default=False, help="Exclude hidden files") parser.add_argument("-H", "--human-readable", action="store_true", default=False, help="Human readable output") args = parser.parse_args() scanner = SystemScanner(args.path) if args.exclude_hidden: scanner.add_exclude_prefix(".") try: root_files = scanner.scan() except SystemScannerError as e: sys.exit("SystemScannerError: {}".format(str(e))) if args.human_readable: def print_file(file: SystemFile, level: int): sys.stdout.write(" "*level) sys.stdout.write("{} {} {}\n".format( file.name, "d" if file.is_dir else "f", file.size )) for child in file.children: print_file(child, level+1) for root_file in root_files: