Skip to content

Commit

Permalink
Cleanup cookiecutter bonanza
Browse files Browse the repository at this point in the history
Signed-off-by: Luiz Carvalho <lucarval@redhat.com>
  • Loading branch information
lcarva committed Mar 22, 2019
1 parent faee879 commit 15c4e3d
Show file tree
Hide file tree
Showing 19 changed files with 115 additions and 91 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,17 @@ venv.bak/
# Rope project settings
.ropeproject

# VS Code settings
.vscode

# mkdocs documentation
/site

# mypy
.mypy_cache/

# Flask
settings.cfg
settings.py

# Local application cache
/dest
11 changes: 4 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
all: run

clean:
rm -rf venv && rm -rf *.egg-info && rm -rf dist && rm -rf *.log*
rm -rf venv && rm -rf *.egg-info && rm -rf dist && rm -rf *.log* && rm -rf .tox

venv:
virtualenv --python=python3 venv && venv/bin/python setup.py develop

run: venv
FLASK_APP=cachito CACHITO_SETTINGS=../settings.cfg venv/bin/flask run
FLASK_APP=cachito/wsgi:app venv/bin/flask run

test: venv
CACHITO_SETTINGS=../settings.cfg venv/bin/python -m unittest discover -s tests

sdist: venv test
venv/bin/python setup.py sdist
test:
tox
45 changes: 10 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,50 +10,25 @@ Run the application:

And open it in the browser at [http://127.0.0.1:5000/](http://127.0.0.1:5000/)


## Prerequisites

This is built to be used with Python 3. Update `Makefile` to switch to Python 2 if needed.
This is built to be used with Python 3.

Some Flask dependencies are compiled during installation, so `gcc` and Python header files need to be present.
For example, on Ubuntu:

apt install build-essential python3-dev
For example, on Fedora:

dnf install gcc python3-devel

## Development environment and release process

- create virtualenv with Flask and cachito installed into it (latter is installed in
[develop mode](http://setuptools.readthedocs.io/en/latest/setuptools.html#development-mode) which allows
modifying source code directly without a need to re-install the app): `make venv`

- run development server in debug mode: `make run`; Flask will restart if source code is modified

- run tests: `make test` (see also: [Testing Flask Applications](http://flask.pocoo.org/docs/0.12/testing/))

- create source distribution: `make sdist` (will run tests first)

- to remove virtualenv and built distributions: `make clean`

- to add more python dependencies: add to `install_requires` in `setup.py`

- to modify configuration in development environment: edit file `settings.cfg`; this is a local configuration file
and it is *ignored* by Git - make sure to put a proper configuration file to a production environment when
deploying

- create virtualenv with Flask and cachito installed into it (latter is installed in
[develop mode](http://setuptools.readthedocs.io/en/latest/setuptools.html#development-mode) which allows
modifying source code directly without a need to re-install the app): `make venv`

## Deployment
- run development server in debug mode: `make run`; Flask will restart if source code is modified

If you are interested in an out-of-the-box deployment automation, check out accompanying
[`cookiecutter-flask-ansible`](https://github.com/candidtim/cookiecutter-flask-ansible).
- run tests: `make test` (see also: [Testing Flask Applications](http://flask.pocoo.org/docs/0.12/testing/))

Or, check out [Deploying with Fabric](http://flask.pocoo.org/docs/0.12/patterns/fabric/#fabric-deployment) on one of the
possible ways to automate the deployment.
- to remove virtualenv and built distributions: `make clean`

In either case, generally the idea is to build a package (`make sdist`), deliver it to a server (`scp ...`),
install it (`pip install cachito.tar.gz`), ensure that configuration file exists and
`CACHITO_SETTINGS` environment variable points to it, ensure that user has access to the
working directory to create and write log files in it, and finally run a
[WSGI container](http://flask.pocoo.org/docs/0.12/deploying/wsgi-standalone/) with the application.
And, most likely, it will also run behind a
[reverse proxy](http://flask.pocoo.org/docs/0.12/deploying/wsgi-standalone/#proxy-setups).
- to add more python dependencies: add to `install_requires` in `setup.py` and `requirements.txt`
4 changes: 2 additions & 2 deletions cachito-cli.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/bin/env python
# SPDX-License-Identifier: GPL-3.0-or-later

import logging
import os
import os.path
import sys
import tarfile

import docker
Expand All @@ -30,7 +30,7 @@ def download_remote(remote, dest_dir='dest'):
if remote['server'] == 'github.com':
url = 'https://github.com/{repo}/archive/{ref}.tar.gz'.format(**remote)
else:
raise NotImplemented('{server} is not supported'.format(**remote))
raise NotImplementedError('{server} is not supported'.format(**remote))
logging.debug('url is %s', url)

try:
Expand Down
17 changes: 0 additions & 17 deletions cachito/__init__.py
Original file line number Diff line number Diff line change
@@ -1,17 +0,0 @@
import os
from flask import Flask

app = Flask(__name__)
app.config.from_object('cachito.default_settings')
app.config.from_envvar('CACHITO_SETTINGS')

if not app.debug:
import logging
from logging.handlers import TimedRotatingFileHandler
# https://docs.python.org/3.6/library/logging.handlers.html#timedrotatingfilehandler
file_handler = TimedRotatingFileHandler(os.path.join(app.config['LOG_DIR'], 'cachito.log'), 'midnight')
file_handler.setLevel(logging.WARNING)
file_handler.setFormatter(logging.Formatter('<%(asctime)s> <%(levelname)s> %(message)s'))
app.logger.addHandler(file_handler)

import cachito.views
10 changes: 10 additions & 0 deletions cachito/api_v1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# SPDX-License-Identifier: GPL-3.0-or-later
from flask import Blueprint, jsonify


api_v1 = Blueprint('api_v1', __name__)


@api_v1.route('/ping', methods=['GET'])
def ping():
return jsonify(True)
16 changes: 16 additions & 0 deletions cachito/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# SPDX-License-Identifier: GPL-3.0-or-later
from flask import Flask
from cachito.splash import splash
from cachito.api_v1 import api_v1


# See app factory pattern:
# http://flask.pocoo.org/docs/0.12/patterns/appfactories/
def create_app(config_filename=None):
app = Flask(__name__)
if config_filename:
app.config.from_pyfile(config_filename)

app.register_blueprint(splash)
app.register_blueprint(api_v1, url_prefix='/api/v1')
return app
2 changes: 0 additions & 2 deletions cachito/default_settings.py

This file was deleted.

10 changes: 10 additions & 0 deletions cachito/splash.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# SPDX-License-Identifier: GPL-3.0-or-later
from flask import render_template, Blueprint


splash = Blueprint('splash', __name__, static_folder='static', template_folder='template')


@splash.route('/', methods=['GET'])
def index():
return render_template('index.html')
2 changes: 1 addition & 1 deletion cachito/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
<link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}">
</head>
<body>
Welcome to cachito
Welcome to cachito!
</body>
</html>
9 changes: 0 additions & 9 deletions cachito/views.py

This file was deleted.

5 changes: 5 additions & 0 deletions cachito/wsgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# SPDX-License-Identifier: GPL-3.0-or-later
from cachito.app import create_app


app = create_app()
3 changes: 3 additions & 0 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-rrequirements.txt

pytest
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# SPDX-License-Identifier: GPL-3.0-or-later
from setuptools import setup, find_packages

setup(
Expand Down
11 changes: 11 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# SPDX-License-Identifier: GPL-3.0-or-later
import pytest


from cachito.wsgi import create_app


@pytest.fixture(scope='session')
def client():
"""Return Flask application client for the pytest session."""
return create_app().test_client()
7 changes: 7 additions & 0 deletions tests/test_api_v1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# SPDX-License-Identifier: GPL-3.0-or-later
import json


def test_ping(client):
rv = client.get('/api/v1/ping')
assert json.loads(rv.data.decode('utf-8')) is True
17 changes: 0 additions & 17 deletions tests/test_cachito.py

This file was deleted.

6 changes: 6 additions & 0 deletions tests/test_splash.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# # SPDX-License-Identifier: GPL-3.0-or-later


def test_index(client):
rv = client.get('/')
assert 'Welcome to cachito' in rv.data.decode('utf-8')
25 changes: 25 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[tox]
envlist = lint,py36

[testenv]
deps =
-rdev-requirements.txt
whitelist_externals =
make
setenv =
CACHITO_SETTINGS={toxinidir}/settings.cfg
commands =
py.test -vvv tests/ {posargs}

[testenv:lint]
deps =
flake8
commands =
python -m flake8 {posargs}

[flake8]
show-source = True
max-line-length = 100
exclude = venv,.git,.tox,dist,*egg
# W504 line break after binary operator
ignore = W504

0 comments on commit 15c4e3d

Please sign in to comment.