#!/usr/bin/env python
#
import sys
import tempfile
import subprocess

image_file=""
index_file=""

# Sector size used by './search_INDX' tool.
SEC_SIZE=512

# use 64 on a 64-bit system
WISP="./wisp32"


def args_parse():
    global image_file, index_file

    if len(sys.argv) != 3:
        printf >>sys.stderr, """Error: invalid usage.
Usage: %s <NTFS_disk_image>  <INDX_list>

Where <NTFS_disk_image> is a device file or dd/ddrescue image file and
<INDX_list> is a text file with one number per line with 512 bytes sectors
starting with 'INDX' text (NTFS directory index file)
        """
        return False

    image_file = sys.argv[1]
    index_file = sys.argv[2]
    return True


def sanity_checks():
    # TODO:
    # <image file is big>
    # <index file is text with number in first line
    # wisp is downloaded into current dir
    return True


class IndexList:
    """ Reading a text file with a number in each line """
    def __init__(self, filename):
        self.fp = open(filename, 'r')
    def __iter__(self):
        return self
    def next(self):
        line = self.fp.readline().strip()
        if len(line):
            return int(line)
        else:
            raise StopIteration

def parse_wisp_output(output_filename):
    print "parse wisp output"
    return



def do_job():
    f = open(image_file, 'rb')

    for s in IndexList(index_file):

        # read INDX from disk and store to tmp file
        f.seek(s * SEC_SIZE)
        indx = f.read(1024*1024)  # FIXME: I do not know how to detect the end of INDX
        tmpfile = tempfile.NamedTemporaryFile(prefix="ntfs_indx_")
        tmp.write(indx)
        tmp.flush()

        # Parse record-by-record filling directory tree
        p = subprocess.Popen(
            args = [ WISP, '-valid', '-csv', '-base10', '-indxfile', image_file ]
            )
        p.run()

        for line in p:
            print line
        p.wait()



if __name__ == "__main__":
    if args_parse() and sanity_checks():
        do_job()

@@ -1551,20 +1551,16 @@
      tmp
-file
  = tempf
@@ -1734,16 +1734,41 @@
 .Popen(%0A
+            shell=False,%0A
         
@@ -1832,20 +1832,19 @@
 e', 
-image_fil
+tmp.nam
 e %5D
+,
 %0A   
@@ -1852,17 +1852,40 @@
         
-)
+stdout = subprocess.PIPE
 %0A       
@@ -1889,14 +1889,12 @@
     
-p.run(
+    
 )%0A%0A 
@@ -1913,16 +1913,23 @@
 ine in p
+.stdout
 :%0A      
@@ -1940,24 +1940,62 @@
   print line
+.strip()%0A%0A        print %22----------%22%0A%0A
 %0A        p.w

implement wispering invocation