python : creating a snapshot program


snapshot.py
-------------------------------------------------------
#!/bin/env python


import sys, os, snapshothelper


def menu ():
    os.system("clear")
    print '''DIRECTORY/FILE COMPARISON TOOL
    ====================================
    Please type a number and press enter:
    1. Create a snapshot
    2. List snapshot files
    3. Compare snapshots
    4. Help
    5. Exit
    '''

    choice = raw_input("\t")
    return choice


choice = ""
while choice != 5 :
    choice = menu()
    if choice == "1" :
        os.system('clear')
        print '''CREATE SNAPSHOT
        ===================================='''
        directory = raw_input("Enter the directory name to create snapshort of :  ")

        filename = raw_input("enter the name of the snaphort file to create :  ")
        snapshothelper.createSnapshot(directory, filename)

    elif choice == "2" :
        os.system("clear")
        print '''
        LIST SNAPSHOT FILES
        ====================================
        Enter the file extension for your snapshot files
        (for example, ‘snp’ if your files end in ‘.snp’):
        '''
        extension = raw_input('\t\t')
        snapshothelper.listSnapshot(extension)

    elif choice == "3":
        os.system('clear')
        print '''
        COMPARE SNAPSHOTS
        ====================================
        '''

        snap1 = raw_input("Enter the filename of snapshot 1: ")
        snap2 = raw_input("Enter the filename of snapshot 2: ")
        snapshothelper.compareSnapshots(snap1, snap2)

    elif choice == "4":
         snapshothelper.showHelp()
    else :
        if choice != "5":
            snapshothelper.invalid
---------------------------------------------------------------------------------------------------------

snapshothelper.py

#!/bin/env python

import os, sys, pickle, difflib

def createSnapshot(directory,filename) :
    cumulative_directories = []
    cumulative_files = []

    for root, dirs, files in os.walk(directory):
        cumulative_directories = cumulative_directories + dirs
        cumulative_files = cumulative_files + files

    try:
        output = open(filename, 'wb')
        pickle.dump(cumulative_directories,output, -1)
        pickle.dump(cumulative_files,output, -1)
        output.close
    except:
        print "error encounterd trying to save file "

    raw_input("press ENTER to continue")
    return

def listSnapshot(extention):
    snaplist= []
    filelist = os.listdir(os.curdir)
    for item in filelist:
        if item.endswith(extention):
            snaplist.append(item)

    print '''
    Snapshot list
    ===============================
    '''
    print(snaplist)
    raw_input("Press ENTER to continue")
    return

def compareSnapshots(snap1, snap2):

    try:
        pickle1_file = open(snap1, 'rb')
        dirs1 = pickle.load(pickle1_file)
        files1 = pickle.load(pickle1_file)
        pickle1_file.close()

        pickle2_file = open(snap2, 'rb')
        dirs2 = pickle.load(pickle2_file)
        files2 = pickle.load(pickle2_file)
        pickle2_file.close()

    except:
        print "problems incurred accessing snapshot files"
        raw_input("Press ENTER to continue ")
        return

    result_dirs = list(difflib.unified_diff(dirs1, dirs2))
    result_files = list(difflib.unified_diff(files1, files2))


    added_files = []
    removed_files = []
    added_dirs = []
    removed_dirs = []

    for result in result_dirs:
        if result.find("\n") == -1 :
            if result.startswith("+"):
                res_add = result.strip("+")
                added_dirs.append(res_add)
            if result.startswith("-"):
                res_remvd = result.strip("-")
                removed_dirs.append(res_remvd)


    for result in result_files:
        if result.find("\n") == -1 :
            if result.startswith("+"):
                res_add = result.strip("+")
                added_files.append(res_add)
            if result.startswith("-"):
                res_remvd = result.strip("-")
                removed_files.append(res_remvd)


    print "\n\n Added directories are :\n "
    printList(added_dirs)

    print "\n\n Removed directories are :\n "
    printList(removed_dirs)

    print "\n\n Added files are :\n "
    printList(added_files)

    print "\n\n Removed files  are :\n "
    printList(removed_files)


    raw_input("Press ENTER to continue ")
    return

def printList(item):
    if not  item:
        print "No datafound"
        return

    for ite in item:
        print "\t" + ite


def invalidChoice():
    sys.stderr.write("INVALID CHOICE, TRY AGAIN!")
    raw_input("\n\nPress [Enter] to continue...")
    return

No comments:

Other Articles

Enter your email address: