From f95935ab51cb4943f782fcec237a2a6701660ed2 Mon Sep 17 00:00:00 2001 From: Scott Worley Date: Tue, 4 Apr 2023 20:22:43 -0700 Subject: [PATCH] A minimal "hello world" python web server --- .gitignore | 2 ++ Changelog | 3 +++ README.md | 1 + default.nix | 19 +++++++++++++++++++ git-pre-commit-hook | 22 ++++++++++++++++++++++ hellowebpy.py | 20 ++++++++++++++++++++ setup.py | 13 +++++++++++++ test.sh | 33 +++++++++++++++++++++++++++++++++ tests/says-hello.sh | 20 ++++++++++++++++++++ 9 files changed, 133 insertions(+) create mode 100644 .gitignore create mode 100644 Changelog create mode 100644 README.md create mode 100644 default.nix create mode 100755 git-pre-commit-hook create mode 100644 hellowebpy.py create mode 100644 setup.py create mode 100755 test.sh create mode 100755 tests/says-hello.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1d26af3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.mypy_cache +result diff --git a/Changelog b/Changelog new file mode 100644 index 0000000..a9e9056 --- /dev/null +++ b/Changelog @@ -0,0 +1,3 @@ +## [1.0.0] - 2023-04-04 + +Initial release diff --git a/README.md b/README.md new file mode 100644 index 0000000..f892d0c --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +A minimal "hello world" python web server. diff --git a/default.nix b/default.nix new file mode 100644 index 0000000..c256aea --- /dev/null +++ b/default.nix @@ -0,0 +1,19 @@ +{ pkgs ? import { }, lint ? false }: + +pkgs.python3Packages.callPackage +({ autopep8, buildPythonPackage, curl, lib, mypy, pylint, }: + buildPythonPackage rec { + pname = "hellowebpy"; + version = "1.0.0"; + src = lib.cleanSource ./.; + PIP_DISABLE_PIP_VERSION_CHECK = 1; + checkInputs = [ curl mypy ] ++ lib.optionals lint [ autopep8 pylint ]; + doCheck = true; + checkPhase = "./test.sh"; + meta = { + description = "A minimal 'hello world' python web server."; + homepage = "https://git.scottworley.com/hellowebpy"; + license = pkgs.lib.licenses.gpl3; + maintainers = with pkgs.lib.maintainers; [ chkno ]; + }; + }) { } diff --git a/git-pre-commit-hook b/git-pre-commit-hook new file mode 100755 index 0000000..85b4445 --- /dev/null +++ b/git-pre-commit-hook @@ -0,0 +1,22 @@ +#!/usr/bin/env bash + +# Copy me to .git/hooks/pre-commit + +set -e + +cleanup() { + if [[ "$D" && -e "$D" ]];then + rm -rf "$D" + fi +} +trap cleanup EXIT + +D=$(mktemp -d) +[[ "$D" && -d "$D" ]] + +git checkout-index --prefix="$D/" -a +pushd "$D" + +nix-shell --arg lint true --run './test.sh lint' + +popd diff --git a/hellowebpy.py b/hellowebpy.py new file mode 100644 index 0000000..113911c --- /dev/null +++ b/hellowebpy.py @@ -0,0 +1,20 @@ +from http.server import BaseHTTPRequestHandler, HTTPServer + +listen_address = ('', 8080) + + +class HelloServer(BaseHTTPRequestHandler): + def do_GET(self) -> None: + self.send_response(200) + self.send_header("Content-type", "text/html") + self.end_headers() + self.wfile.write(bytes("Hello Web", "utf-8")) + + +def main() -> None: + webServer = HTTPServer(listen_address, HelloServer) + webServer.serve_forever() + + +if __name__ == "__main__": + main() diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..0bce1ad --- /dev/null +++ b/setup.py @@ -0,0 +1,13 @@ +from setuptools import setup + +setup( + name='hellowebpy', + version='1.0.0', + description="A minimal 'hello world' python web server.", + author="Scott Worley", + author_email="scottworley@scottworley.com", + url="https://git.scottworley.com/hellowebpy", + py_modules=['hellowebpy'], + license="GPL-3.0", + entry_points={'console_scripts': ['hellowebpy = hellowebpy:main']}, +) diff --git a/test.sh b/test.sh new file mode 100755 index 0000000..9ccbe26 --- /dev/null +++ b/test.sh @@ -0,0 +1,33 @@ +#!/bin/sh + +set -e + +PARALLELISM=4 + +find . -name build -prune -o -name dist -prune -o -name '*.py' -print0 | + xargs -0 mypy --strict --ignore-missing-imports --no-warn-unused-ignores + +for test in tests/*;do + if [ ! -d "$test" ];then + echo "### Running test $test" >&2 + "$test" + fi +done + +find . -name '*_test.py' -print0 | xargs -0 -r -n1 python3 + +if [ "$1" = lint ];then + + find . -name '*.py' -print0 | xargs -0 pylint --reports=n --persistent=n --ignore-imports=y -d fixme,invalid-name,missing-docstring,subprocess-run-check,too-few-public-methods + + formatting_needs_fixing=$( + find . -name '*.py' -print0 | + xargs -P "$PARALLELISM" -0 -n1 autopep8 --diff -aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + ) + if [ "$formatting_needs_fixing" ];then + echo "Formatting needs fixing:" + echo "$formatting_needs_fixing" + exit 1 + fi + +fi diff --git a/tests/says-hello.sh b/tests/says-hello.sh new file mode 100755 index 0000000..2c1a646 --- /dev/null +++ b/tests/says-hello.sh @@ -0,0 +1,20 @@ +#!/bin/sh + +set -eu + +python3 hellowebpy.py & +pid=$! + +exit_status=1 + +for _ in $(seq 10);do + if curl --max-time 2 http://localhost:8080 | grep 'Hello Web';then + echo SUCCESS + exit_status=0 + break + fi + sleep 1 +done + +kill "$pid" +exit "$exit_status" -- 2.44.1