#!/usr/bin/python3 """ Copyright (c) 2019-2020, Ian Santopietro All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ import glob import logging import os import repolib logging.basicConfig(level=logging.DEBUG) logging.debug('Loaded logger') print( "This program will attempt to convert your software sources to DEB822-style" " Format. The DEB822 format is a newer style for specifying APT sources, " "and will eventually deprecate the one-line style format currently in " "widespread use. See\n\n" "https://manpages.debian.org/stretch/apt/sources.list.5.en.html#DEB822-STYLE_FORMAT" "\n\nfor more information.\n\n" "For safety, the old *.list files will be saved in their original locations " "with their entries commented out. If something doesn't work after running " "this script, try deleting the .sources files in /etc/apt/sources.list.d/ " "and uncommenting the appropriate lines in these files, then try again.\n\n" ) def standard_conversion(): do_conversion = input("Convert third-party sources? (y/N): ") if do_conversion == '': do_conversion = 'n' logging.debug(do_conversion) if do_conversion[0].lower() != 'y': return list_files = glob.glob('/etc/apt/sources.list.d/*.list') logging.debug('List of input files: %s' % list_files) for list_file in list_files: logging.info('Converting %s' % list_file) source = repolib.Source(enabled=True) source.uris = [] source.suites = [] source.components = [] source.options = {} source.set_source_enabled(False) new_filename = os.path.basename(list_file) new_filename = new_filename.split('.')[0] source.filename = "{}.sources".format(new_filename) source_raw = [] with open(list_file, 'r') as current_list: for line in current_list: if 'deb' in line: source_raw.append(line.strip()) for repo_line in source_raw: if 'deb ' in repo_line: if repo_line.startswith('#'): source.set_enabled(False) if not '[' in repo_line: repo_list = repo_line.strip('#').split() source.uris.append(repo_list[1]) source.suites.append(repo_list[2]) source.components.append(repo_list[3]) else: repo_list = repo_line.strip('#').split() source.uris.append(repo_list[2]) source.suites.append(repo_list[3]) source.components.append(repo_list[4]) options_str = repo_list[1].strip('[ ]') options_list = options_str.split(',') for option in options_list: option_key = option.split('=')[0] option_val = option.split('=')[1] option_key = source.translate_options(option_key) source.options[option_key] = option_val elif 'deb-src' in repo_line: if not repo_line.startswith('#'): source.set_source_enabled(True) print( 'Converted source for {}:\n'.format(os.path.basename(list_file)) ) print(source.make_source_string()) accept_source = input('\nIs this okay? (Y/n): ').lower() if accept_source == '': accept_source = 'y' logging.debug('Accepted? %s ' % accept_source) if accept_source == 'y': source.save_to_disk() new_file_contents = '' with open(list_file, 'r') as current_list: for line in current_list: new_line = '# {}'.format(line) new_file_contents += new_line with open(list_file, 'w') as current_list: current_list.write(new_file_contents) def system_conversion(): do_conversion = input("Convert system sources? (y/N): ") if do_conversion == '': do_conversion = 'n' logging.debug(do_conversion) if do_conversion[0].lower() != 'y': return system_source = repolib.SystemSource() logging.info(system_source.make_source_string()) with open('/etc/apt/sources.list') as sources_list: for line in sources_list: deb_line = line.split(' ') if deb_line[0] == 'deb': if not deb_line[1].strip('/') in system_source.uris: logging.debug('%s is not in the system sources' % deb_line[1]) system_source.uris.append(deb_line[1]) if not deb_line[2] in system_source.suites: system_source.suites.append(deb_line[2]) system_comps = [ 'main', 'universe', 'multiverse', 'restricted' ] for comp in deb_line[3:]: if not comp.strip() in system_source.components and comp.strip() in system_comps: system_source.components.append(comp) print('Converted system source:\n\n') print(system_source.make_source_string()) accept_sys_conversion = input('\n\n Is this okay? (Y/n): ').lower() if accept_sys_conversion == '': accept_sys_conversion = 'y' if accept_sys_conversion == 'y': system_source.save_to_disk() new_file_contents = '' with open('/etc/apt/sources.list', 'r') as sources_list: for line in sources_list: new_line = '# {}'.format(line) new_file_contents += new_line with open('/etc/apt/sources.list', 'w') as sources_list: sources_list.write(new_file_contents) print('Conversion complete!') standard_conversion() system_conversion()