Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sage
Path: blob/develop/build/sage_bootstrap/download/cmdline.py
4055 views
1
# -*- coding: utf-8 -*-
2
"""
3
View for the Download Commandline UI
4
5
This module handles the main "sage-download-file" commandline utility.
6
"""
7
8
# ****************************************************************************
9
# Copyright (C) 2015-2016 Volker Braun <[email protected]>
10
# 2015 Jeroen Demeyer
11
# 2020 Matthias Koeppe
12
#
13
# This program is free software: you can redistribute it and/or modify
14
# it under the terms of the GNU General Public License as published by
15
# the Free Software Foundation, either version 2 of the License, or
16
# (at your option) any later version.
17
# https://www.gnu.org/licenses/
18
# ****************************************************************************
19
20
import sys
21
import logging
22
log = logging.getLogger()
23
24
import argparse
25
26
from sage_bootstrap.download.app import Application
27
from sage_bootstrap.env import SAGE_DISTFILES
28
from sage_bootstrap.util import is_url
29
30
31
description = \
32
"""
33
Download files from a given URL or from the Sage mirror network.
34
"""
35
36
37
def make_parser():
38
"""
39
The commandline argument parser for sage-download-file
40
"""
41
parser = argparse.ArgumentParser(
42
description=description,
43
)
44
parser.add_argument('--log', dest='log', default=None,
45
help='one of [DEBUG, INFO, ERROR, WARNING, CRITICAL]')
46
47
parser.add_argument(
48
'--print-fastest-mirror', action='store_true',
49
help='Print out the fastest mirror. All other arguments are ignored in that case.')
50
51
parser.add_argument(
52
'--quiet', action='store_true',
53
help='Hide progress bar')
54
55
parser.add_argument(
56
'--timeout', type=float, default=None,
57
help='Timeout for network operations')
58
59
parser.add_argument(
60
'--allow-upstream', action="store_true",
61
help='Whether to fall back to downloading from the upstream URL')
62
63
parser.add_argument(
64
'url_or_tarball', type=str, nargs='?', default=None,
65
help="""A http:// url or a tarball filename. In the latter case, the
66
tarball is downloaded from the mirror network and its checksum
67
is verified.""")
68
69
parser.add_argument(
70
'destination', type=str, nargs='?', default=None,
71
help="""Where to write the file. If the destination is not specified, a url
72
will be downloaded and the content written to stdout and a
73
tarball will be saved under {SAGE_DISTFILES}""".format(SAGE_DISTFILES=SAGE_DISTFILES))
74
75
parser.add_argument(
76
'--no-check-certificate', action='store_true',
77
help='Do not check SSL certificates for https connections')
78
79
return parser
80
81
82
def run():
83
parser = make_parser()
84
if len(sys.argv) == 1:
85
parser.print_help()
86
return
87
args = parser.parse_args(sys.argv[1:])
88
if args.log is not None:
89
level = getattr(logging, args.log.upper())
90
log.setLevel(level=level)
91
log.debug('Commandline arguments: %s', args)
92
if args.no_check_certificate:
93
try:
94
import ssl
95
ssl._create_default_https_context = ssl._create_unverified_context
96
except ImportError:
97
pass
98
app = Application(timeout=args.timeout, quiet=args.quiet)
99
if (not args.print_fastest_mirror) and (args.url_or_tarball is None):
100
parser.print_help()
101
print()
102
print('error: either --print-fastest-mirror or url_or_tarball is required')
103
sys.exit(2)
104
if args.print_fastest_mirror:
105
app.print_fastest_mirror()
106
elif is_url(args.url_or_tarball):
107
app.download_url(args.url_or_tarball, args.destination)
108
else:
109
app.download_tarball(args.url_or_tarball, args.destination, args.allow_upstream)
110
111
112
def format_error(message):
113
stars = '*' * 72 + '\n'
114
sys.stderr.write(stars)
115
try:
116
import traceback
117
traceback.print_exc(file=sys.stderr)
118
sys.stderr.write(stars)
119
except BaseException:
120
pass
121
sys.stderr.write(message)
122
sys.stderr.write(stars)
123
124
125
def run_safe():
126
try:
127
run()
128
except Exception as error:
129
try:
130
format_error(error)
131
finally:
132
sys.exit(1)
133
134
135
if __name__ == '__main__':
136
run_safe()
137
138