[Courses] [python] Lesson 8: Extras

Prana Peddi prana.lists at gmail.com
Mon Aug 8 18:47:19 UTC 2011


Hi,
For this assignment, I tried to automate a task that I constantly do at
work.
I frequently need to test software updates on an embedded system. To keep my
runtime scripts unchanged when i make an update, I create a softlink called
fsroot to point to the new update. The script does the following tasks:
1. remove old softlink to fsroot
2. create a new softlink to a new input path
3. restart tftp and nfs-services
I tried to use all the concepts we learnt in this lesson and hence the
script is more complicated than it needs to be. Here's the script:

#! /usr/bin/python
import sys
import os
import re
import subprocess

def usage():
        print "Usage: ./linkfsroot.py <path of new fsroot>"

def printold():
        t = os.popen('ls -l fsroot')
        str = "Old path:"
        for line in t:
                l = re.split(" ", line)
                for i in range(7,len(l)):
                        str = str + l[i]
        print str,

def printnew():
        t = subprocess.Popen(["ls", "-l", "fsroot"], stdout=subprocess.PIPE)
        str = "New path:"
        for line in t.stdout:
                l = re.split(" ", line)
                for i in range(7,len(l)):
                        str = str + l[i]
        print str,

def createlink(path):
        subprocess.call(["rm", "fsroot"])
        subprocess.call(["ln", "-s", path, "fsroot"])

def restartservice():
        os.system('sudo /etc/init.d/xinetd restart')
        os.system('sudo /etc/init.d/nfs-kernel-server restart')

try:
        inpath = sys.argv[1]
        printold()
        createlink(inpath)
        restartservice()
        printnew()
        print 'created links and restarted tftp and nfsserver'
except IndexError, err:
        print "IndexError:", err
        usage()
except Exception, err:
        print "Error:", err
        usage()

Thanks,
Prana.


More information about the Courses mailing list