()
Command line interface for the ``apt-smart`` program.
Command line interface for the ``apt-smart`` program.
Command line interface for the apt - smart program .
def main():
    """Command line interface for the ``apt-smart`` program."""
    # Initialize logging to the terminal and system log.
    coloredlogs.install(syslog=True)
    # Command line option defaults.
    context = LocalContext()
    updater = AptMirrorUpdater(context=context)
    limit = MAX_MIRRORS
    url_char_len = URL_CHAR_LEN
    ubuntu_mode = False
    chroot_path = ''
    codename = ''
    actions = []
    # Parse the command line arguments.
    try:
        options, arguments = getopt.getopt(sys.argv[1:], 'r:fF:blL:c:auUx:m:vVR:C:qh', [
            'remote-host=', 'find-current-mirror', 'find-best-mirror', 'file-to-read=',
            'list-mirrors', 'url-char-len=', 'change-mirror=', 'auto-change-mirror', 'update',
            'update-package-lists', 'ubuntu', 'exclude=', 'max=', 'verbose', 'version', 'create-chroot=',
            'codename=', 'quiet', 'help',
        ])
        for option, value in options:
            if option in ('-r', '--remote-host'):
                if actions:
                    msg = "The %s option should be the first option given on the command line!"
                    raise Exception(msg % option)
                context = RemoteContext(value)
                updater = AptMirrorUpdater(context=context)
            elif option in ('-f', '--find-current-mirror'):
                actions.append(functools.partial(report_current_mirror, updater))
            elif option in ('-F', '--file-to-read'):
                updater.custom_mirror_file_path = value
            elif option in ('-b', '--find-best-mirror'):
                actions.append(functools.partial(report_best_mirror, updater))
            elif option in ('-l', '--list-mirrors'):
                actions.append(functools.partial(report_available_mirrors, updater))
            elif option in ('-L', '--url-char-len'):
                url_char_len = int(value)
            elif option in ('-c', '--change-mirror'):
                if value.strip().startswith(('http://', 'https://', 'ftp://', 'mirror://')):
                    actions.append(functools.partial(updater.change_mirror, value))
                else:
                    raise Exception("\'%s\' is not a valid mirror URL" % value)
            elif option in ('-a', '--auto-change-mirror'):
                actions.append(updater.change_mirror)
            elif option in ('-u', '--update', '--update-package-lists'):
                actions.append(updater.smart_update)
            elif option in ('-U', '--ubuntu'):
                ubuntu_mode = True
            elif option in ('-x', '--exclude'):
                actions.insert(0, functools.partial(updater.ignore_mirror, value))
            elif option in ('-m', '--max'):
                limit = int(value)
            elif option in ('-v', '--verbose'):
                coloredlogs.increase_verbosity()
            elif option in ('-V', '--version'):
                output("Version: %s on Python %i.%i", updater_version, sys.version_info[0], sys.version_info[1])
                return
            elif option in ('-C', '--codename'):
                codename = value
            elif option in ('-R', '--create-chroot'):
                chroot_path = value
            elif option in ('-q', '--quiet'):
                coloredlogs.decrease_verbosity()
            elif option in ('-h', '--help'):
                usage(__doc__)
                return
            else:
                assert False, "Unhandled option!"
        if codename and not chroot_path:
            assert False, "--codename must be used with valid -R to specify chroot path"
        if chroot_path:
            actions.append(functools.partial(updater.create_chroot, chroot_path, codename=codename))
        if not actions:
            usage(__doc__)
            return
        # Propagate options to the Python API.
        updater.max_mirrors = limit
        updater.url_char_len = url_char_len
        updater.ubuntu_mode = ubuntu_mode
    except Exception as e:
        warning("Error: Failed to parse command line arguments! (%s)" % e)
        sys.exit(1)
    # Perform the requested action(s).
    try:
        for callback in actions:
            callback()
    except Exception:
        logger.exception("Encountered unexpected exception! Aborting ..")
        sys.exit(1)
def main ( ) : # Initialize logging to the terminal and system log. coloredlogs . install ( syslog = True ) # Command line option defaults. context = LocalContext ( ) updater = AptMirrorUpdater ( context = context ) limit = MAX_MIRRORS url_char_len = URL_CHAR_LEN ubuntu_mode = False chroot_path = '' codename = '' actions = [ ] # Parse the command line arguments. try : options , arguments = getopt . getopt ( sys . argv [ 1 : ] , 'r:fF:blL:c:auUx:m:vVR:C:qh' , [ 'remote-host=' , 'find-current-mirror' , 'find-best-mirror' , 'file-to-read=' , 'list-mirrors' , 'url-char-len=' , 'change-mirror=' , 'auto-change-mirror' , 'update' , 'update-package-lists' , 'ubuntu' , 'exclude=' , 'max=' , 'verbose' , 'version' , 'create-chroot=' , 'codename=' , 'quiet' , 'help' , ] ) for option , value in options : if option in ( '-r' , '--remote-host' ) : if actions : msg = "The %s option should be the first option given on the command line!" raise Exception ( msg % option ) context = RemoteContext ( value ) updater = AptMirrorUpdater ( context = context ) elif option in ( '-f' , '--find-current-mirror' ) : actions . append ( functools . partial ( report_current_mirror , updater ) ) elif option in ( '-F' , '--file-to-read' ) : updater . custom_mirror_file_path = value elif option in ( '-b' , '--find-best-mirror' ) : actions . append ( functools . partial ( report_best_mirror , updater ) ) elif option in ( '-l' , '--list-mirrors' ) : actions . append ( functools . partial ( report_available_mirrors , updater ) ) elif option in ( '-L' , '--url-char-len' ) : url_char_len = int ( value ) elif option in ( '-c' , '--change-mirror' ) : if value . strip ( ) . startswith ( ( 'http://' , 'https://' , 'ftp://' , 'mirror://' ) ) : actions . append ( functools . partial ( updater . change_mirror , value ) ) else : raise Exception ( "\'%s\' is not a valid mirror URL" % value ) elif option in ( '-a' , '--auto-change-mirror' ) : actions . append ( updater . change_mirror ) elif option in ( '-u' , '--update' , '--update-package-lists' ) : actions . append ( updater . smart_update ) elif option in ( '-U' , '--ubuntu' ) : ubuntu_mode = True elif option in ( '-x' , '--exclude' ) : actions . insert ( 0 , functools . partial ( updater . ignore_mirror , value ) ) elif option in ( '-m' , '--max' ) : limit = int ( value ) elif option in ( '-v' , '--verbose' ) : coloredlogs . increase_verbosity ( ) elif option in ( '-V' , '--version' ) : output ( "Version: %s on Python %i.%i" , updater_version , sys . version_info [ 0 ] , sys . version_info [ 1 ] ) return elif option in ( '-C' , '--codename' ) : codename = value elif option in ( '-R' , '--create-chroot' ) : chroot_path = value elif option in ( '-q' , '--quiet' ) : coloredlogs . decrease_verbosity ( ) elif option in ( '-h' , '--help' ) : usage ( __doc__ ) return else : assert False , "Unhandled option!" if codename and not chroot_path : assert False , "--codename must be used with valid -R to specify chroot path" if chroot_path : actions . append ( functools . partial ( updater . create_chroot , chroot_path , codename = codename ) ) if not actions : usage ( __doc__ ) return # Propagate options to the Python API. updater . max_mirrors = limit updater . url_char_len = url_char_len updater . ubuntu_mode = ubuntu_mode except Exception as e : warning ( "Error: Failed to parse command line arguments! (%s)" % e ) sys . exit ( 1 ) # Perform the requested action(s). try : for callback in actions : callback ( ) except Exception : logger . exception ( "Encountered unexpected exception! Aborting .." ) sys . exit ( 1 )