CVS: src/si/extras entitycheck.py,1.18,1.19

Mats Wichmann <[email protected]>
Newsgroups gmane.linux.lsb.implementation
Message-ID <[email protected]>
Update of /cvsroot/lsb/src/si/extras
In directory sc8-pr-cvs1:/tmp/cvs-serv11327

Modified Files:
	entitycheck.py 
Log Message:
Made retrieval message-printing routines class methods instead of global.
Easier to track the message as instance data...


Index: entitycheck.py
===================================================================
RCS file: /cvsroot/lsb/src/si/extras/entitycheck.py,v
retrieving revision 1.18
retrieving revision 1.19
diff -C2 -r1.18 -r1.19
*** entitycheck.py	8 Jan 2003 17:12:40 -0000	1.18
--- entitycheck.py	10 Jan 2003 16:53:10 -0000	1.19
***************
*** 46,66 ****
      sys.exit(code)
  
- # these two functions print messages during file retrieval
- initial_text = "" # needs to be global so feedback() can access it
- 
- def running_output(front, message):
-     """Print running output of (front, message) using \b and \r tricks."""
-     output = "%s %s" % (front, message)
-     padding = " " * (80 - len(output))
-     print "%s%s%s\r" % ("\b" * (len(front) + 1), output, padding),
- 
- def feedback(block_count, block_size, total):
-     """Callback function for urllib.urlretrieve()"""
-     so_far = block_count * block_size
-     running_output(initial_text, "%d of %d bytes retrieved" % (so_far, total))
-     sys.stdout.flush()		# force text to be displayed
- 
- BLOCKSIZE = 1024*1024
- 
  class entity:
      """entity class, an instance is created for each entity read
--- 46,49 ----
***************
*** 71,74 ****
--- 54,59 ----
          self.file = file
  
+     BLOCKSIZE = 1024*1024
+ 
      def domd5(self):
          """Generate md5sum for this entity's filename, and save"""
***************
*** 76,80 ****
          sum = md5.new()
          while 1:
!             block = f.read(BLOCKSIZE)
              if not block:
                  break
--- 61,65 ----
          sum = md5.new()
          while 1:
!             block = f.read(entity.BLOCKSIZE)
              if not block:
                  break
***************
*** 84,101 ****
          self.md5sum = "%02x"*len(s) % tuple(map(ord, s))
  
      def fetch(self, locations, fallback, destination):
          """retrieve the file for an entity.
!         Calls urllib.urlretrieve() up to three times to retrieve to
!         destination from main or alternate location as found
!         in the 'locations' entity list, or if neither works from
!         the 'fallback'. Returns a string indicating status.
          """
!         global initial_text
!         message = "not found"
          for loc in locations:
!             initial_text = "%s ->" % self.name
              if self.name != loc.name: continue
              pkgpath = "%s/%s" % (destination, self.file)
!             print initial_text,
              sys.stdout.flush()		# force text to be displayed
  
--- 69,99 ----
          self.md5sum = "%02x"*len(s) % tuple(map(ord, s))
  
+     def running_output(self):
+ 	"""Print running output of (front, message) using \b and \r tricks."""
+ 	output = "%s %s" % (self.front, self.message)
+ 	padding = " " * (80 - len(output))
+ 	print "%s%s%s\r" % ("\b" * (len(self.front) + 1), output, padding),
+ 
+     def feedback(self, block_count, block_size, total):
+ 	"""Callback function for urllib.urlretrieve()"""
+ 	so_far = block_count * block_size
+         self.message = "%d of %d bytes retrieved" % (so_far, total)
+ 	self.running_output()
+ 	sys.stdout.flush()		# force text to be displayed
+ 
      def fetch(self, locations, fallback, destination):
          """retrieve the file for an entity.
!         Calls urllib.urlretrieve() up to three times to retrieve the
! 	package. Retrieval locations are looked up in 'locations',
! 	the name of a locations instance must match our name, that
! 	instance contains a primary and optional secondary url to
! 	find the package. A 'fallback' url is tried if they fail.
          """
!         self.message = "not found"
          for loc in locations:
!             self.front = "%s ->" % self.name
              if self.name != loc.name: continue
              pkgpath = "%s/%s" % (destination, self.file)
!             print self.front,
              sys.stdout.flush()		# force text to be displayed
  
***************
*** 103,107 ****
                  print "fetch %s from (%s, %s, %s) to %s" % (self.file,
                          loc.path, loc.alternate, fallback, destination)
!                 message = "skipped"
                  break
  
--- 101,105 ----
                  print "fetch %s from (%s, %s, %s) to %s" % (self.file,
                          loc.path, loc.alternate, fallback, destination)
!                 self.message = "skipped"
                  break
  
***************
*** 112,129 ****
  
                  try:
!                     filename, mime = urllib.urlretrieve(to_get, pkgpath, feedback)
                      if mime and mime.gettype() == 'text/html':
                          raise IOError
!                     message = "completed"
                      break
                  except IOError:
                      if os.path.exists(pkgpath): os.remove(pkgpath)
              else:	# if we didn't "break" out of loop, fetch failed
!                 message = "retrieval failed"
              break
  
!         running_output(initial_text, message)
          print
!         return message
  
  def parse_entities():
--- 110,128 ----
  
                  try:
!                     filename, mime = urllib.urlretrieve(to_get, pkgpath, self.feedback)
                      if mime and mime.gettype() == 'text/html':
                          raise IOError
!                     self.message = "completed"
                      break
                  except IOError:
                      if os.path.exists(pkgpath): os.remove(pkgpath)
              else:	# if we didn't "break" out of loop, fetch failed
!                 self.message = "retrieval failed"
              break
  
!         self.running_output()
          print
! 	if self.message == "completed": return 1
! 	else: return 0
  
  def parse_entities():
***************
*** 314,326 ****
      global initial_text
      for pkg in missing_packages:
!         rv = pkg.fetch(locations, fallback_url, package_path)
!         if rv == 'completed':
              retrieved = retrieved + 1
!         if rv == 'retrieval failed':
!             fails.append(pkg)
!         if rv == 'not found':
!             # should not happen, but we track so we can catch where
!             # an entity is defined but not listed in package_locations
!             missing.append(pkg)
      return (retrieved, fails, missing)
  
--- 313,325 ----
      global initial_text
      for pkg in missing_packages:
!         if pkg.fetch(locations, fallback_url, package_path):
              retrieved = retrieved + 1
! 	else:
!             if rv == 'retrieval failed':
!                 fails.append(pkg)
!             if rv == 'not found':
!                 # should not happen, but we track so we can catch where
!                 # an entity is defined but not listed in package_locations
!                 missing.append(pkg)
      return (retrieved, fails, missing)
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.