# # SizeLogger.py # # # Copyright (c) 2018, Perforce Software, Inc. All rights reserved. # # 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. # # $Id: //guest/sven_erik_knop/P4Pythonlib/triggers/SizeLogger.py#1 $ # # # # Simple trigger that logs the summed up size of a change # from __future__ import print_function import P4Triggers from configparser import ConfigParser import sys import json P4_SECTION = 'P4' LOG_SECTION = 'Log' DEFAULT_LOGFILE = 'sizes.log' NEW_CONTENT = ("add", "edit") class SizeLoggerTrigger(P4Triggers.P4Trigger): def __init__(self, configFile): parser = ConfigParser() with open(configFile) as f: parser.read_file(f) kargs = {} if parser.has_section(P4_SECTION): kargs = dict(parser.items(P4_SECTION)) P4Triggers.P4Trigger.__init__(self, **kargs) self.logfile = DEFAULT_LOGFILE if parser.has_section(LOG_SECTION): self.logfile = parser.get(LOG_SECTION, "logfile", fallback=DEFAULT_LOGFILE) def validate(self): data = dict() data["change"] = self.change.change data["user"] = self.change.user data["client"] = self.change.client data["timestamp"] = self.change.time.strftime("%Y-%m-%d %H:%M:%S") data["total_size"] = sum([ x.revisions[0].fileSize for x in self.change.files if x.revisions[0].action in NEW_CONTENT]) with open(self.logfile, 'a+') as f: print(json.dumps(data), file=f) if __name__ == '__main__': if len(sys.argv) < 3: print("Usage : {} <configfile> <change>".format(sys.argv[0])) sys.exit(1) configFile = sys.argv[1] change = sys.argv[2] trigger = SizeLoggerTrigger(configFile) trigger.parseChange(change)
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#1 | 23692 | Sven Erik Knop |
Simple trigger that logs sizes of submitted changelists. Invoke with python3 SizeLogger.py <config> <change> where config is the filename of a config file containing the following two sections: -------------- [P4] port=1666 user=tom password=A154ABF11F094B5D288CA0BE9F6F1086 [Log] logfile=mylogfile.log -------------- The P4 connection will use the standard defaults for any parameters not defined (such P4PORT, P4USER) The default log file is called 'sizes.log' Since this trigger is based on the standard P4Triggers.py, it will also create a separate log file (p4triggers.log) for any errors that occur from a connection failure or other exception thrown. Install this trigger like this in the triggers table: SizeLogger change-commit //depot/path/... "python3 SizeLogger.py configfile.name %change%" You need to have P4Triggers.py in the same path as the SizeLogger.py file. --------- This trigger uses 'p4 describe' to collect all files in the change, filters out any files that do not have 'add' or 'edit' action, and then sums up the remaining file sizes - so branches and renames are ignored. The output in the log file is in JSON format for easy parsing. --------- I also updated P4Triggers.py to add size and digest to the internal stored change |