#!/usr/bin/env python
#
import csv, sys, os.path

def error(message):
	sys.stderr.write(message + '\n')
	sys.exit(1)
	
def cygpath_from_winpath(winabspath):
	return("/cygdrive/"+winabspath[0].lower()+winabspath[2:].replace('\\','/'))
	
def main(argv):
	if len(argv)!=(3+1) and len(argv)!=(4+1):
		error('usage: %s root_WPID procmon_log.csv proj_base_path [dep_file]' % os.path.basename(argv[0]))
	
	remove_deleted_files=1
	proc_set = [argv[1]]
	log_fname = argv[2]
	proj_base = argv[3]
	if proj_base.endswith('\\'):
		proj_base=proj_base[:-1]
	
	# don't use set because I want a consistent order.
	init_read_files = []
	ever_write_files = []
	
	# read the string data
	with open(log_fname,  'r') as f:
		reader = csv.DictReader(f, delimiter=',')
		for line in reader:
			#create the process tree (start in Detail (PPID) column)
			if line["Operation"].startswith('Process Start') and line["Parent PID"] in proc_set:
				proc_set.append(line["PID"])
				continue
			
			#Then filter events by the WPID tree.
			if line["Operation"].startswith('ReadFile') and line["PID"] in proc_set and line["Path"].startswith(proj_base):
				#fname = line["Path"][(len(proj_base)+1):]
				fname = line["Path"]
				if fname not in ever_write_files and fname not in init_read_files:
					init_read_files.append(fname)
						
			if line["Operation"].startswith('WriteFile') and line["PID"] in proc_set and line["Path"].startswith(proj_base):
				#fname = line["Path"][(len(proj_base)+1):]
				fname = line["Path"]
				if fname not in ever_write_files:
					ever_write_files.append(fname)
	
	if remove_deleted_files:
		init_read_files = [fname for fname in init_read_files if os.path.isfile(fname)]
		ever_write_files = [fname for fname in ever_write_files if os.path.isfile(fname)]
	
	#Output the information
	if len(argv)==(4+1):
		outfile = argv[4]
		with open(outfile, "w") as dep_file:
			for fname in ever_write_files:
				fname_rel = os.path.relpath(cygpath_from_winpath(fname))
				dep_file.write(fname_rel + ' ')
			
			dep_file.write(' : ')
			
			for fname in init_read_files:
				fname_rel = os.path.relpath(cygpath_from_winpath(fname))
				dep_file.write(' ' + fname_rel)
			
			dep_file.write('\n')
	
	else:
		print 'Project files initially read:'
		for fname in init_read_files:
			fname_rel = os.path.relpath(fname)
			print fname_rel
		
		print ''
		print 'Project files ever written:'
		for fname in ever_write_files:
			fname_rel = os.path.relpath(fname)
			print fname_rel

if  __name__ =='__main__':
	main(sys.argv)
	