#!/usr/bin/env python3 # -*- coding: utf-8 -*- # # Copyright (c) 2013 Sven Erik Knop, Perforce Software Ltd # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are # met: # # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the # distribution. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PERFORCE # SOFTWARE, INC. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # # User contributed content on the Perforce Public Depot is not supported by Perforce, # although it may be supported by its author. This applies to all contributions # even those submitted by Perforce employees. # # Maintainer: Sven Erik Knop, sknop@perforce.com 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. |