Google
 
Web pyadmin.blogspot.com

Tuesday, June 06, 2006

An interactive restore script

Im here again with a simple(useless?) utility which can simply restore specified files from a tar archive.You can specify some pattern and the program will show the file names starting with the specified pattern and u can select the required files from the archive and restore.That much simple.
The usage is as follows:
restore.py source.c /dev/nrtape
The code follows:

#!/usr/bin/python

# 'restore.py'. Invoke it as, for example,
# restore.py source.c /dev/nrtape
# to show all the archived files named 'source.c'
# which the user can select for retrieval.

import os,sys,Tkinter

try:
pattern = sys.argv[1]
archive = sys.argv[2]
except (IndexError,NameError):
print 'Usage:restore.py pattern archive'
sys.exit

def esf():
# Construct the string which lists all selected files,
# separated by a single blank. Use that string to
# specify the exact listof files to extract from the
# archive.
command = 'tar xf %s %s' % (archive,
' '.join([lb.get(index) for index in lb.curselection()]))
os.system(command)

# MULTIPLE so that we can select and extract several files in a
# single operation.
lb = Tkinter.Listbox(height = 12, width = 30, selectmode = Tkinter.MULTIPLE)
lb.pack()
Tkinter.Button(text = "Extract selected files", command = esf).pack()

# The "[:-1]" says, "ignore the trailing newline tar emits".
try:
for qualified_name in os.popen('tar tf %s' % archive).read().split('\n')[:-1]:
# Does the basename of this item match the pattern?
if os.path.basename(qualified_name).count(pattern) > 0:
lb.insert(Tkinter.END, qualified_name)
except NameError:
sys.exit
# Show the GUI panel.
Tkinter.mainloop()