Tuesday, November 20, 2012

Python work with Flash drive

Задача:
1. Написать функцию которая записывает на Flash Disk в NAND определенную информацию( set_information_on_flash('/dev/sdb1', 'inputBuffer') )
2. Написать функцию которая считывает с Flash Disk файл определенную информацию(get_information_from_flash('/dev/sdb1', 'outputBuffer') )
3. Написать функцию которая сравнивает записанную информацию на Flash Disk в NAND с считанной информации с Flash Disk.(compare_information_in_out_file('outputBuffer','inputBuffer') )

#!/usr/bin/python

import commands
import re
import os
import csv

def main():

  set_information_on_flash('/dev/sdb1', 'inputBuffer')
  get_information_from_flash('/dev/sdb1', 'outputBuffer')
  compare_information_in_out_file('outputBuffer','inputBuffer')
 
def get_information_from_flash(devise, fileOutput):
    if os.path.exists('./%s' % (fileOutput)) == True:
        cmd = "dd if=%s of=%s bs=1 count=10 skip=1000" % (devise, fileOutput)
        output = commands.getoutput(cmd)
        print "Information getted"
    else:
        cmd = "touch ./%s" % (fileOutput)
        output = commands.getoutput(cmd)
        print "Please restart program, file %s is created" % (fileOutput)

def set_information_on_flash(devise, fileInput):
    if os.path.exists('./%s' % (fileInput)) == True:
        cmd = "dd if=%s of=%s bs=1 count=10 seek=1000 conv=notrunc" % (fileInput, devise)
        print cmd
        output = commands.getoutput(cmd)
        print "Information writed"
    else:
        cmd = "touch ./%s" % (fileInput)
        cmd1 = "echo 'AaBbCcDdEe' > ./%s" % (fileInput)
        output = commands.getoutput(cmd)
        output = commands.getoutput(cmd1)
        print "Please restart program, file %s is created" % (fileInput)

def compare_information_in_out_file(fileOutput,fileInput):
        fo = open(fileOutput, 'r')
        checkFile = fo.read(10)
        fo1 = open(fileInput, 'r')
        checkFile1 = fo1.read(10)
        print "Read from Outputfile = " + checkFile
        print "Read from Input file = " + checkFile1
        if checkFile != checkFile1:
            print "Reading is false"
        else:
            print "Reading is true both file is equal"
        fo.close()
        fo1.close()

if __name__ == '__main__':
  main()