-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstall_scripts.py
140 lines (130 loc) · 5.5 KB
/
install_scripts.py
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
"""
Installing shortcut scripts
"""
# Note -- this was incorrectly deprecated by sage in https://github.com/sagemath/sage/pull/37856
import os
def install_scripts(directory=None, ignore_existing=False):
r"""
Running ``install_scripts(directory)`` creates scripts in the
given directory that run various software components included with
Sage. Each of these scripts essentially just runs ``sage --CMD``
where ``CMD`` is also the name of the script:
- 'gap' runs GAP
- 'gp' runs the PARI/GP interpreter
- 'ipython' runs IPython
- 'maxima' runs Maxima
- 'mwrank' runs mwrank
- 'R' runs R
- 'singular' runs Singular
- 'sqlite3' runs SQLite version 3
This command:
- verbosely tells you which scripts it adds, and
- will *not* overwrite any scripts you already have in the given
directory.
INPUT:
- ``directory`` - string; the directory into which to put the
scripts. This directory must exist and the user must have write
and execute permissions.
- ``ignore_existing`` - bool (optional, default False): if True,
install script even if another version of the program is in your
path.
OUTPUT: Verbosely prints what it is doing and creates files in
``directory`` that are world executable and readable.
.. note::
You may need to run ``sage`` as ``root`` in order to run
``install_scripts`` successfully, since the user running
``sage`` needs write permissions on ``directory``. Note
that one good candidate for ``directory`` is
``'/usr/local/bin'``, so from the shell prompt, you could run ::
sudo sage -c "install_scripts('/usr/local/bin')"
.. note::
Running ``install_scripts(directory)`` will be most helpful if
``directory`` is in your path.
AUTHORS:
- William Stein: code / design
- Arthur Gaer: design
- John Palmieri: revision, 2011-07 (:issue:`11602`)
EXAMPLES::
sage: import tempfile
sage: from sage.misc.dist import install_scripts
sage: with tempfile.TemporaryDirectory() as d:
....: install_scripts(d, ignore_existing=True)
Checking that Sage has the command 'gap' installed
...
"""
if directory is None:
# We do this since the intended user of install_scripts
# will likely be pretty clueless about how to use Sage or
# its help system.
from . import sagedoc
print(sagedoc.format(install_scripts.__doc__))
print("USAGE: install_scripts('directory')")
return
if not os.path.exists(directory):
print(f"Error: '{directory}' does not exist.")
return
if not os.path.isdir(directory):
print(f"Error: '{directory}' is not a directory.")
return
if not (os.access(directory, os.W_OK) and os.access(directory, os.X_OK)):
print(f"Error: you do not have write permission for '{directory}'.")
return
from sage.misc.sage_ostools import have_program
from sage.env import SAGE_LOCAL
if not SAGE_LOCAL:
print(f"Error: This installation of Sage does not use SAGE_LOCAL, so install_scripts makes no sense.")
return
script_created = False
SAGE_BIN = os.path.join(SAGE_LOCAL, 'bin')
# See if 'directory' is already in PATH, and then remove
# SAGE_LOCAL/bin from PATH so that we can later check whether
# cmd is available outside of Sage.
PATH = os.environ['PATH'].split(os.pathsep)
PATH = [d for d in PATH if os.path.exists(d)]
dir_in_path = any(os.path.samefile(directory, d) for d in PATH)
PATH = os.pathsep.join(d for d in PATH
if not os.path.samefile(d, SAGE_BIN))
for cmd in ['gap', 'gp', 'ipython', 'maxima',
'mwrank', 'R', 'singular', 'sqlite3']:
print(f"Checking that Sage has the command '{cmd}' installed")
# Check to see if Sage includes cmd.
cmd_inside_sage = have_program(cmd, path=SAGE_BIN)
if not cmd_inside_sage:
print(f"The command '{cmd}' is not available as part of Sage; " +
"not creating script.")
print()
continue
cmd_outside_sage = have_program(cmd, path=PATH)
if cmd_outside_sage:
print(f"The command '{cmd}' is installed outside of Sage;", end=' ')
if not ignore_existing:
print("not creating script.")
print()
continue
print("trying to create script anyway...")
else:
print(f"Creating script for '{cmd}'...")
# Install shortcut.
target = os.path.join(directory, cmd)
if os.path.exists(target):
print(f"The file '{target}' already exists; not adding script.")
else:
with open(target, 'w') as o:
o.write('#!/bin/sh\n')
o.write('exec sage --%s "$@"\n' % cmd)
print(f"Created script '{target}'")
os.system(f'chmod a+rx {target}')
script_created = True
print()
if script_created:
print("Finished creating scripts.")
print()
print("You need not do this again even if you upgrade or move Sage.")
print("The only requirement is that your PATH contains both")
print("'{}' and the directory containing the command 'sage'.".format(directory))
if not dir_in_path:
print()
print("Warning: '{}' is not currently in your PATH.".format(directory))
print()
else:
print("No scripts created.")