2021-04-01 15:05:27 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import argparse
|
2023-08-18 12:00:30 +02:00
|
|
|
import os
|
2021-04-01 15:05:27 +02:00
|
|
|
import shutil
|
|
|
|
import sys
|
|
|
|
|
|
|
|
ap = argparse.ArgumentParser()
|
|
|
|
ap.add_argument("source")
|
|
|
|
ap.add_argument("destination")
|
2023-08-18 13:16:06 +02:00
|
|
|
ap.add_argument("--mode", "-m", type=lambda x: int(x, base=8))
|
2021-04-01 15:05:27 +02:00
|
|
|
|
|
|
|
args = ap.parse_args()
|
|
|
|
|
|
|
|
with open(args.source) as inf:
|
2023-08-18 12:00:30 +02:00
|
|
|
tmp_dest = args.destination + "." + str(os.getpid())
|
|
|
|
with open(tmp_dest, "w") as outf:
|
2021-04-01 15:05:27 +02:00
|
|
|
first_line = inf.readline()
|
|
|
|
if first_line.startswith("#!"):
|
|
|
|
print("#!", sys.executable, file=outf)
|
|
|
|
else:
|
|
|
|
print("#!", sys.executable, file=outf)
|
|
|
|
outf.write(first_line)
|
|
|
|
for line in inf:
|
|
|
|
outf.write(line)
|
2023-08-18 13:16:06 +02:00
|
|
|
if args.mode is not None:
|
|
|
|
os.chmod(tmp_dest, args.mode)
|
|
|
|
else:
|
|
|
|
shutil.copymode(args.source, tmp_dest)
|
2023-08-18 12:00:30 +02:00
|
|
|
os.rename(tmp_dest, args.destination)
|