home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / bin / pysupport-movemodules < prev    next >
Encoding:
Text File  |  2006-08-02  |  3.0 KB  |  92 lines

  1. #! /usr/bin/python
  2. #
  3. # copyright (c) 2006 Josselin Mouette <joss@debian.org>
  4. # Licensed under the GNU Lesser General Public License, version 2.1
  5. # See COPYING for details
  6.  
  7. from optparse import OptionParser
  8. import os,os.path,md5,re
  9.  
  10. sourcepath='usr/share/python-support'
  11. extensionpath='usr/lib/python-support'
  12.  
  13. parser = OptionParser(usage="usage: %prog [options] [directory [...]]")
  14.  
  15. parser.add_option("-v", "--verbose", action="store_true", dest="verbose",
  16.                   help="verbose output", default=False)
  17. parser.add_option("-p", "--package", dest="package")
  18.  
  19. (options, args) = parser.parse_args()
  20.  
  21. py_supported = os.popen("pyversions -s").readline().rstrip().split()
  22.  
  23. if not args:
  24.   parser.error("No directory to process.")
  25.  
  26. for basedir in args:
  27.   if not os.path.isdir(basedir):
  28.     parser.error("%s is not a directory."%basedir)
  29.  
  30. class filelist:
  31.   def __init__(self):
  32.     self.d={}
  33.     self.pylist=[]
  34.   def addsum(self,file,pyver,sum):
  35.     if file not in self.d:
  36.       self.d[file]={}
  37.     self.d[file][pyver]=sum
  38.   def addpyver(self,pyver):
  39.     self.pylist.append(pyver)
  40.   def isallthesame(self,file):
  41.     if file.endswith(".so"):
  42.       # If there is a .so, no need to even check, it must be moved
  43.       return False
  44.     elif re.search('\.so(?:\.\d+){0,3}$', file):
  45.       print "%s: this shared object should not be versioned"%file
  46.       return False
  47.     try:
  48.       s=[ self.d[file][pyver] for pyver in self.pylist ]
  49.     except KeyError:
  50.       return False
  51.     return (s.count(s[0]) == len(self.pylist))
  52.   def list(self,file):
  53.     return self.d[file].keys()
  54.   def __iter__(self):
  55.     return iter(self.d)
  56.  
  57. for basedir in args:
  58.   basedir=basedir.rstrip('/')
  59.   package=options.package
  60.   if not package:
  61.     package=os.path.split(basedir)[1]
  62.   if not package:
  63.     raise "Unable to extract the package name."
  64.  
  65.   file_dict=filelist()
  66.   for pyvers in py_supported:
  67.     pydir=os.path.join(basedir,"usr/lib",pyvers,"site-packages")
  68.     if not os.path.isdir(pydir):
  69.       continue
  70.     file_dict.addpyver(pyvers)
  71.     for dir, dirs, files in os.walk(pydir):
  72.       reldir = dir[len(pydir):].lstrip('/')
  73.       for curfile in files:
  74.         relfile = os.path.join(reldir,curfile)
  75.         absfile = os.path.join(pydir,relfile)
  76.         file_dict.addsum(relfile,pyvers,md5.new(file(absfile).read()).digest())
  77.  
  78.   for relfile in file_dict:
  79.     splitfile=not file_dict.isallthesame(relfile)
  80.     destdir=os.path.join(sourcepath,package)
  81.     for pyver in file_dict.list(relfile):
  82.       sourcefile=os.path.join(basedir,"usr/lib",pyver,"site-packages",relfile)
  83.       if relfile.endswith(".pyc") or relfile.endswith(".pyo"):
  84.         os.remove(sourcefile)
  85.         continue
  86.       if splitfile:
  87.         destdir=os.path.join(extensionpath,package,pyver)
  88.       os.renames(sourcefile,os.path.join(basedir,destdir,relfile))
  89.   # Handle the case when there are only .so files
  90.   if os.path.isdir(os.path.join(basedir,extensionpath)) and not os.path.isdir(os.path.join(basedir,sourcepath,package)):
  91.     os.makedirs(os.path.join(basedir,sourcepath,package))
  92.