views:

104

answers:

1

Hi all,

how to compare the checksums in a list corresponding to a file path with the file path in the operating system In Python?

import os,sys,libxml2

files=[]
sha1s=[]

doc = libxml2.parseFile('files.xml')
for path in doc.xpathEval('//File/Path'):
  files.append(path.content)
for sha1 in doc.xpathEval('//File/Hash'):
  sha1s.append(sha1.content)

for entry in zip(files,sha1s):
  print entry

the files.xml contains

<Files>
    <File>
        <Path>usr/share/doc/dialog/samples/form1</Path>
        <Type>doc</Type>
        <Size>1222</Size>
        <Uid>0</Uid>
        <Gid>0</Gid>
        <Mode>0755</Mode>
        <Hash>49744d73e8667d0e353923c0241891d46ebb9032</Hash>
    </File>
    <File>
        <Path>usr/share/doc/dialog/samples/form3</Path>
        <Type>doc</Type>
        <Size>1294</Size>
        <Uid>0</Uid>
        <Gid>0</Gid>
        <Mode>0755</Mode>
        <Hash>f30277f73e468232c59a526baf3a5ce49519b959</Hash>
    </File>
</Files>

I need to compare the sha1 checksum in between tags corresponding to the file specified in between the tags, with the same file path in base Operating system.

A: 
import hashlib
import libxml2

doc = libxml2.parseFile('files.xml')
filePaths = ["/" + path.content for path in doc.xpathEval('//File/Path')]
xmlDigests = [hash.content for hash in doc.xpathEval('//File/Hash')]

for filePath, xmlDigest in zip(filePaths, xmlDigests):
    with open(filePath) as inFile:
        digester = hashlib.sha1()
        digester.update(inFile.read())
        fileDigest = digester.hexdigest()
        if xmlDigest != fileDigest:
            print "Mismatch for %s (XML: %s, FILESYSTEM: %s)" % (filePath,
                xmlDigest, fileDigest)
Peter Lyons