#!/usr/bin/env python3 import argparse import os import shutil import sys ap = argparse.ArgumentParser() ap.add_argument("source") ap.add_argument("destination") ap.add_argument("--mode", "-m", type=lambda x: int(x, base=8)) args = ap.parse_args() with open(args.source) as inf: tmp_dest = args.destination + "." + str(os.getpid()) with open(tmp_dest, "w") as outf: 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) if args.mode is not None: os.chmod(tmp_dest, args.mode) else: shutil.copymode(args.source, tmp_dest) os.rename(tmp_dest, args.destination)