Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/misc/dist.py
4045 views
1
"""
2
Installing shortcut scripts
3
"""
4
5
import os
6
7
def install_scripts(directory=None, ignore_existing=False):
8
r"""
9
Running ``install_scripts(directory)`` creates scripts in the
10
given directory that run various software components included with
11
Sage. Each of these scripts essentially just runs ``sage --CMD``
12
where ``CMD`` is also the name of the script:
13
14
- 'gap' runs GAP
15
- 'gp' runs the PARI/GP interpreter
16
- 'hg' runs Mercurial
17
- 'ipython' runs IPython
18
- 'maxima' runs Maxima
19
- 'mwrank' runs mwrank
20
- 'R' runs R
21
- 'singular' runs Singular
22
- 'sqlite3' runs SQLite version 3
23
- 'kash' runs Kash if it is installed (Kash is an optional Sage
24
package)
25
- 'M2' runs Macaulay2 if it is installed (Macaulay2 is an
26
experimental Sage package)
27
28
This command:
29
30
- verbosely tells you which scripts it adds, and
31
32
- will *not* overwrite any scripts you already have in the given
33
directory.
34
35
INPUT:
36
37
- ``directory`` - string; the directory into which to put the
38
scripts. This directory must exist and the user must have write
39
and execute permissions.
40
41
- ``ignore_existing`` - bool (optional, default False): if True,
42
install script even if another version of the program is in your
43
path.
44
45
OUTPUT: Verbosely prints what it is doing and creates files in
46
``directory`` that are world executable and readable.
47
48
.. note::
49
50
You may need to run ``sage`` as ``root`` in order to run
51
``install_scripts`` successfully, since the user running
52
``sage`` needs write permissions on ``directory``. Note
53
that one good candidate for ``directory`` is
54
``'/usr/local/bin'``, so from the shell prompt, you could run ::
55
56
sudo sage -c "install_scripts('/usr/local/bin')"
57
58
.. note::
59
60
Running ``install_scripts(directory)`` will be most helpful if
61
``directory`` is in your path.
62
63
AUTHORS:
64
65
- William Stein: code / design
66
67
- Arthur Gaer: design
68
69
- John Palmieri: revision, 2011-07 (trac ticket #11602)
70
71
EXAMPLES::
72
73
sage: install_scripts(SAGE_TMP, ignore_existing=True)
74
Checking that Sage has the command 'gap' installed
75
...
76
"""
77
if directory is None:
78
# We do this since the intended user of install_scripts
79
# will likely be pretty clueless about how to use Sage or
80
# its help system.
81
import sagedoc
82
print sagedoc.format(install_scripts.__doc__)
83
print "USAGE: install_scripts('directory')"
84
return
85
86
if not os.path.exists(directory):
87
print "Error: '%s' does not exist." % directory
88
return
89
90
if not os.path.isdir(directory):
91
print "Error: '%s' is not a directory." % directory
92
return
93
94
if not (os.access(directory, os.W_OK) and os.access(directory, os.X_OK)):
95
print "Error: you do not have write permission for '%s'." % directory
96
return
97
98
from sage.misc.sage_ostools import have_program
99
script_created = False
100
SAGE_ROOT = os.environ['SAGE_ROOT']
101
SAGE_BIN = os.path.join(SAGE_ROOT, 'local', 'bin')
102
# See if 'directory' is already in PATH, and then remove
103
# SAGE_ROOT/local/bin from PATH so that we can later check whether
104
# cmd is available outside of Sage.
105
PATH = os.environ['PATH'].split(os.pathsep)
106
PATH = [d for d in PATH if os.path.exists(d)]
107
dir_in_path = any([os.path.samefile(directory, d) for d in PATH])
108
PATH = os.pathsep.join([d for d in PATH if not
109
os.path.samefile(d, SAGE_BIN)])
110
for cmd in ['gap', 'gp', 'hg', 'ipython', 'maxima',
111
'mwrank', 'R', 'singular', 'sqlite3', 'M2', 'kash']:
112
print "Checking that Sage has the command '%s' installed" % cmd
113
# Check to see if Sage includes cmd.
114
cmd_inside_sage = have_program(cmd, path=SAGE_BIN)
115
cmd_outside_sage = have_program(cmd, path=PATH)
116
if not cmd_inside_sage:
117
print ("The command '%s' is not available as part " %cmd
118
+ "of Sage; not creating script.")
119
print
120
continue
121
if cmd_outside_sage:
122
print "The command '%s' is installed outside of Sage;" % cmd,
123
if not ignore_existing:
124
print "not creating script."
125
print
126
continue
127
print "trying to create script anyway..."
128
else:
129
print "Creating script for '%s'..." % cmd
130
# Install shortcut.
131
target = os.path.join(directory, cmd)
132
if os.path.exists(target):
133
print "The file '%s' already exists; not adding script."%(target)
134
else:
135
o = open(target,'w')
136
o.write('#!/bin/sh\n')
137
o.write('exec sage --%s "$@"\n'%cmd)
138
o.close()
139
print "Created script '%s'"%target
140
os.system('chmod a+rx %s'%target)
141
script_created = True
142
print
143
144
if script_created:
145
print "Finished creating scripts."
146
print
147
print "You need not do this again even if you upgrade or move Sage."
148
print "The only requirement is that your PATH contains both"
149
print "'%s' and the directory containing the command 'sage'." % directory
150
if not dir_in_path:
151
print
152
print "Warning: '%s' is not currently in your PATH." % directory
153
print
154
else:
155
print "No scripts created."
156
157