home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / doc / python-apt / examples / dpkg-contents.py < prev    next >
Encoding:
Python Source  |  2009-03-30  |  1.7 KB  |  56 lines

  1. #!/usr/bin/python
  2. """Emulate dpkg --contents"""
  3.  
  4. import grp
  5. import pwd
  6. import stat
  7. import sys
  8. import time
  9.  
  10. import apt_inst
  11.  
  12.  
  13. def format_mode(what, mode):
  14.     """Return the symbolic mode"""
  15.     s_mode = dict(DIR="d", HARDLINK="h", FILE="-").get(what)
  16.     s_mode += ((mode & stat.S_IRUSR) and "r" or "-")
  17.     s_mode += ((mode & stat.S_IWUSR) and "w" or "-")
  18.     s_mode += ((mode & stat.S_IXUSR) and (mode & stat.S_ISUID and "s" or "x")
  19.                                       or (mode & stat.S_ISUID and "S" or "-"))
  20.     s_mode += ((mode & stat.S_IRGRP) and "r" or "-")
  21.     s_mode += ((mode & stat.S_IWGRP) and "w" or "-")
  22.     s_mode += ((mode & stat.S_IXGRP) and (mode & stat.S_ISGID and "s" or "x")
  23.                                       or (mode & stat.S_ISGID and "S" or "-"))
  24.     s_mode += ((mode & stat.S_IROTH) and "r" or "-")
  25.     s_mode += ((mode & stat.S_IWOTH) and "w" or "-")
  26.     s_mode += ((mode & stat.S_IXOTH) and "x" or "-")
  27.     return s_mode
  28.  
  29.  
  30. def callback(what, name, link, mode, uid, gid, size, mtime, major, minor):
  31.     """callback for debExtract"""
  32.     s_mode = format_mode(what, mode)
  33.     s_owner = "%s/%s" % (pwd.getpwuid(uid)[0], grp.getgrgid(gid)[0])
  34.     s_size = "%9d" % size
  35.     s_time = time.strftime("%Y-%m-%d %H:%M", time.localtime(mtime))
  36.     s_name = name.startswith(".") and name or ("./" + name)
  37.     if link:
  38.         s_name += " link to %s" % link
  39.     print s_mode, s_owner, s_size, s_time, s_name
  40.  
  41.  
  42. def main():
  43.     """Main function"""
  44.     if len(sys.argv) < 2:
  45.         print >> sys.stderr, "need filename argumnet"
  46.         sys.exit(1)
  47.  
  48.     fobj = open(sys.argv[1])
  49.     try:
  50.         apt_inst.debExtract(fobj, callback, "data.tar.gz")
  51.     finally:
  52.         fobj.close()
  53.  
  54. if __name__ == "__main__":
  55.     main()
  56.