def main(): from_path = os.path.expanduser(sys.argv[1]) filestat_file = os.path.join(os.path.dirname(__file__), '../data/filestat.lst') dirstat_file = os.path.join(os.path.dirname(__file__), '../data/dirstat.lst') json_file = os.path.join(os.path.dirname(__file__), '../data/data.json') #scan_file_sizes(from_path, stat_file) #calc_dir_sizes(filestat_file, dirstat_file) lines = FileUtil.read_all(dirstat_file).splitlines() iter_dir_sizes = FileUtil.parse_path_sizes(lines) root = SizeNode.build_tree(iter_dir_sizes) node = root.find_node(os.path.join(from_path)) #nodes = sorted([c for c in node.name2childs.values()], key=lambda n: n.size, reverse=True) nodes = node.name2childs.values() min_size = node.size * 0.001 #print "min_size", min_size #nodes = SquareTreeMap.norm_node_sizes(nodes) rects = SquareTreeMap.squarify_size_nodes(node.name, nodes, 0., 0., 700., 400., min_size) # rects = square_node(node, 0., 0., 700., 400.) # print 'rects', rects write_d3_rect_json(json_file, rects)
def test_02_size_tree(self): from fileutil import FileUtil from sizenode import SizeNode lines = """\ node_modules/promise/domains: 15908 node_modules/promise/lib: 15912 node_modules/promise/node_modules: 34383 node_modules/promise/node_modules/asap: 34383 node_modules/promise/setimmediate: 15886 node_modules/promise/src: 16314 """.splitlines() path_sizes = FileUtil.parse_path_sizes(lines) self.assertEqual(len([ps for ps in path_sizes]), 6) path_sizes = FileUtil.parse_path_sizes(lines) tree = SizeNode.build_tree(path_sizes) self.assertTrue(tree) self.assertTrue(tree.name2childs) self.assertEqual(tree.find_node("node_modules/promise/node_modules/asap").size, 34383)
def calc_dir_sizes(filestat_file, dirstat_file): lines = FileUtil.read_all(filestat_file).splitlines() dir2size = {} for filepath, size in FileUtil.parse_path_sizes(lines): dir2size[filepath] = size dirpath = os.path.dirname(filepath) while dirpath: try: dir2size[dirpath] += size except KeyError: dir2size[dirpath] = size dirpath = dirpath.rpartition("/")[0] iter_lines = FileUtil.combine_path_sizes(sorted(dir2size.items())) FileUtil.write_all(dirstat_file, iter_lines)
def test_01_file_util(self): from fileutil import FileUtil test_file = __file__ + ".test_01.data" path_sizes = [ ("path1", 1), ("path2", 2), ("path3", 3), ] iter_lines = FileUtil.combine_path_sizes(path_sizes) FileUtil.write_all(test_file, iter_lines) data = FileUtil.read_all(test_file) self.assertEqual(""" path1: 1 path2: 2 path3: 3 """.strip(), data.strip()) path2size = dict([(path, size) for path, size in FileUtil.parse_path_sizes(data.splitlines())]) self.assertEqual(path2size['path2'], 2)