forked from seba/servefile
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.
This commit is contained in:
parent
3d46950d6c
commit
19c1b000a4
|
@ -0,0 +1,3 @@
|
||||||
|
from . import servefile
|
||||||
|
|
||||||
|
servefile.main()
|
|
@ -1109,7 +1109,7 @@ class AuthenticationHandler():
|
||||||
|
|
||||||
|
|
||||||
def main():
|
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('--version', action='version', version='%(prog)s ' + __version__)
|
||||||
parser.add_argument('target', metavar='file/directory', type=str)
|
parser.add_argument('target', metavar='file/directory', type=str)
|
||||||
parser.add_argument('-p', '--port', type=int, default=8080, \
|
parser.add_argument('-p', '--port', type=int, default=8080, \
|
8
setup.py
8
setup.py
|
@ -1,4 +1,4 @@
|
||||||
#!/usr/bin/python
|
#!/usr/bin/env python
|
||||||
|
|
||||||
from setuptools import setup
|
from setuptools import setup
|
||||||
|
|
||||||
|
@ -18,6 +18,12 @@ setup(
|
||||||
'pytest',
|
'pytest',
|
||||||
'requests',
|
'requests',
|
||||||
],
|
],
|
||||||
|
packages=["servefile"],
|
||||||
|
entry_points={
|
||||||
|
"console_scripts": [
|
||||||
|
"servefile = servefile.servefile:main",
|
||||||
|
],
|
||||||
|
},
|
||||||
classifiers=[
|
classifiers=[
|
||||||
'Development Status :: 5 - Production/Stable',
|
'Development Status :: 5 - Production/Stable',
|
||||||
'Environment :: Console',
|
'Environment :: Console',
|
||||||
|
|
|
@ -27,9 +27,15 @@ def run_servefile():
|
||||||
def _run_servefile(args, **kwargs):
|
def _run_servefile(args, **kwargs):
|
||||||
if not isinstance(args, list):
|
if not isinstance(args, list):
|
||||||
args = [args]
|
args = [args]
|
||||||
print("running with args", args)
|
if kwargs.pop('standalone', None):
|
||||||
servefile_path = str(Path(__file__).parent.parent / 'servefile')
|
# directly call servefile.py
|
||||||
p = subprocess.Popen([sys.executable, servefile_path] + args, **kwargs)
|
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))
|
time.sleep(kwargs.get('timeout', 0.3))
|
||||||
instances.append(p)
|
instances.append(p)
|
||||||
|
|
||||||
|
@ -85,9 +91,9 @@ def check_download(expected_data=None, path='/', fname=None, status_code=200, **
|
||||||
return r # for additional tests
|
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)
|
# 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()
|
s.wait()
|
||||||
version = s.stdout.readline().decode().strip()
|
version = s.stdout.readline().decode().strip()
|
||||||
|
|
||||||
|
@ -95,6 +101,15 @@ def test_version(run_servefile):
|
||||||
assert version == 'servefile 0.4.4'
|
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):
|
def test_correct_headers(run_servefile, datadir):
|
||||||
data = "NOOT NOOT"
|
data = "NOOT NOOT"
|
||||||
p = datadir({'testfile': data}) / 'testfile'
|
p = datadir({'testfile': data}) / 'testfile'
|
||||||
|
@ -325,6 +340,7 @@ def test_https(run_servefile, datadir):
|
||||||
urllib3.disable_warnings()
|
urllib3.disable_warnings()
|
||||||
check_download(data, protocol='https', verify=False)
|
check_download(data, protocol='https', verify=False)
|
||||||
|
|
||||||
|
|
||||||
def test_https_big_download(run_servefile, datadir):
|
def test_https_big_download(run_servefile, datadir):
|
||||||
# test with about 10 mb of data
|
# test with about 10 mb of data
|
||||||
data = "x" * (10 * 1024 ** 2)
|
data = "x" * (10 * 1024 ** 2)
|
||||||
|
|
Loading…
Reference in New Issue