/[gentoo-src]/portage/pym/portage_checksum.py
Gentoo

Contents of /portage/pym/portage_checksum.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.12 - (show annotations) (download) (as text)
Sat Feb 26 06:35:20 2005 UTC (9 years, 6 months ago) by jstubbs
Branch: MAIN
CVS Tags: HEAD
Branch point for: portage_2_1
Changes since 1.11: +2 -1 lines
File MIME type: text/x-python
Brought forward changes from portage_2_0

1 # portage_checksum.py -- core Portage functionality
2 # Copyright 1998-2004 Gentoo Foundation
3 # Distributed under the terms of the GNU General Public License v2
4 # $Header: /var/cvsroot/gentoo-src/portage/pym/portage_checksum.py,v 1.11 2004/11/07 11:58:29 ferringb Exp $
5 cvs_id_string="$Id: portage_checksum.py,v 1.10.2.1 2005/01/16 02:35:33 carpaski Exp $"[5:-2]
6
7 from portage_const import PRIVATE_PATH,PRELINK_BINARY
8 import os
9 import shutil
10 import stat
11 import portage_exec
12 import portage_util
13 import portage_locks
14 import sha
15
16 prelink_capable = False
17 if os.path.exists(PRELINK_BINARY):
18 if portage_exec.spawn(PRELINK_BINARY+" --version",fd_pipes={}) == 0:
19 prelink_capable=1
20
21 def perform_md5(x, calc_prelink=0):
22 return perform_checksum(x, md5hash, calc_prelink)[0]
23
24 def perform_sha1(x, calc_prelink=0):
25 return perform_checksum(x, sha1hash, calc_prelink)[0]
26
27 def perform_all(x, calc_prelink=0):
28 mydict = {}
29 mydict["SHA1"] = perform_sha1(x, calc_prelink)
30 mydict["MD5"] = perform_md5(x, calc_prelink)
31 return mydict
32
33 def get_valid_checksum_keys():
34 return ["SHA1", "MD5"]
35
36 def verify_all(filename, mydict, calc_prelink=0, strict=0):
37 # Dict relates to single file only.
38 # returns: (passed,reason)
39 file_is_ok = True
40 reason = "Reason unknown"
41 try:
42 if mydict["size"] != os.stat(filename)[stat.ST_SIZE]:
43 return False,"Filesize does not match recorded size"
44 except OSError, e:
45 return False, str(e)
46 for x in mydict.keys():
47 if x == "size":
48 continue
49 elif x == "SHA1":
50 if mydict[x] != perform_sha1(filename, calc_prelink=calc_prelink):
51 if strict:
52 raise portage_exception.DigestException, "Failed to verify '$(file)s' on checksum type '%(type)s'" % {"file":filename, "type":x}
53 else:
54 file_is_ok = False
55 reason = "Failed on %s verification" % (x,)
56 break
57 elif x == "MD5":
58 if mydict[x] != perform_md5(filename, calc_prelink=calc_prelink):
59 if strict:
60 raise portage_exception.DigestException, "Failed to verify '$(file)s' on checksum type '%(type)s'" % {"file":filename, "type":x}
61 else:
62 file_is_ok = False
63 reason = "Failed on %s verification" % (x,)
64 break
65 return file_is_ok,reason
66
67 # We _try_ to load this module. If it fails we do the slow fallback.
68 try:
69 import fchksum
70
71 def md5hash(filename):
72 return fchksum.fmd5t(filename)
73
74 except ImportError:
75 import md5
76 def md5hash(filename):
77 f = open(filename, 'rb')
78 blocksize=32768
79 data = f.read(blocksize)
80 size = 0L
81 sum = md5.new()
82 while data:
83 sum.update(data)
84 size = size + len(data)
85 data = f.read(blocksize)
86 f.close()
87
88 return (sum.hexdigest(),size)
89
90 def sha1hash(filename):
91 f = open(filename, 'rb')
92 blocksize=32768
93 data = f.read(blocksize)
94 size = 0L
95 sum = sha.new()
96 while data:
97 sum.update(data)
98 size = size + len(data)
99 data = f.read(blocksize)
100 f.close()
101
102 return (sum.hexdigest(),size)
103
104 def perform_checksum(filename, hash_function=md5hash, calc_prelink=0):
105 myfilename = filename[:]
106 prelink_tmpfile = PRIVATE_PATH+"/prelink-checksum.tmp."+str(os.getpid())
107 mylock = None
108
109 if calc_prelink and prelink_capable:
110 mylock = portage_locks.lockfile(prelink_tmpfile, wantnewlockfile=1)
111 # Create non-prelinked temporary file to md5sum.
112 # Raw data is returned on stdout, errors on stderr.
113 # Non-prelinks are just returned.
114 try:
115 shutil.copy2(filename,prelink_tmpfile)
116 except SystemExit, e:
117 raise
118 except Exception,e:
119 portage_util.writemsg("!!! Unable to copy file '"+str(filename)+"'.\n")
120 portage_util.writemsg("!!! "+str(e)+"\n")
121 sys.exit(1)
122 portage_exec.spawn(PRELINK_BINARY+" --undo "+prelink_tmpfile,fd_pipes={})
123 myfilename=prelink_tmpfile
124
125 myhash, mysize = hash_function(myfilename)
126
127 if calc_prelink and prelink_capable:
128 os.unlink(prelink_tmpfile)
129 portage_locks.unlockfile(mylock)
130
131 return (myhash,mysize)

  ViewVC Help
Powered by ViewVC 1.1.20