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