-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfigure.py
executable file
·32 lines (27 loc) · 1.31 KB
/
Configure.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
import os, sys, subprocess
def add_interpreter_path(directory, interpreter_path, i_language):
if i_language == "python":
ending = ".py"
elif i_language == "perl":
ending = ".pl"
for r, d, files in os.walk(directory):
if len(files) != 0:
for filename in files:
if filename.endswith(ending):
filepath = os.path.join(r, filename)
with open(filepath, 'r+') as f:
lines = f.readlines()
if not lines[0].startswith('#!'):
lines.insert(0, f'#!{interpreter_path}') # Default Python interpreter
f.seek(0)
f.writelines(lines)
#print(f'Added shebang line to {filename}')
else:
#print(lines[0], [interpreter_path])
pass
# Example usage:
directory = sys.argv[1]
python_interpreter_path = subprocess.run("which python", shell=True, capture_output=True, text=True).stdout
add_interpreter_path(directory, python_interpreter_path, "python")
perl_interpreter_path = subprocess.run("which perl", shell=True, capture_output=True, text=True).stdout
add_interpreter_path(directory, perl_interpreter_path, "perl")