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/config.ini
config/debug.log config/debug.log
config/sqlite.db config/sqlite.db
config/info.log config/info.log
alembic.ini
README.ini

View file

@ -1,19 +1,25 @@
FROM python:3.9-alpine FROM python:3.9-alpine
RUN mkdir -p /app
WORKDIR /app
RUN apk add tzdata build-base libffi-dev py3-cffi --no-cache 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 TZ=America/New_York
ENV VIRTUAL_ENV=/app/env ENV VIRTUAL_ENV=/src/env
ENV PATH="$VIRTUAL_ENV/bin:$PATH" 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 RUN python -m venv $VIRTUAL_ENV
COPY requirements.txt . COPY requirements.txt /app/
RUN pip3 install -r requirements.txt RUN pip3 install -r requirements.txt
COPY ./src/ /app/
COPY ./src/ /app/src/
COPY main.py /app/
VOLUME /config 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 python -m venv env
source env/bin/activate source env/bin/activate
pip install -r requirements.txt pip install -r requirements.txt
cd src python main.py
python run.py
``` ```
### Docker ### 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 # for 'autogenerate' support
# from myapp import mymodel # from myapp import mymodel
# target_metadata = mymodel.Base.metadata # 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 target_metadata = Base.metadata
# other values from the config, defined by the needs of env.py, # other values from the config, defined by the needs of env.py,
# can be acquired: # can be acquired:
# my_important_option = config.get_main_option("my_important_option") # my_important_option = config.get_main_option("my_important_option")

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -1,11 +1,12 @@
import logging import logging
import os
from logging.handlers import RotatingFileHandler from logging.handlers import RotatingFileHandler
# log info level logs to stdout and debug to debug file # log info level logs to stdout and debug to debug file
log_format = "%(levelname)s %(asctime)s - %(message)s" log_format = "%(levelname)s %(asctime)s - %(message)s"
logging.basicConfig( 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, level=logging.DEBUG,
format=log_format) format=log_format)
@ -14,7 +15,7 @@ console_output.setLevel(logging.INFO)
console_format = logging.Formatter(log_format) console_format = logging.Formatter(log_format)
console_output.setFormatter(console_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_file.setLevel(logging.INFO)
info_log_format = logging.Formatter(log_format) info_log_format = logging.Formatter(log_format)
info_log_file.setFormatter(info_log_format) info_log_file.setFormatter(info_log_format)

View file

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

View file

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

View file

@ -1,12 +1,11 @@
import threading import threading
from threading import Thread from threading import Thread
from time import sleep
from flask_basicauth import BasicAuth 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): class WebServerThread(threading.Thread):