Switch branches/tags
Find file
Fetching contributors…
Cannot retrieve contributors at this time
763 lines (551 sloc) 22.3 KB
#! /usr/bin/python3
# -*- coding: utf-8 -*-
Continuous integration server, ghetto style
What it is
Ghetto-CI is a Python script in 145 statements fullfilling your dirty `continuous integration <>`_ needs.
`Source code (one file, only 145 statements) is on Github <>`_
and for your convenience the script is bundled in `VVV package on PyPi <>`_.
What it does
0. The script must be run on your server periodically
1. Runs svn update on a source folder (VCS backend easy to customize)
2. Checks if there are new commits since the last run
3. Run tests if the source code in fact was changed
4. See if the tests status since the last run has changed from success to failure or vice versa
5. Send email notifications to the team that now fecal matter impacts the rotary ventlidation device
Why to use
To improve the quality and cost effectiveness of your little software project,
you want to detect code changes breaking your project [automated tests].
You might want to do this
`without installing 48 MB of Java software <>`_ on your server.
On the other hand,
`very good SaaS oriented alternatives are tied to public Github repositories <>`_.
Homebrew shell scripts for tasks like this are nice, but no one wants to read or touch shell scripts
written by others.
*Ghetto-CI* script is mostly self-contained,
easy to understand, easy to hack into pieces, for your very own need.
It is a solution that *scales down*. Just toss it on a corner of a server
and it will churn happily and keep its owners proud.
*Ghetto-CI* does not you give fancy web interface, statistics, bling bling
or even a pony. However, it tells when someone breaks something and
it is time for team building via `blanket party <>`_.
.. note ::
As a prerequisitement you need a working Python 3 command
installed on your operating system with virtualenv package.
For detailed instructions see `VVV installation manual <>`_
.. note ::
If you don't feel eggy you can
just `grab the self-contained source file <>`_
as long as you have plac package also installed for your Python.
Create Python 3 virtualenv and run Ghetto-CI script using Python interpreter configured under this virtualenv::
# We install GhettoCI directly under our UNIX user home folder
cd ~
virtualenv -p python3 vvv-venv
source vvv-venv/bin/activate
pip install vvv
# ghetto-ci now lives in vvv-venv/bin/ghetto-ci
# Running the script, using the Python environment prepared
# to see that everything works (source command above
# has added this to PATH)
ghetto-ci -h
You need to prepare
* A software repository folder. This must be pre-checked out Subversion repository where
ghetto-ci can run ``svn up`` command.
* A command to execute unit tests and such. This command must return process exit code 0 on success.
If you don't bother writing tests, low end alternative is just
`lint and validate your source code <>`_.
* A file storing the test status. Ghetto-CI status file keeps track whether the last round
or tests succeeded or failed. You'll get email reports only when the test status changed -
there is little need to get "tests succeeded" email for every commit.
* Email server details to send out notifications. Gmail works perfectly if you
are in short of SMTP servers.
Example of a command for running continuous integration against ``/my/svn/repo`` checkout where
``bin/test`` command is used to run the unit tests::
# Will print output to console because email notification details are not given
ghetto-ci /my/svn/repo /tmp/ "cd /my/svn/repo && bin/test"
If the tests status have changed since the last run, or the running fails
due to internal error, the command outputs the result. Otherwise
command outputs nothing. Exit code 0 indicates that test succeeded.
Deployment on the server
.. note ::
On Windows you can accomplish this using any automator provided by your operating system vendor.
Createa a wrapper script which executes all the CI commands (TODO example).
Create a wrapper shell script which runs ghetto-ci command with 5 minute interval
# Run CI check for every 5 minutes
while true
sleep 300
Start script automatically on the server start-up (TODO).
You might to use ``-force -alwaysoutput`` arguments with test runs.
You can also evaluate against UNIX command `true` and `false` to e.g.
test email output::
ghetto-ci -force -alwaysoutput settings here... /repo /tmp/ true
Skype integration example
You can receive a build status message to `a Skype chat via Sevabot <>`_.
ghetto-ci -force -alwaysoutput \
-skypeurl "http://localhost:5000/zapier/87858ec5841cd97d127b642a81de1d20/secret/" \
src/sits-eggs \
/tmp/ \
'bin/test -s Products.SitsHospital -t test_new_protocol'
Complex usage example
Below is a real life example how you define one shell script, again triggered
by Cron job, to poll several SVN repositories which have different tests to run.
# Example ghetto-ci integration for Plone buildout and custom add-ons.
# Using dummy gmail account for outgoing messages.
# Install VVV under buildout
# Run this file in buildout main folder:
# cd ~/mybuildoutfolder
# src/my-repo/
# The list of persons who will receive resports of test status changes
# Ghetto-CI template command which is run against multiple repos / multiple test commands
# We use localhost 25 as the SMTP server -> assume your UNIX server has postfix
# or something configured... could be servers also here
GHETTOCI="vvv-venv/bin/ghetto-ci \
-smtpserver \
-smtpport 465 \
-smtpuser \
-smtppassword OMGITISFULLofKITT3NS \
-receivers \"$RECEIVERS\" \
-envelopefrom \"Continuous integration service <>\" \
# Note that SVN revision info is folding down in the folders
# so you can target tests to a specific SVN repo subfolder
# Note: eval needs to be used due to shell script quotation mark fuzz
# See that buildout completes (no changes in the external environment, like someone accidentally
# publishing broken packages on PyPI). We actually place buildout.cfg under this SVN repo
# and then just symlink it
eval $GHETTOCI src/my-repo 'bin/buildout'
# Run tests against hospital product
eval $GHETTOCI src/my-repo/Products.Hospital \"bin/test -s Products.Hospital\"
# Run tests against patient product
eval $GHETTOCI src/my-repo/Products.Patient \"bin/test -s Products.Patient\"
`plac rocks for command line parsing <>`_,
especially with Python 3.
The code is pylint valid - and beautiful, Pythonic.
Future tasks
To make this script even more awesome, the following can be considered
* Using some Python library as the abstraction layer over different
version control systems
* Using more intelligent Python library for the notifications:
have email, IRC and Skype notifications (How Skype bots are built nowadays?)
* Use a proper emailing library. I still believe it is easier to configure one GMail account for SMTP purposes instead of Postfix or Exim.
Also GMail nicely collects outgoing messages to log even if email delivery has temporary problems.
* I would be happy if someone told how to change *-smtpport* styles options to *--smtp-port* with plac
* I would be also happy if someone shows how to tame shell script quotation marks properly
* All tips to make Python source even more beautiful welcome
Source and credits
`Ghetto-CI lives on Github, in VVV project <>`_.
Mikko Ohtamaa, the original author (`blog <>`_, `Twitter <>`_)
Licensed under `WTFPL <>`_.
`Ghetto style? <>`_.
__author__ = "Mikko Ohtamaa <>"
__license__ = "WTFPL"
# pylint complains about abstract Python 3 members and ABCMeta, this will be fixed in the future
# pylint: disable=R0201, W0611
# Python imports
#from abc import ABCMeta, abstractmethod
from email.mime.text import MIMEText
import pickle
import os
import sys
import subprocess
import select
from smtplib import SMTP_SSL, SMTP
# Py3k
from urllib.parse import urlencode
from urllib.request import urlopen
except ImportError:
# Bearded imports
from urllib import urlencode
from urllib2 import urlopen
# Third party
import plac
#: Template used in email notifications
Last commit %(commit)s by %(author)s:
Test output:
def split_first(line, separator):
Split a string to (first part, remainder)
parts = line.split(separator)
return parts[0], separator.join(parts[1:])
def shell(cmdline, pipe_through=False):
Execute a shell command.
:param cmdline: Command line as you would type it in shell
:param pipe_through: Echo the command back to stdout / stderr
:returns: (exitcode, stdout / stderr output as string) tuple
p = subprocess.Popen(cmdline, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
output = []
while True:
reads = [p.stdout.fileno(), p.stderr.fileno()]
ret =, [], [])
for fd in ret[0]:
if fd == p.stdout.fileno():
read = p.stdout.readline()
if pipe_through:
if fd == p.stderr.fileno():
read = p.stderr.readline()
if pipe_through:
if p.poll() != None:
return (p.returncode, "".join(output))
class Status:
Stored pickled CI status of a test run.
Use Python pickling serialization for making status info persistent.
def __init__(self):
#: Set to True of False by automation,
# but we have a special value for the first run
# to get output always
self.test_success = "xxx"
self.last_commit_id = None
def read(cls, path):
Read status file.
Return fresh status if file does not exist.
if not os.path.exists(path):
# Status file do not exist, get default status
return Status()
f = open(path, "rb")
return pickle.load(f)
def write(cls, path, status):
Write status file
f = open(path, "wb")
pickle.dump(status, f)
class Repo(object):
Define interface for presenting one monitored software repository in ghetto-ci.
def __init__(self, path):
:param path: Abs FS path to monitored repository
self.path = path
def update(self):
""" Update repo from version control """
def get_last_commit_info(self):
Get the last commit status.
:return tuple(commit id, commiter, message, raw_output) or (None, None, None, raw_output) on error
return (None, None, None)
class SVNRepo(Repo):
""" Handle Subversion repository update and last commit info extraction """
def update(self):
Run repo update in a source folder
exitcode, output = shell("svn up %s" % self.path)
return exitcode == 0, output
def get_last_commit_info(self):
Get the last commit status.
# Get output from svn info
info, output = self.get_svn_info()
if not info:
return (None, None, None, output)
# Get output from svn log
log_success, author, log = self.get_svn_log()
if not log_success:
return (None, None, None, log)
return (info["Last Changed Rev"], author, log, output)
def get_svn_log(self):
Extract the last commit author and message.
:return: tuple (success, last commiter, output / last commit message)
exit_code, output = shell("svn log -l 1 %s" % self.path)
if exit_code != 0:
return (False, None, output)
# ------------------------------------------------------------------------
# r6101 | xxx | 2012-04-28 15:57:14 +0300 (Sat, 28 Apr 2012) | 1 line
lines = output.split("\n")
author_line = lines[1]
author = author_line.split("|")[1].strip()
return (True, author, output)
def get_svn_info(self):
Get svn info output parsed to dict
exit_code, output = shell("svn info %s" % self.path)
if exit_code != 0:
return None, output
data = {}
for line in output.split("\n"):
key, value = split_first(line, ":")
data[key] = value
return data, output
class EmailNotifier(object):
Intelligent spam being. Print messages to stdout and send emai if
SMTP server details are available.
def __init__(self, server, port, username, password, receivers, from_address, envelope_from):
:param server: SMTP server
:param port: SMTP port, autodetects SSL
:param username: SMTP credentials
:param password: SMTP password
:param receivers: String, comma separated list of receivers
:param from_email: Sender's email address
self.server = server
self.port = port
self.username = username
self.password = password
self.receivers = receivers
self.from_address = from_address
self.envelope_from = envelope_from
def send_email_notification(self, subject, output):
Further info:
:param receivers: list of email addresses
:param subject: Email subject
:param output: Email payload
if not self.receivers:
raise RuntimeError("Cannot send email - no receivers given")
if self.port == 465:
# SSL encryption from the start
smtp = SMTP_SSL(self.server, self.port)
# Plain-text SMTP, or opt-in to SSL using starttls() command
smtp = SMTP(self.server, self.port)
msg = MIMEText(output, "plain")
msg['Subject'] = subject
if self.envelope_from:
msg['From'] = self.envelope_from
msg['From'] = self.from_address
# Add visible receivers header
msg["To"] = self.receivers
# SMTP never works on the first attempt...
# SMTP authentication is optional
if self.username and self.password:
smtp.login(self.username, self.password)
# Convert comma-separated sl
receivers = [email.strip() for email in self.receivers.split(",")]
smtp.sendmail(self.from_address, receivers, msg.as_string())
def notify(self, subject, output):
Notify about the tests status.
if self.server:
self.send_email_notification(subject, output)
class PrintNotifier(object):
Print status to stdout
def print_notification(self, subject, output):
Dump the notification to stdout
print("-" * len(subject))
def notify(self, subject, output):
Notify about the tests status.
self.print_notification(subject, output)
class SkypeNotifier(object):
Sevabot based Skype notifications.
def __init__(self, url):
self.url = url
def send_skype_message(self, msg):
# Zapier hook format
payload = dict(data=msg)
# Construct full URL to sevabot zapier hook
post_data = urlencode(payload)
r = urlopen(self.url, post_data) # Will abort on non-HTTP 200 answer
def notify(self, subject, output):
Notify about the tests status.
Skype gets only the subject line.
if self.url:
def run_tests(test_command, pipethrough):
Run testing command.
Assume exit code = 0 -> test success
exitcode, output = shell(test_command, pipethrough)
return (exitcode == 0, output)
# Parsing command line with plac rocks
smtpserver=("SMTP server address for mail out. Required if you indent to use email notifications.", "option"),
smtpport=("SMTP server port for mail out", "option", None, int),
smtpuser=("SMTP server username", "option"),
smtppassword=("SMTP server password", "option"),
smtpfrom=("Notification email From address", "option"),
envelopefrom=("Verbose Name <> sender address in outgoing email", "option"),
receivers=("Notification email receives as comma separated string", "option"),
force=("Run tests regardless if there have been any repository updates", "flag"),
alwaysoutput=("Result notification regardless whether test status has changed since the last run", "flag"),
alwaysfailure=("Failed result notification regardless whether test status has changed since the last run", "flag"),
pipethrough=("Show test command stdout and stderr as it happens", "flag"),
skypeurl=("Send build status to Skype chat (Sevabot integration)", "option"),
repository=("Monitored source control repository (SVN)", "positional"),
statusfile=("Status file to hold CI history of tests", "positional"),
testcommand=("Command to run tests. Exit code 0 indicates test success", "positional"),
def main(
A simple continuous integration server.
Ghetto-CI will monitor the software repository.
Give a (Subversion) software repository and a test command run test against it.
Make this command run regularly e.g. using UNIX cron service.
You will get email notification when test command status changes from exit code 0.
For more information see
if not repository:
sys.exit("No repository argument given")
if not statusfile:
sys.exit("No status file argument given")
if not testcommand:
sys.exit("No test command argument given")
# Create notification backends
email_notifier = EmailNotifier(server=smtpserver, port=smtpport,
username=smtpuser, password=smtppassword, from_address=smtpfrom,
receivers=receivers, envelope_from=envelopefrom)
skype_notifier = SkypeNotifier(url=skypeurl)
print_notifier = PrintNotifier()
notifiers = [email_notifier, skype_notifier, print_notifier]
def notify(subject, msg):
for n in notifiers:
n.notify(subject, msg)
repo = SVNRepo(repository)
status =
success, output = repo.update()
# Handle repo update failure
if not success:
notify("Could not update repository: %s. Probably not valid SVN repo?\n" % repository, output)
return 1
commit_id, commit_author, commit_message, output = repo.get_last_commit_info()
# Handle repo info failure
if not commit_id:
notify("Could not get commit info: %s\n%s" % (repository, output), "Check svn info by hand")
return 1
# See if repository status has changed
if commit_id != status.last_commit_id or force:
test_success, output = run_tests(testcommand, pipethrough)
# No new commits, nothing to do
print("No changes in repo %s" % repository)
return 0
# Test run status have changed since last run
# .. or test failed and we report failures always
if ((test_success != status.test_success) or alwaysoutput) or (alwaysfailure and not test_success):
notification_body = NOTIFICATION_BODY_TEMPLATE % dict(commit=commit_id, author=commit_author,
commit_message=commit_message, test_output=output)
if test_success:
subject = "☀ Test succeed for %s" % testcommand
subject = "☂ Test fail for %s" % testcommand
notify(subject, notification_body)
# Update persistent test status
new_status = Status()
new_status.last_commit_id = commit_id
new_status.test_success = test_success
Status.write(statusfile, new_status)
if test_success:
return 0
return 1
def entry_point():
Enter the via entry_point declaration.
Handle UNIX style application exit values
import plac
exitcode =
if __name__ == "__main__":