Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
3-manifolds
GitHub Repository: 3-manifolds/Sage_macOS
Path: blob/main/Sage_framework/fix_scripts.py
170 views
1
import sys
2
import os
3
4
class ScriptFile:
5
def __init__(self, fullpath):
6
self.fullpath = fullpath
7
8
def fix_shebang(self, shebang):
9
nodes = shebang.split(os.path.sep)
10
m = nodes.index('Versions')
11
sage_version = nodes[m+1]
12
tail = os.path.join(*nodes[m+2:])
13
return '#!/var/tmp/sage-%s-current/'%sage_version + tail
14
15
def fix(self):
16
with open(self.fullpath, 'r') as infile:
17
shebang = infile.readline()
18
rest = infile.read()
19
new_shebang = self.fix_shebang(shebang)
20
with open(self.fullpath, 'w') as outfile:
21
outfile.write(new_shebang + '\n')
22
outfile.write(rest)
23
24
def shebang_check(path):
25
if os.path.islink(path):
26
return False
27
with open(path, 'rb') as inputfile:
28
first = inputfile.read(2)
29
return first == b'#!'
30
31
def fix_scripts(directory):
32
for dirpath, dirnames, filenames in os.walk(directory):
33
for filename in filenames:
34
fullpath = os.path.join(dirpath, filename)
35
if shebang_check(fullpath):
36
ScriptFile(fullpath).fix()
37
38
if __name__ == '__main__':
39
try:
40
directory = sys.argv[1]
41
except IndexError:
42
print('Usage python3 fixscripts.py <directory>')
43
fix_scripts(directory)
44
45