From 52baef667c2fe75638bca24fab2a5a4a6df02c0e Mon Sep 17 00:00:00 2001 From: levi-rs Date: Mon, 5 Dec 2016 07:34:27 -0600 Subject: [PATCH 1/3] 73 Use appdirs for logging directory - Use appdirs to discover appropriate logging directory - Create directory if it doesn't exist - Prevent deployments to dankbot server --- circle.yml | 2 +- dankbot/__init__.py | 5 ++++- dankbot/cli.py | 17 ++++++++++++----- setup.py | 11 ++++++----- 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/circle.yml b/circle.yml index d2a1709..872abd9 100644 --- a/circle.yml +++ b/circle.yml @@ -27,6 +27,6 @@ test: deployment: production: - branch: master + branch: notmaster commands: - ssh $DEPLOY_HOST $DEPLOY_PATH/deploy.sh diff --git a/dankbot/__init__.py b/dankbot/__init__.py index 74f4e66..d6af615 100644 --- a/dankbot/__init__.py +++ b/dankbot/__init__.py @@ -1,4 +1,7 @@ - from ._version import get_versions __version__ = get_versions()['version'] del get_versions + +APPNAME = "dankbot" +APPAUTHOR = "DankCity" + diff --git a/dankbot/cli.py b/dankbot/cli.py index d02fb44..3f8d638 100644 --- a/dankbot/cli.py +++ b/dankbot/cli.py @@ -1,14 +1,16 @@ - - +import os import sys import logging -from os import path from configparser import ConfigParser from logging.handlers import RotatingFileHandler +import appdirs + from dankbot.dankbot import DankBot +from dankbot import APPNAME, APPAUTHOR -LOG_FILE = "/var/log/dankbot/dankbot.log" +LOG_DIR = appdirs.user_log_dir(APPNAME, APPAUTHOR) +LOG_FILE = os.path.join(LOG_DIR, "dankbot.log") def configure_logger(): @@ -17,6 +19,10 @@ def configure_logger(): :param dir_path: String, path to current directory """ + # Create the logging directory if it doesn't exist + if not os.path.exists(LOG_DIR): + os.makedirs(LOG_DIR) + # Formatting formatter = logging.Formatter('[%(levelname)s %(asctime)s] %(message)s') @@ -44,11 +50,12 @@ def main(): logger = configure_logger() logger.info("Dankbot run starting") + logger.info("Logging to: {0}".format(LOG_FILE)) # Load the configuration options logger.info("Loading Dankbot Configuration") config = ConfigParser() - config_path = path.join(path.dirname(__file__), u'dankbot.ini') + config_path = os.path.join(os.path.dirname(__file__), u'dankbot.ini') config.read(config_path) try: diff --git a/setup.py b/setup.py index 4d88e82..e2c137c 100644 --- a/setup.py +++ b/setup.py @@ -18,11 +18,12 @@ ] }, install_requires=[ + 'appdirs==1.4.0', + 'configparser==3.5.0', + 'imgurpython==1.1.7', + 'mysqlclient==1.3.9', 'praw==3.6.0', - 'retryz', - 'slacker', - 'mysqlclient', - 'imgurpython', - 'configparser>=3.5.0b2', + 'retryz==0.1.8', + 'slacker==0.9.30', ], ) From 0c46ed2cf8734fd05c23fcfe311458fd1268732c Mon Sep 17 00:00:00 2001 From: levi-rs Date: Mon, 5 Dec 2016 09:10:55 -0600 Subject: [PATCH 2/3] 73 Update logging to use dankbot.ini config --- dankbot/__init__.py | 1 - dankbot/cli.py | 67 +++++++++++++++++++++++--------------- dankbot/dankbot.sample.ini | 33 +++++++++++++++++++ dankbot/logs/dankbot.log | 0 dankbot/sample.dankbot.ini | 19 ----------- tests/test_cli.py | 2 +- 6 files changed, 75 insertions(+), 47 deletions(-) create mode 100644 dankbot/dankbot.sample.ini delete mode 100644 dankbot/logs/dankbot.log delete mode 100644 dankbot/sample.dankbot.ini diff --git a/dankbot/__init__.py b/dankbot/__init__.py index d6af615..2d49e0a 100644 --- a/dankbot/__init__.py +++ b/dankbot/__init__.py @@ -4,4 +4,3 @@ APPNAME = "dankbot" APPAUTHOR = "DankCity" - diff --git a/dankbot/cli.py b/dankbot/cli.py index 3f8d638..e13bf5d 100644 --- a/dankbot/cli.py +++ b/dankbot/cli.py @@ -9,19 +9,24 @@ from dankbot.dankbot import DankBot from dankbot import APPNAME, APPAUTHOR -LOG_DIR = appdirs.user_log_dir(APPNAME, APPAUTHOR) -LOG_FILE = os.path.join(LOG_DIR, "dankbot.log") +def load_config(): + config = ConfigParser() + config_path = os.path.join(os.path.dirname(__file__), u'dankbot.ini') + config.read(config_path) -def configure_logger(): + return config + + +def configure_logger(log_config): """ Creates a rotating log - :param dir_path: String, path to current directory + :param config_dir_path: String, path to current directory """ - # Create the logging directory if it doesn't exist - if not os.path.exists(LOG_DIR): - os.makedirs(LOG_DIR) + # Create Logger object + logger = logging.getLogger(__name__) + logger.setLevel(logging.DEBUG) # Formatting formatter = logging.Formatter('[%(levelname)s %(asctime)s] %(message)s') @@ -30,33 +35,43 @@ def configure_logger(): stdout_handler = logging.StreamHandler(sys.stdout) stdout_handler.setLevel(logging.DEBUG) stdout_handler.setFormatter(formatter) + logger.addHandler(stdout_handler) - # Set up file logging with rotating file handler - rotate_fh = RotatingFileHandler(LOG_FILE, backupCount=5, maxBytes=1000000) - rotate_fh.setLevel(logging.DEBUG) - rotate_fh.setFormatter(formatter) + # Log to file if set in config + if log_config.getboolean('log_to_file'): + log_dir = log_config['directory'] or appdirs.user_log_dir(APPNAME, APPAUTHOR) + log_path = os.path.join(log_dir, log_config['file_name']) - # Create Logger object - logger = logging.getLogger(__name__) - logger.setLevel(logging.DEBUG) - logger.addHandler(stdout_handler) - logger.addHandler(rotate_fh) + # Create the logging directory if it doesn't exist + if not os.path.exists(log_dir): # pragma: no cover + os.makedirs(log_dir) - return logger + # Set up file logging with rotating file handler + backups = log_config.getint('backups') + max_bytes = log_config.getint('max_bytes') + rotate_fh = RotatingFileHandler( + log_path, backupCount=backups, maxBytes=max_bytes + ) + rotate_fh.setLevel(logging.DEBUG) + rotate_fh.setFormatter(formatter) + logger.addHandler(rotate_fh) + else: # pragma: no cover + log_path = None -def main(): - # Setup the logger - logger = configure_logger() + return logger, log_path - logger.info("Dankbot run starting") - logger.info("Logging to: {0}".format(LOG_FILE)) +def main(): # Load the configuration options - logger.info("Loading Dankbot Configuration") - config = ConfigParser() - config_path = os.path.join(os.path.dirname(__file__), u'dankbot.ini') - config.read(config_path) + config = load_config() + + # Load the logger + logger, log_path = configure_logger(config['dankbot']) + + logger.info("Dankbot run starting") + logger.info("Dankbot configuration loaded") + logger.info("Logging to: {0}".format(log_path)) # pylint: disable=W1202 try: DankBot(config, logger).find_and_post_memes() diff --git a/dankbot/dankbot.sample.ini b/dankbot/dankbot.sample.ini new file mode 100644 index 0000000..f82c4af --- /dev/null +++ b/dankbot/dankbot.sample.ini @@ -0,0 +1,33 @@ +[dankbot] +# Leave directory blank and dankbot will determine the best place to +# log to your platform +log_to_file: false +directory: +file_name: dankbot.log +backups: 5 +max_bytes: 1000000 + +[slack] +# Follow instructions at https://my.slack.com/services/new/bot +token: +channel: #random + +[reddit] +# r/dankmemes, r/funnygifs, etc +subreddits: dankmemes, funnygifs + +[imgur] +# Register at https://api.imgur.com/oauth2/addclient +# Select Anonymous usage +client_id: +client_secret: + +[mysql] +# depricated, switching ti sqlite +database: +username: +password: + +[misc] +include_nsfw: false +max_memes: 3 diff --git a/dankbot/logs/dankbot.log b/dankbot/logs/dankbot.log deleted file mode 100644 index e69de29..0000000 diff --git a/dankbot/sample.dankbot.ini b/dankbot/sample.dankbot.ini deleted file mode 100644 index 62ffe65..0000000 --- a/dankbot/sample.dankbot.ini +++ /dev/null @@ -1,19 +0,0 @@ -[slack] -token: -channel: #random - -[reddit] -subreddits: dankmemes, fishpost - -[imgur] -client_id: -client_secret: - -[mysql] -database: -username: -password: - -[misc] -include_nsfw: -max_memes: 3 diff --git a/tests/test_cli.py b/tests/test_cli.py index 83b7389..d1d3abd 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -22,7 +22,7 @@ def test_main(dankbot, _, gl_mock): @patch('dankbot.cli.configure_logger') @patch('dankbot.cli.DankBot') def test_main_exception(dankbot, logger_mock): - logger_mock.return_value = logger_mock + logger_mock.return_value = logger_mock, None dankbot.return_value = ValueError("Mock exception") cli.main() From b84ee42a1211cd12cb9033c6281f1573067c9dc5 Mon Sep 17 00:00:00 2001 From: levi-rs Date: Mon, 5 Dec 2016 10:34:37 -0600 Subject: [PATCH 3/3] Use sample config for testing --- dankbot/cli.py | 4 +++- dankbot/dankbot.sample.ini | 2 +- tests/test_cli.py | 2 ++ 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/dankbot/cli.py b/dankbot/cli.py index e13bf5d..0a13242 100644 --- a/dankbot/cli.py +++ b/dankbot/cli.py @@ -9,10 +9,12 @@ from dankbot.dankbot import DankBot from dankbot import APPNAME, APPAUTHOR +DEFAULT_CONFIG = 'dankbot.ini' + def load_config(): config = ConfigParser() - config_path = os.path.join(os.path.dirname(__file__), u'dankbot.ini') + config_path = os.path.join(os.path.dirname(__file__), DEFAULT_CONFIG) config.read(config_path) return config diff --git a/dankbot/dankbot.sample.ini b/dankbot/dankbot.sample.ini index f82c4af..47e5ec3 100644 --- a/dankbot/dankbot.sample.ini +++ b/dankbot/dankbot.sample.ini @@ -1,7 +1,7 @@ [dankbot] # Leave directory blank and dankbot will determine the best place to # log to your platform -log_to_file: false +log_to_file: true directory: file_name: dankbot.log backups: 5 diff --git a/tests/test_cli.py b/tests/test_cli.py index d1d3abd..6efe368 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -4,6 +4,7 @@ from dankbot import cli +@patch('dankbot.cli.DEFAULT_CONFIG', 'dankbot.sample.ini') @patch('dankbot.cli.logging.getLogger') @patch('dankbot.cli.RotatingFileHandler') @patch('dankbot.cli.DankBot') @@ -19,6 +20,7 @@ def test_main(dankbot, _, gl_mock): assert dankbot.find_and_post_memes.called +@patch('dankbot.cli.DEFAULT_CONFIG', 'dankbot.sample.ini') @patch('dankbot.cli.configure_logger') @patch('dankbot.cli.DankBot') def test_main_exception(dankbot, logger_mock):