-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcheck-integrity-archives
executable file
·119 lines (92 loc) · 2.83 KB
/
check-integrity-archives
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Pisi archives integrity checker
#
# Parses source repo pisi-index.xml or pisi-index.xml.bz2 file and checks sha1sum of archives
#
# can be given package names with components as command line parameters
#
import os
import sys
import piksemel
import bz2
import hashlib
indexfile = "pisi-index.xml"
archiveDir = "/var/cache/pisi/archives"
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):
pkgUri = _node.getTagData("SourceURI")
archiveUri = _node.getTagData("Archive")
archiveSum = _node.getTag("Archive").getAttribute("sha1sum")
return pkgUri.replace("/pspec.xml", ""), archiveUri, archiveSum
def parseXmlData(_index):
tmpDict = {}
for pkg in _index.tags("SpecFile"):
tmpUri, tmpSrcUri, tmpHash = getPkgValues(pkg.getTag("Source"))
tmpDict[tmpUri] = [tmpSrcUri, tmpHash]
return tmpDict
def checkHash(pkguri, pkginfo):
foundhash = ""
pkg = os.path.basename(pkginfo[0])
hash = pkginfo[1]
try:
h = hashlib.sha1()
f = open(os.path.join(archiveDir, 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(pkguri)
except IOError:
if debug:
printColorize(pkg, "0;31", "not found !")
missinglist.append(pkguri)
if __name__ == "__main__":
missinglist = []
errorlist = []
searchPkgs = []
debug = False
args = sys.argv
if len(args) > 1:
if args[1] == "-d":
args.remove("-d")
debug = True
if len(args) > 1:
searchPkgs.extend(args[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")
missinglist.append(package)
if len(errorlist):
print
print "\x1b[1;30mFound errors in ...\x1b[0m"
print
for e in errorlist:
print e
if debug and len(missinglist):
print
print "\x1b[1;30mCould not find ...\x1b[0m"
print
for e in missinglist:
print e