Packaging python script to Debian follows strict instructions, using the following instructions, most of the steps can be skipped hence making it easy and fast.
If you like video go to https://www.youtube.com/watch?v=a9GzDZB5VeU. This video doesn't show setup.py file creating which is required, but excluding that portion everything is same as below.
Lets convert this mymood.py into deb package.
import time
def analyze():
try:
print '\nHello! I am Dr.Psycho, your mood analyzer:)'
print 'Answer me:'
time.sleep(2)
answer_list = []
def validate_ans(ans):
try:
ans = int(ans)
print ans
if ans not in [1, 2]:
print 'Enter 1 or 2\n'
return None
elif ans == 1:
return ans
elif ans == 2:
return 0
except Exception:
print 'Enter 1 or 2\n'
return None
print '\nHow many questions can you answer?'
print '1. More than 20\n2. Less than 10'
ans = raw_input()
while True:
ans = validate_ans(ans)
if ans != None:
break
ans = raw_input()
answer_list.append(ans)
print '\nYou are the worst thing ever existed?'
print '1. Nope\n2. Kind of'
ans = raw_input()
while True:
ans = validate_ans(ans)
if ans != None:
break
ans = raw_input()
answer_list.append(ans)
print '\nAre you searching for meaning of life?'
print '1. I dont\n2. Yes, I will find it some day'
ans = raw_input()
while True:
ans = validate_ans(ans)
if ans != None:
break
ans = raw_input()
answer_list.append(ans)
print '\nLast question. Do you feel happy if everyone died except you?'
print '1. Not at all \n2. Oh yes'
ans = raw_input()
while True:
ans = validate_ans(ans)
if ans != None:
break
ans = raw_input()
answer_list.append(ans)
print '\nHow does this yet another question feel?'
print '1. smiling\n2. *******\n'
ans = raw_input()
while True:
ans = validate_ans(ans)
if ans != None:
break
ans = raw_input()
answer_list.append(ans)
score = sum(answer_list)
if score == 0:
mood_status = 'You are totally messed up, Better consult psychiatrist'
elif score == 1:
mood_status = 'Very frustrated'
elif score == 3:
mood_status = 'Bored and depressed'
elif score in [3, 4]:
mood_status = 'Normal'
elif score == 5:
mood_status = 'Enlightened'
print '\nDr. Psycho thanks for your patience. \nYour mood: %s' % mood_status
except KeyboardInterrupt:
print '\nBye :('
Project Structure:
mymood
debian
changelog
compat
control
rules
copyright
mymood
__init__.py
mymood.py
command_line.py
README.md
setup.py
Don't create the above structure, it will be created as you go through below steps.
1. Creating mymood and its sub directories:
$mkdir mymood
$cd mymood
$mkdir debian
$mkdir mymood
2. Setting up mymood/mymood directory:
Goto mymood/mymood directory and paste mymood.py script as shown in project structure.
Within mymood/mymood create __init__.py
$touch __init__.py
Within mymood/mymood directory create command_line.py. setup.py needs this to run script from bash just by typing mymood.
$vim command_line.py
copy the following into command_line.py
from mymood import analyze
def main():
analyze()
3. Python's setup.py file creation:
Create a setup.py file in mymood directory as shown in project structure above.
The below code is simple and minimal setup.py for mymood, copy and change as you require.
from setuptools import setup
setup(name='mymood',
version='0.1',
description='your mood analyzer',
author='mp',
author_email='[email protected]',
license='MIT',
packages=['mymood'],
entry_points = {
'console_scripts': ['mymood=mymood.command_line:main'],
},
zip_safe=False)
Console scripts is used to run the script from terminal. So from bash you can just type mymood and run the script.
4. Setting up debian directory
$mkdir debian
Devscripts package is needed to create changelog file.
$cd debian
$sudo apt-get install devscripts
$dch --create
Now edit the file as follows:
mymood (1.0) UNRELEASED; urgency=medium
* Initial release. (Closes: #XXXXXX)
-- druuu <[email protected]> Wed, 10 Feb 2016 15:33:03 +0530
5. Create copyright, compat, control and rules files in debian directory.
$touch compat
$touch copyright
$touch control
$touch rules
the control file looks like:
Source: mymood
Section: python
Priority: optional
Maintainer: druuu, <[email protected]>
Build-Depends: debhelper (>= 7),
Standards-Version: 3.9.2
Package: mymood
Architecture: all
Section: python
Depends: python, ${misc:Depends}, ${python:Depends}
Description: simple mood checker
mymood is a simple mood checker.
It analyze the mood by simple questions.
So this file lists dependencies, package description. The description is of two types, short and long.
Short is "simple mood checker' and the long 'mymood is a simple mood checker. It analyze the mood by simple questions'.
In the long description it should not exceed 80 characters per line.
The rules file:
#!/usr/bin/make -f
%:
dh $@ --with python2
Note that you have python2 or python3 based on your script, dont just put python.
Also not that you use tab before dh, not spaces
The other files can be left empty.
6. Finally lets create a deb package.
Goto mymood top level direcotry and build package.
mymood <-- you should be inside this directory
debian
changelog
compat
control
rules
copyright
mymood
__init__.py
mymood.py
command_line.py
README.md
setup.py
$fakeroot dpkg-buildpackage -b
This should create the following structure:
mymood_1.0_all.deb <--success! we got deb package
mymood_1.0_amd64.changes
mymood
build
debian
tmp
DEBIAN
etc..
usr
etc..
changelog
compat
control
rules
copyright
debhelper.log
files
substvars
mymood
__init__.py
mymood.py
command_line.py
mymood.egg-info
README.md
setup.py
As you can see we get mymood_1.0_all.deb package wich can be installed by "ubuntu software center".
Ubuntu Software Center is recomended as it automatically installs dependencies
or
simply
$sudo dpkg -i mymood_1.0_all.deb <-- this command throws errors if dependencies not satisfied. Use ubuntu softare center.
Video for the above fosshelp blog post: https://www.youtube.com/watch?v=a9GzDZB5VeU.