From 19c1b000a429b1762fa1ade31349cff2566aa674 Mon Sep 17 00:00:00 2001 From: Sebastian Lohff Date: Mon, 7 Sep 2020 00:23:58 +0200 Subject: [PATCH] Make servefile a python package servefile is now a valid python package. The single servefile.py can still be used as a script by just putting it into PATH and making it executable. Additionally when installed via pip a wrapper script is created, calling the module's main(). python -m servefile works as well. --- servefile/__init__.py | 0 servefile/__main__.py | 3 +++ servefile => servefile/servefile.py | 2 +- setup.py | 8 +++++++- tests/test_servefile.py | 26 +++++++++++++++++++++----- 5 files changed, 32 insertions(+), 7 deletions(-) create mode 100644 servefile/__init__.py create mode 100644 servefile/__main__.py rename servefile => servefile/servefile.py (99%) diff --git a/servefile/__init__.py b/servefile/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/servefile/__main__.py b/servefile/__main__.py new file mode 100644 index 0000000..bc3ea0a --- /dev/null +++ b/servefile/__main__.py @@ -0,0 +1,3 @@ +from . import servefile + +servefile.main() diff --git a/servefile b/servefile/servefile.py similarity index 99% rename from servefile rename to servefile/servefile.py index 9cf96f6..2f6a588 100755 --- a/servefile +++ b/servefile/servefile.py @@ -1109,7 +1109,7 @@ class AuthenticationHandler(): def main(): - parser = argparse.ArgumentParser(description='Serve a single file via HTTP.') + parser = argparse.ArgumentParser(prog='servefile', description='Serve a single file via HTTP.') parser.add_argument('--version', action='version', version='%(prog)s ' + __version__) parser.add_argument('target', metavar='file/directory', type=str) parser.add_argument('-p', '--port', type=int, default=8080, \ diff --git a/setup.py b/setup.py index 4ecfb08..e4d499c 100755 --- a/setup.py +++ b/setup.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python from setuptools import setup @@ -18,6 +18,12 @@ setup( 'pytest', 'requests', ], + packages=["servefile"], + entry_points={ + "console_scripts": [ + "servefile = servefile.servefile:main", + ], + }, classifiers=[ 'Development Status :: 5 - Production/Stable', 'Environment :: Console', diff --git a/tests/test_servefile.py b/tests/test_servefile.py index 8132f6b..1f19bb2 100644 --- a/tests/test_servefile.py +++ b/tests/test_servefile.py @@ -27,9 +27,15 @@ def run_servefile(): def _run_servefile(args, **kwargs): if not isinstance(args, list): args = [args] - print("running with args", args) - servefile_path = str(Path(__file__).parent.parent / 'servefile') - p = subprocess.Popen([sys.executable, servefile_path] + args, **kwargs) + if kwargs.pop('standalone', None): + # directly call servefile.py + servefile_path = [str(Path(__file__).parent.parent / 'servefile' / 'servefile.py')] + else: + # call servefile as python module + servefile_path = ['-m', 'servefile'] + + print("running {} with args {}".format(", ".join(servefile_path), args)) + p = subprocess.Popen([sys.executable] + servefile_path + args, **kwargs) time.sleep(kwargs.get('timeout', 0.3)) instances.append(p) @@ -85,9 +91,9 @@ def check_download(expected_data=None, path='/', fname=None, status_code=200, ** return r # for additional tests -def test_version(run_servefile): +def _test_version(run_servefile, standalone): # we expect the version on stdout (python3.4+) or stderr(python2.6-3.3) - s = run_servefile('--version', stdout=subprocess.PIPE, stderr=subprocess.STDOUT) + s = run_servefile('--version', standalone=standalone, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) s.wait() version = s.stdout.readline().decode().strip() @@ -95,6 +101,15 @@ def test_version(run_servefile): assert version == 'servefile 0.4.4' +def test_version(run_servefile): + _test_version(run_servefile, standalone=False) + + +def test_version_standalone(run_servefile): + # test if servefile also works by calling servefile.py directly + _test_version(run_servefile, standalone=True) + + def test_correct_headers(run_servefile, datadir): data = "NOOT NOOT" p = datadir({'testfile': data}) / 'testfile' @@ -325,6 +340,7 @@ def test_https(run_servefile, datadir): urllib3.disable_warnings() check_download(data, protocol='https', verify=False) + def test_https_big_download(run_servefile, datadir): # test with about 10 mb of data data = "x" * (10 * 1024 ** 2)