Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagesmc
Path: blob/master/src/sage/env.py
8815 views
1
"""
2
Sage Runtime Environment
3
4
AUTHORS:
5
6
- \R. Andrew Ohana (2012): Initial version.
7
8
"""
9
10
########################################################################
11
# Copyright (C) 2013 R. Andrew Ohana <[email protected]>
12
#
13
# Distributed under the terms of the GNU General Public License (GPL)
14
# as published by the Free Software Foundation; either version 2 of
15
# the License, or (at your option) any later version.
16
#
17
# http://www.gnu.org/licenses/
18
########################################################################
19
20
import os, socket
21
import version
22
23
opj = os.path.join
24
25
# set default values for sage environment variables
26
# every variable can be overwritten by os.environ
27
SAGE_ENV = dict()
28
29
# Helper to build the SAGE_ENV dictionary
30
def _add_variable_or_fallback(key, fallback, force=False):
31
"""
32
Set ``SAGE_ENV[key]``.
33
34
If ``key`` is an environment variable, this is the
35
value. Otherwise, the ``fallback`` is used.
36
37
INPUT:
38
39
- ``key`` -- string.
40
41
- ``fallback`` -- anything.
42
43
- ``force`` -- boolean (optional, default is ``False``). Whether
44
to always use the fallback, regardless of environment variables.
45
46
EXAMPLES::
47
48
sage: import os, sage.env
49
sage: sage.env.SAGE_ENV = dict()
50
sage: os.environ['SAGE_FOO'] = 'foo'
51
sage: sage.env._add_variable_or_fallback('SAGE_FOO', '---$SAGE_URL---')
52
sage: sage.env.SAGE_FOO
53
'foo'
54
sage: sage.env.SAGE_ENV['SAGE_FOO']
55
'foo'
56
57
If the environment variable does not exist, the fallback is
58
used. Previously-declared variables are replaced if they are
59
prefixed with a dollar sign::
60
61
sage: _ = os.environ.pop('SAGE_BAR', None) # ensure that SAGE_BAR does not exist
62
sage: sage.env._add_variable_or_fallback('SAGE_BAR', '---$SAGE_FOO---')
63
sage: sage.env.SAGE_BAR
64
'---foo---'
65
sage: sage.env.SAGE_ENV['SAGE_BAR']
66
'---foo---'
67
"""
68
global SAGE_ENV
69
try:
70
import os
71
value = os.environ[key]
72
except KeyError:
73
value = fallback
74
if force:
75
value = fallback
76
for k,v in SAGE_ENV.iteritems():
77
if isinstance(k, basestring):
78
value = value.replace('$'+k, v)
79
SAGE_ENV[key] = value
80
globals()[key] = value
81
82
# system info
83
_add_variable_or_fallback('UNAME', os.uname()[0])
84
_add_variable_or_fallback('HOSTNAME', socket.gethostname())
85
_add_variable_or_fallback('LOCAL_IDENTIFIER','$HOSTNAME.%s'%os.getpid())
86
87
# bunch of sage directories and files
88
_add_variable_or_fallback('SAGE_ROOT', None)
89
_add_variable_or_fallback('SAGE_LOCAL', opj('$SAGE_ROOT', 'local'))
90
_add_variable_or_fallback('SAGE_ETC', opj('$SAGE_LOCAL', 'etc'))
91
_add_variable_or_fallback('SAGE_SHARE', opj('$SAGE_LOCAL', 'share'))
92
93
# SAGE_LIB is the site-packages directory if the sage library
94
# has been installed, otherwise it is the same of SAGE_SRC
95
_add_variable_or_fallback('SAGE_SRC', opj('$SAGE_ROOT', 'src'))
96
_add_variable_or_fallback('SAGE_LIB', os.path.dirname(os.path.dirname(__file__)))
97
98
_add_variable_or_fallback('SAGE_EXTCODE', opj('$SAGE_SHARE', 'sage', 'ext'))
99
_add_variable_or_fallback('SAGE_LOGS', opj('$SAGE_ROOT', 'logs', 'pkgs'))
100
_add_variable_or_fallback('SAGE_SPKG_INST', opj('$SAGE_LOCAL', 'var', 'lib', 'sage', 'installed'))
101
_add_variable_or_fallback('SAGE_DOC', opj('$SAGE_SRC', 'doc'))
102
_add_variable_or_fallback('DOT_SAGE', opj(os.environ.get('HOME','$SAGE_ROOT'), '.sage'))
103
_add_variable_or_fallback('SAGE_DOT_GIT', opj('$SAGE_ROOT', '.git'))
104
105
# misc
106
_add_variable_or_fallback('SAGE_URL', 'http://sage.math.washington.edu/sage/')
107
_add_variable_or_fallback('REALM', 'sage.math.washington.edu')
108
_add_variable_or_fallback('TRAC_SERVER_URI', 'https://trac.sagemath.org')
109
_add_variable_or_fallback('SAGE_REPO_AUTHENTICATED', 'ssh://[email protected]:2222/sage.git')
110
_add_variable_or_fallback('SAGE_REPO_ANONYMOUS', 'git://trac.sagemath.org/sage.git')
111
_add_variable_or_fallback('SAGE_VERSION', version.version)
112
_add_variable_or_fallback('SAGE_DATE', version.date)
113
114
# post process
115
if ' ' in DOT_SAGE:
116
if UNAME[:6] == 'CYGWIN':
117
# on windows/cygwin it is typical for the home directory
118
# to have a space in it. Fortunately, users also have
119
# write privileges to c:\cygwin\home, so we just put
120
# .sage there.
121
_add_variable_or_fallback('DOT_SAGE', "/home/.sage", force=True)
122
else:
123
print("Your home directory has a space in it. This")
124
print("will probably break some functionality of Sage. E.g.,")
125
print("the GAP interface will not work. A workaround")
126
print("is to set the environment variable HOME to a")
127
print("directory with no spaces that you have write")
128
print("permissions to before you start sage.")
129
130
# delete temporary variables used for setting up sage.env
131
del opj, os, socket, version
132
133