Skip to content

Instantly share code, notes, and snippets.

@spiccinini
Last active September 22, 2020 00:13
Show Gist options
  • Save spiccinini/c98129c5c3d55b291c01da63977ff3dc to your computer and use it in GitHub Desktop.
Save spiccinini/c98129c5c3d55b291c01da63977ff3dc to your computer and use it in GitHub Desktop.
Simple bash shell installer creator. Embeds a directory with files and a run.sh script as a tar.gz inside a shell script that extracts itself and executes run.sh
import argparse
import os
import subprocess
import tempfile
def create(indir, outfile):
script = """set -x
tmpdir=$(mktemp -d)
# unpack
tail -cTAR_SIZE "$0" | tar xvfz - -C ${tmpdir}
test -e "${tmpdir}/run.sh" && cd ${tmpdir} && sh run.sh
exit
"""
with tempfile.TemporaryDirectory() as tmpdirname:
tmp_tar_name = os.path.join(tmpdirname, "data.tar.gz")
subprocess.run(["tar", "cfz", tmp_tar_name, "-C", indir, "."])
with open(tmp_tar_name, "rb") as tar:
tar_data = tar.read()
with open(outfile, "wb") as outfile:
script = script.replace("TAR_SIZE", str(len(tar_data)))
outfile.write(script.encode("ascii"))
outfile.write(tar_data)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="""
Creates a shell installer that embeds a tar file from an input directory.
Place a run.sh script in the input directory and it will be executed by the installer
script.
""")
parser.add_argument('inputdir', help='Input directory.')
parser.add_argument('outfile', help='Output installer script (eg: installer.sh).')
args = parser.parse_args()
create(args.inputdir, args.outfile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment