diff --git a/.gitignore b/.gitignore index 0d20b64..1f5b04e 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ *.pyc +dist/ +pysfx.egg-info/ diff --git a/pysfx b/pysfx deleted file mode 100644 index 3d7635a..0000000 --- a/pysfx +++ /dev/null @@ -1,73 +0,0 @@ -#!/usr/bin/env python - -import zlib, base64, sys, argparse, os -from gzipreader import GzipReader -from b64reader import Base64Reader - -parser = argparse.ArgumentParser(description="Creates an SFX from a specified archive or file.") -parser.add_argument("-a", help="Treat the input file as a tar.gz archive that needs to be extracted upon running the SFX.", action="store_true", dest="is_archive") -parser.add_argument("-s", help="Define a command to be run after extraction of the SFX. %%NAME will be replaced with the path of the extracted file or folder. " - "For archives, the working directory is set to the extraction directory.", action="store", dest="command") -parser.add_argument("input_file", metavar="INPUTFILE", type=str, nargs=1, help="The file to read from. Use a dash (-) to read from STDIN instead.") -parser.add_argument("output_file", metavar="OUTPUTFILE", type=str, nargs=1, help="The file to write to. Use a dash (-) to write to STDOUT instead.") -options = vars(parser.parse_args()) - -if options['input_file'][0] == "-": - infile = sys.stdin - extension = "dat" -else: - infile = open(options['input_file'][0], "rb") - extension = os.path.splitext(options['input_file'][0]) - -if options['output_file'][0] == "-": - outfile = sys.stdout -else: - outfile = open(options['output_file'][0], "wb") - -if options['is_archive'] == True: - is_archive = "True" - extension = "tar.gz" -else: - is_archive = "False" - -if options['command']: - run_after_extract = "True" - command = options['command'] -else: - run_after_extract = "False" - command = "" - -template = open("%s/unpack.template" % os.path.dirname(__file__), "r") - -variables = { - "run_after_extract": run_after_extract, - "targz": is_archive, - "extension": extension, - "command": command -} - -for curline in template: - if curline.startswith('"""EOFDATA'): - # Found the EOF data marker, insert packed data before - # moving on with the next line. - outfile.write(curline) - - data = b"" - reader = Base64Reader(GzipReader(infile)) - chunk_size = 128 - - while True: - chunk = reader.read(chunk_size) - - if chunk == "": - break - - outfile.write(chunk + "\n") - else: - if "{%" in curline: - for variable_key, variable_value in variables.iteritems(): - curline = curline.replace("{%%%s}" % variable_key, variable_value) - - outfile.write(curline) - -outfile.close() diff --git a/pysfx/__init__.py b/pysfx/__init__.py new file mode 100644 index 0000000..b74eb55 --- /dev/null +++ b/pysfx/__init__.py @@ -0,0 +1,72 @@ +import zlib, base64, sys, argparse, os +from gzipreader import GzipReader +from b64reader import Base64Reader + +def main(): + parser = argparse.ArgumentParser(description="Creates an SFX from a specified archive or file.") + parser.add_argument("-a", help="Treat the input file as a tar.gz archive that needs to be extracted upon running the SFX.", action="store_true", dest="is_archive") + parser.add_argument("-s", help="Define a command to be run after extraction of the SFX. %%NAME will be replaced with the path of the extracted file or folder. " + "For archives, the working directory is set to the extraction directory.", action="store", dest="command") + parser.add_argument("input_file", metavar="INPUTFILE", type=str, nargs=1, help="The file to read from. Use a dash (-) to read from STDIN instead.") + parser.add_argument("output_file", metavar="OUTPUTFILE", type=str, nargs=1, help="The file to write to. Use a dash (-) to write to STDOUT instead.") + options = vars(parser.parse_args()) + + if options['input_file'][0] == "-": + infile = sys.stdin + extension = "dat" + else: + infile = open(options['input_file'][0], "rb") + extension = os.path.splitext(options['input_file'][0]) + + if options['output_file'][0] == "-": + outfile = sys.stdout + else: + outfile = open(options['output_file'][0], "wb") + + if options['is_archive'] == True: + is_archive = "True" + extension = "tar.gz" + else: + is_archive = "False" + + if options['command']: + run_after_extract = "True" + command = options['command'] + else: + run_after_extract = "False" + command = "" + + template = open("%s/unpack.py" % os.path.dirname(__file__), "r") + + variables = { + "run_after_extract": run_after_extract, + "targz": is_archive, + "extension": extension, + "command": command + } + + for curline in template: + if curline.startswith('"""EOFDATA'): + # Found the EOF data marker, insert packed data before + # moving on with the next line. + outfile.write(curline) + + data = b"" + reader = Base64Reader(GzipReader(infile)) + chunk_size = 128 + + while True: + chunk = reader.read(chunk_size) + + if chunk == "": + break + + outfile.write(chunk + "\n") + else: + if "{%" in curline: + for variable_key, variable_value in variables.iteritems(): + curline = curline.replace("{%%%s}" % variable_key, variable_value) + + outfile.write(curline) + + outfile.close() diff --git a/b64reader.py b/pysfx/b64reader.py similarity index 100% rename from b64reader.py rename to pysfx/b64reader.py diff --git a/gzipreader.py b/pysfx/gzipreader.py similarity index 100% rename from gzipreader.py rename to pysfx/gzipreader.py diff --git a/pysfx/pysfx.py b/pysfx/pysfx.py new file mode 100644 index 0000000..a6afb73 --- /dev/null +++ b/pysfx/pysfx.py @@ -0,0 +1,72 @@ +import zlib, base64, sys, argparse, os +from gzipreader import GzipReader +from b64reader import Base64Reader + +def main(): + parser = argparse.ArgumentParser(description="Creates an SFX from a specified archive or file.") + parser.add_argument("-a", help="Treat the input file as a tar.gz archive that needs to be extracted upon running the SFX.", action="store_true", dest="is_archive") + parser.add_argument("-s", help="Define a command to be run after extraction of the SFX. %%NAME will be replaced with the path of the extracted file or folder. " + "For archives, the working directory is set to the extraction directory.", action="store", dest="command") + parser.add_argument("input_file", metavar="INPUTFILE", type=str, nargs=1, help="The file to read from. Use a dash (-) to read from STDIN instead.") + parser.add_argument("output_file", metavar="OUTPUTFILE", type=str, nargs=1, help="The file to write to. Use a dash (-) to write to STDOUT instead.") + options = vars(parser.parse_args()) + + if options['input_file'][0] == "-": + infile = sys.stdin + extension = "dat" + else: + infile = open(options['input_file'][0], "rb") + extension = os.path.splitext(options['input_file'][0]) + + if options['output_file'][0] == "-": + outfile = sys.stdout + else: + outfile = open(options['output_file'][0], "wb") + + if options['is_archive'] == True: + is_archive = "True" + extension = "tar.gz" + else: + is_archive = "False" + + if options['command']: + run_after_extract = "True" + command = options['command'] + else: + run_after_extract = "False" + command = "" + + template = open("%s/unpack.template" % os.path.dirname(__file__), "r") + + variables = { + "run_after_extract": run_after_extract, + "targz": is_archive, + "extension": extension, + "command": command + } + + for curline in template: + if curline.startswith('"""EOFDATA'): + # Found the EOF data marker, insert packed data before + # moving on with the next line. + outfile.write(curline) + + data = b"" + reader = Base64Reader(GzipReader(infile)) + chunk_size = 128 + + while True: + chunk = reader.read(chunk_size) + + if chunk == "": + break + + outfile.write(chunk + "\n") + else: + if "{%" in curline: + for variable_key, variable_value in variables.iteritems(): + curline = curline.replace("{%%%s}" % variable_key, variable_value) + + outfile.write(curline) + + outfile.close() diff --git a/unpack.template b/pysfx/unpack.py similarity index 100% rename from unpack.template rename to pysfx/unpack.py diff --git a/script/pysfx b/script/pysfx new file mode 100644 index 0000000..d5a4356 --- /dev/null +++ b/script/pysfx @@ -0,0 +1,4 @@ +#!/usr/bin/env python + +import pysfx +pysfx.main() diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..7190945 --- /dev/null +++ b/setup.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python + +""" +distutils/setuptools install script. See inline comments for packaging documentation. +""" + +import os +import sys + +try: + from setuptools import setup + # hush pyflakes + setup +except ImportError: + from distutils.core import setup + +try: + from distutils.command.build_py import build_py_2to3 as build_py +except ImportError: + from distutils.command.build_py import build_py + +packages = ['pysfx'] + +package_dir = {} + +package_data = {} + +scripts = [ + 'script/pysfx' +] + + +setup( + name='pysfx', + version='0.1', + maintainer='Sven Slootweg', + maintainer_email='admin@cryto.net', + description='Tool for creating self-extracting Python scripts with autorun.', + url='http://www.cryto.net/projects/pysfx/', + packages=packages, + package_dir=package_dir, + package_data=package_data, + include_package_data=True, + scripts=scripts, + install_requires=['argparse'], + cmdclass={'build_py': build_py} +) +