reorganization

- Had to reorganize so that Alembic's env.py could reference the tables in both local environment and a docker environment without manually modifying the python sys path. Maybe there is a better way idk
This commit is contained in:
mcinj 2022-05-20 12:20:08 -04:00
parent 3670d1522f
commit a101447eeb
22 changed files with 77 additions and 64 deletions

View File

@ -1,4 +1,6 @@
config/config.ini
config/debug.log
config/sqlite.db
config/info.log
config/info.log
alembic.ini
README.ini

View File

@ -1,19 +1,25 @@
FROM python:3.9-alpine
RUN mkdir -p /app
WORKDIR /app
RUN apk add tzdata build-base libffi-dev py3-cffi --no-cache
RUN mkdir -p /app/src
WORKDIR /app
ENV TZ=America/New_York
ENV VIRTUAL_ENV=/app/env
ENV VIRTUAL_ENV=/src/env
ENV PATH="$VIRTUAL_ENV/bin:$PATH"
ENV BOT_CONFIG_DIR="../config"
ENV BOT_DB_URL="sqlite:///../config/sqlite.db"
ENV BOT_ALEMBIC_CONFIG="./src/alembic"
RUN python -m venv $VIRTUAL_ENV
COPY requirements.txt .
COPY requirements.txt /app/
RUN pip3 install -r requirements.txt
COPY ./src/ /app/
COPY ./src/ /app/src/
COPY main.py /app/
VOLUME /config
CMD ["python3", "run.py"]
CMD ["python3", "main.py"]

View File

@ -20,8 +20,7 @@ The bot is specially designed for [SteamGifts.com](https://www.steamgifts.com/)
python -m venv env
source env/bin/activate
pip install -r requirements.txt
cd src
python run.py
python main.py
```
### Docker

5
main.py Normal file
View File

@ -0,0 +1,5 @@
from src.bot import run
if __name__ == '__main__':
run.entry()

View File

@ -18,9 +18,11 @@ if config.config_file_name is not None:
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
from src.models import TableNotification, TableSteamItem, TableGiveaway, Base
from src.bot.models import TableNotification, TableSteamItem, TableGiveaway, Base
target_metadata = Base.metadata
# other values from the config, defined by the needs of env.py,
# can be acquired:
# my_important_option = config.get_main_option("my_important_option")

View File

@ -1,9 +1,9 @@
from configparser import ConfigParser
from random import randint, randrange
import log
from .log import get_logger
logger = log.get_logger(__name__)
logger = get_logger(__name__)
class ConfigException(Exception):

View File

@ -1,3 +1,4 @@
import os
from datetime import datetime, timedelta
import sqlalchemy
@ -8,18 +9,15 @@ from sqlalchemy import func
from sqlalchemy.orm import Session
from sqlalchemy_utils import database_exists
import log
from models import TableNotification, TableGiveaway, TableSteamItem
from .log import get_logger
from .models import TableNotification, TableGiveaway, TableSteamItem
logger = log.get_logger(__name__)
engine = None
logger = get_logger(__name__)
engine = sqlalchemy.create_engine(f"{os.getenv('BOT_DB_URL', 'sqlite:///./config/sqlite.db')}", echo=False)
engine.connect()
def create_engine(db_url: str):
global engine
if not engine:
engine = sqlalchemy.create_engine(db_url, echo=False)
engine.connect()
return engine
@ -49,9 +47,6 @@ def run_migrations(script_location: str, db_url: str) -> None:
command.upgrade(alembic_cfg, 'head')
class NotificationHelper:
@classmethod

View File

@ -7,11 +7,11 @@ from bs4 import BeautifulSoup
from requests.adapters import HTTPAdapter
from urllib3.util import Retry
import log
from database import NotificationHelper, GiveawayHelper
from giveaway import Giveaway
from .log import get_logger
from .database import NotificationHelper, GiveawayHelper
from .giveaway import Giveaway
logger = log.get_logger(__name__)
logger = get_logger(__name__)
class SteamGiftsException(Exception):

View File

@ -1,8 +1,8 @@
import re
import log
from .log import get_logger
import time
logger = log.get_logger(__name__)
logger = get_logger(__name__)
class Giveaway:

View File

@ -7,10 +7,10 @@ from time import sleep
from dateutil import tz
import log
from enter_giveaways import EnterGiveaways
from .log import get_logger
from .enter_giveaways import EnterGiveaways
logger = log.get_logger(__name__)
logger = get_logger(__name__)
class GiveawayThread(threading.Thread):

View File

@ -1,11 +1,12 @@
import logging
import os
from logging.handlers import RotatingFileHandler
# log info level logs to stdout and debug to debug file
log_format = "%(levelname)s %(asctime)s - %(message)s"
logging.basicConfig(
handlers=[RotatingFileHandler('../config/debug.log', maxBytes=500000, backupCount=10)],
handlers=[RotatingFileHandler(f"{os.getenv('BOT_CONFIG_DIR', './config')}/debug.log", maxBytes=500000, backupCount=10)],
level=logging.DEBUG,
format=log_format)
@ -14,7 +15,7 @@ console_output.setLevel(logging.INFO)
console_format = logging.Formatter(log_format)
console_output.setFormatter(console_format)
info_log_file = RotatingFileHandler('../config/info.log', maxBytes=100000, backupCount=10)
info_log_file = RotatingFileHandler(f"{os.getenv('BOT_CONFIG_DIR', './config')}/info.log", maxBytes=100000, backupCount=10)
info_log_file.setLevel(logging.INFO)
info_log_format = logging.Formatter(log_format)
info_log_file.setFormatter(info_log_format)

View File

@ -1,10 +1,10 @@
import http.client
import urllib
import log
from database import NotificationHelper
from .log import get_logger
from .database import NotificationHelper
logger = log.get_logger(__name__)
logger = get_logger(__name__)
class Notification:

View File

@ -1,17 +1,18 @@
import os
from time import sleep
import log
from config_reader import ConfigReader, ConfigException
from enter_giveaways import SteamGiftsException
from giveaway_thread import GiveawayThread
from notification import Notification
from database import run_migrations, create_engine
from webserver_thread import WebServerThread
from .log import get_logger
from .config_reader import ConfigReader, ConfigException
from .enter_giveaways import SteamGiftsException
from .giveaway_thread import GiveawayThread
from .notification import Notification
from .database import run_migrations, create_engine
from .webserver_thread import WebServerThread
logger = log.get_logger(__name__)
config_file_name = '../config/config.ini'
db_url = 'sqlite:///../config/sqlite.db'
alembic_migration_files = '../alembic'
logger = get_logger(__name__)
config_file_name = f"{os.getenv('BOT_CONFIG_DIR', './config')}/config.ini"
db_url = f"{os.getenv('BOT_DB_URL', 'sqlite:///./config/sqlite.db')}"
alembic_migration_files = os.getenv('BOT_ALEMBIC_CONFIG_DIR', './src/alembic')
def run():
@ -59,19 +60,22 @@ def run():
exit(-1)
if __name__ == '__main__':
def entry():
logger.info("""
-------------------------------------------------------------------------------------
_____ _ _ __ _ ____ _
/ ____|| | (_) / _|| | | _ \ | |
| (___ | |_ ___ __ _ _ __ ___ __ _ _ | |_ | |_ ___ | |_) | ___ | |_
\___ \ | __|/ _ \ / _` || '_ ` _ \ / _` || || _|| __|/ __| | _ < / _ \ | __|
____) || |_| __/| (_| || | | | | || (_| || || | | |_ \__ \ | |_) || (_) || |_
|_____/ \__|\___| \__,_||_| |_| |_| \__, ||_||_| \__||___/ |____/ \___/ \__|
__/ |
|___/
-------------------------------------------------------------------------------------
""")
-------------------------------------------------------------------------------------
_____ _ _ __ _ ____ _
/ ____|| | (_) / _|| | | _ \ | |
| (___ | |_ ___ __ _ _ __ ___ __ _ _ | |_ | |_ ___ | |_) | ___ | |_
\___ \ | __|/ _ \ / _` || '_ ` _ \ / _` || || _|| __|/ __| | _ < / _ \ | __|
____) || |_| __/| (_| || | | | | || (_| || || | | |_ \__ \ | |_) || (_) || |_
|_____/ \__|\___| \__,_||_| |_| |_| \__, ||_||_| \__||___/ |____/ \___/ \__|
__/ |
|___/
-------------------------------------------------------------------------------------
""")
run_migrations(alembic_migration_files, db_url)
create_engine(db_url)
run()
if __name__ == '__main__':
entry()

View File

@ -1,12 +1,11 @@
import threading
from threading import Thread
from time import sleep
from flask_basicauth import BasicAuth
import log
from .log import get_logger
logger = log.get_logger(__name__)
logger = get_logger(__name__)
class WebServerThread(threading.Thread):