commit 947594e76c0c0c31f44d03caee83ead4d49d76c9 Author: Felipe Martín Date: Mon May 20 13:01:24 2013 +0200 Initial commit: Base files and folder structure diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1ff6ee0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.tox/ +*.pyc +/virtualenv diff --git a/MANIFEST b/MANIFEST new file mode 100644 index 0000000..6e595b8 --- /dev/null +++ b/MANIFEST @@ -0,0 +1,4 @@ +# file GENERATED by distutils, do NOT edit +README +setup.py +minecraftcodex/__init__.py diff --git a/README b/README new file mode 100644 index 0000000..5d3fcb5 --- /dev/null +++ b/README @@ -0,0 +1,4 @@ +Minecraft Codex +=============== + +A database helper for Minecraft players. diff --git a/config/development/local_settings.py b/config/development/local_settings.py new file mode 100644 index 0000000..e69de29 diff --git a/config/development/requirements.pip b/config/development/requirements.pip new file mode 100644 index 0000000..487f151 --- /dev/null +++ b/config/development/requirements.pip @@ -0,0 +1 @@ +-r ../requirements.pip diff --git a/config/production/deploy.py b/config/production/deploy.py new file mode 100644 index 0000000..b36decf --- /dev/null +++ b/config/production/deploy.py @@ -0,0 +1,288 @@ +# Libs +import sys +import os +from subprocess import call, Popen, PIPE +from shutil import copytree + +# Config +APPS_PATH = './app' +CONFIGS_PATH = './config' +APP_DIRECTORY = '%s/minecraftcodex' % APPS_PATH + +GIT_REPOSITORY = 'git@bitbucket.org:fmartingr/minecraftcodex.git' +GIT_BRANCH = 'master' +GIT_DOWNLOAD_DIR = './src' +GIT_APP_PATH = 'minecraftcodex' +GIT_PATH_THIS = '%s/config/production/deploy.py' % GIT_DOWNLOAD_DIR + +VIRTUALENV_PATH = './virtualenv' + +CHECK_SCRIPT_UPDATE = False +THIS_NAME = 'deploy.py' + +PYTHON = { + 'py': 'python-2.7', + 'pip': 'pip-2.7', + 'virtualenv': 'virtualenv-2.7' +} + +PIP_REQUIREMENTS_FILES = [ + #'%s/config/requirements.pip' % GIT_DOWNLOAD_DIR, + '%s/config/production/requirements.pip' % GIT_DOWNLOAD_DIR +] + +CONFIG_FILES = [ + ( + '%s/config/development/local_settings.py' % GIT_DOWNLOAD_DIR, + '%s/local_settings.py' % CONFIGS_PATH + ), +] + +FIXTURES = [ + #'%s/config/development/initial_data.json' % GIT_DOWNLOAD_DIR +] + +REQUIREMENTS = [ + 'git', +] + +ENVIRONMENT_VARIABLES = [ + 'DATABASE_URL' +] + +CONTINUE = True + + +# Functions +class Colors: + END = '\033[0m' + + def __init__(self, c='ter,'): + if c == 'custom': + self.custom() + else: + self.term() + + def custom(self): + self.WHITE = '\033[89m' + self.BLACK = '\033[90m' + self.RED = '\033[91m' + self.GREEN = '\033[92m' + self.YELLOW = '\033[93m' + self.BLUE = '\033[94m' + self.PURPLE = '\033[95m' + self.CYAN = '\033[96m' + + def term(self): + self.WHITE = '\033[29m' + self.BLACK = '\033[30m' + self.RED = '\033[31m' + self.GREEN = '\033[32m' + self.YELLOW = '\033[33m' + self.BLUE = '\033[34m' + self.PURPLE = '\033[35m' + self.CYAN = '\033[36m' + +colors = Colors('custom') + + +def echo(string, end='\r\n', color=None): + if color: + sys.stdout.write("%s%s%s" % ( + color, + string, + colors.END + )) + else: + sys.stdout.write(string) + if end: + sys.stdout.write("\n") + sys.stdout.flush() + + +# Shortcurts +def check_status(status=None, words=['done', 'failed']): + if status is not None: + if status == 0: + echo(words[0], color=colors.GREEN) + else: + echo(words[1], color=colors.RED) + pass + #exit(-1) + + +def exists(path): + return os.path.exists(path) + + +def title(string): + print("") + echo("[==] %s" % string, color=colors.PURPLE) + + +def info(string): + echo("[ i] %s" % string, color=colors.BLUE) + + +def error(string): + echo("[ E] %s" % string, color=colors.RED) + + +def success(string): + echo("[OK] %s" % string, color=colors.GREEN) + + +def sub(string, end=''): + echo(" %s " % string, end=end) + + +# Checking if there's an active virtualenv +if 'VIRTUAL_ENV' in os.environ: + error('Active virtualenv detected. Deactivate it first.') + exit(-2) + +os.system('clear') +info("Running from %s" % os.getcwd()) + + +# =============== CHECK REQUIREMENTS =============== +title('Checking requirements') +for req in REQUIREMENTS: + sub('%s:' % req) + status = call(["which", req], stdout=open(os.devnull, 'wb')) + check_status(status, words=['present', 'not present']) + if status != 0: + CONTINUE = False + +# ========== CHECK ENVIRONMENT VARIABLES ========== +title('Checking environment variables') +for env in ENVIRONMENT_VARIABLES: + sub('%s:' % env) + if env in os.environ: + status = 0 + else: + CONTINUE = False + status = -1 + check_status(status, words=['present', 'not present']) + +# Prequisites check +if not CONTINUE: + print("") + error('Prequisites not met. Abort.') + #error('You should CTRL+C now, errors may occur!') + exit(1) + +# ================== GIT DOWNLOAD ================== +title('Getting last source code') +if not exists(GIT_DOWNLOAD_DIR): + os.mkdir(GIT_DOWNLOAD_DIR) + sub('git clone:') + status = call(['git', 'clone', '-b', GIT_BRANCH, GIT_REPOSITORY, GIT_DOWNLOAD_DIR], + stdout=open(os.devnull, 'wb'), + stderr=open(os.devnull, 'wb')) +else: + sub('git pull:') + status = call(['git', 'pull'], cwd=GIT_DOWNLOAD_DIR, + stdout=open(os.devnull, 'wb'), + stderr=open(os.devnull, 'wb')) + +check_status(status) + +# =========== CHECK SCRIPT UPDATE ================== +if CHECK_SCRIPT_UPDATE: + title('Checking if deploy script is updated on repository') + actual_size = os.stat('./%s.py' % THIS_NAME).st_size + repository_size = os.stat(GIT_PATH_THIS).st_size + if repository_size != actual_size: + sub('Script is updated.', end='\r\n') + + sub('Deleting old script:') + status = call(['rm', './%s.py' % THIS_NAME], stdout=open(os.devnull, 'wb')) + check_status(status) + + sub('Installing new version:') + status = call(['cp', GIT_PATH_THIS, './%s.py' % THIS_NAME], + stdout=open(os.devnull, 'wb')) + check_status(status) + info('Restarting execution!') + os.system('%s ./%s.py' % (PYTHON['py'], THIS_NAME)) + exit(0) + else: + sub('Script is up-to-date.', end='\r\n') + +# =========== VIRTUALENV ========================= +title('Checking and updating virtualenv') +if not exists(VIRTUALENV_PATH): + sub('Creating virtualenv:') + status = call([PYTHON['virtualenv'], '-q', '--distribute', VIRTUALENV_PATH], + stdout=open(os.devnull, 'wb')) + check_status(status) +else: + sub('Virtualenv exists.', end='\r\n') + +title('Updating requirements') +for requirements in PIP_REQUIREMENTS_FILES: + sub('From %s:' % requirements) + if exists(requirements): + if os.stat(requirements).st_size > 0: + status = call( + 'source %s/bin/activate && pip install -r %s' % ( + VIRTUALENV_PATH, + requirements, + ), + stdout=open(os.devnull, 'wb'), + shell=True, executable='/bin/bash' + ) + check_status(status) + else: + echo('empty', color=colors.YELLOW) + else: + echo('not exist', color=colors.RED) + +# ================== APP INSTALL ================== +title('Installing the app') +sub('Removing old data (if any):') +if exists(APP_DIRECTORY): + status = call(['rm', '-rf', APP_DIRECTORY], + stdout=open(os.devnull, 'wb')) +check_status(status) + +if not exists(APPS_PATH): + call(['mkdir', 'app']) + +sub('Copy source to application dir:') +git_path = "%s/%s" % (GIT_DOWNLOAD_DIR, GIT_APP_PATH) +app_path = "%s" % (APP_DIRECTORY) +try: + copytree(git_path, app_path) + echo('done', color=colors.GREEN) +except Exception as error: + echo(error, color=colors.RED) +#status = call(['cp', '-r', git_path, app_path], +# stdout=open(os.devnull, 'wb')) +#check_status(status) + +title('Installing config files') +if not exists(CONFIGS_PATH): + call(['mkdir', CONFIGS_PATH]) + call(['touch', "%s/__init__.py" % CONFIGS_PATH]) + +for from_file, to_file in CONFIG_FILES: + sub('%s:' % os.path.basename(from_file)) + if exists(from_file): + if os.stat(from_file).st_size > 0: + status = call(['cp', from_file, to_file], + stdout=open(os.devnull, 'wb')) + check_status(status, words=[os.path.basename(to_file), 'error!']) + else: + echo('empty', color=colors.YELLOW) + else: + echo('not exists', color=colors.RED) + +# ============ DATABASE MIGRATIONS ============== +title('Database migrations') +# Syncdb +sub('Not ready yet!') + +print("") +success('Finished!') diff --git a/config/production/local_settings.py b/config/production/local_settings.py new file mode 100644 index 0000000..e69de29 diff --git a/config/production/production.pip b/config/production/production.pip new file mode 100644 index 0000000..487f151 --- /dev/null +++ b/config/production/production.pip @@ -0,0 +1 @@ +-r ../requirements.pip diff --git a/config/requirements.pip b/config/requirements.pip new file mode 100644 index 0000000..2e5a716 --- /dev/null +++ b/config/requirements.pip @@ -0,0 +1,2 @@ +Django==1.5.1 +jingo==0.6.1 diff --git a/fabfile.py b/fabfile.py new file mode 100644 index 0000000..479a034 --- /dev/null +++ b/fabfile.py @@ -0,0 +1,168 @@ +## +# This fabric script only add automation to tasks for +# local development, is not intended to be used for +# deploys or stuff. +## + +# IMPORTS +from fabric.api import local, env, prefix, execute +from fabric.context_managers import settings, hide +from os.path import exists +from os import environ, stat +from fabric.colors import yellow, red, green +from subprocess import PIPE, Popen +import sys + +# Fabric env +env.hosts = ['localhost'] +env.activate = 'source virtualenv/bin/activate' +env.python = None + +# Development data +env.development_ddbb = 'dev.sqlite3' + +# Configuration +#python_version = "3.3.1" +python_version = "2.7.4" + + +# Functions +def echo(string, end='\r\n'): + sys.stdout.write(string) + if end: + sys.stdout.write("\n") + sys.stdout.flush() + + +def active_virtualenv(): + result = 'VIRTUAL_ENV' in environ + if result: + print red("Active virtualenv detected! You must deactivate it first.") + exit(-1) + return result + + +def python(): + if env.python is None: + print(yellow('Looking for python %s ...' % python_version)) + + # System python + echo(yellow('- System python: '), end='') + active = Popen('python --version', + stdout=PIPE, + stderr=PIPE, + shell=True) + version = active.communicate()[1].split()[1] + if version == python_version: + print(green('found!')) + env.python = 'python' + return(env.python) + else: + print(red(version)) + + # Alternate install + echo(yellow('- Alternate install: '), end='') + active = Popen('python3 --version', + stdout=PIPE, + stderr=PIPE, + shell=True) + version = active.communicate()[1].split()[1] + if version == python_version: + print(green('found!')) + env.python = 'python3' + return(env.python) + else: + if active.returncode == 0: + print(red(version)) + else: + print(red('not found')) + + # PythonZ + echo(yellow('- PythonZ: '), end='') + active = Popen('$HOME/.pythonz/pythons/CPython-3.3.1/bin/python3 --version', + stdout=PIPE, + stderr=PIPE, + shell=True) + output = active.communicate() + version = output[1].split()[1] + if version == python_version: + print(green('found!')) + env.python = "~/.pythonz/pythons/CPython-3.3.1/bin/python3" + return(env.python) + else: + if active.returncode == 0: + print(red(version)) + else: + print(red('not found')) + + # Anything found? D: + print(red('Python %s was not found!' % python_version)) + exit(-1) + else: + return env.python + + +# Virtualenv +def create_virtualenv(): + if not exists('./virtualenv'): + print(yellow('Making virtualenv')) + local("virtualenv -q --distribute -p %s ./virtualenv" % python()) + else: + print(green('Virtualenv already exists.')) + + +# Requirements +def install_requirements(): + if not active_virtualenv(): + with prefix(env.activate): + if stat('./config/development/requirements.pip').st_size > 0: + print(yellow('Installing development requirements')) + local('pip install -q -r ./config/development/requirements.pip') + + +def install(package): + "Install packages on the environment using fab install:" + if not active_virtualenv(): + with prefix(env.activate): + local('pip install ' + package) + + +def requirements(environ='global'): + if not active_virtualenv(): + with prefix(env.activate): + local('pip freeze') + + +# Easy-mode +def prepare(): + execute(create_virtualenv) + execute(install_requirements) + + +def p(): + execute(prepare) + + +def run(): + if not active_virtualenv(): + with prefix(env.activate): + environ['BACKEND_SETTINGS'] = 'config.development.backend_settings' + local('python app.py') + + +def test(): + if not active_virtualenv(): + with prefix(env.activate): + local('python -m unittest discover') + with settings(hide('warnings', 'running', 'stdout', 'stderr'), + warn_only=True): + local('find . -type d -name __pycache__ -exec rm -rf {} \;', + capture=True) + + +def tox(): + local('tox') + with settings(hide('warnings', 'running', 'stdout', 'stderr'), + warn_only=True): + local('find . -type d -name __pycache__ -exec rm -rf {} \;', + capture=True) diff --git a/minecraftcodex/__init__.py b/minecraftcodex/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..3d59ec6 --- /dev/null +++ b/setup.py @@ -0,0 +1,10 @@ +#!/usr/bin/env python +from distutils.core import setup + +setup(name='minecraftcodex', + version='0.0.0', + author='Felipe Martin', + author_email='fmartingr@me.com', + url='http://www.minecraftcodex.com', + packages=['minecraftcodex'] + ) diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000..8d7e11c --- /dev/null +++ b/tox.ini @@ -0,0 +1,9 @@ +[tox] +envlist = py27, py33 + +[testenv] +commands = {envpython} -m unittest discover + +deps = + Django==1.5.1 + jingo==0.6.1