home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / python / debpython / tools.py < prev    next >
Encoding:
Python Source  |  2012-05-04  |  3.8 KB  |  121 lines

  1. # -*- coding: UTF-8 -*-
  2. # Copyright ┬⌐ 2010 Piotr O┼╝arowski <piotr@debian.org>
  3. #
  4. # Permission is hereby granted, free of charge, to any person obtaining a copy
  5. # of this software and associated documentation files (the "Software"), to deal
  6. # in the Software without restriction, including without limitation the rights
  7. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. # copies of the Software, and to permit persons to whom the Software is
  9. # furnished to do so, subject to the following conditions:
  10. #
  11. # The above copyright notice and this permission notice shall be included in
  12. # all copies or substantial portions of the Software.
  13. #
  14. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. # THE SOFTWARE.
  21.  
  22. from __future__ import with_statement
  23. import logging
  24. import re
  25. from cPickle import dumps
  26. from os import symlink
  27. from debpython.version import getver
  28.  
  29. log = logging.getLogger(__name__)
  30. EGGnPTH_RE = re.compile(r'(.*?)(-py\d\.\d(?:-[^.]*)?)?(\.egg-info|\.pth)$')
  31. SHEBANG_RE = re.compile(r'^#!\s*/usr/bin/(?:env\s+)?(python(\d+\.\d+)?(?:-dbg)?).*')
  32.  
  33.  
  34. def sitedir(version, package=None, gdb=False):
  35.     """Return path to site-packages directory.
  36.  
  37.     >>> sitedir((2, 5))
  38.     '/usr/lib/python2.5/site-packages/'
  39.     >>> sitedir((2, 7), 'python-foo', True)
  40.     'debian/python-foo/usr/lib/debug/usr/lib/python2.7/dist-packages/'
  41.     """
  42.     if isinstance(version, basestring):
  43.         version = tuple(int(i) for i in version.split('.'))
  44.  
  45.     if version >= (2, 6):
  46.         path = "/usr/lib/python%d.%d/dist-packages/" % version
  47.     else:
  48.         path = "/usr/lib/python%d.%d/site-packages/" % version
  49.  
  50.     if gdb:
  51.         path = "/usr/lib/debug%s" % path
  52.     if package:
  53.         path = "debian/%s%s" % (package, path)
  54.  
  55.     return path
  56.  
  57.  
  58. def relpath(target, link):
  59.     """Return relative path.
  60.  
  61.     >>> relpath('/usr/share/python-foo/foo.py', '/usr/bin/foo', )
  62.     '../share/python-foo/foo.py'
  63.     """
  64.     t = target.split('/')
  65.     l = link.split('/')
  66.     while l[0] == t[0]:
  67.         del l[0], t[0]
  68.     return '/'.join(['..'] * (len(l) - 1) + t)
  69.  
  70.  
  71. def relative_symlink(target, link):
  72.     """Create relative symlink."""
  73.     return symlink(relpath(target, link), link)
  74.  
  75.  
  76. def shebang2pyver(fname):
  77.     """Check file's shebang.
  78.  
  79.     :rtype: tuple
  80.     :returns: pair of Python interpreter string and Python version
  81.     """
  82.     try:
  83.         with open(fname) as fp:
  84.             data = fp.read(32)
  85.             match = SHEBANG_RE.match(data)
  86.             if not match:
  87.                 return None
  88.             res = match.groups()
  89.             if res != (None, None):
  90.                 if res[1]:
  91.                     res = res[0], getver(res[1])
  92.                 return res
  93.     except IOError:
  94.         log.error('cannot open %s', fname)
  95.  
  96.  
  97. def clean_egg_name(name):
  98.     """Remove Python version and platform name from Egg files/dirs.
  99.  
  100.     >>> clean_egg_name('python_pipeline-0.1.3_py3k-py3.1.egg-info')
  101.     'python_pipeline-0.1.3_py3k.egg-info'
  102.     >>> clean_egg_name('Foo-1.2-py2.7-linux-x86_64.egg-info')
  103.     'Foo-1.2.egg-info'
  104.     """
  105.     match = EGGnPTH_RE.match(name)
  106.     if match and match.group(2) is not None:
  107.         return ''.join(match.group(1, 3))
  108.     return name
  109.  
  110.  
  111. class memoize(object):
  112.     def __init__(self, func):
  113.         self.func = func
  114.         self.cache = {}
  115.  
  116.     def __call__(self, *args, **kwargs):
  117.         key = dumps((args, kwargs))
  118.         if key not in self.cache:
  119.             self.cache[key] = self.func(*args, **kwargs)
  120.         return self.cache[key]
  121.