#! /usr/bin/python3
# vim: et ts=4 sw=4

# Copyright © 2010-2013 Piotr Ożarowski <piotr@debian.org>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

import logging
import os
import re
import sys
from optparse import OptionParser, SUPPRESS_HELP
from os.path import exists, join
from shutil import copy as fcopy
from dhpython.debhelper import DebHelper
from dhpython.depends import Dependencies
from dhpython.interpreter import Interpreter, EXTFILE_RE
from dhpython.version import supported, default, Version, VersionRange
from dhpython.pydist import validate as validate_pydist
from dhpython.fs import fix_locations, Scan
from dhpython.option import Option
from dhpython.tools import pyinstall, pyremove

# initialize script
logging.basicConfig(format='%(levelname).1s: dh_python3 '
                           '%(module)s:%(lineno)d: %(message)s')
log = logging.getLogger('dhpython')
os.umask(0o22)
DEFAULT = default('cpython3')
SUPPORTED = supported('cpython3')


class Scanner(Scan):
    def handle_ext(self, fpath):
        path, fname = fpath.rsplit('/', 1)
        tagver = EXTFILE_RE.search(fname)
        if tagver is None:
            # yeah, python3.1 is not covered, but we don't want to
            # mess with non-Python libraries, don't we?
            return
        tagver = tagver.groupdict()['ver']
        if tagver is None:
            return
        tagver = Version("%s.%s" % (tagver[0], tagver[1]))
        return tagver


def main():
    usage = '%prog -p PACKAGE [-V [X.Y][-][A.B]] DIR [-X REGEXPR]\n'
    parser = OptionParser(usage, version='%prog 2.20151103ubuntu1.1', option_class=Option)
    parser.add_option('--no-guessing-deps', action='store_false',
                      dest='guess_deps', default=True,
                      help='disable guessing dependencies')
    parser.add_option('--skip-private', action='store_true', default=False,
                      help='don\'t check private directories')
    parser.add_option('-v', '--verbose', action='store_true', default=False,
                      help='turn verbose mode on')
    # arch=False->arch:all only, arch=True->arch:any only, None->all of them
    parser.add_option('-i', '--indep', action='store_false',
                      dest='arch', default=None,
                      help='act on architecture independent packages')
    parser.add_option('-a', '-s', '--arch', action='store_true',
                      dest='arch', help='act on architecture dependent packages')
    parser.add_option('-q', '--quiet', action='store_false', dest='verbose',
                      help='be quiet')
    parser.add_option('-p', '--package', action='append',
                      help='act on the package named PACKAGE')
    parser.add_option('-N', '--no-package', action='append',
                      help='do not act on the specified package')
    parser.add_option('--compile-all', action='store_true', default=False,
                      help='compile all files from given private directory '
                           'in postinst, not just the ones provided by the '
                           'package')
    parser.add_option('-V', type='version_range', dest='vrange',
                      help='specify list of supported Python versions. ' +
                           'See py3compile(1) for examples')
    parser.add_option('-X', '--exclude', action='append', dest='regexpr',
                      help='exclude items that match given REGEXPR. You may '
                           'use this option multiple times to build up a list'
                           ' of things to exclude.')
    parser.add_option('--depends', action='append',
                      help='translate given requirements into Debian '
                           'dependencies and add them to ${python3:Depends}. '
                           'Use it for missing items in requires.txt.')
    parser.add_option('--recommends', action='append',
                      help='translate given requirements into Debian '
                           'dependencies and add them to ${python3:Recommends}')
    parser.add_option('--suggests', action='append',
                      help='translate given requirements into Debian '
                           'dependencies and add them to ${python3:Suggests}')
    parser.add_option('--requires', action='append',
                      help='translate requirements from given file into Debian '
                           'dependencies and add them to ${python3:Depends}')
    parser.add_option('--shebang',
                      help='use given command as shebang in scripts')
    parser.add_option('--ignore-shebangs', action='store_true', default=False,
                      help='do not translate shebangs into Debian dependencies')
    parser.add_option('--no-dbg-cleaning', action='store_false',
                      dest='clean_dbg_pkg', default=True,
                      help='do not remove files from debug packages')
    parser.add_option('--no-ext-rename', action='store_true',
                      default=False, help='do not add magic tags nor multiarch'
                                          ' tuples to extension file names)')
    parser.add_option('--no-shebang-rewrite', action='store_true',
                      default=False, help='do not rewrite shebangs')
    # ignore some debhelper options:
    parser.add_option('-O', help=SUPPRESS_HELP)

    options, args = parser.parse_args(sys.argv[1:] +
                                      os.environ.get('DH_OPTIONS', '').split())
    # regexpr option type is not used so lets check patterns here
    for pattern in options.regexpr or []:
        # fail now rather than at runtime
        try:
            pattern = re.compile(pattern)
        except Exception:
            log.error('regular expression is not valid: %s', pattern)
            exit(1)

    if not args:
        private_dir = None
    else:
        private_dir = args[0]
        if not private_dir.startswith('/'):
            # handle usr/share/foo dirs (without leading slash)
            private_dir = '/' + private_dir
    # TODO: support more than one private dir at the same time (see :meth:scan)
    if options.skip_private:
        private_dir = False

    if options.verbose or os.environ.get('DH_VERBOSE') == '1':
        log.setLevel(logging.DEBUG)
        log.debug('version: 2.20151103ubuntu1.1')
        log.debug('argv: %s', sys.argv)
        log.debug('options: %s', options)
        log.debug('args: %s', args)
        log.debug('supported Python versions: %s (default=%s)',
                  ','.join(str(v) for v in SUPPORTED), DEFAULT)
    else:
        log.setLevel(logging.INFO)

    try:
        dh = DebHelper(options, impl='cpython3')
    except Exception as e:
        log.error('cannot initialize DebHelper: %s', e)
        exit(2)
    if not options.vrange and dh.python_version:
        options.vrange = VersionRange(dh.python_version)

    interpreter = Interpreter('python3')
    for package, pdetails in dh.packages.items():
        if options.arch is False and pdetails['arch'] != 'all' or \
                options.arch is True and pdetails['arch'] == 'all':
            continue
        log.debug('processing package %s...', package)
        interpreter.debug = package.endswith('-dbg')

        if not private_dir:
            try:
                pyinstall(interpreter, package, options.vrange)
            except Exception as err:
                log.error("%s.pyinstall: %s", package, err)
                exit(4)
            try:
                pyremove(interpreter, package, options.vrange)
            except Exception as err:
                log.error("%s.pyremove: %s", package, err)
                exit(5)
            fix_locations(package, interpreter, SUPPORTED, options)
        stats = Scanner(interpreter, package, private_dir, options).result

        dependencies = Dependencies(package, 'cpython3')
        dependencies.parse(stats, options)

        if stats['ext_vers']:
            dh.addsubstvar(package, 'python3:Versions',
                           ', '.join(str(v) for v in sorted(stats['ext_vers'])))
            ps = package.split('-', 1)
            if len(ps) > 1 and ps[0] == 'python3':
                dh.addsubstvar(package, 'python3:Provides',
                               ', '.join("python%s-%s" % (i, ps[1])
                               for i in sorted(stats['ext_vers'])))

        pyclean_added = False  # invoke pyclean only once in maintainer script
        if stats['compile']:
            args = ''
            if options.vrange:
                args += "-V %s" % options.vrange
            dh.autoscript(package, 'postinst', 'postinst-py3compile', args)
            dh.autoscript(package, 'prerm', 'prerm-py3clean', '')
            pyclean_added = True
        for pdir, details in sorted(stats['private_dirs'].items()):
            if not details.get('compile'):
                continue
            if not pyclean_added:
                dh.autoscript(package, 'prerm', 'prerm-py3clean', '')
                pyclean_added = True

            args = pdir

            ext_for = details.get('ext_vers')
            ext_no_version = details.get('ext_no_version')
            if ext_for is None and not ext_no_version:  # no extension
                shebang_versions = list(i.version for i in details.get('shebangs', [])
                                        if i.version and i.version.minor)
                if not options.ignore_shebangs and len(shebang_versions) == 1:
                    # only one version from shebang
                    args += " -V %s" % shebang_versions[0]
                elif options.vrange and options.vrange != (None, None):
                    args += " -V %s" % options.vrange
            elif ext_no_version:
                # at least one extension's version not detected
                if options.vrange and '-' not in str(options.vrange):
                    ver = str(options.vrange)
                else:  # try shebang or default Python version
                    ver = (list(i.version for i in details.get('shebangs', [])
                                if i.version and i.version.minor) or [None])[0] or DEFAULT
                dependencies.depend("python%s" % ver)
                args += " -V %s" % ver
            else:
                extensions = sorted(ext_for)
                vr = VersionRange(minver=extensions[0], maxver=extensions[-1])
                args += " -V %s" % vr

            for pattern in options.regexpr or []:
                args += " -X '%s'" % pattern.replace("'", r"'\''")

            dh.autoscript(package, 'postinst', 'postinst-py3compile', args)

        dependencies.export_to(dh)

        pydist_file = join('debian', "%s.pydist" % package)
        if exists(pydist_file):
            if not validate_pydist(pydist_file):
                log.warning("%s.pydist file is invalid", package)
            else:
                dstdir = join('debian', package, 'usr/share/python3/dist/')
                if not exists(dstdir):
                    os.makedirs(dstdir)
                fcopy(pydist_file, join(dstdir, package))

    dh.save()

if __name__ == '__main__':
    main()
