File: pysupport-movemodules

package info (click to toggle)
python-support 0.5.6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 172 kB
  • ctags: 84
  • sloc: python: 434; perl: 212; sh: 20; makefile: 11
file content (95 lines) | stat: -rwxr-xr-x 3,139 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#! /usr/bin/python
#
# copyright (c) 2006 Josselin Mouette <joss@debian.org>
# Licensed under the GNU Lesser General Public License, version 2.1
# See COPYING for details

from optparse import OptionParser
import os,os.path,md5,re,sys

sourcepath='usr/share/python-support'
extensionpath='usr/lib/python-support'

parser = OptionParser(usage="usage: %prog [options] [directory [...]]")

parser.add_option("-v", "--verbose", action="store_true", dest="verbose",
                  help="verbose output", default=False)
parser.add_option("-p", "--package", dest="package")

(options, args) = parser.parse_args()

sys.path.append("/usr/lib/python-support/private/")
from pysupport import py_supported

# Set the umask so that directories are created with correct permissions
os.umask(022)

if not args:
  parser.error("No directory to process.")

for basedir in args:
  if not os.path.isdir(basedir):
    parser.error("%s is not a directory."%basedir)

class filelist:
  def __init__(self):
    self.d={}
    self.pylist=[]
  def addsum(self,file,pyver,sum):
    if file not in self.d:
      self.d[file]={}
    self.d[file][pyver]=sum
  def addpyver(self,pyver):
    self.pylist.append(pyver)
  def isallthesame(self,file):
    if file.endswith(".so"):
      # If there is a .so, no need to even check, it must be moved
      return False
    elif re.search('\.so(?:\.\d+){0,3}$', file):
      print "%s: this shared object should not be versioned"%file
      return False
    try:
      s=[ self.d[file][pyver] for pyver in self.pylist ]
    except KeyError:
      return False
    return (s.count(s[0]) == len(self.pylist))
  def list(self,file):
    return self.d[file].keys()
  def __iter__(self):
    return iter(self.d)

for basedir in args:
  basedir=basedir.rstrip('/')
  package=options.package
  if not package:
    package=os.path.split(basedir)[1]
  if not package:
    raise "Unable to extract the package name."

  file_dict=filelist()
  for pyvers in py_supported:
    pydir=os.path.join(basedir,"usr/lib",pyvers,"site-packages")
    if not os.path.isdir(pydir):
      continue
    file_dict.addpyver(pyvers)
    for dir, dirs, files in os.walk(pydir):
      reldir = dir[len(pydir):].lstrip('/')
      for curfile in files:
        relfile = os.path.join(reldir,curfile)
        absfile = os.path.join(pydir,relfile)
        file_dict.addsum(relfile,pyvers,md5.new(file(absfile).read()).digest())

  for relfile in file_dict:
    splitfile=not file_dict.isallthesame(relfile)
    destdir=os.path.join(sourcepath,package)
    for pyver in file_dict.list(relfile):
      sourcefile=os.path.join(basedir,"usr/lib",pyver,"site-packages",relfile)
      if relfile.endswith(".pyc") or relfile.endswith(".pyo"):
        os.remove(sourcefile)
        continue
      if splitfile:
        destdir=os.path.join(extensionpath,package,pyver)
      os.renames(sourcefile,os.path.join(basedir,destdir,relfile))
  # Handle the case when there are only .so files
  if os.path.isdir(os.path.join(basedir,extensionpath)) and not os.path.isdir(os.path.join(basedir,sourcepath,package)):
    os.makedirs(os.path.join(basedir,sourcepath,package))