File indexing completed on 2024-03-24 15:18:28

0001 # -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*-
0002 #
0003 # SPDX-FileCopyrightText: 2017 Harald Sitter <sitter@kde.org>
0004 #
0005 # SPDX-License-Identifier: GPL-3.0-or-later
0006 
0007 """woosh woosh
0008 
0009   Simple magic. debs property is an array of debs that get pulled via apt
0010   and unpacked into the installdir for staging. Key difference to builtin
0011   stage-packages is that this entirely disregards dependencies, so they
0012   need to be resolved another way.
0013 """
0014 
0015 import logging
0016 import glob
0017 import os
0018 import re
0019 import shutil
0020 import subprocess
0021 
0022 import snapcraft.plugins.make
0023 
0024 logger = logging.getLogger(__name__)
0025 
0026 class StabeDebsPlugin(snapcraft.BasePlugin):
0027 
0028     @classmethod
0029     def schema(cls):
0030         schema = super().schema()
0031         schema['properties']['debs'] = {
0032             'type': 'array',
0033             'minitems': 0,
0034             'uniqueItems': True,
0035             'items': {
0036                 'type': 'string',
0037             }
0038         }
0039 
0040         schema['properties']['exclude-debs'] = {
0041             'type': 'array',
0042             'minitems': 0,
0043             'uniqueItems': True,
0044             'items': {
0045                 'type': 'string',
0046             }
0047         }
0048 
0049         return schema
0050 
0051     @classmethod
0052     def get_build_properties(cls):
0053         # Inform Snapcraft of the properties associated with building. If these
0054         # change in the YAML Snapcraft will consider the build step dirty.
0055         return ['debs', 'exclude-debs']
0056 
0057     def __init__(self, name, options, project):
0058         super().__init__(name, options, project)
0059 
0060     def exclude(self, file):
0061         basename = os.path.basename(file)
0062         name = re.split('^(.+)_([^_]+)_([^_]+)\.deb$', basename)[1]
0063         return name in (self.options.exclude_debs or [])
0064 
0065     def build(self):
0066         super().build()
0067 
0068         logger.debug(os.getcwd())
0069         if self.options.debs:
0070             cmd = ['apt-get',
0071                    '-y',
0072                    '-o', 'Debug::NoLocking=true',
0073                    '-o', 'Dir::Cache::Archives=' + self.builddir,
0074                    '--reinstall',
0075                    '--download-only', 'install'] + self.options.debs
0076             subprocess.check_call(cmd, cwd=self.builddir)
0077 
0078         pkgs_abs_path = glob.glob(os.path.join(self.builddir, '*.deb'))
0079         for pkg in pkgs_abs_path:
0080             logger.debug(pkg)
0081             if self.exclude(pkg):
0082                 continue
0083             logger.debug('extract')
0084             subprocess.check_call(['dpkg-deb', '--extract', pkg, self.installdir])
0085 
0086         # # Non-recursive stage, not sure this ever has a use case with
0087         # # exclusion in the picture
0088         # for deb in self.options.debs:
0089         #     logger.debug(deb)
0090         #     subprocess.check_call(['apt', 'download', deb])
0091         #
0092         # pkgs_abs_path = glob.glob(os.path.join(self.builddir, '*.deb'))
0093         # for pkg in pkgs_abs_path:
0094         #     logger.debug(pkg)
0095         #     subprocess.check_call(['dpkg-deb', '--extract', pkg, self.installdir])