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