Skip to content

Commit

Permalink
Convert bouncer to an application (#36)
Browse files Browse the repository at this point in the history
Some of the upcoming changes include adding multiple config files (supervisor,
collectd), and it doesn't really make sense to keep bouncer as a Python
package anymore in my opinion.

This also means that we won't have specific versions anymore but have
the same approach to version numbers as h, namely the date and sha of
the most recent commit. This will also be reflected in the version
number that we report to Sentry.
  • Loading branch information
chdorner authored and nickstenning committed Feb 10, 2017
1 parent fe400bb commit c8eaeff
Show file tree
Hide file tree
Showing 17 changed files with 111 additions and 107 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bouncer/_version.py export-subst
9 changes: 5 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
*.egg-info
*.pyc
node_modules

.coverage
bouncer/static/scripts/bundle.js
.coverage.*
.cache
dist
.tox

node_modules
bouncer/static/scripts/bundle.js
9 changes: 3 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,14 @@ RUN addgroup -S bouncer \
WORKDIR /var/lib/bouncer

# Copy packaging
COPY README.rst package.json setup.* ./
COPY README.rst package.json requirements.txt ./

RUN npm install --production \
&& npm cache clean

COPY gunicorn.conf.py ./
RUN pip install --no-cache-dir -r requirements.txt

# Copy the rest of the application files
COPY bouncer ./bouncer/

RUN pip install --no-cache-dir -e .
COPY . .

# Persist the static directory.
VOLUME ["/var/lib/bouncer/bouncer/static"]
Expand Down
10 changes: 0 additions & 10 deletions MANIFEST.in

This file was deleted.

19 changes: 4 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
BUILD_ID := $(shell python -c 'f = open("bouncer/__about__.py"); exec(f.read()); print(__version__)')
DOCKER_TAG = dev

deps:
pip install --upgrade pip
pip install --upgrade wheel
pip install -e .[dev]
pip install --use-wheel -r requirements-dev.txt
npm install

dev:
Expand All @@ -16,22 +15,12 @@ test:
tox
./node_modules/karma/bin/karma start karma.config.js

.PHONY: dist
dist: dist/bouncer-$(BUILD_ID).tar.gz

dist/bouncer-$(BUILD_ID).tar.gz:
python setup.py sdist

dist/bouncer-$(BUILD_ID): dist/bouncer-$(BUILD_ID).tar.gz
tar -C dist -zxf $<

.PHONY: docker
docker: dist/bouncer-$(BUILD_ID)
docker build -t hypothesis/bouncer:$(DOCKER_TAG) $<
docker:
git archive HEAD | docker build -t hypothesis/bouncer:$(DOCKER_TAG) -

.PHONY: clean
clean:
find . -type f -name "*.py[co]" -delete
find . -type d -name "__pycache__" -delete
rm -f node_modules/.uptodate bouncer.egg-info/.uptodate
rm -rf dist
rm -f node_modules/.uptodate
6 changes: 0 additions & 6 deletions bouncer/__about__.py

This file was deleted.

5 changes: 5 additions & 0 deletions bouncer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

import pyramid.config

from bouncer._version import get_version

__all__ = ('__version__',)
__version__ = get_version()


def settings():
"""
Expand Down
72 changes: 72 additions & 0 deletions bouncer/_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# -*- coding: utf-8 -*-

import datetime
import subprocess
from subprocess import DEVNULL # Python 3

__all__ = ('get_version',)

# git-archive substitution markers. When this file is written out by a `git
# archive` command, these will be replaced by the short commit hash and the
# commit date, respectively.
VERSION_GIT_REF = '$Format:%h$'
VERSION_GIT_DATE = '$Format:%ct$'

# Fallback version in case we cannot derive the version.
VERSION_UNKNOWN = '0+unknown'


def fetch_git_ref():
ref = subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD'],
stderr=DEVNULL).strip()
return ref.decode('utf-8')


def fetch_git_date(ref):
ts = subprocess.check_output(['git', 'show', '-s', '--format=%ct', ref])
return datetime.datetime.fromtimestamp(int(ts))


def fetch_git_dirty():
dirty_tree = subprocess.call(['git', 'diff-files', '--quiet']) != 0
dirty_index = subprocess.call(['git', 'diff-index', '--quiet',
'--cached', 'HEAD']) != 0
return dirty_tree or dirty_index


def git_version():
ref = fetch_git_ref()
date = fetch_git_date(ref)
dirty = fetch_git_dirty()
return pep440_version(date, ref, dirty)


def git_archive_version():
ref = VERSION_GIT_REF
date = datetime.datetime.fromtimestamp(int(VERSION_GIT_DATE))
return pep440_version(date, ref)


def pep440_version(date, ref, dirty=False):
"""Build a PEP440-compliant version number from the passed information."""
return '{date}+g{ref}{dirty}'.format(date=date.strftime('%Y%m%d'),
ref=ref,
dirty='.dirty' if dirty else '')


def get_version():
"""Fetch the current application version."""
# First we try to retrieve the current application version from git.
try:
return git_version()
except (subprocess.CalledProcessError, FileNotFoundError):
pass

# We are not in a git checkout or extracting the version from git failed,
# so we attempt to read a version written into the header of this file by
# `git archive`.
if not VERSION_GIT_REF.startswith('$'):
return git_archive_version()

# If neither of these strategies work, we fall back to VERSION_UNKNOWN.
return VERSION_UNKNOWN
4 changes: 2 additions & 2 deletions bouncer/sentry.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from pyramid import tweens
import raven

from bouncer import __about__
from bouncer import __version__


def get_raven_client(request):
Expand All @@ -22,7 +22,7 @@ def get_raven_client(request):

def includeme(config):
config.registry["raven.client"] = raven.Client(
release=__about__.__version__)
release=__version__)

config.add_request_method(
get_raven_client,
Expand Down
7 changes: 7 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-r requirements.txt

coverage
mock
prospector
pytest
pytest-cov
9 changes: 9 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
certifi==2016.9.26
elasticsearch>=2.0.0,<3.0.0
gunicorn==19.4.5
pyramid==1.6.1
pyramid-jinja2==2.6.2
requests==2.12.4
requests-aws4auth==0.9
raven==5.10.2
statsd==3.2.1
7 changes: 0 additions & 7 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
[check-manifest]
ignore =
*/test
*/test/*
.*
node_modules

[pep257]
ignore = D202
explain = true
Expand Down
56 changes: 0 additions & 56 deletions setup.py

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 3 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

[tox]
envlist = py35
skipsdist = True

[pytest]
testpaths = tests
Expand All @@ -14,4 +15,5 @@ deps =
coverage
mock
pytest
commands = coverage run -m pytest {posargs:bouncer/}
-r{toxinidir}/requirements.txt
commands = coverage run --parallel --source bouncer,tests/bouncer -m pytest {posargs:tests/bouncer/}

0 comments on commit c8eaeff

Please sign in to comment.