# HG changeset patch
# User Jeroen Demeyer <jdemeyer@cage.ugent.be>
# Date 1347623711 -7200
# Node ID b044bcfdc01e57f639cbae4a68e71c179db6391b
# Parent 207c916c20bb3e6c5bbd51d3c0965b0539a16986
Move sage-make_relative to sage-location
diff --git a/sage-location b/sage-location
a
|
b
|
|
6 | 6 | SAGE_LOCAL = os.environ['SAGE_LOCAL'] |
7 | 7 | |
8 | 8 | location_file = os.path.join(SAGE_LOCAL, 'lib', 'sage-current-location.txt') |
| 9 | force_file = os.path.join(SAGE_LOCAL, 'lib', 'sage-force-relocate.txt') |
9 | 10 | flags_file = os.path.join(SAGE_LOCAL, 'lib', 'sage-flags.txt') |
10 | 11 | |
11 | 12 | |
… |
… |
|
181 | 182 | f.write(definition + config) |
182 | 183 | |
183 | 184 | |
| 185 | def make_scripts_relative(): |
| 186 | """ |
| 187 | For all interpreter scripts in ``$SAGE_LOCAL/bin`` running python, |
| 188 | replace first line by "#!/usr/bin/env python" |
| 189 | """ |
| 190 | os.chdir(os.path.join(SAGE_LOCAL, 'bin')) |
| 191 | for filename in os.listdir('.'): |
| 192 | # Only ordinary files |
| 193 | if not os.path.isfile(filename): |
| 194 | continue |
| 195 | |
| 196 | try: |
| 197 | with open(filename, 'r+') as f: |
| 198 | # Read at most 512 bytes, this should be more than enough. |
| 199 | # If we don't find '\n' in the first 512 bytes, we most likely |
| 200 | # have a binary file. |
| 201 | L = f.readline(512) |
| 202 | # Make sure we read a complete line |
| 203 | if len(L) < 1 or L[-1] != '\n': |
| 204 | continue |
| 205 | # Is the first line "#!.../python"? |
| 206 | if L.startswith("#!") and L.find("/python") >= 0: |
| 207 | # Read the rest of the file |
| 208 | script = f.read() |
| 209 | |
| 210 | # Write the file again with a proper interpreter line |
| 211 | f.seek(0) |
| 212 | f.truncate() |
| 213 | f.write("#!/usr/bin/env python\n" + script) |
| 214 | except IOError: |
| 215 | pass |
| 216 | |
| 217 | |
184 | 218 | def remove_files(path, remove_extensions): |
185 | 219 | """ |
186 | 220 | Walk the tree starting at ``path``, and remove all files with |
… |
… |
|
210 | 244 | check_processor_flags() |
211 | 245 | update_library_files() |
212 | 246 | update_pkgconfig_files() |
| 247 | make_scripts_relative() |
213 | 248 | |
214 | 249 | # Compiled python files need to be regenerated, so we remove them: |
215 | 250 | remove_files(os.path.join(SAGE_LOCAL, 'lib', 'python'), |
… |
… |
|
226 | 261 | # OLD_SAGE_ROOT is None if this is a first-time install. |
227 | 262 | OLD_SAGE_ROOT = read_location_file() |
228 | 263 | |
229 | | if OLD_SAGE_ROOT != SAGE_ROOT: |
| 264 | # If the force file exists (test that by deleting it), |
| 265 | # then always run sage_locate(). |
| 266 | try: |
| 267 | os.unlink(force_file) |
| 268 | force_relocate = True |
| 269 | except OSError: |
| 270 | force_relocate = False |
| 271 | |
| 272 | if OLD_SAGE_ROOT != SAGE_ROOT or force_relocate: |
230 | 273 | if OLD_SAGE_ROOT is None: |
231 | 274 | print "This looks like the first time you are running Sage." |
| 275 | elif force_relocate: |
| 276 | print "Forcing sage-location, probably because a new package was installed." |
232 | 277 | else: |
233 | 278 | print "The Sage installation tree has moved" |
234 | 279 | print "from %s" % OLD_SAGE_ROOT |
diff --git a/sage-make_relative b/sage-make_relative
deleted file mode 100755
+
|
-
|
|
1 | | #!/usr/bin/env python |
2 | | |
3 | | import os |
4 | | |
5 | | # For all interpreter scripts running python, |
6 | | # replace first line by "#!/usr/bin/env python" |
7 | | for filename in os.listdir('.'): |
8 | | # Only ordinary files |
9 | | if not os.path.isfile(filename): |
10 | | continue |
11 | | |
12 | | try: |
13 | | with open(filename, 'r+') as F: |
14 | | # Read at most 512 bytes, this should be more than enough. |
15 | | # If we don't find '\n' in the first 512 bytes, we most likely |
16 | | # have a binary file. |
17 | | L = F.readline(512) |
18 | | # Make sure we read a complete line |
19 | | if len(L) < 1 or L[-1] != '\n': |
20 | | continue |
21 | | # Is the first line "#!.../python"? |
22 | | if L.startswith("#!") and L.find("/python") >= 0: |
23 | | # Read the rest of the file |
24 | | script = F.read() |
25 | | |
26 | | # Write the file again with a proper interpreter line |
27 | | print "Making %s script relocatable"%filename |
28 | | F.seek(0) |
29 | | F.write("#!/usr/bin/env python\n") |
30 | | F.write(script) |
31 | | # Truncate everything which follows in this file |
32 | | F.truncate() |
33 | | except IOError: |
34 | | pass |