Path: blob/develop/build/sage_bootstrap/download/cmdline.py
4055 views
# -*- coding: utf-8 -*-1"""2View for the Download Commandline UI34This module handles the main "sage-download-file" commandline utility.5"""67# ****************************************************************************8# Copyright (C) 2015-2016 Volker Braun <[email protected]>9# 2015 Jeroen Demeyer10# 2020 Matthias Koeppe11#12# This program is free software: you can redistribute it and/or modify13# it under the terms of the GNU General Public License as published by14# the Free Software Foundation, either version 2 of the License, or15# (at your option) any later version.16# https://www.gnu.org/licenses/17# ****************************************************************************1819import sys20import logging21log = logging.getLogger()2223import argparse2425from sage_bootstrap.download.app import Application26from sage_bootstrap.env import SAGE_DISTFILES27from sage_bootstrap.util import is_url282930description = \31"""32Download files from a given URL or from the Sage mirror network.33"""343536def make_parser():37"""38The commandline argument parser for sage-download-file39"""40parser = argparse.ArgumentParser(41description=description,42)43parser.add_argument('--log', dest='log', default=None,44help='one of [DEBUG, INFO, ERROR, WARNING, CRITICAL]')4546parser.add_argument(47'--print-fastest-mirror', action='store_true',48help='Print out the fastest mirror. All other arguments are ignored in that case.')4950parser.add_argument(51'--quiet', action='store_true',52help='Hide progress bar')5354parser.add_argument(55'--timeout', type=float, default=None,56help='Timeout for network operations')5758parser.add_argument(59'--allow-upstream', action="store_true",60help='Whether to fall back to downloading from the upstream URL')6162parser.add_argument(63'url_or_tarball', type=str, nargs='?', default=None,64help="""A http:// url or a tarball filename. In the latter case, the65tarball is downloaded from the mirror network and its checksum66is verified.""")6768parser.add_argument(69'destination', type=str, nargs='?', default=None,70help="""Where to write the file. If the destination is not specified, a url71will be downloaded and the content written to stdout and a72tarball will be saved under {SAGE_DISTFILES}""".format(SAGE_DISTFILES=SAGE_DISTFILES))7374parser.add_argument(75'--no-check-certificate', action='store_true',76help='Do not check SSL certificates for https connections')7778return parser798081def run():82parser = make_parser()83if len(sys.argv) == 1:84parser.print_help()85return86args = parser.parse_args(sys.argv[1:])87if args.log is not None:88level = getattr(logging, args.log.upper())89log.setLevel(level=level)90log.debug('Commandline arguments: %s', args)91if args.no_check_certificate:92try:93import ssl94ssl._create_default_https_context = ssl._create_unverified_context95except ImportError:96pass97app = Application(timeout=args.timeout, quiet=args.quiet)98if (not args.print_fastest_mirror) and (args.url_or_tarball is None):99parser.print_help()100print()101print('error: either --print-fastest-mirror or url_or_tarball is required')102sys.exit(2)103if args.print_fastest_mirror:104app.print_fastest_mirror()105elif is_url(args.url_or_tarball):106app.download_url(args.url_or_tarball, args.destination)107else:108app.download_tarball(args.url_or_tarball, args.destination, args.allow_upstream)109110111def format_error(message):112stars = '*' * 72 + '\n'113sys.stderr.write(stars)114try:115import traceback116traceback.print_exc(file=sys.stderr)117sys.stderr.write(stars)118except BaseException:119pass120sys.stderr.write(message)121sys.stderr.write(stars)122123124def run_safe():125try:126run()127except Exception as error:128try:129format_error(error)130finally:131sys.exit(1)132133134if __name__ == '__main__':135run_safe()136137138