[Python-checkins] python/dist/src/Lib/plat-mac pimp.py,1.2,1.3

jackjansen@users.sourceforge.net jackjansen@users.sourceforge.net
Mon, 10 Feb 2003 05:38:47 -0800


Update of /cvsroot/python/python/dist/src/Lib/plat-mac
In directory sc8-pr-cvs1:/tmp/cvs-serv5925

Modified Files:
	pimp.py 
Log Message:
Use MD5 checksums to check archive integrity and forestall downloads.


Index: pimp.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/plat-mac/pimp.py,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** pimp.py	10 Feb 2003 13:08:04 -0000	1.2
--- pimp.py	10 Feb 2003 13:38:44 -0000	1.3
***************
*** 5,8 ****
--- 5,9 ----
  import plistlib
  import distutils.util
+ import md5
  
  _scriptExc_NotInstalled = "pimp._scriptExc_NotInstalled"
***************
*** 26,29 ****
--- 27,35 ----
  ]
  
+ class MyURLopener(urllib.FancyURLopener):
+ 	"""Like FancyURLOpener, but we do want to get errors as exceptions"""
+ 	def http_error_default(self, url, fp, errcode, errmsg, headers):
+ 		urllib.URLopener.http_error_default(self, url, fp, errcode, errmsg, headers)
+ 
  class PimpPreferences:
  	def __init__(self, 
***************
*** 179,183 ****
  			prerequisites=None,
  			preInstall=None,
! 			postInstall=None):
  		self._db = db
  		self.name = name
--- 185,190 ----
  			prerequisites=None,
  			preInstall=None,
! 			postInstall=None,
! 			MD5Sum=None):
  		self._db = db
  		self.name = name
***************
*** 191,194 ****
--- 198,202 ----
  		self._preInstall = preInstall
  		self._postInstall = postInstall
+ 		self._MD5Sum = MD5Sum
  		
  	def dump(self):
***************
*** 214,217 ****
--- 222,227 ----
  		if self._postInstall:
  			dict['postInstall'] = self._postInstall
+ 		if self._MD5Sum:
+ 			dict['MD5Sum'] = self._MD5Sum
  		return dict
  		
***************
*** 288,298 ****
  		path = urllib.url2pathname(path)
  		filename = os.path.split(path)[1]
! 		self.archiveFilename = os.path.join(self._db.preferences.downloadDir, filename)
! 		if self._cmd(output, self._db.preferences.downloadDir, "curl",
! 				"--output", self.archiveFilename,
! 				self.downloadURL):
! 			return "download command failed"
  		if not os.path.exists(self.archiveFilename) and not NO_EXECUTE:
  			return "archive not found after download"
  			
  	def unpackSinglePackage(self, output):
--- 298,322 ----
  		path = urllib.url2pathname(path)
  		filename = os.path.split(path)[1]
! 		self.archiveFilename = os.path.join(self._db.preferences.downloadDir, filename)			
! 		if not self._archiveOK():
! 			if self._cmd(output, self._db.preferences.downloadDir,
! 					"curl",
! 					"--output", self.archiveFilename,
! 					self.downloadURL):
! 				return "download command failed"
  		if not os.path.exists(self.archiveFilename) and not NO_EXECUTE:
  			return "archive not found after download"
+ 		if not self._archiveOK():
+ 			return "archive does not have correct MD5 checksum"
+ 			
+ 	def _archiveOK(self):
+ 		if not os.path.exists(self.archiveFilename):
+ 			return 0
+ 		if not self._MD5Sum:
+ 			sys.stderr.write("Warning: no MD5Sum for %s\n" % _fmtpackagename(self))
+ 			return 1
+ 		data = open(self.archiveFilename, 'rb').read()
+ 		checksum = md5.new(data).hexdigest()
+ 		return checksum == self._MD5Sum
  			
  	def unpackSinglePackage(self, output):
***************
*** 403,407 ****
  	db.appendURL(prefs.pimpDatabase)
  	
! 	if mode =='list':
  		if not args:
  			args = db.listnames()
--- 427,433 ----
  	db.appendURL(prefs.pimpDatabase)
  	
! 	if mode == 'dump':
! 		db.dump(sys.stdout)
! 	elif mode =='list':
  		if not args:
  			args = db.listnames()
***************
*** 419,423 ****
  				print "\tHome page:\t", pkg.longdesc
  				print "\tDownload URL:\t", pkg.downloadURL
! 	if mode =='status':
  		if not args:
  			args = db.listnames()
--- 445,449 ----
  				print "\tHome page:\t", pkg.longdesc
  				print "\tDownload URL:\t", pkg.downloadURL
! 	elif mode =='status':
  		if not args:
  			args = db.listnames()
***************
*** 473,476 ****
--- 499,503 ----
  		print "       pimp [-v] -l [package ...]  Show package information"
  		print "       pimp [-vf] -i package ...   Install packages"
+ 		print "       pimp -d                     Dump database to stdout"
  		print "Options:"
  		print "       -v  Verbose"
***************
*** 479,483 ****
  		
  	try:
! 		opts, args = getopt.getopt(sys.argv[1:], "slifv")
  	except getopt.Error:
  		_help()
--- 506,510 ----
  		
  	try:
! 		opts, args = getopt.getopt(sys.argv[1:], "slifvd")
  	except getopt.Error:
  		_help()
***************
*** 496,503 ****
  				_help()
  			mode = 'list'
! 		if o == '-L':
  			if mode:
  				_help()
! 			mode = 'longlist'
  		if o == '-i':
  			mode = 'install'
--- 523,530 ----
  				_help()
  			mode = 'list'
! 		if o == '-d':
  			if mode:
  				_help()
! 			mode = 'dump'
  		if o == '-i':
  			mode = 'install'