Created
January 2, 2013 20:23
-
-
Save thepaul/4437650 to your computer and use it in GitHub Desktop.
make byte-compiled python code into an executable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# | |
# runnable-pyc | |
usage_info = \ | |
"""runnable-pyc - make compiled Python code into an executable file | |
Usage: runnable-pyc <pycfile> [<outputfile>] | |
If no outputfile is specified, and pycfile ends in '.pyc', then the default | |
outputfile is the same as pycfile with the '.pyc' chopped off. This program | |
will not overwrite an existing file, though. | |
The outputfile will be made mode 0755. This is not yet overrideable. | |
Note that the output file is just the input file with some special shell | |
code prepended. It will still require Python (and sh). | |
""" | |
def usage(): | |
sys.stderr.write(usage_info) | |
import sys, os | |
shellcode = \ | |
"""#!/bin/sh | |
# created by runnable-pyc | |
exec %s - "$0" <<'EOShell' | |
import sys, marshal | |
f = file(sys.argv[1], 'rb') | |
f.seek(%%-4s) | |
exec marshal.load(f) | |
EOShell | |
""" % sys.executable | |
shellcode %= (len(shellcode) + 8) | |
def make_runnable(fname, outfname): | |
outf = file(outfname, 'wb') | |
outf.write(shellcode) | |
outf.write(file(fname, 'rb').read()) | |
outf.close() | |
os.chmod(outfname, 0755) | |
def main(): | |
if len(sys.argv) < 2 or len(sys.argv) > 3: | |
usage() | |
sys.exit(1) | |
if len(sys.argv) == 3: | |
outfname = sys.argv[2] | |
elif sys.argv[1].endswith('.pyc'): | |
outfname = sys.argv[1][:-4] | |
else: | |
sys.stderr.write("Don't know what to call output file.\n") | |
sys.exit(2) | |
if os.path.exists(outfname): | |
sys.stderr.write("Output file already exists.\n") | |
sys.exit(3) | |
make_runnable(sys.argv[1], outfname) | |
if __name__ == "__main__": | |
main() |
This code will only work with Python 2, which is quite dead. I’m not sure how much change it would require to work in Python 3.
But i compiled python file
…On Wed, 23 Mar 2022, 1:13 am paul cannon, ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
This code will only work with Python 2, which is quite dead. I’m not sure
how much change it would require to work in Python 3.
—
Reply to this email directly, view it on GitHub
<https://gist.github.com/4437650#gistcomment-4106903>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AQHY5IBY5Z5CPKH342BWMK3VBJAX5ANCNFSM5RLT4KWQ>
.
You are receiving this because you commented.Message ID:
***@***.***>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When i compiledwith this file than i get like this error
