CoCalc Shared Fileswww / calc / calc / sage.pyOpen in CoCalc with one click!
Author: William A. Stein
1
"""
2
sage.py -- compute things using call to chroot SAGE
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
#
10
# This code is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
# General Public License for more details.
14
#
15
# The full text of the GPL is available at:
16
#
17
# http://www.gnu.org/licenses/
18
#*****************************************************************************
19
20
sage_root = "/home/sage/home/sage"
21
max_lines = 10000
22
SAGE = "ulimit -t 20; sudo sage-chroot"
23
24
import os # operating system calls
25
26
def sage(cmd):
27
cmd = '%colors NoColor\nprint "__SAGESTART__"\n' + cmd
28
p = os.popen3(SAGE)
29
p[0].write(cmd + '\n'*4)
30
p[0].close()
31
out = [p[1].readline(), p[1].readline()]
32
p[1].readline(); p[1].readline()
33
p[1].readline(); p[1].readline()
34
for L in p[1].xreadlines():
35
if L.rstrip().lstrip() != "sage:":
36
out.append(L)
37
if len(out) > max_lines:
38
out.append("(output truncated)")
39
break
40
out = "".join(out).replace("sage: ","")
41
i = out.find('__SAGESTART__')
42
out = out[i+len('__SAGESTART__'):]
43
err = p[2].read()
44
if len(err.split()) > 0:
45
out = "Errors: %s\n%s"%(err, out)
46
return out
47