[Yum] code cleanup: one function in one source: readHeader

Vladimir Bormotov bor at vb.dn.ua
Mon Jul 22 19:16:13 UTC 2002


		Hi, seth!

>>>>> "sv" == seth vidal <skvidal at phy.duke.edu> writes:

>> >  I offer to allocate the common functions in a separate module.
>> >  package.py, rpmStuff.py, pkgStuff.py, pkg_tools.py...
>> >  I do not know, I like all these names;-)

>> Thats fine w/me - two prerequisites:

>> 1. no classes. make it all procedural for now.

 ok. ;-)

 
>> 2. comment the code heavily.

 surely


 sv> one more thing - it would probably be most useful if the functions got
 sv> broken out into:

 sv> rpmUtils.py, hdrUtils.py and sysUtils.py


 sv> they being, respectively, stuff to manipulate rpms, stuff to
 sv> manipulate the headers and/or info in the headers (ie ver comparison)
 
 where best place for
 
 def readHeader(file_name):
     """get rpm-header data from file"""
  
   ...
   
 with current implementation (read header from .rpm as well as .hdr)?
 
 hdrUtil?  rpmUtil?  split function?


 with OO-decomposition I will write:
 
 class rpm_header:
     """rpm-header data representation and routines"""
       
    def __init__(self, file_name):
	"""read header from a given file"""
        # part of readHeader implementation: read from .hdr
        try:
            log(6, 'load gzipped header "%s"' % file_name)
            _fd = gzip.open(file_name, 'r')
            _hdr = _fd.read()
        except:
            log(6, 'load plain header "%s"' % file_name)
            _fd = open(file_name, 'r')
            _hdr = _fd.read()
        try:
            log(6, 'decode header "%s"' % file_name)
            self.hdr = rpm.headerLoad(_hdr)
        except:
            log(4, 'Error decoding header file "%s"' % file_name)
            self.hdr = None
        fd.close()

 
    def getTag(self, tag_name):
        """get header field by name"""
        
        if tag_name == 'NAME':
            return self.hdr[rpm.RPMTAG_NAME]
            
          ...  # map other string tag_names, to rpm.RPMTAG_*
	
	elif tag_name == 'EPOCH':
	    _epoch = self.hdr[rpm.RPMTAG_EPOCH]
	    if _epoch is None: 
	        return '0'
            else:
                return str(self.hdr[rpm.RPMTAG_EPOCH])
        else:
            return 'Unknown tag name %s' % tag_name
            # or 
            # raise ValueError, 'Unknown tag_name = %s' % tag_name


 
    def getENVRA(self):
	"""get epoch, name, ver, rel, arch from"""
          ...  # map list of tag-names to tuple with tag values



 class rpm_package(rpm_header):
     """rpm-package data representation and routines"""

     def __init__(self, file_name):
         """read package data from given file"""
         # some code portion from current readHader
         log(6, 'load header from package "%s"' % file_name)
         _fd = os.open(file_name, os.O_RDONLY)
         self.hdr = rpm.headerFromPackage(fd)[0]
         os.close(fd)
     
       ...


 result: 

   - we not mix rpm/hdr routines.  All code clean, and transparetn like
     glass ;-)

   - real getTag() code will be only at one place - rpm_header class
     implementation, and can be easy used from rpm_package class instances.

   - many other code will be writed once.  Mostly, in rpm_header
     implementation


 from every instance of rpm_header class or rpm_pckage class we can call
 getTag('NAME') method, and get (for example) same result as we get after
 call

  rpm -qp --queryformat '%{NAME}' some-package-file.rpm

 at shell prompt.

 nice?  But, ok, "no classs for now".


 Just say "where best place of readHader function?" ;-)


 sv> and generic stuff to mkdirs/find stuff on the system.

 no questions/suggestions ;-)
 
-- 
		Bor.



More information about the Yum mailing list