def add_arguments(self, *, parser): # noqa: D102 assets_directory = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'assets') blacklist_path = os.path.join(assets_directory, 'apt_package_blacklist.txt') sources_list_path = os.path.join(assets_directory, 'xenial.sources.list') if get_ubuntu_distribution_version() == 'bionic': sources_list_path = os.path.join(assets_directory, 'bionic.sources.list') elif get_ubuntu_distribution_version() == 'focal': sources_list_path = os.path.join(assets_directory, 'focal.sources.list') parser.add_argument( '--apt-package-blacklist', default=blacklist_path, help='A file with newline separated names of packages that should' 'not be installed into the bundle') parser.add_argument( '--apt-sources-list', default=sources_list_path, help='Path to the apt sources list that should be used for package' 'installation in the bundle.') parser.add_argument( '--apt-allow-insecure', action='store_true', help='Sets Acquire::AllowInsecureRepositories and ' 'Acquire::AllowDowngradeToInsecureRepositories to True. See ' 'apt-secure(8) manpage for more information.')
def should_load(self): # noqa: D102 """Determine if this plugin should load.""" try: get_ubuntu_distribution_version() except ValueError: # Not on ubuntu, shouldn't load return False try: import apt # noqa: F401 except ImportError: raise RuntimeError(""" Please install python3-apt in order to bundle your application You can do this by executing `apt-get install python3-apt`. If you are using a venv you will need to re-create it with the '--system-site-packages' option in order to have access to the library. """) return True
def add_arguments(self, *, parser): # noqa: D102 assets_directory = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'assets') blacklist_path = os.path.join( assets_directory, 'apt_package_blacklist.txt') sources_list_path = os.path.join(assets_directory, 'xenial.sources.list') if get_ubuntu_distribution_version() == 'bionic': sources_list_path = os.path.join(assets_directory, 'bionic.sources.list') parser.add_argument( '--apt-package-blacklist', default=blacklist_path, help='A file with newline separated names of packages that should' 'not be installed into the bundle') parser.add_argument( '--apt-sources-list', default=sources_list_path, help='Path to the apt sources list that should be used for package' 'installation in the bundle.')
def setup(self): # noqa: D102 # Importing apt here allows us to run # unit tests on OSX import apt # Get config values before creating cache, as the cache changes # the global config values when initialized. trusted = apt.apt_pkg.config.find_file('Dir::Etc::Trusted') trustedparts = apt.apt_pkg.config.find_file('Dir::Etc::TrustedParts') # Create cache after getting config values, the cache changes # global config values. self._cache = apt.Cache(rootdir=self._cache_dir, progress=apt.progress.text.OpProgress()) # Always get config values before creating the cache. apt.apt_pkg.config.set('APT::Install-Recommends', 'False') apt.apt_pkg.config.set('Dir::Etc::Trusted', trusted) apt.apt_pkg.config.set('Dir::Etc::TrustedParts', trustedparts) apt.apt_pkg.config.set('Acquire::BrokenProxy', 'true') apt.apt_pkg.config.set('Acquire::http::Pipeline-Depth', '0') apt.apt_pkg.config.set('Acquire::http::No-Cache', 'true') apt.apt_pkg.config.set('Acquire::http::Retries', '3') if self.allow_insecure: apt.apt_pkg.config.set('Acquire::AllowInsecureRepositories', 'True') apt.apt_pkg.config.set( 'Acquire::AllowDowngradeToInsecureRepositories', 'True') apt.apt_pkg.config.clear('APT::Update::Post-Invoke-Success') sources_list_file = os.path.join(self._cache_dir, 'etc', 'apt', 'sources.list') os.makedirs(os.path.dirname(sources_list_file), exist_ok=True) with open(sources_list_file, 'w') as f: with open(self.context.args.apt_sources_list, 'r') as sources: f.write(sources.read()) if self.include_sources: os.makedirs(self.sources_path, exist_ok=True) # We need to open and close the cache before calling update because # of a bug # if we don't do this then the update fails to pull anything from the # remote repos self._cache.open() self._cache.close() # Update the cache to the latest, we might not want to do this if the # cache already exists? try: self._cache.update() except apt.cache.FetchFailedException as e: logger.error('Could not fetch from repositories: {}'.format(e)) raise RuntimeError('Failed to fetch from repositories. Did ' 'you set your keys correctly?') self._cache.open() # Workaround for pip-requirements not installing python-pip self.add_to_install_list('python3-pip') # Currently not available in Focal if get_ubuntu_distribution_version() != 'focal': self.add_to_install_list('python-pip')