#!/usr/bin/env python3 import P4 import sys import argparse class DirectorySizes: def __init__(self, path, depth, revisions): self.range = revisions self.depth = depth self.p4 = P4.P4() self.p4.prog = "DirectorySizes" self.p4.connect() pattern = path + "/*" * depth dirs = self.find_directories(pattern) self.dir_sizes = self.find_sizes(dirs, revisions) def find_directories(self, pattern): dirs = [ x['dir'] for x in self.p4.run_dirs(pattern) ] return dirs def find_sizes(self, dirs, range): result = {} for d in dirs: size = int(self.p4.run_sizes('-sa', "{dir}/...{range}".format(dir=d, range=range))[0]['fileSize']) result[d] = size self.print_size(d, size) return result def print_size(self, directory, size): print("{dir:50} : {size:16} ({human})".format(dir=directory, size=size, human=self.human(size))) def human(self, size): units = [" B", " KB", " MB", " GB", " TB"] factor = 1024 current = size for u in units: if current < factor: return str(current) + u else: current = round(current / factor, 2) return str(current) + " PB" if __name__ == "__main__": parser = argparse.ArgumentParser(description="Directory sizes") parser.add_argument("-d","--depth", default=1, type=int) parser.add_argument("-p","--path") parser.add_argument("-r","--range") args = parser.parse_args() tool = DirectorySizes(args.path, args.depth, args.range)
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#6 | 8294 | Sven Erik Knop |
Updated directory_sizes with -z option and cleaned up some of the code. Should be working fine for everyone now :-) |
||
#5 | 8293 | Sven Erik Knop |
... and now with proper formatting as well. I declare this version finished. Have fun :-) |
||
#4 | 8292 | Sven Erik Knop |
Much better version of the directory_sizes script. Now recursive, easier to understand and maintain ... and faster I believe. Only thing missing now is proper formatting. |
||
#3 | 8291 | Sven Erik Knop |
This is a nice try, but going the wrong way. We need a recursive algorithm, which will be simpler yet more powerful. |
||
#2 | 8287 | Sven Erik Knop | Updated copyright and no warranty notice. | ||
#1 | 8283 | Sven Erik Knop |
Set of useful scripts and triggers in P4Python. Most require Python 3.X to run, but can be made to use Python 2.7 if necessary. The scripts will also need P4Python 2012.2 + to work. changes_month_depot analysis the usage in changes and bytes of a Perforce Server. directory_sizes list the sizes of files within a directory of Perforce hash_database attempts to find identical files submitted several times (not lazy copies) open_view_clients lists all clients with open views Run the first 3 scripts against a (db only sufficient) replica, they can be very heavy on a server. The last file is a simple client form-in trigger to prevent open views. |