Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagesmc
Path: blob/master/src/sage/misc/banner.py
8814 views
1
# -*- coding: utf-8 -*-
2
r"""
3
Sage version and banner info
4
"""
5
6
#*****************************************************************************
7
# Copyright (C) 2005 William Stein <[email protected]>
8
#
9
# Distributed under the terms of the GNU General Public License (GPL)
10
# as published by the Free Software Foundation; either version 2 of
11
# the License, or (at your option) any later version.
12
# http://www.gnu.org/licenses/
13
#*****************************************************************************
14
15
16
from sage.env import SAGE_VERSION, SAGE_DATE, SAGE_SRC
17
18
def version(clone = False):
19
"""
20
Return the version of Sage.
21
22
INPUT:
23
nothing
24
OUTPUT:
25
str
26
27
EXAMPLES:
28
sage: version()
29
'Sage Version ..., Release Date: ...'
30
sage: version(clone=True)
31
('Sage Version ..., Release Date: ...',
32
'Mercurial clone branch: ...')
33
"""
34
import os
35
branch = os.popen("ls -l "+SAGE_SRC).read().split()[-1][5:]
36
v = 'Sage Version %s, Release Date: %s'%(SAGE_VERSION, SAGE_DATE)
37
if clone:
38
return v,"Mercurial clone branch: %s"%branch
39
return v
40
41
42
def banner_text():
43
"""
44
Text for the Sage banner.
45
46
INPUT:
47
48
None
49
50
OUTPUT:
51
52
A string containing the banner message.
53
54
EXAMPLES::
55
56
sage: print sage.misc.banner.banner_text()
57
┌────────────────────────────────────────────────────────────────────┐
58
│ Sage Version ...
59
"""
60
bars = u"─"*68
61
s = u'┌' + bars + u'┐'
62
s += u"\n│ %-66s │\n" % version()
63
s += u"│ %-66s │\n" % 'Type "notebook()" for the browser-based notebook interface.'
64
s += u"│ %-66s │\n" % 'Type "help()" for help.'
65
#s += u"│ %-66s │\n" % 'Distributed under the GNU General Public License V2.'
66
s += u'└' + bars + u'┘'
67
pre = version_dict()['prerelease']
68
if pre:
69
red_in = '\033[31m'
70
red_out = '\033[0m'
71
bars2 = bars.replace(u'─', u'━')
72
s += '\n'
73
s += red_in + u'┏' + bars2 + u'┓' + '\n'
74
s += u"┃ %-66s ┃\n" % 'Warning: this is a prerelease version, and it may be unstable.'
75
s += u'┗' + bars2 + u'┛' + red_out
76
return s.encode('utf8')
77
78
79
def banner():
80
"""
81
Print the Sage banner.
82
83
INPUT:
84
85
None
86
87
OUTPUT:
88
89
None
90
91
EXAMPLES::
92
93
sage: banner()
94
┌────────────────────────────────────────────────────────────────────┐
95
│ Sage Version ..., Release Date: ...
96
│ Type "notebook()" for the browser-based notebook interface. │
97
│ Type "help()" for help. │
98
...
99
"""
100
print banner_text()
101
102
103
def version_dict():
104
"""
105
A dictionary describing the version of Sage.
106
107
INPUT:
108
nothing
109
OUTPUT:
110
dictionary with keys 'major', 'minor', 'tiny', 'prerelease'
111
112
This process the Sage version string and produces a dictionary.
113
It expects the Sage version to be in one of these forms:
114
N.N
115
N.N.N
116
N.N.N.N
117
N.N.str
118
N.N.N.str
119
N.N.N.N.str
120
where 'N' stands for an integer and 'str' stands for a string.
121
The first integer is stored under the 'major' key and the second
122
integer under 'minor'. If there is one more integer, it is stored
123
under 'tiny'; if there are two more integers, then they are stored
124
together as a float N.N under 'tiny'. If there is a string, then
125
the key 'prerelease' returns True.
126
127
For example, if the Sage version is '3.2.1', then the dictionary
128
is {'major': 3, 'minor': 2, 'tiny': 1, 'prerelease': False}.
129
If the Sage version is '3.2.1.2', then the dictionary is
130
{'major': 3, 'minor': 2, 'tiny': 1.200..., 'prerelease': False}.
131
If the Sage version is '3.2.alpha0', then the dictionary is
132
{'major': 3, 'minor': 2, 'tiny': 0, 'prerelease': True}.
133
134
EXAMPLES:
135
sage: from sage.misc.banner import version_dict
136
sage: print "Sage major version is %s" % version_dict()['major']
137
Sage major version is ...
138
sage: version_dict()['major'] == int(sage.version.version.split('.')[0])
139
True
140
"""
141
v = SAGE_VERSION.split('.')
142
dict = {}
143
dict['major'] = int(v[0])
144
dict['minor'] = int(v[1])
145
dict['tiny'] = 0
146
dict['prerelease'] = False
147
try:
148
dummy = int(v[-1])
149
except ValueError: # when last entry is not an integer
150
dict['prerelease'] = True
151
if (len(v) == 3 and not dict['prerelease']) or len(v) > 3:
152
dict['tiny'] = int(v[2])
153
try:
154
teeny = int(v[3])
155
dict['tiny'] += 0.1 * teeny
156
except (ValueError, IndexError):
157
pass
158
return dict
159
160
def require_version(major, minor=0, tiny=0, prerelease=False,
161
print_message=False):
162
"""
163
True if Sage version is at least major.minor.tiny.
164
165
INPUT:
166
major -- integer
167
minor -- integer (optional, default = 0)
168
tiny -- float (optional, default = 0)
169
prerelease -- boolean (optional, default = False)
170
print_message -- boolean (optional, default = False)
171
172
OUTPUT:
173
True if major.minor.tiny is <= version of Sage, False otherwise
174
175
For example, if the Sage version number is 3.1.2, then
176
require_version(3, 1, 3) will return False, while
177
require_version(3, 1, 2) will return True.
178
If the Sage version is 3.1.2.alpha0, then
179
require_version(3, 1, 1) will return True, while, by default,
180
require_version(3, 1, 2) will return False. Note, though, that
181
require_version(3, 1, 2, prerelease=True) will return True:
182
if the optional argument prerelease is True, then a prerelease
183
version of Sage counts as if it were the released version.
184
185
If optional argument print_message is True and this function
186
is returning False, print a warning message.
187
188
EXAMPLES:
189
sage: from sage.misc.banner import require_version
190
sage: require_version(2, 1, 3)
191
True
192
sage: require_version(821, 4)
193
False
194
sage: require_version(821, 4, print_message=True)
195
This code requires at least version 821.4 of Sage to run correctly.
196
You are running version ...
197
False
198
"""
199
vers = version_dict()
200
prerelease_checked = (prerelease if vers['prerelease'] else True)
201
if (vers['major'] > major
202
or (vers['major'] == major and vers['minor'] > minor)
203
or (vers['major'] == major and vers['minor'] == minor
204
and vers['tiny'] > tiny)
205
or (vers['major'] == major and vers['minor'] == minor
206
and vers['tiny'] == tiny and prerelease_checked)):
207
return True
208
else:
209
if print_message:
210
print "This code requires at least version",
211
print "%g" % (major + 0.1 * minor + 0.01 * tiny,),
212
print "of Sage to run correctly."
213
print "You are running version %s." % SAGE_VERSION
214
return False
215
216