From d812a0628da4f7b16c34a1cd7f04fd9b91f673bd Mon Sep 17 00:00:00 2001 From: vas3k Date: Mon, 6 Jan 2020 22:12:12 +0100 Subject: [PATCH] Docker, makefile and other bullshit --- .gitignore | 1 + Dockerfile | 9 ++++++++ Makefile | 37 ++++++++++++++++++++++++++++++ README.md | 7 ++++++ docker-compose.yml | 30 ++++++++++++++++++++++++ infomate/__init__.py | 1 + infomate/settings.py | 6 ++--- mypy.ini | 10 ++++++++ requirements.txt | 2 ++ setup.cfg | 3 +++ setup.py | 47 ++++++++++++++++++++++++++++++++++++++ templates/board.html | 7 ++++-- utils/__init__.py | 0 utils/wait_for_postgres.py | 14 ++++++++++++ 14 files changed, 169 insertions(+), 5 deletions(-) create mode 100644 Dockerfile create mode 100644 Makefile create mode 100644 docker-compose.yml create mode 100644 mypy.ini create mode 100644 setup.cfg create mode 100644 setup.py create mode 100644 utils/__init__.py create mode 100644 utils/wait_for_postgres.py diff --git a/.gitignore b/.gitignore index 144c1b8..0d79597 100644 --- a/.gitignore +++ b/.gitignore @@ -30,6 +30,7 @@ pip-log.txt # Mac OS X .DS_Store +.mypy_cache private_settings.py local_settings.py media/images diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..0baa093 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,9 @@ +FROM python:3.7 + +WORKDIR /app +ARG requirements=requirements.txt + +ADD . /app + +RUN pip install --no-cache-dir -e . +RUN pip install --no-cache-dir -r $requirements diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..74bf1cc --- /dev/null +++ b/Makefile @@ -0,0 +1,37 @@ +PROJECT_NAME=infomate + +all: run + +# Install dev requirements +dev-requirements: + @pip3 install -r requirements.txt + +# Migrate database to the latest version +migrate: + @python3 manage.py migrate + +# Check types with mypy +mypy: + mypy $(PROJECT_NAME) + +# Lint code with flake8 +lint: + flake8 $(PROJECT_NAME) + +# Runs dev server +run: + @python3 manage.py runserver + +# Run dev server in docker +docker_run: + @python3 ./utils/wait_for_postgres.py + @python3 manage.py migrate + @python3 manage.py runserver + +# Initialize feeds from boards.yml +feed_init: + @python3 ./scripts/initialize.py + +# Refresh RSS feeds +feed_refresh: + @python3 ./scripts/update.py diff --git a/README.md b/README.md index 084251a..896f7b8 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,9 @@ # infomate.club + Experimental project + +### Run + +``` +$ docker-compose up +``` diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..460cd0c --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,30 @@ +version: '3.7' + +services: + infomate_app: + build: + context: . + args: + requirements: requirements.txt + command: make docker_run + container_name: infomate_app + environment: + - DEBUG=True + - PYTHONUNBUFFERED=1 + restart: always + volumes: + - .:/app:delegated + depends_on: + - postgres + ports: + - "8000:8000" + + postgres: + image: postgres:11 + container_name: infomate_postgres + environment: + - POSTGRES_USER=postgres + - POSTGRES_PASSWORD=postgres + - POSTGRES_DB=infomate + ports: + - 5432 diff --git a/infomate/__init__.py b/infomate/__init__.py index e69de29..3dc1f76 100644 --- a/infomate/__init__.py +++ b/infomate/__init__.py @@ -0,0 +1 @@ +__version__ = "0.1.0" diff --git a/infomate/settings.py b/infomate/settings.py index 49c44ee..f94b332 100644 --- a/infomate/settings.py +++ b/infomate/settings.py @@ -50,8 +50,8 @@ "ENGINE": "django.db.backends.postgresql_psycopg2", "NAME": "infomate", "USER": "postgres", # redefined in private_settings.py - "PASSWORD": "", - "HOST": "127.0.0.1", + "PASSWORD": "postgres", + "HOST": "postgres", "PORT": "5432", } } @@ -105,7 +105,7 @@ try: # poor mans' private settings - from .private_settings import * + from infomate.private_settings import * except ImportError: pass diff --git a/mypy.ini b/mypy.ini new file mode 100644 index 0000000..8abe40f --- /dev/null +++ b/mypy.ini @@ -0,0 +1,10 @@ +[mypy] +ignore_missing_imports = True + +check_untyped_defs = True +disallow_any_generics = True +disallow_untyped_defs = True +follow_imports = silent +strict_optional = True +warn_redundant_casts = True +warn_unused_ignores = True diff --git a/requirements.txt b/requirements.txt index 0982401..3a1cdd1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ Django==2.2.8 +psycopg2==2.8.3 click==7.0 awesome-slugify>=1.6.5 requests==2.22.0 @@ -7,4 +8,5 @@ pyyaml==5.2 feedparser==5.2.1 sentry-sdk==0.13.5 pyjwt==1.7.1 +nltk==3.4.4 newspaper3k>=0.2.8 diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..cc135b5 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,3 @@ +[flake8] +max-line-length = 119 +exclude = etc static templates migrations diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..dafddf3 --- /dev/null +++ b/setup.py @@ -0,0 +1,47 @@ +import os +import re +import pathlib +from typing import List + +from setuptools import find_packages, setup + +REGEXP = re.compile(r'^__version__\W*=\W*"([\d.abrc]+)"') +PARENT = pathlib.Path(__file__).parent + + +def read_version(): + init_py = os.path.join( + os.path.dirname(__file__), + "infomate", + "__init__.py", + ) + + with open(init_py) as f: + for line in f: + match = REGEXP.match(line) + if match is not None: + return match.group(1) + else: + msg = f"Cannot find version in ${init_py}" + raise RuntimeError(msg) + + +def read_requirements(path: str) -> List[str]: + file_path = PARENT / path + with open(file_path) as f: + return f.read().split("\n") + + +setup( + name="infomate", + version=read_version(), + description="infomate", + platforms=["POSIX"], + packages=find_packages(), + package_data={ + "": ["docs/*.*"] + }, + include_package_data=True, + install_requires=read_requirements("requirements.txt"), + zip_safe=False, +) diff --git a/templates/board.html b/templates/board.html index 199879b..17735d1 100644 --- a/templates/board.html +++ b/templates/board.html @@ -59,8 +59,10 @@ {% if article.image %} {{ article.title }} {% endif %} - {{ article.title|truncatechars:100 }} - {% if article.description and article.description|length > 20 %} + + {{ article.title|truncatechars:100 }} + + {% if article.description or article.summary %} {% if article.summary %} {{ article.summary|striptags|nl2br|truncatechars:300|safe }} @@ -69,6 +71,7 @@ {% endif %} {% endif %} + {{ article.natural_created_at }} diff --git a/utils/__init__.py b/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/utils/wait_for_postgres.py b/utils/wait_for_postgres.py new file mode 100644 index 0000000..add111e --- /dev/null +++ b/utils/wait_for_postgres.py @@ -0,0 +1,14 @@ +import socket +import time +import random + +if __name__ == "__main__": + while True: + try: + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: + sock.connect(("postgres", 5432)) + print("Postgres had started") + break + except socket.error: + print("Waiting for postgres") + time.sleep(0.5 + (random.randint(0, 100) / 1000))