/usr/lib/python2.7/dist-packages/sage/env.py is in sagemath-common 7.4-9.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | """
Sage Runtime Environment
AUTHORS:
- \R. Andrew Ohana (2012): Initial version.
"""
########################################################################
# Copyright (C) 2013 R. Andrew Ohana <andrew.ohana@gmail.com>
#
# Distributed under the terms of the GNU General Public License (GPL)
# as published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
#
# http://www.gnu.org/licenses/
########################################################################
from __future__ import absolute_import
import os
import socket
import site
from . import version
opj = os.path.join
# set default values for sage environment variables
# every variable can be overwritten by os.environ
SAGE_ENV = dict()
# Helper to build the SAGE_ENV dictionary
def _add_variable_or_fallback(key, fallback, force=False):
"""
Set ``SAGE_ENV[key]``.
If ``key`` is an environment variable, this is the
value. Otherwise, the ``fallback`` is used.
INPUT:
- ``key`` -- string.
- ``fallback`` -- anything.
- ``force`` -- boolean (optional, default is ``False``). Whether
to always use the fallback, regardless of environment variables.
EXAMPLES::
sage: import os, sage.env
sage: sage.env.SAGE_ENV = dict()
sage: os.environ['SAGE_FOO'] = 'foo'
sage: sage.env._add_variable_or_fallback('SAGE_FOO', '---$SAGE_URL---')
sage: sage.env.SAGE_FOO
'foo'
sage: sage.env.SAGE_ENV['SAGE_FOO']
'foo'
If the environment variable does not exist, the fallback is
used. Previously-declared variables are replaced if they are
prefixed with a dollar sign::
sage: _ = os.environ.pop('SAGE_BAR', None) # ensure that SAGE_BAR does not exist
sage: sage.env._add_variable_or_fallback('SAGE_BAR', '---$SAGE_FOO---')
sage: sage.env.SAGE_BAR
'---foo---'
sage: sage.env.SAGE_ENV['SAGE_BAR']
'---foo---'
"""
global SAGE_ENV
import six
try:
import os
value = os.environ[key]
except KeyError:
value = fallback
if force:
value = fallback
if isinstance(value, six.string_types):
for k,v in sorted(SAGE_ENV.items(), reverse=True):
if isinstance(v, six.string_types):
value = value.replace('$'+k, v)
SAGE_ENV[key] = value
globals()[key] = value
# system info
_add_variable_or_fallback('UNAME', os.uname()[0])
_add_variable_or_fallback('HOSTNAME', socket.gethostname())
_add_variable_or_fallback('LOCAL_IDENTIFIER','$HOSTNAME.%s'%os.getpid())
# bunch of sage directories and files
_add_variable_or_fallback('SAGE_ROOT', None)
_add_variable_or_fallback('SAGE_LOCAL', None)
_add_variable_or_fallback('SAGE_ETC', opj('$SAGE_LOCAL', 'etc'))
_add_variable_or_fallback('SAGE_SCRIPTS_DIR',opj('$SAGE_LOCAL', 'bin'))
_add_variable_or_fallback('SAGE_INC', opj('$SAGE_LOCAL', 'include'))
_add_variable_or_fallback('SAGE_SHARE', opj('$SAGE_LOCAL', 'share'))
_add_variable_or_fallback('SAGE_SRC', opj('$SAGE_ROOT', 'src'))
try:
sitepackages_dirs = site.getsitepackages()
except AttributeError: # in case of use inside virtualenv
sitepackages_dirs = [os.path.join(os.path.dirname(site.__file__),
'site-packages')]
_add_variable_or_fallback('SITE_PACKAGES', [d for d in sitepackages_dirs if "local" not in d])
_add_variable_or_fallback('SAGE_LIB', opj('$SAGE_LOCAL', 'lib', 'python2.7', 'dist-packages'))
_add_variable_or_fallback('SAGE_CYTHONIZED', opj('$SAGE_ROOT', 'src', 'build', 'cythonized'))
# Used by sage/misc/package.py. Should be SAGE_SRC_ROOT in VPATH.
_add_variable_or_fallback('SAGE_PKGS', opj('$SAGE_ROOT', 'build', 'pkgs'))
_add_variable_or_fallback('SAGE_EXTCODE', opj('$SAGE_SHARE', 'sage', 'ext'))
_add_variable_or_fallback('SAGE_LOGS', opj('$SAGE_ROOT', 'logs', 'pkgs'))
_add_variable_or_fallback('SAGE_SPKG_INST', opj('$SAGE_ROOT', 'local', 'var', 'lib', 'sage', 'installed'))
_add_variable_or_fallback('SAGE_DOC_SRC', opj('$SAGE_SRC', 'doc'))
_add_variable_or_fallback('SAGE_DOC', opj('$SAGE_SHARE', 'doc', 'sage'))
_add_variable_or_fallback('DOT_SAGE', opj(os.environ.get('HOME','$SAGE_ROOT'), '.sage'))
_add_variable_or_fallback('SAGE_DOT_GIT', opj('$SAGE_ROOT', '.git'))
_add_variable_or_fallback('SAGE_DISTFILES', opj('$SAGE_ROOT', 'upstream'))
# misc
_add_variable_or_fallback('SAGE_URL', 'http://sage.math.washington.edu/sage/')
_add_variable_or_fallback('REALM', 'sage.math.washington.edu')
_add_variable_or_fallback('TRAC_SERVER_URI', 'https://trac.sagemath.org')
_add_variable_or_fallback('SAGE_REPO_AUTHENTICATED', 'ssh://git@trac.sagemath.org:2222/sage.git')
_add_variable_or_fallback('SAGE_REPO_ANONYMOUS', 'git://trac.sagemath.org/sage.git')
_add_variable_or_fallback('SAGE_VERSION', version.version)
_add_variable_or_fallback('SAGE_DATE', version.date)
_add_variable_or_fallback('SAGE_BANNER', '')
_add_variable_or_fallback('SAGE_IMPORTALL', 'yes')
# post process
if ' ' in DOT_SAGE:
if UNAME[:6] == 'CYGWIN':
# on windows/cygwin it is typical for the home directory
# to have a space in it. Fortunately, users also have
# write privileges to c:\cygwin\home, so we just put
# .sage there.
_add_variable_or_fallback('DOT_SAGE', "/home/.sage", force=True)
else:
print("Your home directory has a space in it. This")
print("will probably break some functionality of Sage. E.g.,")
print("the GAP interface will not work. A workaround")
print("is to set the environment variable HOME to a")
print("directory with no spaces that you have write")
print("permissions to before you start sage.")
CYGWIN_VERSION = None
if UNAME[:6] == 'CYGWIN':
import re
_uname = os.uname()
if len(_uname) >= 2:
m = re.match(r'(\d+\.\d+\.\d+)\(.+\)', _uname[2])
if m:
CYGWIN_VERSION = tuple(map(int, m.group(1).split('.')))
del m
del _uname, re
# things that need DOT_SAGE
_add_variable_or_fallback('PYTHON_EGG_CACHE', opj('$DOT_SAGE', '.python-eggs'))
_add_variable_or_fallback('SAGE_STARTUP_FILE', opj('$DOT_SAGE', 'init.sage'))
# delete temporary variables used for setting up sage.env
del opj, os, socket, version, site
def sage_include_directories(use_sources=False):
"""
Return the list of include directories for compiling Sage extension modules.
INPUT:
- ``use_sources`` -- (default: False) a boolean
OUTPUT:
a list of include directories to be used to compile sage code
1. while building sage (use_sources='True')
2. while using sage (use_sources='False')
EXAMPLES:
Expected output while using sage
::
sage: import sage.env
sage: sage.env.sage_include_directories()
['.../include',
'.../include/python...',
'.../python.../numpy/core/include',
'.../python.../dist-packages',
'.../python.../dist-packages/sage/ext']
"""
import os, numpy
import distutils.sysconfig
opj = os.path.join
include_directories = [SAGE_INC,
distutils.sysconfig.get_python_inc(),
numpy.get_include()]
if use_sources :
include_directories.extend([SAGE_SRC,
opj(SAGE_SRC, 'sage', 'ext')])
include_directories.extend([SAGE_CYTHONIZED,
opj(SAGE_CYTHONIZED, 'sage', 'ext')])
else:
include_directories.extend([SAGE_LIB,
opj(SAGE_LIB, 'sage', 'ext')])
return include_directories
|