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 <robert.loehning@qt.io>
This commit is contained in:
Christian Stenger
2018-12-07 14:06:07 +01:00
parent ba00dbbe22
commit d363c8101b

View File

@@ -38,11 +38,13 @@ lastToken = [None, None]
stopTokens = ('OP', 'NAME', 'NUMBER', 'ENDMARKER') stopTokens = ('OP', 'NAME', 'NUMBER', 'ENDMARKER')
def parseCommandLine(): def parseCommandLine():
global directory, onlyRemovable, sharedFolders global directory, onlyRemovable, sharedFolders, deleteObjects
parser = OptionParser("\n%prog [OPTIONS] [DIRECTORY]") parser = OptionParser("\n%prog [OPTIONS] [DIRECTORY]")
parser.add_option("-o", "--only-removable", dest="onlyRemovable", parser.add_option("-o", "--only-removable", dest="onlyRemovable",
action="store_true", default=False, action="store_true", default=False,
help="list removable objects only") help="list removable objects only")
parser.add_option("-d", "--delete", dest="delete",
action="store_true", default=False)
parser.add_option("-s", dest="sharedFolders", parser.add_option("-s", dest="sharedFolders",
action="store", type="string", default="", action="store", type="string", default="",
help="comma-separated list of shared folders") help="comma-separated list of shared folders")
@@ -56,6 +58,7 @@ def parseCommandLine():
parser.print_help() parser.print_help()
sys.exit(1) sys.exit(1)
onlyRemovable = options.onlyRemovable onlyRemovable = options.onlyRemovable
deleteObjects = options.delete
sharedFolders = map(os.path.abspath, options.sharedFolders.split(',')) sharedFolders = map(os.path.abspath, options.sharedFolders.split(','))
def collectObjects(): def collectObjects():
@@ -153,15 +156,53 @@ def printResult():
print print
return None return None
def main(): def deleteRemovable():
global useCounts, objMap 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) objMap = checkDirectory(directory)
useCounts = dict.fromkeys(collectObjects(), 0) useCounts = dict.fromkeys(collectObjects(), 0)
findUsages() findUsages()
atLeastOneRemovable = printResult() atLeastOneRemovable = printResult()
deletedAtLeastOne = deleteObjects and deleteRemovable()
mssg = None
if atLeastOneRemovable: if atLeastOneRemovable:
print "\nAfter removing the listed objects you should re-run this tool" mssg = "\nAfter removing the listed objects you should re-run this tool\n"
print "to find objects that might have been used only by these objects.\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 return 0
if __name__ == '__main__': if __name__ == '__main__':