We will be writing a python script to create an installer for apache open office for Linux.

Here is the code, we will go through it step by step.

#!/usr/bin/env python
import subprocess
import optparse
import platform

Here, we have imported some python libraries.

  • subprocess: is used for running the bash commands.
  • optparse: is for command line option parsing.
  • platform: is used for getting the details of the system on which the script is running.
install = []
uninstall = ["sudo apt-get purge openoffice*.* && sudo apt-get autoremove"]
PLATFORM = platform.system()
ARCHITECTURE = platform.architecture()[0]

Here are the global variables.

  • install: for collecting commands for install option.
  • uninstall: for collecting commands for uninstall option.
  • PLATFORM: for identifying operating system.
  • ARCHITECTURE: for identifying processor.

#----------------------------Running commands-----------------------------
def run_commands(cmds):
  Function which run all the commands one by one.
  for cmd in cmds:
    subprocess.call(cmd, shell=True)

The function to run commands one by one.

Here is the controller, which does all the command line handling. Let's take it in parts.

#---------------------------Option parsing--------------------------------
def controller():
    Function which does parsing of command line options and
    also prepares a list of commands that are used for installation.
    global VERBOSE, install, uninstall

    #Create instance of OptionParser Module, included in Standard Library and adds option in it.
    p = optparse.OptionParser(
                          description='For installing Apache open office',
                          version='install-open-office 0.1',
                          usage= '%prog [option]')
    p.add_option('--install','-i', action="store_true", help='installs open office')
    p.add_option('--uninstall', '-u', action="store_true", help='uninstalls open office')
    p.add_option('--Version', '-V', help='specifying version')
    p.add_option('--verbose', '-v',
                action = 'store_true',
                help='prints verbosely',

In this part, we first created an OptionParser object to handle command's options. Then added some custom options and their details. The option parser takes some arguments like the description of the command, the program name, version, and its usage details. Then the ad_option takes option name, its descriptive name, the action on option, (in our case to store default option value as true) and help string for option.

    #Option Handling passes correct parameter to runBash
    options, arguments = p.parse_args()

    if options.verbose:
    if options.install:
    #--------------------------setting commands---------------------------
	    #Removing libreoffice
	    install.append("sudo apt-get remove --purge libreoffice* libexttextcat-data* && sudo apt-get autoremove")
	    #removing openoffice
	    install.append("sudo apt-get purge openoffice*.* && sudo apt-get autoremove")

	    #change to /tmp
	    install.append("cd /tmp")

	    #download the application
	    if PLATFORM == "Linux":
	      ##For 4.0.1
	      if options.Version == "4.0":
	        if ARCHITECTURE == "32bit":
	          INSTALL.append("wget http://sourceforge.net/projects/openofficeorg.mirror/files/4.0.1/binaries/en-US/Apache_OpenOffice_4.0.1_Linux_x86_install-deb_en-US.tar.gz")
	        elif ARCHITECTURE == "64bit":
	          INSTALL.append("wget http://sourceforge.net/projects/openofficeorg.mirror/files/4.0.1/binaries/en-US/Apache_OpenOffice_4.0.1_Linux_x86-64_install-deb_en-US.tar.gz")
	      elif options.Version == "4.1":
	        if PLATFORM == "Linux":
	          if ARCHITECTURE == "32bit":
	            INSTALL.append("wget http://sourceforge.net/projects/openofficeorg.mirror/files/4.1.1/binaries/en-US/Apache_OpenOffice_4.1.1_Linux_x86_install-deb_en-US.tar.gz")
	          elif ARCHITECTURE == "64bit":
	            INSTALL.append("wget http://sourceforge.net/projects/openofficeorg.mirror/files/4.1.1/binaries/en-US/Apache_OpenOffice_4.1.1_Linux_x86-64_install-deb_en-US.tar.gz")
	      print "Wrong operating system detected."

	    install.append("tar -xvf Apache_OpenOffice*.tar.gz")

	    #install deb
	    install.append("sudo dpkg -i en-US/DEBS/*.deb")

	    #install the desktop-integration
	    install.append("sudo dpkg -i en-US/DEBS/desktop-integration/*.deb")

	    value = run_commands(install)

    elif options.uninstall:
        value = run_commands(uninstall)


Here, first we have called the parse_args to parse the collected option in p. And then according to the options used, we have taken different actions. Like:
if verbose is used then set VERBOSE mode on.
if -i is used then append installation command to the global INSTALL list and call the method to run the commands.
if uninstall is used then call the run_commands method with the uninstallation command.
If no option is given then print the help menu.

#Runs all the functions
def main():

#This idiom means the below code only runs when executed from command line
if __name__ == '__main__':

Finally, call the controller in main method and main method is called automatically by the program.

And here the program ends. We have created our installer script to install apache open office. Next, You have to make the script executable.

chmod +x install_apache_open_office.py

To run the script use:

bash install_apache_open_office.py

You can clone the script from Github Gists.
You can also find other installers there for installing anaconda and ruby 2.2. Just download and make them executable and run then.