Skip to content
This repository has been archived by the owner on Nov 14, 2018. It is now read-only.

Here's a first pass at using shielded #16

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.egg-info
*.pyc
env
local.env
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "shielded"]
path = shielded
url = git://github.com/cainus/shielded.git
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ clean:
find . -name \*.pyc -delete

run: env/bin/aspen
./env/bin/aspen --www_root=www --project_root=.
foreman run -e local.env ./env/bin/aspen --www_root=www --project_root=.
Binary file removed OpenSans-Bold.ttf
Binary file not shown.
Binary file removed OpenSans-Regular.ttf
Binary file not shown.
15 changes: 7 additions & 8 deletions badgr/colors.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
"""This module contains RGB color definitions for Badgr.co.
"""This module contains color definitions for Badgr.co, per shielded.
"""

DARK_GREY = (74,74,74)
LIGHT_GREY = (135,135,135)

RED = (179,0,0)
GREEN = (147,188,59)
YELLOW = (177,175,0)
GREEN = "green"
YELLOWGREEN = "yellowgreen"
YELLOW = "yellow"
RED = "red"
LIGHTGRAY = "lightgray"
all_colors = [GREEN, YELLOWGREEN, YELLOW, RED, LIGHTGRAY]
146 changes: 81 additions & 65 deletions badgr/services.py
Original file line number Diff line number Diff line change
@@ -1,90 +1,106 @@
"""This module contains service definions for Badgr.co.

Service classes should take three strings (first, second, bg) and set three
attributes (first, second, and bg).
Service functions should take three strings (first, second, color) and return
three strings (first, second, and color).

first - the first path part => the first word on the badge
second - the second path part => the second word on the badge
bg - an RGB color definition, the background behind the second word on the
badge
color - a color name, the background behind the second word on the badge

"""
import os
import time
from urllib import quote, urlopen

from badgr.colors import RED, YELLOW, GREEN, LIGHT_GREY
from aspen import json
from badgr.colors import RED, YELLOW, YELLOWGREEN, GREEN, LIGHTGRAY


def _test(first, second, color):
"""For testing.
"""
time.sleep(2)
if second == "random":
base = os.environ['CANONICAL_HOST']
url = base + "/random.txt"
second = urlopen(url).read().strip()
return first, second, color


def noop(first, second, color):
return first, second, color


def coveralls(first, second, color):
first = "coverage"
url = "https://coveralls.io/repos/%s/badge.png?branch=master"
fp = urlopen(url % second)
try:
# We get a redirect to an S3 URL.
score = fp.url.split('_')[1].split('.')[0]
second = score + '%'

as_number = int(score)
if as_number < 80:
color = RED
elif as_number < 90:
color = YELLOW
else:
color = GREEN
except (IndexError, ValueError):
second = 'n/a'
color = LIGHTGRAY


class Generic(object):

def __init__(self, first, second, bg):
self.first = first
self.second = second
self.bg = bg


class Coveralls(object):

def __init__(self, first, second, bg):
self.first = "coverage"
url = "https://coveralls.io/repos/%s/badge.png?branch=master"
fp = urlopen(url % second)
try:
# We get a redirect to an S3 URL.
score = fp.url.split('_')[1].split('.')[0]
self.second = score + '%'

as_number = int(score)
if as_number < 80:
self.bg = RED
elif as_number < 90:
self.bg = YELLOW
else:
self.bg = GREEN
except (IndexError, ValueError):
self.second = 'n/a'
self.bg = LIGHT_GREY
return first, second, color


class Gittip(object):
def gittip(first, second, color):
first = "tips"
fp = urlopen("https://www.gittip.com/%s/public.json" % second)
receiving = float(json.loads(fp.read())['receiving'])
second = "$%d / week" % receiving
if receiving == 0:
color = RED
elif receiving < 10:
color = YELLOW
elif receiving < 100:
color = YELLOWGREEN
else:
color = GREEN

def __init__(self, first, second, bg):
self.first = "tips"
fp = urlopen("https://www.gittip.com/%s/public.json" % second)
receiving = json.loads(fp.read())['receiving']
self.second = "$%d / week" % float(receiving)
self.bg = (42, 143, 121) # Gittip green! :)
return first, second, color


class TravisCI(object):
def travis_ci(first, second, color):
first = "build"

def __init__(self, first, second, bg):
self.first = "build"
url = 'https://api.travis-ci.org/repos?slug=%s' % quote(second)
fp = urlopen(url)
repos = json.loads(fp.read())
if repos:
status = repos[0].get('last_build_status', 'n/a')
else:
status = 'n/a'

url = 'https://api.travis-ci.org/repos?slug=%s' % quote(second)
fp = urlopen(url)
repos = json.loads(fp.read())
if repos:
status = repos[0].get('last_build_status', 'n/a')
else:
status = 'n/a'
second = { 0: 'passing'
, 1: 'failing'
, None: 'pending'
, 'n/a': 'n/a'
}.get(status, 'n/a')

self.second = { 0: 'passing'
, 1: 'failing'
, None: 'pending'
, 'n/a': 'n/a'
}.get(status, 'n/a')
color = { 'failing': RED
, 'passing': GREEN
, 'pending': YELLOW
}.get(second, LIGHTGRAY)

self.bg = { 'failing': RED
, 'passing': GREEN
, 'pending': YELLOW
}.get(self.second, LIGHT_GREY)
return first, second, color


services = {}
services['coveralls'] = Coveralls
services['gittip'] = Gittip
services['travis-ci'] = TravisCI
services['_test'] = _test
services['coveralls'] = coveralls
services['gittip'] = gittip
services['travis-ci'] = travis_ci

def get(first):
return services.get(first, Generic)
return services.get(first, noop)
46 changes: 46 additions & 0 deletions bin/pre_compile
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env bash

set -eo pipefail
indent()
{
sed -u 's/^/ /'
}
echo "=========================================="
echo
echo "1: $1"
echo "2: $2"
echo "cwd: $(pwd)"
echo
echo "=========================================="


# Install Node.js.
# ================
# Adapted from https://github.com/heroku/heroku-buildpack-nodejs.

echo "-----> Installing Node.js, version 0.10.15 ..."
mkdir -p vendor/node
cd vendor/node
NODEBALL=http://heroku-buildpack-nodejs.s3.amazonaws.com/nodejs-0.10.15.tgz
curl $NODEBALL | tar zx


# Install shielded.
# =================

echo "-----> Installing shielded ..."
cd $BUILD_DIR/shielded/
../vendor/node/bin/npm install | indent


# Set up environment variables.
# =============================
# This is done with a script in .profile.d/:
#
# https://devcenter.heroku.com/articles/profiled

cd $BUILD_DIR
mkdir -p .profile.d
cat << EOF > .profile.d/badgr-node.sh
export PATH=/app/vendor/node/bin:$PATH
EOF
29 changes: 29 additions & 0 deletions configure-aspen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import os
import time

from aspen import log


def time_request_inbound(request):
request._start = time.time()

def time_request_outbound(response):
start = response.request._start
end = time.time()
elapsed = (end - start) * 1000.0
log("Request served in %.3f ms." % elapsed)

website.hooks.inbound_early += [time_request_inbound]
website.hooks.outbound += [time_request_outbound]



# Up the threadpool size.
# =======================
# Yanked from Gittip. Should upstream this.

def up_minthreads(website):
website.network_engine.cheroot_server.requests.min = \
int(os.environ['ASPEN_THREAD_POOL'])

website.hooks.startup.insert(0, up_minthreads)
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
./vendor/tornado-1.2.1.tar.gz
./vendor/aspen-0.24.3.tar.bz2

./vendor/Pillow-2.0.0.zip
./vendor/hiredis-0.1.1.tar.gz
./vendor/redis-2.7.6.tar.gz
1 change: 0 additions & 1 deletion robots.txt

This file was deleted.

1 change: 1 addition & 0 deletions shielded
Submodule shielded added at a8030d
Binary file removed vendor/Pillow-2.0.0.zip
Binary file not shown.
Binary file added vendor/hiredis-0.1.1.tar.gz
Binary file not shown.
Binary file removed vendor/numpy-1.7.1.tar.gz
Binary file not shown.
Binary file added vendor/redis-2.7.6.tar.gz
Binary file not shown.
Loading