From d363c8101b54f77c17a040e67c168c1f9c68d4a2 Mon Sep 17 00:00:00 2001 From: Christian Stenger Date: Fri, 7 Dec 2018 14:06:07 +0100 Subject: Tests: Allow removal of objects on the fly Add another option to allow deletion of unused objects on the fly. This option should be used with care. It is recommended to run without -d (or --delete) beforehand and check the results. Anyhow, when deletion is wanted it processes as usual, renames the original objects.map to objects.map~ and recreates the objects.map without the objects that could be deleted. Change-Id: I3898e8b9998e33461140bf4c75887a32d106f22c Reviewed-by: Robert Loehning --- tests/system/tools/findUnusedObjects.py | 49 ++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 4 deletions(-) diff --git a/tests/system/tools/findUnusedObjects.py b/tests/system/tools/findUnusedObjects.py index a83355ceeb..09b0064782 100755 --- a/tests/system/tools/findUnusedObjects.py +++ b/tests/system/tools/findUnusedObjects.py @@ -38,11 +38,13 @@ lastToken = [None, None] stopTokens = ('OP', 'NAME', 'NUMBER', 'ENDMARKER') def parseCommandLine(): - global directory, onlyRemovable, sharedFolders + global directory, onlyRemovable, sharedFolders, deleteObjects parser = OptionParser("\n%prog [OPTIONS] [DIRECTORY]") parser.add_option("-o", "--only-removable", dest="onlyRemovable", action="store_true", default=False, help="list removable objects only") + parser.add_option("-d", "--delete", dest="delete", + action="store_true", default=False) parser.add_option("-s", dest="sharedFolders", action="store", type="string", default="", help="comma-separated list of shared folders") @@ -56,6 +58,7 @@ def parseCommandLine(): parser.print_help() sys.exit(1) onlyRemovable = options.onlyRemovable + deleteObjects = options.delete sharedFolders = map(os.path.abspath, options.sharedFolders.split(',')) def collectObjects(): @@ -153,15 +156,53 @@ def printResult(): print return None -def main(): +def deleteRemovable(): global useCounts, objMap + + deletable = filter(lambda x: useCounts[x] == 0, useCounts) + if len(deletable) == 0: + print("Nothing to delete - leaving objects.map untouched") + return + + data = '' + with open(objMap, "r") as objMapFile: + data = objMapFile.read() + + objMapBackup = objMap + '~' + if os.path.exists(objMapBackup): + os.unlink(objMapBackup) + os.rename(objMap, objMapBackup) + + count = 0 + with open(objMap, "w") as objMapFile: + for line in data.splitlines(): + try: + obj = line.split('\t')[0] + if obj in deletable: + count += 1 + continue + objMapFile.write(line + '\n') + except: + print("Something's wrong in line '%s'" % line) + + print("Deleted %d items, old objects.map has been moved to objects.map~" % count) + return count > 0 + +def main(): + global useCounts, objMap, deleteObjects objMap = checkDirectory(directory) useCounts = dict.fromkeys(collectObjects(), 0) findUsages() atLeastOneRemovable = printResult() + deletedAtLeastOne = deleteObjects and deleteRemovable() + + mssg = None if atLeastOneRemovable: - print "\nAfter removing the listed objects you should re-run this tool" - print "to find objects that might have been used only by these objects.\n" + mssg = "\nAfter removing the listed objects you should re-run this tool\n" + if deletedAtLeastOne: + mssg = "\nYou should re-run this tool\n" + if mssg: + print(mssg + "to find objects that might have been referenced only by removed objects.\n") return 0 if __name__ == '__main__': -- cgit v1.2.1