-
Notifications
You must be signed in to change notification settings - Fork 7
/
check-integrity-packages
executable file
·102 lines (80 loc) · 2.42 KB
/
check-integrity-packages
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Pisi repository integrity checker
#
# Parses pisi-index.xml or pisi-index.xml.bz2 file and checks sha1sum of pisi packages
# and delta.pisi packages in working directory
#
# can be given packages as command line parameters
#
import os
import sys
import piksemel
import bz2
import hashlib
indexfile = "pisi-index.xml"
def printColorize(p, c, s):
print "\x1b[1;33m%s \x1b[%sm %s\x1b[0m" % (p, c, s)
def getXmlData(_file):
if os.path.exists(_file):
return piksemel.parse(_file)
elif os.path.exists("%s.bz2" % _file):
indexdata = bz2.decompress(file("%s.bz2" % _file).read())
return piksemel.parseString(indexdata)
else:
print "\x1b[1;31mcould not find pisi index file\x1b[0m"
sys.exit(1)
def getPkgValues(_node):
pkgName = _node.getTagData("PackageURI")
pkgHash = _node.getTagData("PackageHash")
return pkgName, pkgHash
def parseXmlData(_index):
tmpDict = {}
for pkg in _index.tags("Package"):
tmpname, tmphash = getPkgValues(pkg)
tmpDict[tmpname] = tmphash
parent = pkg.getTag("DeltaPackages")
if parent:
for node in parent.tags("Delta"):
tmpname, tmphash = getPkgValues(node)
tmpDict[tmpname] = tmphash
return tmpDict
def checkHash(pkg, hash):
foundhash = ""
try:
h = hashlib.sha1()
f = open(pkg)
h.update(f.read())
foundhash = h.hexdigest()
f.close()
if hash == foundhash:
printColorize(pkg, "1;32", "OK")
else:
printColorize(pkg, "0;31", "expected %s got %s" % (hash, foundhash))
errorlist.append(pkg)
except IOError:
printColorize(pkg, "0;31", "not found !")
errorlist.append(pkg)
if __name__ == "__main__":
searchPkgs = []
errorlist = []
if len(sys.argv) > 1:
searchPkgs.extend(sys.argv[1:])
xmldata = getXmlData(indexfile)
pkgDict = parseXmlData(xmldata)
if not len(searchPkgs):
searchPkgs = pkgDict.keys()
searchPkgs.sort()
for package in searchPkgs:
if package in pkgDict:
checkHash(package, pkgDict[package])
else:
printColorize(package, "0;31", "is not in index")
errorlist.append(package)
if len(errorlist):
print
print "Found errors in ..."
print
for e in errorlist:
print e