Skip to content

Commit

Permalink
Place constants in own file and utils function in utils
Browse files Browse the repository at this point in the history
  • Loading branch information
ewized committed Sep 1, 2016
1 parent 3691c8b commit 8d22c1f
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 34 deletions.
19 changes: 3 additions & 16 deletions pycloud/app.py
Expand Up @@ -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 """
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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')
Expand Down
8 changes: 1 addition & 7 deletions pycloud/cloud.py
Expand Up @@ -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 """

Expand Down
35 changes: 35 additions & 0 deletions 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 <http://www.gnu.org/licenses/>.

""" 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/'
5 changes: 1 addition & 4 deletions pycloud/handlers.py
Expand Up @@ -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:
Expand Down
3 changes: 1 addition & 2 deletions pycloud/session.py
Expand Up @@ -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:
Expand Down
11 changes: 11 additions & 0 deletions pycloud/utils.py
Expand Up @@ -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')
Expand Down Expand Up @@ -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
7 changes: 2 additions & 5 deletions setup.py
Expand Up @@ -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

Expand Down

0 comments on commit 8d22c1f

Please sign in to comment.