Python’s Virtualenv working with Jenkins

If you use virtualenv to isolate your python project’s environment, and want your code tested automatically — read on, else ignore.

virtualenv isolates your project’s python environment

virtualenv makes sure you lock down your project’s main directory and all subdirectories of it. This ‘lockdown’ means that you never touches your global python binary, or any globally installed libraries (like “sudo pip install ipython” ).

Once locked down, you install all packages again, even those you have globally installed. This enables you to have one version of flask globally installed, but another version in your project. All dependencies can be listed in a separate file and validate a precise environment for you to work with. Tightly controlled dependencies is key to a deployment without surprises.

Jenkins checks the health of your project for each change

Jenkins is a CI server which means it does a lot of repeating stuff so you can focus on doing more important stuff. More specifically, it listens for changes to your project’s version control system (like git).

When changes are detected, the project is built and the test suite is executed. If any step fails, the CI server tells you that it did.

Setup Jenkins, and make it use virtualenv

Jenkins needs some massaging before it handles the hijacked environment of virtualenv. This is how I did it for my local git repository:

  • Download and install Jenkins
  • Start it, it should be up on http://localhost:8080
  • Install the Git Plugin
  • Setup a new project with these properties:
    • Source Code Management: add the URI to your local repository,
    • /Users/you/Sites/asdf in my case. Make sure the jenkins user can read this directory, otherwise the Jenkins GUI will tell you something random about invalid git repo, without a hint about a permissions error.
    • Build Triggers: Poll SCM (with an interval like 0 * * * *). This is needed because
      you’re too lazy to build manually; and
      you can not trigger builds with a git post-commit hook otherwise
    • Build > Execute shell. I’ve used two steps, one for setting up the environment and one for the actual tests:

# Setup a proper path, I call my virtualenv dir "venv" and
# I've got the virtualenv command installed in /usr/local/bin
PATH=$WORKSPACE/venv/bin:/usr/local/bin:$PATH
if [ ! -d "venv" ]; then
        virtualenv venv
fi
. venv/bin/activate
pip install -r requirements.txt --download-cache=/tmp/$JOB_NAME

and


. venv/bin/activate
which python

# reply should be
/home/user/.virtualenv/bin/python

Smart Doorbell using Raspberry Pi

About a year ago, I had to do a project titled Smart Home Automation using a Raspberry Pi and Arduino, part of the project  incorporated a doorbell but not just any doorbell but a Smart doorbell.

Why do I call it a Smart Doorbell?

A classic doorbell can be defined as a signalling device typically placed near an entry door to a building. When a visitor presses a button the bell rings inside the building, alerting the occupant to the presence of the visitor.

The smart doorbell, works in that manner however when a visitor presses the doorbell button it notifies the occupant with an images of the visitor and also sends an email, sms and push notification (to an smart device) to the occupant in case they are not able to hear the bell ringing.

If the button is pressed a pre-recorded voice notification is played for the occupant to check the door as there might be someone, this happens concurrently with an sms notification while a picture of the visitor is captured and sent to both email and push notification. Initially there was a feature to enable 2-way real-time video communication but due to network latency and high resource usage the feature was deprecated.

Click For A Demo

Comment if you need the code

See code snippets(Initial Revision): https://mmphego.wordpress.com/2015/01/11/smart-doorbell-using-rpi-with-voice-and-email-notification/

Sublime Text useful python snippets

For the past few months, I have been trying to move from using Geany to using Sublime Text as my primary text editor.

I find myself repeating some repetitive operations dozen times a day when coding in python, such as import IPython;IPython.embed() and etc.

So here are some of my Sublime Text snippets to enhance my productivity, and simplify my life.

Sublime Text 3 makes it super easy to create a snippet. You simply select Tools > Developer > New Snippet from the toolbar menu and you’re off to the races!

1. IPython debugger

Type ipyt and press tab to automatically insert IPython debugger under current pointer.

<snippet>
    <content>
        <![CDATA[import IPython;IPython.embed()]]>
    </content>
    <tabTrigger>ipython</tabTrigger>
    <scope>source.python</scope>
    <description>Interactive IPython debugger</description>
</snippet>

2. Docstring

Type docstring and press tab to automatically insert a numpy-style valid
docstring pattern right after your function definition

<snippet>
    <content><![CDATA[
"""${1:One liner description}
Parameters
----------
${2}
Returns
-------
${3}
"""
]]></content>
    <tabTrigger>docstring</tabTrigger>
    <scope>source.python</scope>
    <description>Adds a docstring skeleton to function</description>
</snippet>

and save your snippets with the .sublime-snippet file extension in /home/$USER/.config/sublime-text-3/Packages/User

Bachelor’s project demo

So after completing my diploma, and it becoming redundant in the working industry. I had to set my eyes on a Bachelor’s degree.
Trust me it was not easy, in the sense of being a part-time student, full-time employee, a father, a fiance and being stuck on some island for 15 months then when you come back you’re residing in Cape Town and the university in Pretoria.
I think I can write a book with my life’s story.

Long story short, I managed to complete my degree within 3.5 years instead of 2 – which was a bonus, And now I have set my eyes on a Master’s degree just to challenge myself one thing for sure, one doesn’t need to be stagnant in life.
I believe in continuous improvement of oneself.

Let me stop yapping and show you a demo of my Bachelor’s project

If you like give thumbs up.

Raspberry Pi – using GPIO / WiringPi without root (sudo) access

Dissection by David blog

Just needing to get this down quickly and succinctly…

The Raspberry Pi is super-fun for me as it’s a full OS (Linux, which I really like a lot) with Pins!  Current project: make a (private) website that will let me open/close and check my garage door.  Maybe more on this later.

SUPER frustrating is the inability for a web-server to control the GPIO pins on my Raspberry Pi because the web-server is not running as root.  Really?!?

View original post 336 more words

The Zen of Python, by Tim Peters

While browsing around the Interweb I found a little python poem – Words to-live-by in this world of Pythoning/Pythonian/Pythonization/Pythology or whatever its called.

>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren’t special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one– and preferably only one –obvious way to do it.
Although that way may not be obvious at first unless you’re Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it’s a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea — let’s do more of those!

Smart Doorbell using RPi with voice and email notification.

Part of my project involves having a doorbell, I figured a simple doorbell whereby a visitor just presses a button and then it rings/ notify whoever is it the house, That’s not really creative in my point of view.

So I then decided why not have a doorbell that not only rings when the button is pressed but also tell you someone is at the door and then sends you an email notification just in case you might be away(Not able to hear the bell.) as well as log the time that the button was pressed.

IDEA: It would be really cool if I had a USB Webcam that would take a picture of the person visiting and emails it.

first things first, we’ll need to create the wav file ie TextToSpeech. Using this simple tool below.

pico2wave -w DoorNotify.wav "Someone is ringing the doorbell. Please Go Check"

Next, create a python script that will watch the state of GPIO pin 18 on the RPi and take some action when the doorbell brings it to ‘HIGH’.

#!/usr/bin/env python2.7
__author__ = "Mpho Mphego"
__version__ = "$Revision: 1.1 $"
__description__ = "Smart Doorbell Notifier with voice and email notification : Interrupt driven"
__date__ = "$Date: 2015/01/11 02:23 $"
__copyright__ = "Copyright (c) 2015 Mpho Mphego"
__url__ = "mmphego.wordpress.com"
__license__ = "Python"

import RPi.GPIO as GPIO
import time
import os

# Connect to Relay which will control the 12V Buzzer.
led = 17 #GPIO0
button = 18 #GPIO1

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(led, GPIO.OUT)
time.sleep(0.1)
GPIO.output(led, False)

# GPIO 1 set up as inputs, pulled up to avoid false detection.
# Both ports are wired to connect to GND on button press.
# So we'll be setting up falling edge detection for both
GPIO.setup(button, GPIO.IN, pull_up_down=GPIO.PUD_UP)

# define callback functions
# this will run when an event are detected
def buttonHandler(channel):
    print "falling edge detected on 18"
    GPIO.output(led, True)
    time.sleep(1)
    GPIO.output(led, False)
    os.system("aplay /home/pi/Scripts/Smart_DoorBell/DoorNotify.wav")			
    os.system("python /home/pi/Scripts/Smart_DoorBell/DoorBellLogger.py")
    time.sleep(1)
    GPIO.output(led, True)
    time.sleep(1)
    GPIO.output(led, False)

# when a falling edge is detected on port 1, regardless of whatever 
# else is happening in the program, the function buttonHandler will be run
GPIO.add_event_detect(button, GPIO.FALLING, callback=buttonHandler, bouncetime=5000)

try:
    print "Waiting for button to be pressed"
    while True:
        # To ease the CPU Usage have a 10s delay on while loop.
        time.sleep(10)
        continue
except:
    GPIO.cleanup()       # clean up GPIO on CTRL+C exit
GPIO.cleanup()           # clean up GPIO on normal exit
print "Clean up by resetting all GPIO"

Now for the fun part, the email notification and data log.


#!/usr/bin/env python
__author__ = "Mpho Mphego"
__description__ = "Doorbell notifier time logger and email notifier."
__version__ = "$Revision: 1.0 $"
__date__ = "$Date: 2015/01/10 02:09 $"
__copyright__ = "Copyright (c) 2015 Mpho Mphego"
__license__ = "Python"

import time
import datetime
import os
import smtplib
from email.mime.text import MIMEText

#-----------------------Data Logger-----------------
f=open('/home/pi/Logs/DoorBell_Data_Log.txt','a')
now = datetime.datetime.now()
timestamp = now.strftime("%H:%M on %Y/%m/%d")
outstring1 = " Someone was at the door at " + str(timestamp) 
outstring2 = "\n********************************************* \n "
outstring = outstring1 + outstring2
f.write(outstring)
f.close()

# -----------------------Email Notifier----------------------
# Replace with your details.
USERNAME = "*******@gmail.com"
PASSWORD = "*********"
MAILTO  = "******@gmail.com"

if os.system("ping -c 1 www.google.com >> /dev/null 2&>1") == 0 :
	print "Sending Email Notification"
	msg = MIMEText('Someone is ringing the doorbell! \nGo Check!')
	msg['Subject'] = 'Doorbell notification!'
	msg['From'] = USERNAME
	msg['To'] = MAILTO

	server = smtplib.SMTP('smtp.gmail.com:587')
	server.ehlo_or_helo_if_needed()
	server.starttls()
	server.ehlo_or_helo_if_needed()
	server.login(USERNAME,PASSWORD)
	server.sendmail(USERNAME, MAILTO, msg.as_string())
	server.quit()
else:
	print "Unable to connect to Internet."

To have this run upon booting, we need to edit /etc/rc.local (as root since this is the owner).

sudo nano /etc/rc.local

At the bottom, just above exit 0 we’ll add a call to our script.

python /home/pi/Scripts/Smart_DoorBell/WaitDoorbell.py