From 8d22c1f1fbfc1f753ded7133536e7a1829733efa Mon Sep 17 00:00:00 2001 From: Joshua Rodriguez Date: Thu, 1 Sep 2016 15:25:29 -0700 Subject: [PATCH] Place constants in own file and utils function in utils --- pycloud/app.py | 19 +++---------------- pycloud/cloud.py | 8 +------- pycloud/constants.py | 35 +++++++++++++++++++++++++++++++++++ pycloud/handlers.py | 5 +---- pycloud/session.py | 3 +-- pycloud/utils.py | 11 +++++++++++ setup.py | 7 ++----- 7 files changed, 54 insertions(+), 34 deletions(-) create mode 100644 pycloud/constants.py diff --git a/pycloud/app.py b/pycloud/app.py index ad89a99..2236360 100644 --- a/pycloud/app.py +++ b/pycloud/app.py @@ -22,17 +22,14 @@ import os import signal from time import sleep -from .session import SESSION_DIR, DATA_DIR +from .constants import SESSION_DIR, DATA_DIR, LOG_FOLDER, CONFIG_PATH, CONFIG_FILE, FILE_LOG, RUN_FOLDER, PID_FILE from .handlers import CreateMessaging, StatusMessaging, RemoveMessaging, RankMessaging -from .utils import remove, default_val -from .cloud import Cloud, LOG_FOLDER, CONFIG_PATH, CONFIG_FILE, FILE_LOG +from .utils import remove, default_val, required_paths +from .cloud import Cloud from redis import Redis from redis.exceptions import ConnectionError import yaml -RUN_FOLDER = '/var/run/year4000/pycloud/' -PID_FILE = RUN_FOLDER + 'pycloud.pid' - def start_daemon(nodes=None): """ Spins off a process that runs as a daemon """ @@ -77,7 +74,6 @@ def shutdown_daemon(*args): os.remove(PID_FILE) raise SystemExit('Terminating on signal number {0}'.format(args[0])) - def main(nodes=None): """ Deploy all the needed threads """ nodes = int(0 if nodes is None else nodes) @@ -152,15 +148,6 @@ def daemon_thread(target, name=None): thread.start() return thread -def required_paths(): - """ Make sure the needed folders exist """ - for folder in (SESSION_DIR, DATA_DIR, LOG_FOLDER, CONFIG_PATH, RUN_FOLDER): - if not os.path.exists(folder): - try: - os.makedirs(folder) - finally: - os.chmod(folder, 0o777) # hack fix for an existing bug - if __name__ == '__main__': _log = logging.getLogger('pycloud') diff --git a/pycloud/cloud.py b/pycloud/cloud.py index 4706e74..a771761 100644 --- a/pycloud/cloud.py +++ b/pycloud/cloud.py @@ -18,21 +18,15 @@ import os import sys -import datetime from threading import Lock from json import JSONEncoder from time import time +from .constants import CONFIG_PATH, CONFIG_FILE, LOG_FOLDER, FILE_LOG from .session import Session from .utils import generate_id, check_not_none import psutil -CONFIG_PATH = '/etc/year4000/pycloud/' -CONFIG_FILE = CONFIG_PATH + 'settings.yml' -LOG_FOLDER = '/var/log/year4000/pycloud/' -FILE_LOG = LOG_FOLDER + str(datetime.date.today()) + ".log" - - class Cloud: """ The cloud instance that stores the sessions that are running """ diff --git a/pycloud/constants.py b/pycloud/constants.py new file mode 100644 index 0000000..f7b4e79 --- /dev/null +++ b/pycloud/constants.py @@ -0,0 +1,35 @@ +#!/usr/bin/python3 +# Copyright 2016 Year4000. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +""" Constants needed for the program and setuptools """ + +import datetime + +RUN_FOLDER = '/var/run/year4000/pycloud/' +PID_FILE = RUN_FOLDER + 'pycloud.pid' + +CONFIG_PATH = '/etc/year4000/pycloud/' +CONFIG_FILE = CONFIG_PATH + 'settings.yml' +LOG_FOLDER = '/var/log/year4000/pycloud/' +FILE_LOG = LOG_FOLDER + str(datetime.date.today()) + ".log" + +CREATE_CHANNEL = 'year4000.pycloud.create' +STATUS_CHANNEL = 'year4000.pycloud.status' +REMOVE_CHANNEL = 'year4000.pycloud.remove' +RANK_CHANNEL = 'year4000.pycloud.rank' + +SESSION_DIR = '/var/run/year4000/pycloud/' +DATA_DIR = '/var/lib/year4000/pycloud/' diff --git a/pycloud/handlers.py b/pycloud/handlers.py index fa1fd18..770270c 100644 --- a/pycloud/handlers.py +++ b/pycloud/handlers.py @@ -20,16 +20,13 @@ from json import JSONDecoder import threading import logging +from .constants import CREATE_CHANNEL, STATUS_CHANNEL, REMOVE_CHANNEL, RANK_CHANNEL from .cloud import Rank from .utils import check_not_none from redis.exceptions import RedisError _log = logging.getLogger('pycloud') -CREATE_CHANNEL = 'year4000.pycloud.create' -STATUS_CHANNEL = 'year4000.pycloud.status' -REMOVE_CHANNEL = 'year4000.pycloud.remove' -RANK_CHANNEL = 'year4000.pycloud.rank' class Messaging: diff --git a/pycloud/session.py b/pycloud/session.py index 0562f26..a8659f1 100644 --- a/pycloud/session.py +++ b/pycloud/session.py @@ -21,12 +21,11 @@ import logging import socket from json import JSONEncoder +from .constants import SESSION_DIR, DATA_DIR from .utils import check_not_none, generate_id, remove, default_val _log = logging.getLogger('pycloud') -SESSION_DIR = '/var/run/year4000/pycloud/' -DATA_DIR = '/var/lib/year4000/pycloud/' class Session: diff --git a/pycloud/utils.py b/pycloud/utils.py index dd0cc9e..1b5e121 100644 --- a/pycloud/utils.py +++ b/pycloud/utils.py @@ -22,6 +22,7 @@ import shutil import time import random +from .constants import SESSION_DIR, DATA_DIR, LOG_FOLDER, CONFIG_PATH, RUN_FOLDER FNULL = open(os.devnull, 'w') @@ -102,3 +103,13 @@ def copy_update(old, new, can_update=False): except OSError: message = 'run again with --update' if can_update else 'you have update it yourself' print('ERROR: {0} installed, {1}'.format(new, message)) + + +def required_paths(): + """ Make sure the needed folders exist """ + for folder in (SESSION_DIR, DATA_DIR, LOG_FOLDER, CONFIG_PATH, RUN_FOLDER): + if not os.path.exists(folder): + try: + os.makedirs(folder) + finally: + os.chmod(folder, 0o777) # hack fix for an existing bug diff --git a/setup.py b/setup.py index 92d5e2c..88e2023 100644 --- a/setup.py +++ b/setup.py @@ -16,11 +16,8 @@ import os import sys -from pycloud.utils import install, is_root, copy_update -from pycloud.cloud import LOG_FOLDER, CONFIG_PATH, CONFIG_FILE -from pycloud.session import SESSION_DIR, DATA_DIR -from pycloud.app import RUN_FOLDER, required_paths -from pycloud.utils import is_root +from pycloud.utils import install, is_root, copy_update, required_paths +from pycloud.constants import LOG_FOLDER, CONFIG_PATH, CONFIG_FILE, SESSION_DIR, DATA_DIR, RUN_FOLDER from pycloud import __version__ from setuptools import setup, find_packages