Browse Source

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.
Sebastian Lohff 1 year ago
parent
commit
19c1b000a4
5 changed files with 32 additions and 7 deletions
  1. 0
    0
      servefile/__init__.py
  2. 3
    0
      servefile/__main__.py
  3. 1
    1
      servefile/servefile.py
  4. 7
    1
      setup.py
  5. 21
    5
      tests/test_servefile.py

+ 0
- 0
servefile/__init__.py View File


+ 3
- 0
servefile/__main__.py View File

@@ -0,0 +1,3 @@
1
+from . import servefile
2
+
3
+servefile.main()

servefile → servefile/servefile.py View File

@@ -1109,7 +1109,7 @@ class AuthenticationHandler():
1109 1109
 
1110 1110
 
1111 1111
 def main():
1112
-	parser = argparse.ArgumentParser(description='Serve a single file via HTTP.')
1112
+	parser = argparse.ArgumentParser(prog='servefile', description='Serve a single file via HTTP.')
1113 1113
 	parser.add_argument('--version', action='version', version='%(prog)s ' + __version__)
1114 1114
 	parser.add_argument('target', metavar='file/directory', type=str)
1115 1115
 	parser.add_argument('-p', '--port', type=int, default=8080, \

+ 7
- 1
setup.py View File

@@ -1,4 +1,4 @@
1
-#!/usr/bin/python
1
+#!/usr/bin/env python
2 2
 
3 3
 from setuptools import setup
4 4
 
@@ -18,6 +18,12 @@ setup(
18 18
         'pytest',
19 19
         'requests',
20 20
     ],
21
+    packages=["servefile"],
22
+    entry_points={
23
+        "console_scripts": [
24
+            "servefile = servefile.servefile:main",
25
+        ],
26
+    },
21 27
     classifiers=[
22 28
         'Development Status :: 5 - Production/Stable',
23 29
         'Environment :: Console',

+ 21
- 5
tests/test_servefile.py View File

@@ -27,9 +27,15 @@ def run_servefile():
27 27
     def _run_servefile(args, **kwargs):
28 28
         if not isinstance(args, list):
29 29
             args = [args]
30
-        print("running with args", args)
31
-        servefile_path = str(Path(__file__).parent.parent / 'servefile')
32
-        p = subprocess.Popen([sys.executable, servefile_path] + args, **kwargs)
30
+        if kwargs.pop('standalone', None):
31
+            # directly call servefile.py
32
+            servefile_path = [str(Path(__file__).parent.parent / 'servefile' / 'servefile.py')]
33
+        else:
34
+            # call servefile as python module
35
+            servefile_path = ['-m', 'servefile']
36
+
37
+        print("running {} with args {}".format(", ".join(servefile_path), args))
38
+        p = subprocess.Popen([sys.executable] + servefile_path + args, **kwargs)
33 39
         time.sleep(kwargs.get('timeout', 0.3))
34 40
         instances.append(p)
35 41
 
@@ -85,9 +91,9 @@ def check_download(expected_data=None, path='/', fname=None, status_code=200, **
85 91
     return r  # for additional tests
86 92
 
87 93
 
88
-def test_version(run_servefile):
94
+def _test_version(run_servefile, standalone):
89 95
     # we expect the version on stdout (python3.4+) or stderr(python2.6-3.3)
90
-    s = run_servefile('--version', stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
96
+    s = run_servefile('--version', standalone=standalone, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
91 97
     s.wait()
92 98
     version = s.stdout.readline().decode().strip()
93 99
 
@@ -95,6 +101,15 @@ def test_version(run_servefile):
95 101
     assert version == 'servefile 0.4.4'
96 102
 
97 103
 
104
+def test_version(run_servefile):
105
+    _test_version(run_servefile, standalone=False)
106
+
107
+
108
+def test_version_standalone(run_servefile):
109
+    # test if servefile also works by calling servefile.py directly
110
+    _test_version(run_servefile, standalone=True)
111
+
112
+
98 113
 def test_correct_headers(run_servefile, datadir):
99 114
     data = "NOOT NOOT"
100 115
     p = datadir({'testfile': data}) / 'testfile'
@@ -325,6 +340,7 @@ def test_https(run_servefile, datadir):
325 340
     urllib3.disable_warnings()
326 341
     check_download(data, protocol='https', verify=False)
327 342
 
343
+
328 344
 def test_https_big_download(run_servefile, datadir):
329 345
     # test with about 10 mb of data
330 346
     data = "x" * (10 * 1024 ** 2)

Loading…
Cancel
Save