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 / debhelper.py next >
Encoding:
Python Source  |  2012-05-04  |  8.2 KB  |  199 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. import logging
  23. import re
  24. from os import makedirs, chmod
  25. from os.path import exists, join, dirname
  26.  
  27. log = logging.getLogger(__name__)
  28.  
  29.  
  30. class DebHelper(object):
  31.     """Reinvents the wheel / some dh functionality (Perl is ugly ;-P)"""
  32.  
  33.     def __init__(self, packages=None, no_packages=None):
  34.         self.packages = {}
  35.         self.python_version = None
  36.         source_section = True
  37.         binary_package = None
  38.  
  39.         try:
  40.             fp = open('debian/control', 'r')
  41.         except IOError:
  42.             log.error('cannot find debian/control file')
  43.             exit(15)
  44.  
  45.         for line in fp:
  46.             if not line.strip():
  47.                 source_section = False
  48.                 binary_package = None
  49.                 continue
  50.             if binary_package:
  51.                 if binary_package.startswith('python3'):
  52.                     continue
  53.                 if packages and binary_package not in packages:
  54.                     continue
  55.                 if no_packages and binary_package in no_packages:
  56.                     continue
  57.                 if line.startswith('Architecture:'):
  58.                     arch = line[13:].strip()
  59.                     # TODO: if arch doesn't match current architecture:
  60.                     #del self.packages[binary_package]
  61.                     self.packages[binary_package]['arch'] = arch
  62.                     continue
  63.                 elif line.startswith('Breaks:') and '${python:Breaks}' in line:
  64.                     self.packages[binary_package]['uses_breaks'] = True
  65.                     continue
  66.             elif line.startswith('Package:'):
  67.                 binary_package = line[8:].strip()
  68.                 if binary_package.startswith('python3'):
  69.                     log.debug('skipping Python 3.X package: %s', binary_package)
  70.                     continue
  71.                 if packages and binary_package not in packages:
  72.                     continue
  73.                 if no_packages and binary_package in no_packages:
  74.                     continue
  75.                 self.packages[binary_package] = {'substvars': {},
  76.                                                  'autoscripts': {},
  77.                                                  'rtupdates': [],
  78.                                                  'uses_breaks': False}
  79.             elif line.startswith('Source:'):
  80.                 self.source_name = line[7:].strip()
  81.             elif source_section:
  82.                 if line.startswith('XS-Python-Version:') and not self.python_version:
  83.                     self.python_version = line[18:].strip()
  84.                 if line.startswith('X-Python-Version:'):
  85.                     self.python_version = line[17:].strip()
  86.         log.debug('source=%s, binary packages=%s', self.source_name, \
  87.                                                    self.packages.keys())
  88.  
  89.     def addsubstvar(self, package, name, value):
  90.         """debhelper's addsubstvar"""
  91.         self.packages[package]['substvars'].setdefault(name, []).append(value)
  92.  
  93.     def autoscript(self, package, when, template, args):
  94.         """debhelper's autoscript"""
  95.         self.packages[package]['autoscripts'].setdefault(when, {})\
  96.                             .setdefault(template, []).append(args)
  97.  
  98.     def add_rtupdate(self, package, value):
  99.         self.packages[package]['rtupdates'].append(value)
  100.  
  101.     def save_autoscripts(self):
  102.         for package, settings in self.packages.iteritems():
  103.             autoscripts = settings.get('autoscripts')
  104.             if not autoscripts:
  105.                 continue
  106.  
  107.             for when, templates in autoscripts.iteritems():
  108.                 fn = "debian/%s.%s.debhelper" % (package, when)
  109.                 if exists(fn):
  110.                     data = open(fn, 'r').read()
  111.                 else:
  112.                     data = ''
  113.  
  114.                 new_data = ''
  115.                 for tpl_name, args in templates.iteritems():
  116.                     for i in args:
  117.                         # try local one first (useful while testing dh_python2)
  118.                         fpath = join(dirname(__file__), '..',
  119.                                      "autoscripts/%s" % tpl_name)
  120.                         if not exists(fpath):
  121.                             fpath = "/usr/share/debhelper/autoscripts/%s" % tpl_name
  122.                         tpl = open(fpath, 'r').read()
  123.                         tpl = tpl.replace('#PACKAGE#', package)
  124.                         tpl = tpl.replace('#ARGS#', i)
  125.                         if tpl not in data and tpl not in new_data:
  126.                             new_data += "\n%s" % tpl
  127.                 if new_data:
  128.                     data += "\n# Automatically added by dh_python2:" +\
  129.                             "%s\n# End automatically added section\n" % new_data
  130.                     fp = open(fn, 'w')
  131.                     fp.write(data)
  132.                     fp.close()
  133.  
  134.     def save_substvars(self):
  135.         for package, settings in self.packages.iteritems():
  136.             substvars = settings.get('substvars')
  137.             if not substvars:
  138.                 continue
  139.             fn = "debian/%s.substvars" % package
  140.             if exists(fn):
  141.                 data = open(fn, 'r').read()
  142.             else:
  143.                 data = ''
  144.             for name, values in substvars.iteritems():
  145.                 p = data.find("%s=" % name)
  146.                 if p > -1:  # parse the line and remove it from data
  147.                     e = data[p:].find('\n')
  148.                     line = data[p + len("%s=" % name):\
  149.                                 p + e if e > -1 else None]
  150.                     items = [i.strip() for i in line.split(',') if i]
  151.                     if e > -1 and data[p + e:].strip():
  152.                         data = "%s\n%s" % (data[:p], data[p + e:])
  153.                     else:
  154.                         data = data[:p]
  155.                 else:
  156.                     items = []
  157.                 for j in values:
  158.                     if j not in items:
  159.                         items.append(j)
  160.                 if items:
  161.                     if data:
  162.                         data += '\n'
  163.                     data += "%s=%s\n" % (name, ', '.join(items))
  164.             data = data.replace('\n\n', '\n')
  165.             if data:
  166.                 fp = open(fn, 'w')
  167.                 fp.write(data)
  168.                 fp.close()
  169.  
  170.     def save_rtupdate(self):
  171.         for package, settings in self.packages.iteritems():
  172.             values = settings.get('rtupdates')
  173.             if not values:
  174.                 continue
  175.             d = "debian/%s/usr/share/python/runtime.d" % package
  176.             if not exists(d):
  177.                 makedirs(d)
  178.             fn = "%s/%s.rtupdate" % (d, package)
  179.             if exists(fn):
  180.                 data = open(fn, 'r').read()
  181.             else:
  182.                 data = '#! /bin/sh -e'
  183.             for dname, args in values:
  184.                 cmd = 'if [ "$1" = rtupdate ]; then' +\
  185.                       "\n\tpyclean %s" % dname +\
  186.                       "\n\tpycompile %s %s\nfi" % (args, dname)
  187.                 if cmd not in data:
  188.                     data += "\n%s" % cmd
  189.             if data:
  190.                 fp = open(fn, 'w')
  191.                 fp.write(data)
  192.                 fp.close()
  193.                 chmod(fn, 0755)
  194.  
  195.     def save(self):
  196.         self.save_substvars()
  197.         self.save_autoscripts()
  198.         self.save_rtupdate()
  199.