Skip to content

Instantly share code, notes, and snippets.

@bsidhom
Created June 1, 2018 23:21
Show Gist options
  • Save bsidhom/9ab78a7c4db838460b83341f8e8965d2 to your computer and use it in GitHub Desktop.
Save bsidhom/9ab78a7c4db838460b83341f8e8965d2 to your computer and use it in GitHub Desktop.
Package an executable binary file into a python script
#!/usr/bin/env bash
set -Eeuo pipefail
function main() {
local input_file="$1"
cat <<EOF
from __future__ import print_function
import bz2
import os
import sys
data = (
$(bzip2 <"$input_file" | base64 | sed "s/\(.*\)/ '\1'/")
)
def main():
with open('main', 'wb') as file:
file.write(bz2.decompress(data.decode('base64')))
os.chmod('main', 0o755)
os.execv('./main', ['main'] + sys.argv[1:])
if __name__ == '__main__':
main()
EOF
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment