#!/usr/bin/python2.7 # -*- coding: utf-8 -*- # # Univention Joinscripts """Install join scripts.""" # # Copyright 2007-2014 Univention GmbH # # http://www.univention.de/ # # All rights reserved. # # The source code of this program is made available # under the terms of the GNU Affero General Public License version 3 # (GNU AGPL V3) as published by the Free Software Foundation. # # Binary versions of this program provided by Univention to you as # well as other copyrighted, protected or trademarked materials like # Logos, graphics, fonts, specific documentations and configurations, # cryptographic keys etc. are subject to a license agreement between # you and Univention and not subject to the GNU AGPL V3. # # In the case you use this program under the terms of the GNU AGPL V3, # the program is provided in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public # License with the Debian GNU/Linux or Univention distribution in file # /usr/share/common-licenses/AGPL-3; if not, see # . import os import sys import re import subprocess from glob import glob from optparse import OptionParser from univention.debhelper import doIt, binary_packages def debhelper_file(package, ext): """Name of a package configuration file.""" return os.path.join('debian', '%s.%s.debhelper' % (package, ext)) JOINSCRIPT_DIR = '/usr/lib/univention-install' UNJOINSCRIPT_DIR = '/usr/lib/univention-uninstall' def _install_script(package, suffix, script_dir, dest_filename=None): cfile = '*%s.%s' % (package, suffix) try: filename = glob(cfile)[0] except IndexError: return None dest_path = os.path.join('debian', package, script_dir[1:]) doIt('install', '-m', '755', '-d', dest_path) if dest_filename is None: dest_filename = filename dest_filename = os.path.join(dest_path, os.path.basename(dest_filename)) doIt('install', '-m', '755', filename, dest_filename) return filename def install_joinscript(package): joinscript_name = _install_script(package, 'inst', JOINSCRIPT_DIR) if joinscript_name: source = open(joinscript_name).readlines() if 'python' in source[0]: output = subprocess.check_output([os.path.join('.', joinscript_name), '--supports-unjoin']) if output == '1\n': base, ext = os.path.splitext(joinscript_name) match = re.match(r'^\d+', base) if match: base = '%02d%s' % (99 - int(match.group()), base[match.end():]) unjoinscript_name = base + '-uninstall.uinst' _install_script(package, 'inst', UNJOINSCRIPT_DIR, unjoinscript_name) doIt('perl', '-e', 'use Debian::Debhelper::Dh_Lib;addsubstvar("%s", "misc:Depends", "univention-join (>= 7.1.1-2)");' % package) with open(debhelper_file(package, 'postinst'), 'a') as postinst: postinst.write('# Automatically added by univention-install-joinscript\n') postinst.write('. /usr/share/univention-lib/all.sh\n') postinst.write('call_joinscript %s\n' % joinscript_name) postinst.write('# End automatically added section\n') return joinscript_name def install_unjoinscript(package, joinscript_name, unjoinscript_name=None): if unjoinscript_name is None: unjoinscript_name = _install_script(package, 'uinst', UNJOINSCRIPT_DIR) or _install_script('%s-uninstall' % package, 'uinst', UNJOINSCRIPT_DIR) if unjoinscript_name: with open(debhelper_file(package, 'preinst'), 'a') as preinst: preinst.write('# Automatically added by univention-install-joinscript\n') preinst.write('if [ "$1" = "install" ]; then\n') preinst.write(' test -e %s/%s && rm %s/%s\n' % (JOINSCRIPT_DIR, unjoinscript_name, JOINSCRIPT_DIR, unjoinscript_name)) preinst.write('fi\n') preinst.write('# End automatically added section\n') with open(debhelper_file(package, 'prerm'), 'a') as prerm: prerm.write('# Automatically added by univention-install-joinscript\n') prerm.write('if [ "$1" = "remove" ]; then\n') prerm.write(' cp %s/%s %s\n' % (UNJOINSCRIPT_DIR, unjoinscript_name, JOINSCRIPT_DIR)) prerm.write('fi\n') prerm.write('# End automatically added section\n') with open(debhelper_file(package, 'postrm'), 'a') as postrm: postrm.write('# Automatically added by univention-install-joinscript\n') postrm.write('if [ "$1" = "remove" ]; then\n') postrm.write(' . /usr/share/univention-lib/all.sh\n') postrm.write(' . /usr/share/univention-join/joinscripthelper.lib\n') postrm.write(' call_unjoinscript %s\n' % unjoinscript_name) if joinscript_name: joinscript_name = os.path.basename(joinscript_name) joinscript_name = os.path.splitext(joinscript_name)[0] joinscript_name = re.sub(r'^\d+', '', joinscript_name) postrm.write(' joinscript_remove_script_from_status_file %s\n' % joinscript_name) postrm.write('fi\n') postrm.write('# End automatically added section\n') def main(): """Install (un)joinscripts.""" usage = "%prog" epilog = '''This is a debhelper like program to install Joinscripts and Unjoinscripts into the package build directories. The joinscript *.inst goes to /usr/lib/univention-install/ The unjoinscript *.uinst goes to /usr/lib/univention-uninstall/ ''' parser = OptionParser(usage=usage, epilog=epilog) parser.add_option('-v', '--verbose', dest='verbose', action='store_true', help='Verbose mode: show all commands that modify the package build directory.') options, args = parser.parse_args() if options.verbose: os.environ['DH_VERBOSE'] = '1' if args: parser.error('No argument expected') try: for package in binary_packages(): joinscript_name = install_joinscript(package) install_unjoinscript(package, joinscript_name) except IOError, ex: print >> sys.stderr, ex sys.exit(1) if __name__ == '__main__': main()