Print excessive files in the initrd diff
This commit is contained in:
parent
d1653ce89e
commit
6bd104c627
@ -2,6 +2,8 @@ import sys
|
|||||||
import os
|
import os
|
||||||
import magic
|
import magic
|
||||||
import difflib
|
import difflib
|
||||||
|
import yum
|
||||||
|
import operator
|
||||||
|
|
||||||
|
|
||||||
def main(args):
|
def main(args):
|
||||||
@ -9,9 +11,7 @@ def main(args):
|
|||||||
sourcedir, targetdir = args[1], args[2]
|
sourcedir, targetdir = args[1], args[2]
|
||||||
except IndexError:
|
except IndexError:
|
||||||
print("invalid argument count")
|
print("invalid argument count")
|
||||||
print("usage: python {0} sourcetree targettree > output.diff".format(
|
print("usage: python {0} sourcetree targettree".format(args[0]))
|
||||||
args[0]))
|
|
||||||
|
|
||||||
sys.exit(2)
|
sys.exit(2)
|
||||||
|
|
||||||
if sourcedir.endswith("/"):
|
if sourcedir.endswith("/"):
|
||||||
@ -19,23 +19,22 @@ def main(args):
|
|||||||
if targetdir.endswith("/"):
|
if targetdir.endswith("/"):
|
||||||
targetdir = targetdir[:-1]
|
targetdir = targetdir[:-1]
|
||||||
|
|
||||||
sourcetree = {}
|
# parse sourcedir and targetdir
|
||||||
for root, dnames, fnames in os.walk(sourcedir):
|
sourcetree, targettree = {}, {}
|
||||||
|
for tree, dir in [[sourcetree, sourcedir], [targettree, targetdir]]:
|
||||||
|
for root, dnames, fnames in os.walk(dir):
|
||||||
for fname in fnames:
|
for fname in fnames:
|
||||||
fpath = os.path.join(root, fname)
|
fpath = os.path.join(root, fname)
|
||||||
rpath = fpath.replace(sourcedir, "", 1)
|
rpath = fpath.replace(dir, "", 1)
|
||||||
sourcetree[rpath] = fpath
|
tree[rpath] = fpath
|
||||||
|
|
||||||
|
# set up magic
|
||||||
m = magic.open(magic.MAGIC_NONE)
|
m = magic.open(magic.MAGIC_NONE)
|
||||||
m.load()
|
m.load()
|
||||||
|
|
||||||
for root, dnames, fnames in os.walk(targetdir):
|
# get files missing in source
|
||||||
for fname in fnames:
|
sys.stderr.write("getting files missing in source\n")
|
||||||
fpath = os.path.join(root, fname)
|
for rpath, fpath in targettree.items():
|
||||||
rpath = fpath.replace(targetdir, "", 1)
|
|
||||||
|
|
||||||
sys.stderr.write('processing "%s"\n' % rpath)
|
|
||||||
|
|
||||||
targetfile = fpath
|
targetfile = fpath
|
||||||
try:
|
try:
|
||||||
sourcefile = sourcetree[rpath]
|
sourcefile = sourcetree[rpath]
|
||||||
@ -53,9 +52,8 @@ def main(args):
|
|||||||
#if sourcemode != targetmode:
|
#if sourcemode != targetmode:
|
||||||
# sys.stdout.write('Stat differ: %s\n' % rpath)
|
# sys.stdout.write('Stat differ: %s\n' % rpath)
|
||||||
|
|
||||||
ftype = m.file(fpath)
|
|
||||||
|
|
||||||
# diff only text files
|
# diff only text files
|
||||||
|
ftype = m.file(fpath)
|
||||||
if ftype not in ["ASCII text"]:
|
if ftype not in ["ASCII text"]:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
@ -64,13 +62,51 @@ def main(args):
|
|||||||
with open(sourcefile) as fobj:
|
with open(sourcefile) as fobj:
|
||||||
source = fobj.readlines()
|
source = fobj.readlines()
|
||||||
|
|
||||||
# do a file diff
|
# do the file diff
|
||||||
for line in difflib.unified_diff(source, target,
|
for line in difflib.unified_diff(source, target,
|
||||||
fromfile=sourcefile,
|
fromfile=sourcefile,
|
||||||
tofile=targetfile):
|
tofile=targetfile):
|
||||||
|
|
||||||
sys.stdout.write(line)
|
sys.stdout.write(line)
|
||||||
|
|
||||||
|
# set up yum
|
||||||
|
yb = yum.YumBase()
|
||||||
|
yb.doSackSetup()
|
||||||
|
|
||||||
|
# get excessive files in source
|
||||||
|
sys.stderr.write("getting excessive files in source\n")
|
||||||
|
sizedict, pkgdict = {}, {}
|
||||||
|
for rpath, fpath in sourcetree.items():
|
||||||
|
# if file in target, skip it
|
||||||
|
if rpath in targettree:
|
||||||
|
continue
|
||||||
|
|
||||||
|
# get file size
|
||||||
|
try:
|
||||||
|
sizeinbytes = os.path.getsize(fpath)
|
||||||
|
except OSError:
|
||||||
|
sizeinbytes = 0
|
||||||
|
|
||||||
|
# set link size to 0
|
||||||
|
islink = os.path.islink(fpath)
|
||||||
|
if islink:
|
||||||
|
sizeinbytes = 0
|
||||||
|
|
||||||
|
pkglist = yb.whatProvides(rpath, None, None)
|
||||||
|
pkglist = set(map(lambda pkgobj: pkgobj.name, pkglist))
|
||||||
|
|
||||||
|
for pkg in pkglist:
|
||||||
|
sizedict[pkg] = sizedict.get(pkg, 0) + sizeinbytes
|
||||||
|
pkgdict[pkg] = pkgdict.get(pkg, []) + \
|
||||||
|
[(rpath, sizeinbytes, islink)]
|
||||||
|
|
||||||
|
# sort by size
|
||||||
|
for pkg, size in sorted(sizedict.items(), key=operator.itemgetter(1),
|
||||||
|
reverse=True):
|
||||||
|
|
||||||
|
for item in sorted(pkgdict[pkg]):
|
||||||
|
sys.stdout.write("%s\t%s\n" % (pkg, item))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main(sys.argv)
|
main(sys.argv)
|
||||||
|
Loading…
Reference in New Issue
Block a user